001package org.apache.maven.cli;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.Comparator;
028import java.util.List;
029
030import org.apache.commons.cli.Option;
031import org.codehaus.plexus.PlexusTestCase;
032import org.codehaus.plexus.util.FileUtils;
033
034/**
035 * Pseudo test to generate documentation fragment about supported CLI options.
036 * TODO such documentation generation code should not be necessary as unit test but should be run
037 * during site generation (Velocity? Doxia macro?) 
038 */
039public class CLIManagerTest
040    extends PlexusTestCase
041{
042    private final static String LS = System.getProperty( "line.separator" );
043
044    private static class OptionComparator
045        implements Comparator<Option>
046    {
047        public int compare( Option opt1, Option opt2 )
048        {
049            return opt1.getOpt().compareToIgnoreCase( opt2.getOpt() );
050        }
051    }
052
053    private static class CLIManagerExtension
054        extends CLIManager
055    {
056        public Collection<Option> getOptions()
057        {
058            @SuppressWarnings( "unchecked" )
059            List<Option> optList = new ArrayList<Option>( options.getOptions() );
060            Collections.sort( optList, new OptionComparator() );
061            return optList;
062        }
063    }
064
065    public String getOptionsAsHtml()
066    {
067        StringBuilder sb = new StringBuilder();
068        boolean a = true;
069        sb.append( "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>" );
070        for ( Option option : new CLIManagerExtension().getOptions() )
071        {
072            a = !a;
073            sb.append( "<tr class='" ).append( a ? 'a' : 'b' ).append( "'><td><code>-<a name='" );
074            sb.append( option.getOpt() );
075            sb.append( "'>" );
076            sb.append( option.getOpt() );
077            sb.append( "</a>,--<a name='" );
078            sb.append( option.getLongOpt() );
079            sb.append( "'>" );
080            sb.append( option.getLongOpt() );
081            sb.append( "</a>" );
082            if ( option.hasArg() )
083            {
084                if ( option.hasArgName() )
085                {
086                    sb.append( " &lt;" ).append( option.getArgName() ).append( "&gt;" );
087                }
088                else
089                {
090                    sb.append( ' ' );
091                }
092            }
093            sb.append( "</code></td><td>" );
094            sb.append( option.getDescription() );
095            sb.append( "</td></tr>" );
096            sb.append( LS );
097        }
098        sb.append( "</table>" );
099        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}