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