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