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.building;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Paths;
25  
26  import org.apache.maven.api.model.Model;
27  import org.apache.maven.model.v4.MavenStaxReader;
28  import org.codehaus.plexus.util.xml.Xpp3Dom;
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  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  /**
36   */
37  class DefaultModelBuilderFactoryTest {
38  
39      private static final String BASE_DIR =
40              Paths.get("src", "test", "resources", "poms", "factory").toString();
41  
42      private File getPom(String name) {
43          return new File(Paths.get(BASE_DIR, name + ".xml").toString()).getAbsoluteFile();
44      }
45  
46      @Test
47      void testCompleteWiring() throws Exception {
48          ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
49          assertNotNull(builder);
50  
51          DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
52          request.setProcessPlugins(true);
53          request.setPomFile(getPom("simple"));
54  
55          ModelBuildingResult result = builder.build(request);
56          assertNotNull(result);
57          assertNotNull(result.getEffectiveModel());
58          assertEquals("activated", result.getEffectiveModel().getProperties().get("profile.file"));
59          Xpp3Dom conf = (Xpp3Dom)
60                  result.getEffectiveModel().getBuild().getPlugins().get(0).getConfiguration();
61          assertNotNull(conf);
62          assertEquals("1.5", conf.getChild("source").getValue());
63          assertEquals("  1.5  ", conf.getChild("target").getValue());
64      }
65  
66      @Test
67      void testPomChanges() throws Exception {
68          ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
69          assertNotNull(builder);
70          File pom = getPom("simple");
71  
72          String originalExists =
73                  readPom(pom).getProfiles().get(1).getActivation().getFile().getExists();
74  
75          DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
76          request.setProcessPlugins(true);
77          request.setPomFile(pom);
78          ModelBuildingResult result = builder.build(request);
79          String resultExists = result.getRawModel()
80                  .getProfiles()
81                  .get(1)
82                  .getActivation()
83                  .getFile()
84                  .getExists();
85  
86          assertEquals(originalExists, resultExists);
87          assertTrue(result.getEffectiveModel()
88                  .getProfiles()
89                  .get(1)
90                  .getActivation()
91                  .getFile()
92                  .getExists()
93                  .contains(BASE_DIR));
94      }
95  
96      private static Model readPom(File file) throws Exception {
97          try (InputStream is = Files.newInputStream(file.toPath())) {
98              return new MavenStaxReader().read(is);
99          }
100     }
101 }