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.index.cli;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.PrintStream;
24  import java.util.ArrayList;
25  import java.util.Comparator;
26  import java.util.List;
27  
28  import org.apache.commons.cli.Option;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.junit.Test;
31  
32  public class NexusIndexerCliTest extends AbstractNexusIndexerCliTest {
33  
34      protected NexusIndexerCli cli;
35  
36      @Override
37      public void setUp() throws Exception {
38          super.setUp();
39  
40          cli = new NexusIndexerCli();
41  
42          System.setOut(new PrintStream(out));
43          System.setErr(new PrintStream(out));
44      }
45  
46      @Override
47      protected int execute(String... args) {
48          return cli.execute(args);
49      }
50  
51      private static final String LS = System.getProperty("line.separator");
52  
53      private static class OptionComparator implements Comparator<Option> {
54          public int compare(Option opt1, Option opt2) {
55              return opt1.getOpt().compareToIgnoreCase(opt2.getOpt());
56          }
57      }
58  
59      public String getOptionsAsHtml() {
60          List<Option> optList =
61                  new ArrayList<>(new NexusIndexerCli().buildCliOptions().getOptions());
62          optList.sort(new OptionComparator());
63  
64          StringBuilder sb = new StringBuilder();
65          boolean a = true;
66          sb.append(
67                  "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
68          for (Option option : optList) {
69              a = !a;
70              sb.append("<tr class='").append(a ? 'a' : 'b').append("'><td><code>-<a name='");
71              sb.append(option.getOpt());
72              sb.append("'>");
73              sb.append(option.getOpt());
74              sb.append("</a>,--<a name='");
75              sb.append(option.getLongOpt());
76              sb.append("'>");
77              sb.append(option.getLongOpt());
78              sb.append("</a>");
79              if (option.hasArg()) {
80                  if (option.hasArgName()) {
81                      sb.append(" &lt;").append(option.getArgName()).append("&gt;");
82                  } else {
83                      sb.append(' ');
84                  }
85              }
86              sb.append("</code></td><td>");
87              sb.append(option.getDescription());
88              sb.append("</td></tr>");
89              sb.append(LS);
90          }
91          sb.append("</table>");
92          return sb.toString();
93      }
94  
95      @Test
96      public void testOptionsAsHtml() throws IOException {
97          File options = getTestFile("target/test-classes/options.html");
98          FileUtils.fileWrite(options, "UTF-8", getOptionsAsHtml());
99      }
100 }