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 static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.util.IOUtil;
38  
39  /**
40   * Test class for the active-profiles mojo of the Help Plugin.
41   */
42  public class ActiveProfilesMojoTest
43      extends AbstractMojoTestCase
44  {
45  
46      @Override
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51      }
52  
53      /**
54       * Tests that profiles activated in the settings are resolved.
55       * 
56       * @throws Exception in case of errors.
57       */
58      public void testActiveProfilesFromSettings()
59          throws Exception
60      {
61          File testPom = new File( getBasedir(), "target/test-classes/unit/active-profiles/plugin-config.xml" );
62  
63          ActiveProfilesMojo mojo = (ActiveProfilesMojo) lookupMojo( "active-profiles", testPom );
64  
65          MavenProject project = mock( MavenProject.class );
66          when( project.getInjectedProfileIds() ).thenReturn( getProfiles( Arrays.asList( "from-settings" ),
67                                                                           Collections.<String>emptyList() ) );
68  
69          setUpMojo( mojo, Arrays.asList( project ), "from-settings.txt" );
70  
71          mojo.execute();
72  
73          String file = readFile( "from-settings.txt" );
74          assertTrue( file.contains( "from-settings (source: external)" ) );
75      }
76  
77      /**
78       * Tests that profiles activated in the POM are resolved.
79       * 
80       * @throws Exception in case of errors.
81       */
82      public void testActiveProfilesFromPom()
83          throws Exception
84      {
85          File testPom = new File( getBasedir(), "target/test-classes/unit/active-profiles/plugin-config.xml" );
86  
87          ActiveProfilesMojo mojo = (ActiveProfilesMojo) lookupMojo( "active-profiles", testPom );
88  
89          MavenProject project = mock( MavenProject.class );
90          when( project.getInjectedProfileIds() ).thenReturn( getProfiles( Collections.<String>emptyList(),
91                                                                           Arrays.asList( "from-pom" ) ) );
92  
93          setUpMojo( mojo, Arrays.asList( project ), "from-pom.txt" );
94  
95          mojo.execute();
96  
97          String file = readFile( "from-pom.txt" );
98          assertTrue( file.contains( "from-pom (source: org.apache.maven.test:test:1.0)" ) );
99      }
100 
101     private Map<String, List<String>> getProfiles( List<String> externals, List<String> pom )
102     {
103         Map<String, List<String>> profiles = new HashMap<String, List<String>>();
104         profiles.put( "external", externals ); // from settings
105         profiles.put( "org.apache.maven.test:test:1.0", pom ); // from POM
106         profiles.put( "", Collections.<String>emptyList() ); // from super POM
107         return profiles;
108     }
109 
110     private void setUpMojo( ActiveProfilesMojo mojo, List<MavenProject> projects, String output )
111         throws IllegalAccessException
112     {
113         setVariableValueToObject( mojo, "projects", projects );
114         setVariableValueToObject( mojo, "output",
115                                   new File( getBasedir(), "target/test-classes/unit/active-profiles/" + output ) );
116     }
117 
118     private String readFile( String path )
119         throws FileNotFoundException, IOException
120     {
121         FileInputStream fis = null;
122         try
123         {
124             fis = new FileInputStream( new File( getBasedir(), "target/test-classes/unit/active-profiles/" + path ) );
125             return IOUtil.toString( fis );
126         }
127         finally
128         {
129             IOUtil.close( fis );
130         }
131     }
132 
133 }