View Javadoc

1   package org.apache.maven.tools.plugin.util;
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 org.apache.maven.plugin.descriptor.PluginDescriptor;
23  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
24  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
25  import org.codehaus.plexus.component.repository.ComponentDependency;
26  import org.codehaus.plexus.util.xml.CompactXMLWriter;
27  import org.codehaus.plexus.util.xml.XMLWriter;
28  
29  import java.io.StringWriter;
30  import java.util.Collections;
31  
32  /**
33   * @author jdcasey
34   */
35  public class PluginUtilsTest
36      extends AbstractMojoTestCase
37  {
38      public void testShouldTrimArtifactIdToFindPluginId()
39      {
40          assertEquals( "artifactId", PluginDescriptor.getGoalPrefixFromArtifactId( "maven-artifactId-plugin" ) );
41          assertEquals( "artifactId", PluginDescriptor.getGoalPrefixFromArtifactId( "maven-plugin-artifactId" ) );
42          assertEquals( "artifactId", PluginDescriptor.getGoalPrefixFromArtifactId( "artifactId-maven-plugin" ) );
43          assertEquals( "artifactId", PluginDescriptor.getGoalPrefixFromArtifactId( "artifactId" ) );
44          assertEquals( "artifactId", PluginDescriptor.getGoalPrefixFromArtifactId( "artifactId-plugin" ) );
45          assertEquals( "plugin", PluginDescriptor.getGoalPrefixFromArtifactId( "maven-plugin-plugin" ) );
46      }
47  
48      public void testShouldWriteDependencies()
49          throws Exception
50      {
51          ComponentDependency dependency = new ComponentDependency();
52          dependency.setArtifactId( "testArtifactId" );
53          dependency.setGroupId( "testGroupId" );
54          dependency.setType( "pom" );
55          dependency.setVersion( "0.0.0" );
56  
57          PluginDescriptor descriptor = new PluginDescriptor();
58          descriptor.setDependencies( Collections.singletonList( dependency ) );
59  
60          StringWriter sWriter = new StringWriter();
61          XMLWriter writer = new CompactXMLWriter( sWriter );
62  
63          PluginUtils.writeDependencies( writer, descriptor );
64  
65          String output = sWriter.toString();
66  
67          String pattern = "<dependencies>" + "<dependency>" + "<groupId>testGroupId</groupId>"
68              + "<artifactId>testArtifactId</artifactId>" + "<type>pom</type>" + "<version>0.0.0</version>"
69              + "</dependency>" + "</dependencies>";
70  
71          assertEquals( pattern, output );
72      }
73  
74      public void testShouldFindTwoScriptsWhenNoExcludesAreGiven()
75      {
76          String testScript = "test.txt";
77  
78          String basedir = TestUtils.dirname( testScript );
79  
80          String includes = "**/*.txt";
81  
82          String[] files = PluginUtils.findSources( basedir, includes );
83          assertEquals( 2, files.length );
84      }
85  
86      public void testShouldFindOneScriptsWhenAnExcludeIsGiven()
87      {
88          String testScript = "test.txt";
89  
90          String basedir = TestUtils.dirname( testScript );
91  
92          String includes = "**/*.txt";
93          String excludes = "**/*Excludes.txt";
94  
95          String[] files = PluginUtils.findSources( basedir, includes, excludes );
96          assertEquals( 1, files.length );
97      }
98  
99      public void testIsMavenReport()
100         throws Exception
101     {
102         try
103         {
104             PluginUtils.isMavenReport( null, null );
105         }
106         catch ( IllegalArgumentException e )
107         {
108             assertTrue( true );
109         }
110 
111         String impl = "org.apache.maven.tools.plugin.util.stubs.MavenReportStub";
112 
113         MavenProjectStub stub = new MavenProjectStub();
114         stub.setCompileSourceRoots( Collections.singletonList( getBasedir() + "/target/classes" ) );
115 
116         assertTrue( PluginUtils.isMavenReport( impl, stub ) );
117 
118         impl = "org.apache.maven.tools.plugin.util.stubs.MojoStub";
119         assertFalse( PluginUtils.isMavenReport( impl, stub ) );
120     }
121 
122     public void testMakeHtmlValid()
123     {
124         String javadoc = null;
125         assertEquals( "", PluginUtils.makeHtmlValid( javadoc ) );
126         javadoc = "";
127         assertEquals( "", PluginUtils.makeHtmlValid( javadoc ) );
128 
129         // true HTML
130         javadoc = "Generates <i>something</i> for the project.";
131         assertEquals( "Generates <i>something</i> for the project.", PluginUtils.makeHtmlValid( javadoc ) );
132 
133         // wrong HTML
134         javadoc = "Generates <i>something</i> <b> for the project.";
135         assertEquals( "Generates <i>something</i> <b> for the project.</b>", PluginUtils.makeHtmlValid( javadoc ) );
136 
137         // wrong XHTML
138         javadoc = "Line1<br>Line2";
139         assertEquals( "Line1<br/>Line2", PluginUtils.makeHtmlValid( javadoc ).replaceAll( "\\s", "" ) );
140 
141         // special characters
142         javadoc = "& &amp; < > \u00A0";
143         assertEquals( "&amp; &amp; &lt; &gt; \u00A0", PluginUtils.makeHtmlValid( javadoc ) );
144 
145         // non ASCII characters
146         javadoc = "\u00E4 \u00F6 \u00FC \u00DF";
147         assertEquals( javadoc, PluginUtils.makeHtmlValid( javadoc ) );
148 
149         // non Latin1 characters
150         javadoc = "\u0130 \u03A3 \u05D0 \u06DE";
151         assertEquals( javadoc, PluginUtils.makeHtmlValid( javadoc ) );
152     }
153 
154     public void testDecodeJavadocTags()
155     {
156         String javadoc = null;
157         assertEquals( "", PluginUtils.decodeJavadocTags( javadoc ) );
158 
159         javadoc = "";
160         assertEquals( "", PluginUtils.decodeJavadocTags( javadoc ) );
161 
162         javadoc = "{@code text}";
163         assertEquals( "<code>text</code>", PluginUtils.decodeJavadocTags( javadoc ) );
164 
165         javadoc = "{@code <A&B>}";
166         assertEquals( "<code>&lt;A&amp;B&gt;</code>", PluginUtils.decodeJavadocTags( javadoc ) );
167 
168         javadoc = "{@literal text}";
169         assertEquals( "text", PluginUtils.decodeJavadocTags( javadoc ) );
170 
171         javadoc = "{@literal text}  {@literal text}";
172         assertEquals( "text  text", PluginUtils.decodeJavadocTags( javadoc ) );
173 
174         javadoc = "{@literal <A&B>}";
175         assertEquals( "&lt;A&amp;B&gt;", PluginUtils.decodeJavadocTags( javadoc ) );
176 
177         javadoc = "{@link Class}";
178         assertEquals( "<code>Class</code>", PluginUtils.decodeJavadocTags( javadoc ) );
179 
180         javadoc = "{@linkplain Class}";
181         assertEquals( "Class", PluginUtils.decodeJavadocTags( javadoc ) );
182 
183         javadoc = "{@linkplain #field}";
184         assertEquals( "field", PluginUtils.decodeJavadocTags( javadoc ) );
185 
186         javadoc = "{@linkplain Class#field}";
187         assertEquals( "Class.field", PluginUtils.decodeJavadocTags( javadoc ) );
188 
189         javadoc = "{@linkplain #method()}";
190         assertEquals( "method()", PluginUtils.decodeJavadocTags( javadoc ) );
191 
192         javadoc = "{@linkplain #method(Object arg)}";
193         assertEquals( "method()", PluginUtils.decodeJavadocTags( javadoc ) );
194 
195         javadoc = "{@linkplain #method(Object, String)}";
196         assertEquals( "method()", PluginUtils.decodeJavadocTags( javadoc ) );
197 
198         javadoc = "{@linkplain #method(Object, String) label}";
199         assertEquals( "label", PluginUtils.decodeJavadocTags( javadoc ) );
200 
201         javadoc = "{@linkplain Class#method(Object, String)}";
202         assertEquals( "Class.method()", PluginUtils.decodeJavadocTags( javadoc ) );
203 
204         javadoc = "{@linkplain Class#method(Object, String) label}";
205         assertEquals( "label", PluginUtils.decodeJavadocTags( javadoc ) );
206     }
207 
208     public void testToText()
209         throws Exception
210     {
211         String javadoc = null;
212         assertEquals( "", PluginUtils.toText( javadoc ) );
213         javadoc = "";
214         assertEquals( "", PluginUtils.toText( javadoc ) );
215 
216         // line breaks
217         javadoc = "Line1\nLine2";
218         assertEquals( "Line1 Line2", PluginUtils.toText( javadoc ) );
219         javadoc = "Line1\rLine2";
220         assertEquals( "Line1 Line2", PluginUtils.toText( javadoc ) );
221         javadoc = "Line1\r\nLine2";
222         assertEquals( "Line1 Line2", PluginUtils.toText( javadoc ) );
223         javadoc = "Line1<br>Line2";
224         assertEquals( "Line1\nLine2", PluginUtils.toText( javadoc ) );
225 
226         // true HTML
227         javadoc = "Generates <i>something</i> for the project.";
228         assertEquals( "Generates something for the project.", PluginUtils.toText( javadoc ) );
229 
230         // wrong HTML
231         javadoc = "Generates <i>something</i> <b> for the project.";
232         assertEquals( "Generates something for the project.", PluginUtils.toText( javadoc ) );
233 
234         // javadoc inline tags
235         javadoc = "Generates {@code something} for the project.";
236         assertEquals( "Generates something for the project.", PluginUtils.toText( javadoc ) );
237     }
238 
239 }