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.plugin.LegacySupport;
28  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
29  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
30  import org.apache.maven.project.MavenProject;
31  
32  public class TestBuildClasspathMojo
33      extends AbstractDependencyMojoTestCase
34  {
35      
36      private BuildClasspathMojo mojo;
37  
38      protected void setUp()
39          throws Exception
40      {
41          // required for mojo lookups to work
42          super.setUp( "build-classpath", true );
43          
44          File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
45          mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
46  
47          assertNotNull( mojo );
48          assertNotNull( mojo.getProject() );
49      }
50  
51      /**
52       * Tests the proper discovery and configuration of the mojo.
53       */
54      public void testEnvironment()
55          throws Exception
56      {
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          {
70              mojo.readClasspathFile();
71  
72              fail( "Expected an illegal Argument Exception" );
73          }
74          catch ( IllegalArgumentException e )
75          {
76              // expected to catch this.
77          }
78  
79          mojo.setOutputFile( new File( testDir, "buildClasspath.txt" ) );
80          mojo.execute();
81  
82          String file = mojo.readClasspathFile();
83          assertNotNull( file );
84          assertTrue( file.length() > 0 );
85  
86          assertTrue( file.contains( File.pathSeparator ) );
87          assertTrue( file.contains( File.separator ) );
88  
89          String fileSep = "#####";
90          String pathSep = "%%%%%";
91  
92          mojo.setFileSeparator( fileSep );
93          mojo.setPathSeparator( pathSep );
94          mojo.execute();
95  
96          file = mojo.readClasspathFile();
97          assertNotNull( file );
98          assertTrue( file.length() > 0 );
99  
100         assertFalse( file.contains( File.pathSeparator ) );
101         assertFalse( file.contains( File.separator ) );
102         assertTrue( file.contains( fileSep ) );
103         assertTrue( file.contains( pathSep ) );
104 
105         String propertyValue = project.getProperties().getProperty( "outputProperty" );
106         assertNull( propertyValue );
107         mojo.setOutputProperty( "outputProperty" );
108         mojo.execute();
109         propertyValue = project.getProperties().getProperty( "outputProperty" );
110         assertNotNull( propertyValue );
111 
112     }
113 
114     public void testPath()
115         throws Exception
116     {
117         MavenSession session = newMavenSession( mojo.getProject() );
118         setVariableValueToObject( mojo, "session", session );
119      
120         LegacySupport legacySupport = lookup( LegacySupport.class );
121         legacySupport.setSession( session );    
122         installLocalRepository( legacySupport );
123         
124         Artifact artifact = stubFactory.getReleaseArtifact();
125 
126         StringBuilder sb = new StringBuilder();
127         mojo.setPrefix( null );
128         mojo.setStripVersion( false );
129         mojo.appendArtifactPath( artifact, sb );
130         assertEquals( artifact.getFile().getPath(), sb.toString() );
131 
132         mojo.setLocalRepoProperty( "$M2_REPO" );
133         sb = new StringBuilder();
134         mojo.appendArtifactPath( artifact, sb );
135         assertEquals( "$M2_REPO" + File.separator + artifact.getFile().getName(), sb.toString() );
136 
137         mojo.setLocalRepoProperty( "%M2_REPO%" );
138         sb = new StringBuilder();
139         mojo.appendArtifactPath( artifact, sb );
140         assertEquals( "%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString() );
141 
142         mojo.setLocalRepoProperty( "%M2_REPO%" );
143         sb = new StringBuilder();
144         mojo.setPrependGroupId( true );
145         mojo.appendArtifactPath( artifact, sb );
146         assertEquals( "If prefix is null, prependGroupId has no impact ",
147                       "%M2_REPO%" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, false ),
148                       sb.toString() );
149 
150         mojo.setLocalRepoProperty( "" );
151         mojo.setPrefix( "prefix" );
152         sb = new StringBuilder();
153         mojo.setPrependGroupId( true );
154         mojo.appendArtifactPath( artifact, sb );
155         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, true ),
156                       sb.toString() );
157         mojo.setPrependGroupId( false );
158 
159         mojo.setLocalRepoProperty( "" );
160         mojo.setPrefix( "prefix" );
161         sb = new StringBuilder();
162         mojo.appendArtifactPath( artifact, sb );
163         assertEquals( "prefix" + File.separator + artifact.getFile().getName(), sb.toString() );
164 
165         mojo.setPrefix( "prefix" );
166         mojo.setStripVersion( true );
167         sb = new StringBuilder();
168         mojo.appendArtifactPath( artifact, sb );
169         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ),
170                       sb.toString() );
171 
172     }
173 }