View Javadoc
1   package org.apache.maven.index.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.util.ArrayList;
26  import java.util.Comparator;
27  import java.util.List;
28  
29  import org.apache.commons.cli.Option;
30  import org.apache.maven.index.cli.NexusIndexerCli;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  public class NexusIndexerCliTest
34      extends AbstractNexusIndexerCliTest
35  {
36  
37      protected NexusIndexerCli cli;
38  
39      @Override
40      protected void setUp()
41          throws Exception
42      {
43          super.setUp();
44  
45          cli = new NexusIndexerCli();
46  
47          System.setOut( new PrintStream( out ) );
48          System.setErr( new PrintStream( out ) );
49      }
50  
51      @Override
52      protected int execute( String... args )
53      {
54          return cli.execute( args );
55      }
56  
57      private final static String LS = System.getProperty( "line.separator" );
58  
59      private static class OptionComparator
60          implements Comparator<Option>
61      {
62          public int compare( Option opt1, Option opt2 )
63          {
64              return opt1.getOpt().compareToIgnoreCase( opt2.getOpt() );
65          }
66      }
67  
68      public String getOptionsAsHtml()
69      {
70          @SuppressWarnings( "unchecked" )
71          List<Option> optList = new ArrayList<Option>( new NexusIndexerCli().buildDefaultCliOptions().getOptions() );
72          optList.sort( new OptionComparator() );
73  
74          StringBuilder sb = new StringBuilder();
75          boolean a = true;
76          sb.append( "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>" );
77          for ( Option option : optList )
78          {
79              a = !a;
80              sb.append( "<tr class='" ).append( a ? 'a' : 'b' ).append( "'><td><code>-<a name='" );
81              sb.append( option.getOpt() );
82              sb.append( "'>" );
83              sb.append( option.getOpt() );
84              sb.append( "</a>,--<a name='" );
85              sb.append( option.getLongOpt() );
86              sb.append( "'>" );
87              sb.append( option.getLongOpt() );
88              sb.append( "</a>" );
89              if ( option.hasArg() )
90              {
91                  if ( option.hasArgName() )
92                  {
93                      sb.append( " &lt;" ).append( option.getArgName() ).append( "&gt;" );
94                  }
95                  else
96                  {
97                      sb.append( ' ' );
98                  }
99              }
100             sb.append( "</code></td><td>" );
101             sb.append( option.getDescription() );
102             sb.append( "</td></tr>" );
103             sb.append( LS );
104         }
105         sb.append( "</table>" );
106         return sb.toString();
107     }
108 
109     public void testOptionsAsHtml()
110         throws IOException
111     {
112         File options = getTestFile( "target/test-classes/options.html" );
113         FileUtils.fileWrite( options, "UTF-8", getOptionsAsHtml() );
114     }
115 }