001    package org.apache.maven.tools.plugin.generator;
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    
022    import org.apache.maven.plugin.descriptor.PluginDescriptor;
023    import org.apache.maven.plugin.testing.AbstractMojoTestCase;
024    import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
025    import org.codehaus.plexus.component.repository.ComponentDependency;
026    import org.codehaus.plexus.util.xml.CompactXMLWriter;
027    import org.codehaus.plexus.util.xml.XMLWriter;
028    
029    import java.io.StringWriter;
030    import java.util.Collections;
031    
032    /**
033     * @author jdcasey
034     */
035    public class GeneratorUtilsTest
036        extends AbstractMojoTestCase
037    {
038        public void testShouldWriteDependencies()
039            throws Exception
040        {
041            ComponentDependency dependency = new ComponentDependency();
042            dependency.setArtifactId( "testArtifactId" );
043            dependency.setGroupId( "testGroupId" );
044            dependency.setType( "pom" );
045            dependency.setVersion( "0.0.0" );
046    
047            PluginDescriptor descriptor = new PluginDescriptor();
048            descriptor.setDependencies( Collections.singletonList( dependency ) );
049    
050            StringWriter sWriter = new StringWriter();
051            XMLWriter writer = new CompactXMLWriter( sWriter );
052    
053            GeneratorUtils.writeDependencies( writer, descriptor );
054    
055            String output = sWriter.toString();
056    
057            String pattern =
058                "<dependencies>" + "<dependency>" + "<groupId>testGroupId</groupId>"
059                    + "<artifactId>testArtifactId</artifactId>" + "<type>pom</type>" + "<version>0.0.0</version>"
060                    + "</dependency>" + "</dependencies>";
061    
062            assertEquals( pattern, output );
063        }
064    
065        public void testMakeHtmlValid()
066        {
067            String javadoc = null;
068            assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
069            javadoc = "";
070            assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
071    
072            // true HTML
073            javadoc = "Generates <i>something</i> for the project.";
074            assertEquals( "Generates <i>something</i> for the project.", GeneratorUtils.makeHtmlValid( javadoc ) );
075    
076            // wrong HTML
077            javadoc = "Generates <i>something</i> <b> for the project.";
078            assertEquals( "Generates <i>something</i> <b> for the project.</b>", GeneratorUtils.makeHtmlValid( javadoc ) );
079    
080            // wrong XHTML
081            javadoc = "Line1<br>Line2";
082            assertEquals( "Line1<br/>Line2", GeneratorUtils.makeHtmlValid( javadoc ).replaceAll( "\\s", "" ) );
083    
084            // special characters
085            javadoc = "& &amp; < > \u00A0";
086            assertEquals( "&amp; &amp; &lt; &gt; \u00A0", GeneratorUtils.makeHtmlValid( javadoc ) );
087    
088            // non ASCII characters
089            javadoc = "\u00E4 \u00F6 \u00FC \u00DF";
090            assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
091    
092            // non Latin1 characters
093            javadoc = "\u0130 \u03A3 \u05D0 \u06DE";
094            assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
095        }
096    
097        public void testDecodeJavadocTags()
098        {
099            String javadoc = null;
100            assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
101    
102            javadoc = "";
103            assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
104    
105            javadoc = "{@code text}";
106            assertEquals( "<code>text</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
107    
108            javadoc = "{@code <A&B>}";
109            assertEquals( "<code>&lt;A&amp;B&gt;</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
110    
111            javadoc = "{@literal text}";
112            assertEquals( "text", GeneratorUtils.decodeJavadocTags( javadoc ) );
113    
114            javadoc = "{@literal text}  {@literal text}";
115            assertEquals( "text  text", GeneratorUtils.decodeJavadocTags( javadoc ) );
116    
117            javadoc = "{@literal <A&B>}";
118            assertEquals( "&lt;A&amp;B&gt;", GeneratorUtils.decodeJavadocTags( javadoc ) );
119    
120            javadoc = "{@link Class}";
121            assertEquals( "<code>Class</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
122    
123            javadoc = "{@linkplain Class}";
124            assertEquals( "Class", GeneratorUtils.decodeJavadocTags( javadoc ) );
125    
126            javadoc = "{@linkplain #field}";
127            assertEquals( "field", GeneratorUtils.decodeJavadocTags( javadoc ) );
128    
129            javadoc = "{@linkplain Class#field}";
130            assertEquals( "Class.field", GeneratorUtils.decodeJavadocTags( javadoc ) );
131    
132            javadoc = "{@linkplain #method()}";
133            assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
134    
135            javadoc = "{@linkplain #method(Object arg)}";
136            assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
137    
138            javadoc = "{@linkplain #method(Object, String)}";
139            assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
140    
141            javadoc = "{@linkplain #method(Object, String) label}";
142            assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
143    
144            javadoc = "{@linkplain Class#method(Object, String)}";
145            assertEquals( "Class.method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
146    
147            javadoc = "{@linkplain Class#method(Object, String) label}";
148            assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
149        }
150    
151        public void testToText()
152            throws Exception
153        {
154            String javadoc = null;
155            assertEquals( "", GeneratorUtils.toText( javadoc ) );
156            javadoc = "";
157            assertEquals( "", GeneratorUtils.toText( javadoc ) );
158    
159            // line breaks
160            javadoc = "Line1\nLine2";
161            assertEquals( "Line1 Line2", GeneratorUtils.toText( javadoc ) );
162            javadoc = "Line1\rLine2";
163            assertEquals( "Line1 Line2", GeneratorUtils.toText( javadoc ) );
164            javadoc = "Line1\r\nLine2";
165            assertEquals( "Line1 Line2", GeneratorUtils.toText( javadoc ) );
166            javadoc = "Line1<br>Line2";
167            assertEquals( "Line1\nLine2", GeneratorUtils.toText( javadoc ) );
168    
169            // true HTML
170            javadoc = "Generates <i>something</i> for the project.";
171            assertEquals( "Generates something for the project.", GeneratorUtils.toText( javadoc ) );
172    
173            // wrong HTML
174            javadoc = "Generates <i>something</i> <b> for the project.";
175            assertEquals( "Generates something for the project.", GeneratorUtils.toText( javadoc ) );
176    
177            // javadoc inline tags
178            javadoc = "Generates {@code something} for the project.";
179            assertEquals( "Generates something for the project.", GeneratorUtils.toText( javadoc ) );
180        }
181    
182        public void testIsMavenReport()
183            throws Exception
184        {
185            try
186            {
187                GeneratorUtils.isMavenReport( null, null );
188            }
189            catch ( IllegalArgumentException e )
190            {
191                assertTrue( true );
192            }
193    
194            String impl = "org.apache.maven.tools.plugin.generator.stubs.MavenReportStub";
195    
196            MavenProjectStub stub = new MavenProjectStub();
197            stub.setCompileSourceRoots( Collections.singletonList( getBasedir() + "/target/classes" ) );
198    
199            assertTrue( GeneratorUtils.isMavenReport( impl, stub ) );
200    
201            impl = "org.apache.maven.tools.plugin.util.stubs.MojoStub";
202            assertFalse( GeneratorUtils.isMavenReport( impl, stub ) );
203        }
204    
205    }