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