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 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
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
55
56
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
79
80
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 );
105 profiles.put( "org.apache.maven.test:test:1.0", pom );
106 profiles.put( "", Collections.<String>emptyList() );
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 }