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.
36   * TODO such documentation generation code should not be necessary as unit test but should be run
37   * during site generation (Velocity? Doxia macro?)
38   */
39  public class CLIManagerTest
40      extends PlexusTestCase
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              @SuppressWarnings( "unchecked" )
59              List<Option> optList = new ArrayList<Option>( options.getOptions() );
60              Collections.sort( optList, new OptionComparator() );
61              return optList;
62          }
63      }
64  
65      public String getOptionsAsHtml()
66      {
67          StringBuilder sb = new StringBuilder();
68          boolean a = true;
69          sb.append( "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>" );
70          for ( Option option : new CLIManagerExtension().getOptions() )
71          {
72              a = !a;
73              sb.append( "<tr class='" ).append( a ? 'a' : 'b' ).append( "'><td><code>-<a name='" );
74              sb.append( option.getOpt() );
75              sb.append( "'>" );
76              sb.append( option.getOpt() );
77              sb.append( "</a>,--<a name='" );
78              sb.append( option.getLongOpt() );
79              sb.append( "'>" );
80              sb.append( option.getLongOpt() );
81              sb.append( "</a>" );
82              if ( option.hasArg() )
83              {
84                  if ( option.hasArgName() )
85                  {
86                      sb.append( " &lt;" ).append( option.getArgName() ).append( "&gt;" );
87                  }
88                  else
89                  {
90                      sb.append( ' ' );
91                  }
92              }
93              sb.append( "</code></td><td>" );
94              sb.append( option.getDescription() );
95              sb.append( "</td></tr>" );
96              sb.append( LS );
97          }
98          sb.append( "</table>" );
99          return sb.toString();
100     }
101 
102     public void testOptionsAsHtml()
103         throws IOException
104     {
105         File options = getTestFile( "target/test-classes/options.html" );
106         FileUtils.fileWrite( options, "UTF-8", getOptionsAsHtml() );
107     }
108 }