1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.inheritance;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.maven.api.model.Model;
25 import org.apache.maven.model.building.SimpleProblemCollector;
26 import org.apache.maven.model.io.DefaultModelReader;
27 import org.apache.maven.model.io.DefaultModelWriter;
28 import org.apache.maven.model.io.ModelWriter;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.xmlunit.matchers.CompareMatcher;
32
33 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.junit.jupiter.api.Assertions.assertThrows;
35 import static org.junit.jupiter.api.Assertions.assertTrue;
36
37
38
39 class DefaultInheritanceAssemblerTest {
40 private DefaultModelReader reader;
41
42 private ModelWriter writer;
43
44 private InheritanceAssembler assembler;
45
46 @BeforeEach
47 void setUp() throws Exception {
48 reader = new DefaultModelReader(null);
49 writer = new DefaultModelWriter();
50 assembler = new DefaultInheritanceAssembler();
51 }
52
53 private File getPom(String name) {
54 return new File("src/test/resources/poms/inheritance/" + name + ".xml");
55 }
56
57 private Model getModel(String name) throws IOException {
58 return reader.read(getPom(name), null).getDelegate();
59 }
60
61 @Test
62 void testPluginConfiguration() throws Exception {
63 testInheritance("plugin-configuration");
64 }
65
66
67
68
69
70
71 @Test
72 void testUrls() throws Exception {
73 testInheritance("urls");
74 }
75
76
77
78
79
80 @Test
81 void testFlatUrls() throws IOException {
82 testInheritance("flat-urls");
83 }
84
85
86
87
88
89 @Test
90 void testNoAppendUrls() throws Exception {
91 testInheritance("no-append-urls");
92 }
93
94
95
96
97
98 @Test
99 void testNoAppendUrls2() throws Exception {
100 testInheritance("no-append-urls2");
101 }
102
103
104
105
106
107 @Test
108 void testNoAppendUrls3() throws Exception {
109 testInheritance("no-append-urls3");
110 }
111
112
113
114
115
116
117
118 @Test
119 void testFlatTrickyUrls() throws IOException {
120
121
122 try {
123
124 testInheritance("tricky-flat-artifactId-urls", false);
125
126 } catch (AssertionError afe) {
127
128 assertTrue(
129 afe.getMessage()
130 .contains(
131 "Expected text value 'http://www.apache.org/path/to/parent/child-artifact-id/' but was "
132 + "'http://www.apache.org/path/to/parent/../child-artifact-id/'"),
133 afe.getMessage());
134 }
135
136 testInheritance("tricky-flat-artifactId-urls", true);
137
138
139
140 testInheritance("tricky-flat-directory-urls", false);
141
142 AssertionError afe = assertThrows(
143 AssertionError.class,
144 () -> testInheritance("tricky-flat-directory-urls", true),
145 "should have failed since module reference == directory name != artifactId");
146
147 assertTrue(
148 afe.getMessage()
149 .contains(
150 "Expected text value 'http://www.apache.org/path/to/parent/../child-artifact-id/' but was "
151 + "'http://www.apache.org/path/to/parent/child-artifact-id/'"),
152 afe.getMessage());
153 }
154
155 @Test
156 void testWithEmptyUrl() throws IOException {
157 testInheritance("empty-urls", false);
158 }
159
160 public void testInheritance(String baseName) throws IOException {
161 testInheritance(baseName, false);
162 testInheritance(baseName, true);
163 }
164
165 public void testInheritance(String baseName, boolean fromRepo) throws IOException {
166 Model parent = getModel(baseName + "-parent");
167
168 Model child = getModel(baseName + "-child");
169
170 if (fromRepo) {
171
172
173 parent = Model.newBuilder(parent, true).pomFile(null).build();
174 child = Model.newBuilder(child, true).pomFile(null).build();
175 }
176
177 SimpleProblemCollector problems = new SimpleProblemCollector();
178
179 Model assembled = assembler.assembleModelInheritance(child, parent, null, problems);
180
181
182 File actual = new File(
183 "target/test-classes/poms/inheritance/" + baseName + (fromRepo ? "-build" : "-repo") + "-actual.xml");
184 writer.write(actual, null, assembled);
185
186
187 File expected = getPom(baseName + "-expected");
188
189 assertThat(
190 actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace());
191 }
192
193 @Test
194 void testModulePathNotArtifactId() throws IOException {
195 Model parent = getModel("module-path-not-artifactId-parent");
196
197 Model child = getModel("module-path-not-artifactId-child");
198
199 SimpleProblemCollector problems = new SimpleProblemCollector();
200
201 Model model = assembler.assembleModelInheritance(child, parent, null, problems);
202
203 File actual = new File("target/test-classes/poms/inheritance/module-path-not-artifactId-actual.xml");
204
205 writer.write(actual, null, model);
206
207
208 File expected = getPom("module-path-not-artifactId-expected");
209
210 assertThat(
211 actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace());
212 }
213 }