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.InputStream;
22  import java.io.StringReader;
23  
24  import org.apache.maven.api.model.InputLocation;
25  import org.apache.maven.api.model.InputSource;
26  import org.apache.maven.api.model.Model;
27  import org.apache.maven.model.v4.MavenMerger;
28  import org.apache.maven.model.v4.MavenStaxReader;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertNotNull;
33  
34  class MergerTest {
35  
36      @Test
37      void testMergerPreserveLocations() throws Exception {
38          try (InputStream is = getClass().getResourceAsStream("/poms/factory/complex.xml")) {
39  
40              InputSource inputSource = new InputSource(null, "classpath:/poms/factory/complex.xml");
41              Model model = new MavenStaxReader().read(is, true, inputSource);
42              InputLocation propertiesLocation = model.getLocation("properties");
43              assertNotNull(propertiesLocation);
44              assertEquals(13, propertiesLocation.getLineNumber());
45              assertEquals(5, propertiesLocation.getColumnNumber());
46              InputLocation filterPropLocation = propertiesLocation.getLocation("my.filter.value");
47              assertNotNull(filterPropLocation);
48              assertEquals(14, filterPropLocation.getLineNumber());
49              assertEquals(31, filterPropLocation.getColumnNumber());
50  
51              Model model2 = Model.newBuilder(model).build();
52              propertiesLocation = model2.getLocation("properties");
53              assertNotNull(propertiesLocation);
54              assertEquals(13, propertiesLocation.getLineNumber());
55              assertEquals(5, propertiesLocation.getColumnNumber());
56              filterPropLocation = propertiesLocation.getLocation("my.filter.value");
57              assertNotNull(filterPropLocation);
58              assertEquals(14, filterPropLocation.getLineNumber());
59              assertEquals(31, filterPropLocation.getColumnNumber());
60              assertNotNull(model.getLocation("groupId"));
61  
62              Model mergeInput = new MavenStaxReader()
63                      .read(
64                              new StringReader("<project>\n"
65                                      + "  <properties>\n"
66                                      + "    <my.prop>my-value</my.prop>\n"
67                                      + "  </properties>\n"
68                                      + "</project>"),
69                              true,
70                              new InputSource(null, "merge-input"));
71              propertiesLocation = mergeInput.getLocation("properties");
72              assertNotNull(propertiesLocation);
73              assertEquals(2, propertiesLocation.getLineNumber());
74              assertEquals(3, propertiesLocation.getColumnNumber());
75              filterPropLocation = propertiesLocation.getLocation("my.prop");
76              assertNotNull(filterPropLocation);
77              assertEquals(3, filterPropLocation.getLineNumber());
78              assertEquals(22, filterPropLocation.getColumnNumber());
79  
80              Model result = new MavenMerger().merge(model, mergeInput, true, null);
81              propertiesLocation = result.getLocation("properties");
82              assertNotNull(propertiesLocation);
83              assertEquals(-1, propertiesLocation.getLineNumber());
84              assertEquals(-1, propertiesLocation.getColumnNumber());
85              filterPropLocation = propertiesLocation.getLocation("my.filter.value");
86              assertNotNull(filterPropLocation);
87              assertEquals(14, filterPropLocation.getLineNumber());
88              assertEquals(31, filterPropLocation.getColumnNumber());
89              filterPropLocation = propertiesLocation.getLocation("my.prop");
90              assertNotNull(filterPropLocation);
91              assertEquals(3, filterPropLocation.getLineNumber());
92              assertEquals(22, filterPropLocation.getColumnNumber());
93              assertNotNull(result.getLocation("groupId"));
94  
95              result = new DefaultInheritanceAssembler.InheritanceModelMerger().merge(model, mergeInput, true, null);
96              propertiesLocation = result.getLocation("properties");
97              assertNotNull(propertiesLocation);
98              assertEquals(-1, propertiesLocation.getLineNumber());
99              assertEquals(-1, propertiesLocation.getColumnNumber());
100             filterPropLocation = propertiesLocation.getLocation("my.filter.value");
101             assertNotNull(filterPropLocation);
102             assertEquals(14, filterPropLocation.getLineNumber());
103             assertEquals(31, filterPropLocation.getColumnNumber());
104             filterPropLocation = propertiesLocation.getLocation("my.prop");
105             assertNotNull(filterPropLocation);
106             assertEquals(3, filterPropLocation.getLineNumber());
107             assertEquals(22, filterPropLocation.getColumnNumber());
108             assertNotNull(result.getLocation("groupId"));
109         }
110     }
111 }