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 org.apache.maven.api.model.Model;
25  import org.apache.maven.model.building.SimpleProblemCollector;
26  import org.apache.maven.model.io.DefaultModelReader;
27  import org.apache.maven.model.io.DefaultModelWriter;
28  import org.apache.maven.model.io.ModelWriter;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  import org.xmlunit.matchers.CompareMatcher;
32  
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   */
39  class DefaultInheritanceAssemblerTest {
40      private DefaultModelReader reader;
41  
42      private ModelWriter writer;
43  
44      private InheritanceAssembler assembler;
45  
46      @BeforeEach
47      void setUp() throws Exception {
48          reader = new DefaultModelReader(null);
49          writer = new DefaultModelWriter();
50          assembler = new DefaultInheritanceAssembler();
51      }
52  
53      private File getPom(String name) {
54          return new File("src/test/resources/poms/inheritance/" + name + ".xml");
55      }
56  
57      private Model getModel(String name) throws IOException {
58          return reader.read(getPom(name), null).getDelegate();
59      }
60  
61      @Test
62      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      @Test
72      void testUrls() throws Exception {
73          testInheritance("urls");
74      }
75  
76      /**
77       * Flat directory structure: parent & child POMs in sibling directories, child directory == artifactId.
78       * @throws IOException Model read problem
79       */
80      @Test
81      void testFlatUrls() throws IOException {
82          testInheritance("flat-urls");
83      }
84  
85      /**
86       * MNG-5951 MNG-6059 child.x.y.inherit.append.path="false" test
87       * @throws Exception
88       */
89      @Test
90      void testNoAppendUrls() throws Exception {
91          testInheritance("no-append-urls");
92      }
93  
94      /**
95       * MNG-5951 special case test: inherit with partial override
96       * @throws Exception
97       */
98      @Test
99      void testNoAppendUrls2() throws Exception {
100         testInheritance("no-append-urls2");
101     }
102 
103     /**
104      * MNG-5951 special case test: child.x.y.inherit.append.path="true" in child should not reset content
105      * @throws Exception
106      */
107     @Test
108     void testNoAppendUrls3() throws Exception {
109         testInheritance("no-append-urls3");
110     }
111 
112     /**
113      * Tricky case: flat directory structure, but child directory != artifactId.
114      * Model interpolation does not give same result when calculated from build or from repo...
115      * This is why MNG-5000 fix in code is marked as bad practice (uses file names)
116      * @throws IOException Model read problem
117      */
118     @Test
119     void testFlatTrickyUrls() throws IOException {
120         // parent references child with artifactId (which is not directory name)
121         // then relative path calculation will fail during build from disk but success when calculated from repo
122         try {
123             // build from disk expected to fail
124             testInheritance("tricky-flat-artifactId-urls", false);
125             // fail( "should have failed since module reference == artifactId != directory name" );
126         } catch (AssertionError afe) {
127             // expected failure: wrong relative path calculation
128             assertTrue(
129                     afe.getMessage()
130                             .contains(
131                                     "Expected text value 'http://www.apache.org/path/to/parent/child-artifact-id/' but was "
132                                             + "'http://www.apache.org/path/to/parent/../child-artifact-id/'"),
133                     afe.getMessage());
134         }
135         // but ok from repo: local disk is ignored
136         testInheritance("tricky-flat-artifactId-urls", true);
137 
138         // parent references child with directory name (which is not artifact id)
139         // then relative path calculation will success during build from disk but fail when calculated from repo
140         testInheritance("tricky-flat-directory-urls", false);
141 
142         AssertionError afe = assertThrows(
143                 AssertionError.class,
144                 () -> testInheritance("tricky-flat-directory-urls", true),
145                 "should have failed since module reference == directory name != artifactId");
146         // expected failure
147         assertTrue(
148                 afe.getMessage()
149                         .contains(
150                                 "Expected text value 'http://www.apache.org/path/to/parent/../child-artifact-id/' but was "
151                                         + "'http://www.apache.org/path/to/parent/child-artifact-id/'"),
152                 afe.getMessage());
153     }
154 
155     @Test
156     void testWithEmptyUrl() throws IOException {
157         testInheritance("empty-urls", false);
158     }
159 
160     public void testInheritance(String baseName) throws IOException {
161         testInheritance(baseName, false);
162         testInheritance(baseName, true);
163     }
164 
165     public void testInheritance(String baseName, boolean fromRepo) throws IOException {
166         Model parent = getModel(baseName + "-parent");
167 
168         Model child = getModel(baseName + "-child");
169 
170         if (fromRepo) {
171             // when model is read from repo, a stream is used, then pomFile == null
172             // (has consequences in inheritance algorithm since getProjectDirectory() returns null)
173             parent = Model.newBuilder(parent, true).pomFile(null).build();
174             child = Model.newBuilder(child, true).pomFile(null).build();
175         }
176 
177         SimpleProblemCollector problems = new SimpleProblemCollector();
178 
179         Model assembled = assembler.assembleModelInheritance(child, parent, null, problems);
180 
181         // write baseName + "-actual"
182         File actual = new File(
183                 "target/test-classes/poms/inheritance/" + baseName + (fromRepo ? "-build" : "-repo") + "-actual.xml");
184         writer.write(actual, null, assembled);
185 
186         // check with getPom( baseName + "-expected" )
187         File expected = getPom(baseName + "-expected");
188 
189         assertThat(
190                 actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace());
191     }
192 
193     @Test
194     void testModulePathNotArtifactId() throws IOException {
195         Model parent = getModel("module-path-not-artifactId-parent");
196 
197         Model child = getModel("module-path-not-artifactId-child");
198 
199         SimpleProblemCollector problems = new SimpleProblemCollector();
200 
201         Model model = assembler.assembleModelInheritance(child, parent, null, problems);
202 
203         File actual = new File("target/test-classes/poms/inheritance/module-path-not-artifactId-actual.xml");
204 
205         writer.write(actual, null, model);
206 
207         // check with getPom( "module-path-not-artifactId-effective" )
208         File expected = getPom("module-path-not-artifactId-expected");
209 
210         assertThat(
211                 actual, CompareMatcher.isIdenticalTo(expected).ignoreComments().ignoreWhitespace());
212     }
213 }