View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // required for mojo lookups to work
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       * Tests the proper discovery and configuration of the mojo.
55       */
56      public void testEnvironment() throws Exception {
57          MavenProject project = mojo.getProject();
58  
59          // mojo.silent = true;
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              // expected to catch this.
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 }