View Javadoc

1   package org.apache.maven.plugin.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.artifact.repository.ArtifactRepository;
27  import org.apache.maven.plugin.dependency.AbstractDependencyMojoTestCase;
28  import org.apache.maven.plugin.dependency.fromDependencies.BuildClasspathMojo;
29  import org.apache.maven.plugin.dependency.utils.DependencyUtil;
30  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
31  import org.apache.maven.project.MavenProject;
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
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 = null;
69          try
70          {
71              file = 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.setCpFile( 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.indexOf( File.pathSeparator ) >= 0 );
88          assertTrue( file.indexOf( File.separator ) >= 0 );
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.indexOf( File.pathSeparator ) >= 0 );
102         assertFalse( file.indexOf( File.separator ) >= 0 );
103         assertTrue( file.indexOf( fileSep ) >= 0 );
104         assertTrue( file.indexOf( pathSep ) >= 0 );
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() throws Exception
116     {
117         File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
118         BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
119 
120         assertNotNull( mojo );
121         assertNotNull( mojo.getProject() );
122 
123         ArtifactRepository local = new StubArtifactRepository( stubFactory.getWorkingDir().getPath() );
124         mojo.setLocal( local );
125 
126         Artifact artifact = stubFactory.getReleaseArtifact();
127 
128 
129         StringBuilder sb = new StringBuilder();
130         mojo.setPrefix( null );
131         mojo.setStripVersion( false );
132         mojo.appendArtifactPath( artifact, sb );
133         assertEquals( artifact.getFile().getPath(), sb.toString() );
134 
135         mojo.setLocalRepoProperty( "$M2_REPO" );
136         sb.setLength( 0 );
137         mojo.appendArtifactPath( artifact, sb );
138         assertEquals( "$M2_REPO" + File.separator + artifact.getFile().getName(), sb.toString() );
139 
140         mojo.setLocalRepoProperty( "%M2_REPO%" );
141         sb.setLength( 0 );
142         mojo.appendArtifactPath( artifact, sb );
143         assertEquals( "%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString() );
144 
145         mojo.setLocalRepoProperty( "%M2_REPO%" );
146         sb.setLength( 0 );
147         mojo.setPrependGroupId( true );
148         mojo.appendArtifactPath( artifact, sb );
149         assertEquals("If prefix is null, prependGroupId has no impact ", "%M2_REPO%"+File.separator 
150                      + DependencyUtil.getFormattedFileName( artifact, false, false ), sb.toString());
151         
152         mojo.setLocalRepoProperty( "" );
153         mojo.setPrefix( "prefix" );
154         sb.setLength( 0 );
155         mojo.setPrependGroupId( true );
156         mojo.appendArtifactPath( artifact, sb );
157         assertEquals("prefix"+File.separator+DependencyUtil.getFormattedFileName( artifact, false, true ), 
158                      sb.toString());
159         mojo.setPrependGroupId( false );
160         
161         mojo.setLocalRepoProperty( "" );
162         mojo.setPrefix( "prefix" );
163         sb.setLength( 0 );
164         mojo.appendArtifactPath( artifact, sb );
165         assertEquals("prefix"+File.separator+artifact.getFile().getName(),sb.toString());
166       
167         mojo.setPrefix( "prefix" );
168         mojo.setStripVersion( true );
169         sb.setLength( 0 );
170         mojo.appendArtifactPath( artifact, sb );
171         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ), sb.toString() );
172         
173     }
174 }