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