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