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