View Javadoc
1   package org.apache.maven.plugins.help;
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.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import org.apache.maven.model.Profile;
32  import org.apache.maven.monitor.logging.DefaultLog;
33  import org.apache.maven.plugin.Mojo;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.logging.Logger;
38  import org.codehaus.plexus.logging.LoggerManager;
39  import org.codehaus.plexus.util.IOUtil;
40  
41  /**
42   * Test class for the all-profiles mojo of the Help Plugin.
43   */
44  public class AllProfilesMojoTest
45      extends AbstractMojoTestCase
46  {
47  
48      private InterceptingLog interceptingLogger;
49  
50      @Override
51      protected void setUp()
52          throws Exception
53      {
54          super.setUp();
55          interceptingLogger =
56              new InterceptingLog( getContainer().lookup( LoggerManager.class ).getLoggerForComponent( Mojo.ROLE ) );
57      }
58  
59      /**
60       * Tests the case when no profiles are present for the projects.
61       * 
62       * @throws Exception in case of errors.
63       */
64      public void testNoProfiles()
65          throws Exception
66      {
67          File testPom = new File( getBasedir(), "target/test-classes/unit/all-profiles/plugin-config.xml" );
68  
69          AllProfilesMojo mojo = (AllProfilesMojo) lookupMojo( "all-profiles", testPom );
70  
71          setUpMojo( mojo, Arrays.<MavenProject>asList( new MavenProjectStub() ),
72                     Collections.<org.apache.maven.settings.Profile>emptyList(), "empty.txt" );
73  
74          mojo.execute();
75  
76          assertTrue( interceptingLogger.warnLogs.contains( "No profiles detected!" ) );
77      }
78      
79      /**
80       * Tests the case when profiles are present in the POM and in a parent POM.
81       * 
82       * @throws Exception in case of errors.
83       */
84      public void testProfileFromPom()
85          throws Exception
86      {
87          File testPom = new File( getBasedir(), "target/test-classes/unit/all-profiles/plugin-config.xml" );
88  
89          AllProfilesMojo mojo = (AllProfilesMojo) lookupMojo( "all-profiles", testPom );
90  
91          MavenProjectStub project = new MavenProjectStub();
92          project.getModel().setProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ), newPomProfile( "pro-2", "pom" ) ) );
93          project.setParent( new MavenProjectStub() );
94          project.getParent().getModel().setProfiles( Arrays.asList( newPomProfile( "pro-3", "pom" ) ) );
95          project.setActiveProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ) ) );
96          
97          setUpMojo( mojo, Arrays.<MavenProject>asList( project ),
98                     Collections.<org.apache.maven.settings.Profile>emptyList(), "profiles-from-pom.txt" );
99  
100         mojo.execute();
101 
102         String file = readFile( "profiles-from-pom.txt" );
103         assertTrue( file.contains( "Profile Id: pro-1 (Active: true , Source: pom)" ) );
104         assertTrue( file.contains( "Profile Id: pro-2 (Active: false , Source: pom)" ) );
105         assertTrue( file.contains( "Profile Id: pro-3 (Active: false , Source: pom)" ) );
106     }
107 
108     /**
109      * Tests the case when active profiles are present in the parent POM.
110      * 
111      * @throws Exception in case of errors.
112      */
113     public void testProfileFromParentPom()
114         throws Exception
115     {
116         File testPom = new File( getBasedir(), "target/test-classes/unit/all-profiles/plugin-config.xml" );
117 
118         AllProfilesMojo mojo = (AllProfilesMojo) lookupMojo( "all-profiles", testPom );
119 
120         MavenProjectStub project = new MavenProjectStub();
121         project.setParent( new MavenProjectStub() );
122         project.getParent().getModel().setProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ) ) );
123         project.getParent().setActiveProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ) ) );
124         
125         setUpMojo( mojo, Arrays.<MavenProject>asList( project ),
126                    Collections.<org.apache.maven.settings.Profile>emptyList(), "profiles-from-parent-pom.txt" );
127 
128         mojo.execute();
129 
130         String file = readFile( "profiles-from-parent-pom.txt" );
131         assertTrue( file.contains( "Profile Id: pro-1 (Active: true , Source: pom)" ) );
132     }
133     
134     /**
135      * Tests the case when profiles are present in the settings.
136      * 
137      * @throws Exception in case of errors.
138      */
139     public void testProfileFromSettings()
140         throws Exception
141     {
142         File testPom = new File( getBasedir(), "target/test-classes/unit/all-profiles/plugin-config.xml" );
143 
144         AllProfilesMojo mojo = (AllProfilesMojo) lookupMojo( "all-profiles", testPom );
145 
146         MavenProject project = new MavenProjectStub();
147         project.setActiveProfiles( Arrays.asList( newPomProfile( "settings-1", "settings.xml" ) ) );
148         
149         List<org.apache.maven.settings.Profile> settingsProfiles = new ArrayList<org.apache.maven.settings.Profile>();
150         settingsProfiles.add( newSettingsProfile( "settings-1" ) );
151         settingsProfiles.add( newSettingsProfile( "settings-2" ) );
152         setUpMojo( mojo, Arrays.<MavenProject>asList( project ), settingsProfiles, "profiles-from-settings.txt" );
153 
154         mojo.execute();
155 
156         String file = readFile( "profiles-from-settings.txt" );
157         assertTrue( file.contains( "Profile Id: settings-1 (Active: true , Source: settings.xml)" ) );
158         assertTrue( file.contains( "Profile Id: settings-2 (Active: false , Source: settings.xml)" ) );
159     }
160 
161     private Profile newPomProfile( String id, String source )
162     {
163         Profile profile = new Profile();
164         profile.setId( id );
165         profile.setSource( source );
166         return profile;
167     }
168     
169     private org.apache.maven.settings.Profile newSettingsProfile( String id )
170     {
171         org.apache.maven.settings.Profile profile = new org.apache.maven.settings.Profile();
172         profile.setId( id );
173         return profile;
174     }
175 
176     private void setUpMojo( AllProfilesMojo mojo, List<MavenProject> projects,
177                             List<org.apache.maven.settings.Profile> settingsProfiles, String output )
178         throws IllegalAccessException
179     {
180         setVariableValueToObject( mojo, "projects", projects );
181         setVariableValueToObject( mojo, "settingsProfiles", settingsProfiles );
182         setVariableValueToObject( mojo, "output",
183                                   new File( getBasedir(), "target/test-classes/unit/active-profiles/" + output ) );
184         setVariableValueToObject( mojo, "log", interceptingLogger );
185     }
186 
187     private String readFile( String path )
188         throws FileNotFoundException, IOException
189     {
190         FileInputStream fis = null;
191         try
192         {
193             fis = new FileInputStream( new File( getBasedir(), "target/test-classes/unit/active-profiles/" + path ) );
194             return IOUtil.toString( fis );
195         }
196         finally
197         {
198             IOUtil.close( fis );
199         }
200     }
201     
202     private static final class InterceptingLog
203         extends DefaultLog
204     {
205         List<String> warnLogs = new ArrayList<String>();
206 
207         public InterceptingLog( Logger logger )
208         {
209             super( logger );
210         }
211 
212         @Override
213         public void warn( CharSequence content )
214         {
215             super.warn( content );
216             warnLogs.add( content.toString() );
217         }
218     }
219 
220 }