View Javadoc
1   package org.apache.maven.it;
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 org.apache.maven.it.util.ResourceExtractor;
23  import org.codehaus.plexus.util.xml.Xpp3Dom;
24  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
25  
26  import java.io.File;
27  import java.io.FileReader;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5224">MNG-5175</a>.
33   * test correct injection of settings with profiles in ${settings} in mojo
34   *
35   *
36   */
37  public class MavenITmng5224InjectedSettings
38      extends AbstractMavenIntegrationTestCase
39  {
40      public MavenITmng5224InjectedSettings()
41      {
42          // olamy probably doesn't work with 3.x before 3.0.4
43          super( "[2.0.3,3.0-alpha-1),[3.0.4,)" );
44      }
45  
46  
47      /**
48       *
49       * @throws Exception in case of failure
50       */
51      public void testmng5224_ReadSettings()
52          throws Exception
53      {
54          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5224" );
55  
56          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
57  
58          verifier.addCliOption( "--settings" );
59          verifier.addCliOption( "settings.xml" );
60          //verifier.
61          verifier.executeGoal( "validate" );
62  
63          File settingsFile = new File( verifier.getBasedir(), "target/settings-dump.xml" );
64  
65          FileReader fr = new FileReader( settingsFile );
66  
67          Xpp3Dom dom = Xpp3DomBuilder.build( fr );
68  
69          Xpp3Dom profilesNode = dom.getChild( "profiles" );
70  
71          Xpp3Dom[] profileNodes = profilesNode.getChildren( "profile" );
72  
73          // 3 from the user settings + 1 for the global settings used for its
74          assertEquals( 4, profileNodes.length );
75  
76          /**
77           <profiles>
78           <profile>
79           <id>apache</id>
80           <activation>
81           <activeByDefault>true</activeByDefault>
82           </activation>
83           <properties>
84           <run-its>true</run-its>
85           </properties>
86           </profile>
87           <profile>
88           <id>release</id>
89           <properties>
90           <gpg.passphrase>verycomplicatedpassphrase</gpg.passphrase>
91           </properties>
92           </profile>
93           <profile>
94           <id>fast</id>
95           <properties>
96           <maven.test.skip>true</maven.test.skip>
97           <skipTests>true</skipTests>
98           </properties>
99           </profile>
100          </profiles>
101          **/
102 
103         List<String> profileIds = new ArrayList<>( 4 );
104 
105         for ( Xpp3Dom node : profileNodes )
106         {
107             Xpp3Dom idNode = node.getChild( "id" );
108             profileIds.add( idNode.getValue() );
109             if ( "apache".equals( idNode.getName() ) )
110             {
111                 Xpp3Dom propsNode = node.getChild( "properties" );
112                 assertEquals( "true", propsNode.getChild( "run-its" ).getValue() );
113             }
114             if ( "release".equals( idNode.getName() ) )
115             {
116                 Xpp3Dom propsNode = node.getChild( "properties" );
117                 assertEquals( "verycomplicatedpassphrase", propsNode.getChild( "gpg.passphrase" ).getValue() );
118             }
119             if ( "fast".equals( idNode.getName() ) )
120             {
121                 Xpp3Dom propsNode = node.getChild( "properties" );
122                 assertEquals( "true", propsNode.getChild( "maven.test.skip" ).getValue() );
123                 assertEquals( "true", propsNode.getChild( "skipTests" ).getValue() );
124             }
125         }
126 
127         assertTrue( profileIds.contains( "apache" ) );
128         assertTrue( profileIds.contains( "release" ) );
129         assertTrue( profileIds.contains( "fast" ) );
130         assertTrue( profileIds.contains( "it-defaults" ) );
131 
132         /**
133          <activeProfiles>
134          <activeProfile>it-defaults</activeProfile>
135          <activeProfile>apache</activeProfile>
136          </activeProfiles>
137          */
138 
139         Xpp3Dom activeProfilesNode = dom.getChild( "activeProfiles" );
140 
141         // with maven3 profile activation (activeByDefault) is done later during project building phase
142         // so we have only a "dump" of the settings
143 
144         if ( matchesVersionRange( "[2.0.3,3.0-alpha-1)" ) )
145         {
146             assertEquals( 2, activeProfilesNode.getChildCount() );
147         }
148         else
149         {
150             assertEquals( 1, activeProfilesNode.getChildCount() );
151         }
152 
153         List<String> activeProfiles = new ArrayList<>( 2 );
154 
155         for ( Xpp3Dom node : activeProfilesNode.getChildren() )
156         {
157             activeProfiles.add( node.getValue() );
158         }
159 
160         if ( matchesVersionRange( "[2.0.3,3.0-alpha-1)" ) )
161         {
162             assertTrue( activeProfiles.contains( "apache" ) );
163         }
164         assertTrue( activeProfiles.contains( "it-defaults" ) );
165 
166     }
167 
168 
169 }