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