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