View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.help;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  import static org.mockito.Mockito.mock;
35  import static org.mockito.Mockito.when;
36  
37  /**
38   * Test class for the active-profiles mojo of the Help Plugin.
39   */
40  public class ActiveProfilesMojoTest extends AbstractMojoTestCase {
41  
42      /**
43       * Tests that profiles activated in the settings are resolved.
44       *
45       * @throws Exception in case of errors.
46       */
47      public void testActiveProfilesFromSettings() throws Exception {
48          File testPom = new File(getBasedir(), "target/test-classes/unit/active-profiles/plugin-config.xml");
49  
50          ActiveProfilesMojo mojo = (ActiveProfilesMojo) lookupMojo("active-profiles", testPom);
51  
52          MavenProject project = mock(MavenProject.class);
53          when(project.getInjectedProfileIds())
54                  .thenReturn(getProfiles(Arrays.asList("from-settings"), Collections.<String>emptyList()));
55  
56          setUpMojo(mojo, Arrays.asList(project), "from-settings.txt");
57  
58          mojo.execute();
59  
60          String file = readFile("from-settings.txt");
61          assertTrue(file.contains("from-settings (source: external)"));
62      }
63  
64      /**
65       * Tests that profiles activated in the POM are resolved.
66       *
67       * @throws Exception in case of errors.
68       */
69      public void testActiveProfilesFromPom() throws Exception {
70          File testPom = new File(getBasedir(), "target/test-classes/unit/active-profiles/plugin-config.xml");
71  
72          ActiveProfilesMojo mojo = (ActiveProfilesMojo) lookupMojo("active-profiles", testPom);
73  
74          MavenProject project = mock(MavenProject.class);
75          when(project.getInjectedProfileIds())
76                  .thenReturn(getProfiles(Collections.<String>emptyList(), Arrays.asList("from-pom")));
77  
78          setUpMojo(mojo, Arrays.asList(project), "from-pom.txt");
79  
80          mojo.execute();
81  
82          String file = readFile("from-pom.txt");
83          assertTrue(file.contains("from-pom (source: org.apache.maven.test:test:1.0)"));
84      }
85  
86      private Map<String, List<String>> getProfiles(List<String> externals, List<String> pom) {
87          Map<String, List<String>> profiles = new HashMap<>();
88          profiles.put("external", externals); // from settings
89          profiles.put("org.apache.maven.test:test:1.0", pom); // from POM
90          profiles.put("", Collections.<String>emptyList()); // from super POM
91          return profiles;
92      }
93  
94      private void setUpMojo(ActiveProfilesMojo mojo, List<MavenProject> projects, String output)
95              throws IllegalAccessException {
96          setVariableValueToObject(mojo, "projects", projects);
97          setVariableValueToObject(
98                  mojo, "output", new File(getBasedir(), "target/test-classes/unit/active-profiles/" + output));
99      }
100 
101     private String readFile(String path) throws IOException {
102         try (FileInputStream fis =
103                 new FileInputStream(new File(getBasedir(), "target/test-classes/unit/active-profiles/" + path))) {
104             return IOUtil.toString(fis);
105         }
106     }
107 }