1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40 public class ActiveProfilesMojoTest extends AbstractMojoTestCase {
41
42
43
44
45
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
66
67
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);
89 profiles.put("org.apache.maven.test:test:1.0", pom);
90 profiles.put("", Collections.<String>emptyList());
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 }