1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
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
176 javadoc = "Generates <i>something</i> for the project.";
177 assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
178
179
180 javadoc = "Generates <i>something</i> <b> for the project.";
181 assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
182
183
184
185
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 }