1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.fromDependencies;
20
21 import java.io.File;
22 import java.util.Set;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.LegacySupport;
27 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
28 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
29 import org.apache.maven.project.MavenProject;
30
31 public class TestBuildClasspathMojo extends AbstractDependencyMojoTestCase {
32
33 private BuildClasspathMojo mojo;
34
35 protected void setUp() throws Exception {
36
37 super.setUp("build-classpath", true);
38
39 File testPom = new File(getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml");
40 mojo = (BuildClasspathMojo) lookupMojo("build-classpath", testPom);
41
42 assertNotNull(mojo);
43 assertNotNull(mojo.getProject());
44 }
45
46
47
48
49 public void testEnvironment() throws Exception {
50 MavenProject project = mojo.getProject();
51
52
53 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
54 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
55 artifacts.addAll(directArtifacts);
56
57 project.setArtifacts(artifacts);
58 project.setDependencyArtifacts(directArtifacts);
59
60 mojo.execute();
61 try {
62 mojo.readClasspathFile();
63
64 fail("Expected an illegal Argument Exception");
65 } catch (IllegalArgumentException e) {
66
67 }
68
69 mojo.setOutputFile(new File(testDir, "buildClasspath.txt"));
70 mojo.execute();
71
72 String file = mojo.readClasspathFile();
73 assertNotNull(file);
74 assertTrue(file.length() > 0);
75
76 assertTrue(file.contains(File.pathSeparator));
77 assertTrue(file.contains(File.separator));
78
79 String fileSep = "#####";
80 String pathSep = "%%%%%";
81
82 mojo.setFileSeparator(fileSep);
83 mojo.setPathSeparator(pathSep);
84 mojo.execute();
85
86 file = mojo.readClasspathFile();
87 assertNotNull(file);
88 assertTrue(file.length() > 0);
89
90 assertFalse(file.contains(File.pathSeparator));
91 assertFalse(file.contains(File.separator));
92 assertTrue(file.contains(fileSep));
93 assertTrue(file.contains(pathSep));
94
95 String propertyValue = project.getProperties().getProperty("outputProperty");
96 assertNull(propertyValue);
97 mojo.setOutputProperty("outputProperty");
98 mojo.execute();
99 propertyValue = project.getProperties().getProperty("outputProperty");
100 assertNotNull(propertyValue);
101 }
102
103 public void testPath() throws Exception {
104 MavenSession session = newMavenSession(mojo.getProject());
105 setVariableValueToObject(mojo, "session", session);
106
107 LegacySupport legacySupport = lookup(LegacySupport.class);
108 legacySupport.setSession(session);
109 installLocalRepository(legacySupport);
110
111 Artifact artifact = stubFactory.getReleaseArtifact();
112
113 StringBuilder sb = new StringBuilder();
114 mojo.setPrefix(null);
115 mojo.setStripVersion(false);
116 mojo.appendArtifactPath(artifact, sb);
117 assertEquals(artifact.getFile().getPath(), sb.toString());
118
119 mojo.setLocalRepoProperty("$M2_REPO");
120 sb = new StringBuilder();
121 mojo.appendArtifactPath(artifact, sb);
122 assertEquals("$M2_REPO" + File.separator + artifact.getFile().getName(), sb.toString());
123
124 mojo.setLocalRepoProperty("%M2_REPO%");
125 sb = new StringBuilder();
126 mojo.appendArtifactPath(artifact, sb);
127 assertEquals("%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString());
128
129 mojo.setLocalRepoProperty("%M2_REPO%");
130 sb = new StringBuilder();
131 mojo.setPrependGroupId(true);
132 mojo.appendArtifactPath(artifact, sb);
133 assertEquals(
134 "If prefix is null, prependGroupId has no impact ",
135 "%M2_REPO%" + File.separator + DependencyUtil.getFormattedFileName(artifact, false, false),
136 sb.toString());
137
138 mojo.setLocalRepoProperty("");
139 mojo.setPrefix("prefix");
140 sb = new StringBuilder();
141 mojo.setPrependGroupId(true);
142 mojo.appendArtifactPath(artifact, sb);
143 assertEquals(
144 "prefix" + File.separator + DependencyUtil.getFormattedFileName(artifact, false, true), sb.toString());
145 mojo.setPrependGroupId(false);
146
147 mojo.setLocalRepoProperty("");
148 mojo.setPrefix("prefix");
149 sb = new StringBuilder();
150 mojo.appendArtifactPath(artifact, sb);
151 assertEquals("prefix" + File.separator + artifact.getFile().getName(), sb.toString());
152
153 mojo.setPrefix("prefix");
154 mojo.setStripVersion(true);
155 sb = new StringBuilder();
156 mojo.appendArtifactPath(artifact, sb);
157 assertEquals("prefix" + File.separator + DependencyUtil.getFormattedFileName(artifact, true), sb.toString());
158 }
159 }