1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
61
62
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
81
82
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
110
111
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
136
137
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 }