View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.tools.plugin.generator;
20  
21  import java.io.StringWriter;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.DefaultArtifact;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.codehaus.plexus.component.repository.ComponentDependency;
31  import org.codehaus.plexus.util.xml.CompactXMLWriter;
32  import org.codehaus.plexus.util.xml.XMLWriter;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertFalse;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  /**
40   * @author jdcasey
41   */
42  class GeneratorUtilsTest {
43      @Test
44      void testShouldWriteDependencies() throws Exception {
45          ComponentDependency dependency = new ComponentDependency();
46          dependency.setArtifactId("testArtifactId");
47          dependency.setGroupId("testGroupId");
48          dependency.setType("pom");
49          dependency.setVersion("0.0.0");
50  
51          PluginDescriptor descriptor = new PluginDescriptor();
52          descriptor.setDependencies(Collections.singletonList(dependency));
53  
54          StringWriter sWriter = new StringWriter();
55          XMLWriter writer = new CompactXMLWriter(sWriter);
56  
57          GeneratorUtils.writeDependencies(writer, descriptor);
58  
59          String output = sWriter.toString();
60  
61          String pattern = "<dependencies>" + "<dependency>" + "<groupId>testGroupId</groupId>"
62                  + "<artifactId>testArtifactId</artifactId>" + "<type>pom</type>" + "<version>0.0.0</version>"
63                  + "</dependency>" + "</dependencies>";
64  
65          assertEquals(pattern, output);
66      }
67  
68      /*
69          @Test
70          void testMakeHtmlValid()
71          {
72              String javadoc = null;
73              assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
74              javadoc = "";
75              assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
76  
77              // true HTML
78              javadoc = "Generates <i>something</i> for the project.";
79              assertEquals( "Generates <i>something</i> for the project.", GeneratorUtils.makeHtmlValid( javadoc ) );
80  
81              // wrong HTML
82              javadoc = "Generates <i>something</i> <b> for the project.";
83              assertEquals( "Generates <i>something</i> <b> for the project.</b>", GeneratorUtils.makeHtmlValid( javadoc ) );
84  
85              // wrong XHTML
86              javadoc = "Line1<br>Line2";
87              assertEquals( "Line1<br/>Line2", GeneratorUtils.makeHtmlValid( javadoc ).replaceAll( "\\s", "" ) );
88  
89              // special characters
90              javadoc = "& &amp; < > \u00A0";
91              assertEquals( "&amp; &amp; &lt; &gt; \u00A0", GeneratorUtils.makeHtmlValid( javadoc ) );
92  
93              // non ASCII characters
94              javadoc = "\u00E4 \u00F6 \u00FC \u00DF";
95              assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
96  
97              // non Latin1 characters
98              javadoc = "\u0130 \u03A3 \u05D0 \u06DE";
99              assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
100         }
101     */
102     /*
103     @Test
104     void testDecodeJavadocTags()
105     {
106         String javadoc = null;
107         assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
108 
109         javadoc = "";
110         assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
111 
112         javadoc = "{@code text}";
113         assertEquals( "<code>text</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
114 
115         javadoc = "{@code <A&B>}";
116         assertEquals( "<code>&lt;A&amp;B&gt;</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
117 
118         javadoc = "{@literal text}";
119         assertEquals( "text", GeneratorUtils.decodeJavadocTags( javadoc ) );
120 
121         javadoc = "{@literal text}  {@literal text}";
122         assertEquals( "text  text", GeneratorUtils.decodeJavadocTags( javadoc ) );
123 
124         javadoc = "{@literal <A&B>}";
125         assertEquals( "&lt;A&amp;B&gt;", GeneratorUtils.decodeJavadocTags( javadoc ) );
126 
127         javadoc = "{@link Class}";
128         assertEquals( "<code>Class</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
129 
130         javadoc = "{@linkplain Class}";
131         assertEquals( "Class", GeneratorUtils.decodeJavadocTags( javadoc ) );
132 
133         javadoc = "{@linkplain #field}";
134         assertEquals( "field", GeneratorUtils.decodeJavadocTags( javadoc ) );
135 
136         javadoc = "{@linkplain Class#field}";
137         assertEquals( "Class.field", GeneratorUtils.decodeJavadocTags( javadoc ) );
138 
139         javadoc = "{@linkplain #method()}";
140         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
141 
142         javadoc = "{@linkplain #method(Object arg)}";
143         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
144 
145         javadoc = "{@linkplain #method(Object, String)}";
146         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
147 
148         javadoc = "{@linkplain #method(Object, String) label}";
149         assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
150 
151         javadoc = "{@linkplain Class#method(Object, String)}";
152         assertEquals( "Class.method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
153 
154         javadoc = "{@linkplain Class#method(Object, String) label}";
155         assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
156     }*/
157 
158     @Test
159     void testToText() throws Exception {
160         String javadoc = null;
161         assertEquals("", GeneratorUtils.toText(javadoc));
162         javadoc = "";
163         assertEquals("", GeneratorUtils.toText(javadoc));
164 
165         // line breaks
166         javadoc = "Line1\nLine2";
167         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
168         javadoc = "Line1\rLine2";
169         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
170         javadoc = "Line1\r\nLine2";
171         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
172         javadoc = "Line1<br>Line2";
173         assertEquals("Line1\nLine2", GeneratorUtils.toText(javadoc));
174 
175         // true HTML
176         javadoc = "Generates <i>something</i> for the project.";
177         assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
178 
179         // wrong HTML
180         javadoc = "Generates <i>something</i> <b> for the project.";
181         assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
182 
183         // javadoc inline tags
184         // javadoc = "Generates {@code something} for the project.";
185         // assertEquals( "Generates something for the project.", GeneratorUtils.toText( javadoc ) );
186     }
187 
188     @Test
189     void testIsMavenReport() throws Exception {
190         try {
191             GeneratorUtils.isMavenReport(null, null);
192         } catch (IllegalArgumentException e) {
193             assertTrue(true);
194         }
195 
196         String impl = "org.apache.maven.tools.plugin.generator.stubs.MavenReportStub";
197 
198         MavenProjectStub stub = new MavenProjectStub();
199         stub.setCompileSourceRoots(Collections.singletonList(System.getProperty("basedir") + "/target/classes"));
200 
201         assertTrue(GeneratorUtils.isMavenReport(impl, stub));
202 
203         impl = "org.apache.maven.tools.plugin.util.stubs.MojoStub";
204         assertFalse(GeneratorUtils.isMavenReport(impl, stub));
205     }
206 
207     @Test
208     void testExcludeProvidedScopeFormComponentDependencies() {
209 
210         Artifact a1 = new DefaultArtifact("g", "a1", "1.0", Artifact.SCOPE_COMPILE, "jar", "", null);
211         Artifact a2 = new DefaultArtifact("g", "a2", "2.0", Artifact.SCOPE_PROVIDED, "jar", "", null);
212         Artifact a3 = new DefaultArtifact("g", "a3", "3.0", Artifact.SCOPE_RUNTIME, "jar", "", null);
213         List<Artifact> depList = Arrays.asList(a1, a2, a3);
214 
215         List<ComponentDependency> componentDependencies = GeneratorUtils.toComponentDependencies(depList);
216 
217         assertEquals(2, componentDependencies.size());
218 
219         ComponentDependency componentDependency1 = componentDependencies.get(0);
220         assertEquals(a1.getGroupId(), componentDependency1.getGroupId());
221         assertEquals(a1.getArtifactId(), componentDependency1.getArtifactId());
222         assertEquals(a1.getVersion(), componentDependency1.getVersion());
223         assertEquals(a1.getType(), componentDependency1.getType());
224 
225         ComponentDependency componentDependency2 = componentDependencies.get(1);
226         assertEquals(a3.getGroupId(), componentDependency2.getGroupId());
227         assertEquals(a3.getArtifactId(), componentDependency2.getArtifactId());
228         assertEquals(a3.getVersion(), componentDependency2.getVersion());
229         assertEquals(a3.getType(), componentDependency2.getType());
230     }
231 }