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.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          // required for mojo lookups to work
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       * Tests the proper discovery and configuration of the mojo.
48       */
49      public void testEnvironment() throws Exception {
50          MavenProject project = mojo.getProject();
51  
52          // mojo.silent = true;
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              // expected to catch this.
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 }