View Javadoc
1   package org.apache.maven.plugins.dependency.fromDependencies;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.execution.MavenSession;
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  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
31  import org.sonatype.aether.util.DefaultRepositorySystemSession;
32  
33  public class TestBuildClasspathMojo
34      extends AbstractDependencyMojoTestCase
35  {
36  
37      protected void setUp()
38          throws Exception
39      {
40          // required for mojo lookups to work
41          super.setUp( "build-classpath", true );
42      }
43  
44      /**
45       * tests the proper discovery and configuration of the mojo
46       *
47       * @throws Exception in case of an error.
48       */
49      public void testEnvironment()
50          throws Exception
51      {
52          File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
53          BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
54  
55          assertNotNull( mojo );
56          assertNotNull( mojo.getProject() );
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          String file;
69          try
70          {
71              mojo.readClasspathFile();
72  
73              fail( "Expected an illegal Argument Exception" );
74          }
75          catch ( IllegalArgumentException e )
76          {
77              // expected to catch this.
78          }
79  
80          mojo.setOutputFile( new File( testDir, "buildClasspath.txt" ) );
81          mojo.execute();
82  
83          file = mojo.readClasspathFile();
84          assertNotNull( file );
85          assertTrue( file.length() > 0 );
86  
87          assertTrue( file.contains( File.pathSeparator ) );
88          assertTrue( file.contains( File.separator ) );
89  
90          String fileSep = "#####";
91          String pathSep = "%%%%%";
92  
93          mojo.setFileSeparator( fileSep );
94          mojo.setPathSeparator( pathSep );
95          mojo.execute();
96  
97          file = mojo.readClasspathFile();
98          assertNotNull( file );
99          assertTrue( file.length() > 0 );
100 
101         assertFalse( file.contains( File.pathSeparator ) );
102         assertFalse( file.contains( File.separator ) );
103         assertTrue( file.contains( fileSep ) );
104         assertTrue( file.contains( pathSep ) );
105 
106         String propertyValue = project.getProperties().getProperty( "outputProperty" );
107         assertNull( propertyValue );
108         mojo.setOutputProperty( "outputProperty" );
109         mojo.execute();
110         propertyValue = project.getProperties().getProperty( "outputProperty" );
111         assertNotNull( propertyValue );
112 
113     }
114 
115     public void testPath()
116         throws Exception
117     {
118         File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
119         BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
120 
121         assertNotNull( mojo );
122         assertNotNull( mojo.getProject() );
123 
124         MavenSession session = newMavenSession( mojo.getProject() );
125         setVariableValueToObject( mojo, "session", session );
126 
127         DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
128         repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
129 
130         Artifact artifact = stubFactory.getReleaseArtifact();
131 
132         StringBuilder sb = new StringBuilder();
133         mojo.setPrefix( null );
134         mojo.setStripVersion( false );
135         mojo.appendArtifactPath( artifact, sb );
136         assertEquals( artifact.getFile().getPath(), sb.toString() );
137 
138         mojo.setLocalRepoProperty( "$M2_REPO" );
139         sb.setLength( 0 );
140         mojo.appendArtifactPath( artifact, sb );
141         assertEquals( "$M2_REPO" + File.separator + artifact.getFile().getName(), sb.toString() );
142 
143         mojo.setLocalRepoProperty( "%M2_REPO%" );
144         sb.setLength( 0 );
145         mojo.appendArtifactPath( artifact, sb );
146         assertEquals( "%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString() );
147 
148         mojo.setLocalRepoProperty( "%M2_REPO%" );
149         sb.setLength( 0 );
150         mojo.setPrependGroupId( true );
151         mojo.appendArtifactPath( artifact, sb );
152         assertEquals( "If prefix is null, prependGroupId has no impact ",
153                       "%M2_REPO%" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, false ),
154                       sb.toString() );
155 
156         mojo.setLocalRepoProperty( "" );
157         mojo.setPrefix( "prefix" );
158         sb.setLength( 0 );
159         mojo.setPrependGroupId( true );
160         mojo.appendArtifactPath( artifact, sb );
161         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, true ),
162                       sb.toString() );
163         mojo.setPrependGroupId( false );
164 
165         mojo.setLocalRepoProperty( "" );
166         mojo.setPrefix( "prefix" );
167         sb.setLength( 0 );
168         mojo.appendArtifactPath( artifact, sb );
169         assertEquals( "prefix" + File.separator + artifact.getFile().getName(), sb.toString() );
170 
171         mojo.setPrefix( "prefix" );
172         mojo.setStripVersion( true );
173         sb.setLength( 0 );
174         mojo.appendArtifactPath( artifact, sb );
175         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ),
176                       sb.toString() );
177 
178     }
179 }