View Javadoc
1   package org.apache.maven.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.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  import org.apache.commons.cli.Option;
31  import org.codehaus.plexus.PlexusTestCase;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * Pseudo test to generate documentation fragment about supported CLI options. TODO such documentation generation code
36   * should not be necessary as unit test but should be run during site generation (Velocity? Doxia macro?)
37   */
38  public class CLIManagerDocumentationTest
39      extends PlexusTestCase
40  {
41      private final static String LS = System.getProperty( "line.separator" );
42  
43      private static class OptionComparator
44          implements Comparator<Option>
45      {
46          public int compare( Option opt1, Option opt2 )
47          {
48              return opt1.getOpt().compareToIgnoreCase( opt2.getOpt() );
49          }
50      }
51  
52      private static class CLIManagerExtension
53          extends CLIManager
54      {
55          public Collection<Option> getOptions()
56          {
57              List<Option> optList = new ArrayList<>( options.getOptions() );
58              Collections.sort( optList, new OptionComparator() );
59              return optList;
60          }
61      }
62  
63      public String getOptionsAsHtml()
64      {
65          StringBuilder sb = new StringBuilder( 512 );
66          boolean a = true;
67          sb.append( "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>" );
68          for ( Option option : new CLIManagerExtension().getOptions() )
69          {
70              a = !a;
71              sb.append( "<tr class='" ).append( a ? 'a' : 'b' ).append( "'><td><code>-<a name='" );
72              sb.append( option.getOpt() );
73              sb.append( "'>" );
74              sb.append( option.getOpt() );
75              sb.append( "</a>,--<a name='" );
76              sb.append( option.getLongOpt() );
77              sb.append( "'>" );
78              sb.append( option.getLongOpt() );
79              sb.append( "</a>" );
80              if ( option.hasArg() )
81              {
82                  if ( option.hasArgName() )
83                  {
84                      sb.append( " &lt;" ).append( option.getArgName() ).append( "&gt;" );
85                  }
86                  else
87                  {
88                      sb.append( ' ' );
89                  }
90              }
91              sb.append( "</code></td><td>" );
92              sb.append( option.getDescription() );
93              sb.append( "</td></tr>" );
94              sb.append( LS );
95          }
96          sb.append( "</table>" );
97          return sb.toString();
98      }
99  
100     public void testOptionsAsHtml()
101         throws IOException
102     {
103         File options = getTestFile( "target/test-classes/options.html" );
104         FileUtils.fileWrite( options, "UTF-8", getOptionsAsHtml() );
105     }
106 
107 }