View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cli;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  import org.apache.commons.cli.Option;
30  import org.apache.commons.io.FileUtils;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Pseudo test to generate documentation fragment about supported CLI options. TODO such documentation generation code
35   * should not be necessary as unit test but should be run during site generation (Velocity? Doxia macro?)
36   */
37  class CLIManagerDocumentationTest {
38      private static final String LS = System.lineSeparator();
39  
40      private static class OptionComparator implements Comparator<Option> {
41          public int compare(Option opt1, Option opt2) {
42              String s1 = opt1.getOpt() != null ? opt1.getOpt() : opt1.getLongOpt();
43              String s2 = opt2.getOpt() != null ? opt2.getOpt() : opt2.getLongOpt();
44              return s1.compareToIgnoreCase(s2);
45          }
46      }
47  
48      private static class CLIManagerExtension extends CLIManager {
49          public Collection<Option> getOptions() {
50              List<Option> optList = new ArrayList<>(options.getOptions());
51              Collections.sort(optList, new OptionComparator());
52              return optList;
53          }
54      }
55  
56      String getOptionsAsHtml() {
57          StringBuilder sb = new StringBuilder(512);
58          boolean a = true;
59          sb.append(
60                  "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
61          for (Option option : new CLIManagerExtension().getOptions()) {
62              a = !a;
63              sb.append("<tr class='").append(a ? 'a' : 'b').append("'><td><code>-<a name='");
64              sb.append(option.getOpt());
65              sb.append("'>");
66              sb.append(option.getOpt());
67              sb.append("</a>,--<a name='");
68              sb.append(option.getLongOpt());
69              sb.append("'>");
70              sb.append(option.getLongOpt());
71              sb.append("</a>");
72              if (option.hasArg()) {
73                  if (option.hasArgName()) {
74                      sb.append(" &lt;").append(option.getArgName()).append("&gt;");
75                  } else {
76                      sb.append(' ');
77                  }
78              }
79              sb.append("</code></td><td>");
80              sb.append(option.getDescription());
81              sb.append("</td></tr>");
82              sb.append(LS);
83          }
84          sb.append("</table>");
85          return sb.toString();
86      }
87  
88      @Test
89      void testOptionsAsHtml() throws IOException {
90          File options = new File("target/test-classes/options.html");
91          FileUtils.write(options, getOptionsAsHtml(), "UTF-8");
92      }
93  }