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.codehaus.plexus.component.repository.ComponentDependency;
30  import org.codehaus.plexus.util.xml.CompactXMLWriter;
31  import org.codehaus.plexus.util.xml.XMLWriter;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  
36  /**
37   * @author jdcasey
38   */
39  class GeneratorUtilsTest {
40      @Test
41      void testShouldWriteDependencies() throws Exception {
42          ComponentDependency dependency = new ComponentDependency();
43          dependency.setArtifactId("testArtifactId");
44          dependency.setGroupId("testGroupId");
45          dependency.setType("pom");
46          dependency.setVersion("0.0.0");
47  
48          PluginDescriptor descriptor = new PluginDescriptor();
49          descriptor.setDependencies(Collections.singletonList(dependency));
50  
51          StringWriter sWriter = new StringWriter();
52          XMLWriter writer = new CompactXMLWriter(sWriter);
53  
54          GeneratorUtils.writeDependencies(writer, descriptor);
55  
56          String output = sWriter.toString();
57  
58          String pattern = "<dependencies>" + "<dependency>" + "<groupId>testGroupId</groupId>"
59                  + "<artifactId>testArtifactId</artifactId>" + "<type>pom</type>" + "<version>0.0.0</version>"
60                  + "</dependency>" + "</dependencies>";
61  
62          assertEquals(pattern, output);
63      }
64  
65      /*
66          @Test
67          void testMakeHtmlValid()
68          {
69              String javadoc = null;
70              assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
71              javadoc = "";
72              assertEquals( "", GeneratorUtils.makeHtmlValid( javadoc ) );
73  
74              // true HTML
75              javadoc = "Generates <i>something</i> for the project.";
76              assertEquals( "Generates <i>something</i> for the project.", GeneratorUtils.makeHtmlValid( javadoc ) );
77  
78              // wrong HTML
79              javadoc = "Generates <i>something</i> <b> for the project.";
80              assertEquals( "Generates <i>something</i> <b> for the project.</b>", GeneratorUtils.makeHtmlValid( javadoc ) );
81  
82              // wrong XHTML
83              javadoc = "Line1<br>Line2";
84              assertEquals( "Line1<br/>Line2", GeneratorUtils.makeHtmlValid( javadoc ).replaceAll( "\\s", "" ) );
85  
86              // special characters
87              javadoc = "& &amp; < > \u00A0";
88              assertEquals( "&amp; &amp; &lt; &gt; \u00A0", GeneratorUtils.makeHtmlValid( javadoc ) );
89  
90              // non ASCII characters
91              javadoc = "\u00E4 \u00F6 \u00FC \u00DF";
92              assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
93  
94              // non Latin1 characters
95              javadoc = "\u0130 \u03A3 \u05D0 \u06DE";
96              assertEquals( javadoc, GeneratorUtils.makeHtmlValid( javadoc ) );
97          }
98      */
99      /*
100     @Test
101     void testDecodeJavadocTags()
102     {
103         String javadoc = null;
104         assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
105 
106         javadoc = "";
107         assertEquals( "", GeneratorUtils.decodeJavadocTags( javadoc ) );
108 
109         javadoc = "{@code text}";
110         assertEquals( "<code>text</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
111 
112         javadoc = "{@code <A&B>}";
113         assertEquals( "<code>&lt;A&amp;B&gt;</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
114 
115         javadoc = "{@literal text}";
116         assertEquals( "text", GeneratorUtils.decodeJavadocTags( javadoc ) );
117 
118         javadoc = "{@literal text}  {@literal text}";
119         assertEquals( "text  text", GeneratorUtils.decodeJavadocTags( javadoc ) );
120 
121         javadoc = "{@literal <A&B>}";
122         assertEquals( "&lt;A&amp;B&gt;", GeneratorUtils.decodeJavadocTags( javadoc ) );
123 
124         javadoc = "{@link Class}";
125         assertEquals( "<code>Class</code>", GeneratorUtils.decodeJavadocTags( javadoc ) );
126 
127         javadoc = "{@linkplain Class}";
128         assertEquals( "Class", GeneratorUtils.decodeJavadocTags( javadoc ) );
129 
130         javadoc = "{@linkplain #field}";
131         assertEquals( "field", GeneratorUtils.decodeJavadocTags( javadoc ) );
132 
133         javadoc = "{@linkplain Class#field}";
134         assertEquals( "Class.field", GeneratorUtils.decodeJavadocTags( javadoc ) );
135 
136         javadoc = "{@linkplain #method()}";
137         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
138 
139         javadoc = "{@linkplain #method(Object arg)}";
140         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
141 
142         javadoc = "{@linkplain #method(Object, String)}";
143         assertEquals( "method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
144 
145         javadoc = "{@linkplain #method(Object, String) label}";
146         assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
147 
148         javadoc = "{@linkplain Class#method(Object, String)}";
149         assertEquals( "Class.method()", GeneratorUtils.decodeJavadocTags( javadoc ) );
150 
151         javadoc = "{@linkplain Class#method(Object, String) label}";
152         assertEquals( "label", GeneratorUtils.decodeJavadocTags( javadoc ) );
153     }*/
154 
155     @Test
156     void testToText() throws Exception {
157         String javadoc = null;
158         assertEquals("", GeneratorUtils.toText(javadoc));
159         javadoc = "";
160         assertEquals("", GeneratorUtils.toText(javadoc));
161 
162         // line breaks
163         javadoc = "Line1\nLine2";
164         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
165         javadoc = "Line1\rLine2";
166         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
167         javadoc = "Line1\r\nLine2";
168         assertEquals("Line1 Line2", GeneratorUtils.toText(javadoc));
169         javadoc = "Line1<br>Line2";
170         assertEquals("Line1\nLine2", GeneratorUtils.toText(javadoc));
171 
172         // true HTML
173         javadoc = "Generates <i>something</i> for the project.";
174         assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
175 
176         // wrong HTML
177         javadoc = "Generates <i>something</i> <b> for the project.";
178         assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
179 
180         // javadoc inline tags
181         // javadoc = "Generates {@code something} for the project.";
182         // assertEquals( "Generates something for the project.", GeneratorUtils.toText( javadoc ) );
183     }
184 
185     @Test
186     void testExcludeProvidedScopeFormComponentDependencies() {
187 
188         Artifact a1 = new DefaultArtifact("g", "a1", "1.0", Artifact.SCOPE_COMPILE, "jar", "", null);
189         Artifact a2 = new DefaultArtifact("g", "a2", "2.0", Artifact.SCOPE_PROVIDED, "jar", "", null);
190         Artifact a3 = new DefaultArtifact("g", "a3", "3.0", Artifact.SCOPE_RUNTIME, "jar", "", null);
191         List<Artifact> depList = Arrays.asList(a1, a2, a3);
192 
193         List<ComponentDependency> componentDependencies = GeneratorUtils.toComponentDependencies(depList);
194 
195         assertEquals(2, componentDependencies.size());
196 
197         ComponentDependency componentDependency1 = componentDependencies.get(0);
198         assertEquals(a1.getGroupId(), componentDependency1.getGroupId());
199         assertEquals(a1.getArtifactId(), componentDependency1.getArtifactId());
200         assertEquals(a1.getVersion(), componentDependency1.getVersion());
201         assertEquals(a1.getType(), componentDependency1.getType());
202 
203         ComponentDependency componentDependency2 = componentDependencies.get(1);
204         assertEquals(a3.getGroupId(), componentDependency2.getGroupId());
205         assertEquals(a3.getArtifactId(), componentDependency2.getArtifactId());
206         assertEquals(a3.getVersion(), componentDependency2.getVersion());
207         assertEquals(a3.getType(), componentDependency2.getType());
208     }
209 }