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.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
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
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 @Test
156 void testToText() throws Exception {
157 String javadoc = null;
158 assertEquals("", GeneratorUtils.toText(javadoc));
159 javadoc = "";
160 assertEquals("", GeneratorUtils.toText(javadoc));
161
162
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
173 javadoc = "Generates <i>something</i> for the project.";
174 assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
175
176
177 javadoc = "Generates <i>something</i> <b> for the project.";
178 assertEquals("Generates something for the project.", GeneratorUtils.toText(javadoc));
179
180
181
182
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 }