View Javadoc
1   package org.apache.maven.model.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Model;
23  import org.apache.maven.model.building.SimpleProblemCollector;
24  import org.apache.maven.model.io.DefaultModelReader;
25  import org.apache.maven.model.io.DefaultModelWriter;
26  import org.apache.maven.model.io.ModelReader;
27  import org.apache.maven.model.io.ModelWriter;
28  
29  import org.xmlunit.matchers.CompareMatcher;
30  
31  import junit.framework.TestCase;
32  
33  import java.io.File;
34  import java.io.IOException;
35  
36  import static org.junit.Assert.assertThat;
37  
38  /**
39   * @author Hervé Boutemy
40   */
41  public class DefaultInheritanceAssemblerTest
42      extends TestCase
43  {
44      private ModelReader reader;
45  
46      private ModelWriter writer;
47  
48      private InheritanceAssembler assembler;
49  
50      @Override
51      protected void setUp()
52          throws Exception
53      {
54          super.setUp();
55  
56          reader = new DefaultModelReader();
57          writer = new DefaultModelWriter();
58          assembler = new DefaultInheritanceAssembler();
59      }
60  
61      private File getPom( String name )
62      {
63          return new File( "src/test/resources/poms/inheritance/" + name + ".xml" );
64      }
65  
66      private Model getModel( String name )
67          throws IOException
68      {
69          return reader.read( getPom( name ), null );
70      }
71  
72      public void testPluginConfiguration()
73          throws Exception
74      {
75          testInheritance( "plugin-configuration" );
76      }
77  
78      /**
79       * Check most classical urls inheritance: directory structure where parent POM in parent directory
80       * and child directory == artifactId
81       * @throws IOException Model read problem
82       */
83      public void testUrls()
84          throws Exception
85      {
86          testInheritance( "urls" );
87      }
88  
89      /**
90       * Flat directory structure: parent & child POMs in sibling directories, child directory == artifactId.
91       * @throws IOException Model read problem
92       */
93      public void testFlatUrls()
94          throws IOException
95      {
96          testInheritance( "flat-urls" );
97      }
98  
99      /**
100      * MNG-5951 MNG-6059 child.x.y.inherit.append.path="false" test
101      * @throws Exception
102      */
103     public void testNoAppendUrls()
104         throws Exception
105     {
106         testInheritance( "no-append-urls" );
107     }
108 
109     /**
110      * MNG-5951 special case test: inherit with partial override
111      * @throws Exception
112      */
113     public void testNoAppendUrls2()
114         throws Exception
115     {
116         testInheritance( "no-append-urls2" );
117     }
118 
119     /**
120      * MNG-5951 special case test: child.x.y.inherit.append.path="true" in child should not reset content
121      * @throws Exception
122      */
123     public void testNoAppendUrls3()
124         throws Exception
125     {
126         testInheritance( "no-append-urls3" );
127     }
128 
129     /**
130      * Tricky case: flat directory structure, but child directory != artifactId.
131      * Model interpolation does not give same result when calculated from build or from repo...
132      * This is why MNG-5000 fix in code is marked as bad practice (uses file names)
133      * @throws IOException Model read problem
134      */
135     public void testFlatTrickyUrls()
136         throws IOException
137     {
138         // parent references child with artifactId (which is not directory name)
139         // then relative path calculation will fail during build from disk but success when calculated from repo
140         try
141         {
142             // build from disk expected to fail
143             testInheritance( "tricky-flat-artifactId-urls", false );
144             //fail( "should have failed since module reference == artifactId != directory name" );
145         }
146         catch ( AssertionError afe )
147         {
148             // expected failure: wrong relative path calculation
149             assertTrue( afe.getMessage(),
150                         afe.getMessage().contains(
151                                 "Expected text value 'http://www.apache.org/path/to/parent/child-artifact-id/' but was " +
152                                         "'http://www.apache.org/path/to/parent/../child-artifact-id/'" ) );
153         }
154         // but ok from repo: local disk is ignored
155         testInheritance( "tricky-flat-artifactId-urls", true );
156 
157         // parent references child with directory name (which is not artifact id)
158         // then relative path calculation will success during build from disk but fail when calculated from repo
159         testInheritance( "tricky-flat-directory-urls", false );
160         try
161         {
162             testInheritance( "tricky-flat-directory-urls", true );
163             fail( "should have failed since module reference == directory name != artifactId" );
164         }
165         catch ( AssertionError afe )
166         {
167             // expected failure
168             assertTrue( afe.getMessage(), afe.getMessage().contains(
169                     "Expected text value 'http://www.apache.org/path/to/parent/../child-artifact-id/' but was " +
170                             "'http://www.apache.org/path/to/parent/child-artifact-id/'" ) );
171         }
172     }
173 
174     public void testWithEmptyUrl() 
175         throws IOException
176     {
177         	testInheritance( "empty-urls", false );
178     }
179     
180     public void testInheritance( String baseName )
181         throws IOException
182     {
183         testInheritance( baseName, false );
184         testInheritance( baseName, true );
185     }
186 
187     public void testInheritance( String baseName, boolean fromRepo )
188         throws IOException
189     {
190         Model parent = getModel( baseName + "-parent" );
191 
192         Model child = getModel( baseName + "-child" );
193 
194         if ( fromRepo )
195         {
196             // when model is read from repo, a stream is used, then pomFile == null
197             // (has consequences in inheritance algorithm since getProjectDirectory() returns null)
198             parent.setPomFile( null );
199             child.setPomFile( null );
200         }
201 
202         SimpleProblemCollector problems = new SimpleProblemCollector();
203 
204         assembler.assembleModelInheritance( child, parent, null, problems );
205 
206         // write baseName + "-actual"
207         File actual = new File( "target/test-classes/poms/inheritance/" + baseName
208             + ( fromRepo ? "-build" : "-repo" ) + "-actual.xml" );
209         writer.write( actual, null, child );
210 
211         // check with getPom( baseName + "-expected" )
212         File expected = getPom( baseName + "-expected" );
213 
214         assertThat( actual, CompareMatcher.isIdenticalTo( expected ).ignoreComments().ignoreWhitespace() );
215     }
216 
217     public void testModulePathNotArtifactId()
218         throws IOException
219     {
220         Model parent = getModel( "module-path-not-artifactId-parent" );
221 
222         Model child = getModel( "module-path-not-artifactId-child" );
223 
224         SimpleProblemCollector problems = new SimpleProblemCollector();
225 
226         assembler.assembleModelInheritance( child, parent, null, problems );
227 
228         File actual = new File( "target/test-classes/poms/inheritance/module-path-not-artifactId-actual.xml" );
229 
230         writer.write( actual, null, child );
231 
232         // check with getPom( "module-path-not-artifactId-effective" )
233         File expected = getPom( "module-path-not-artifactId-expected" );
234 
235         assertThat( actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace() );
236     }
237 }