View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Hervé Boutemy
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       * Check most classical urls inheritance: directory structure where parent POM in parent directory
68       * and child directory == artifactId
69       * @throws IOException Model read problem
70       */
71      public void testUrls() throws Exception {
72          testInheritance("urls");
73      }
74  
75      /**
76       * Flat directory structure: parent & child POMs in sibling directories, child directory == artifactId.
77       * @throws IOException Model read problem
78       */
79      public void testFlatUrls() throws IOException {
80          testInheritance("flat-urls");
81      }
82  
83      /**
84       * MNG-5951 MNG-6059 child.x.y.inherit.append.path="false" test
85       * @throws Exception
86       */
87      public void testNoAppendUrls() throws Exception {
88          testInheritance("no-append-urls");
89      }
90  
91      /**
92       * MNG-5951 special case test: inherit with partial override
93       * @throws Exception
94       */
95      public void testNoAppendUrls2() throws Exception {
96          testInheritance("no-append-urls2");
97      }
98  
99      /**
100      * MNG-5951 special case test: child.x.y.inherit.append.path="true" in child should not reset content
101      * @throws Exception
102      */
103     public void testNoAppendUrls3() throws Exception {
104         testInheritance("no-append-urls3");
105     }
106 
107     /**
108      * Tricky case: flat directory structure, but child directory != artifactId.
109      * Model interpolation does not give same result when calculated from build or from repo...
110      * This is why MNG-5000 fix in code is marked as bad practice (uses file names)
111      * @throws IOException Model read problem
112      */
113     public void testFlatTrickyUrls() throws IOException {
114         // parent references child with artifactId (which is not directory name)
115         // then relative path calculation will fail during build from disk but success when calculated from repo
116         try {
117             // build from disk expected to fail
118             testInheritance("tricky-flat-artifactId-urls", false);
119             // fail( "should have failed since module reference == artifactId != directory name" );
120         } catch (AssertionError afe) {
121             // expected failure: wrong relative path calculation
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         // but ok from repo: local disk is ignored
130         testInheritance("tricky-flat-artifactId-urls", true);
131 
132         // parent references child with directory name (which is not artifact id)
133         // then relative path calculation will success during build from disk but fail when calculated from repo
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             // expected failure
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             // when model is read from repo, a stream is used, then pomFile == null
165             // (has consequences in inheritance algorithm since getProjectDirectory() returns null)
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         // write baseName + "-actual"
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         // check with getPom( baseName + "-expected" )
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         // check with getPom( "module-path-not-artifactId-effective" )
200         File expected = getPom("module-path-not-artifactId-expected");
201 
202         assertThat(
203                 actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace());
204     }
205 }