View Javadoc

1   package org.apache.maven.plugin.dependency;
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.utils.DependencyUtil;
28  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
29  import org.apache.maven.project.MavenProject;
30  
31  public class TestBuildClasspathMojo
32      extends AbstractDependencyMojoTestCase
33  {
34  
35      protected void setUp()
36          throws Exception
37      {
38          // required for mojo lookups to work
39          super.setUp( "build-classpath", true );
40      }
41  
42      /**
43       * tests the proper discovery and configuration of the mojo
44       *
45       * @throws Exception
46       */
47      public void testEnvironment()
48          throws Exception
49      {
50          File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
51          BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
52  
53          assertNotNull( mojo );
54          assertNotNull( mojo.getProject() );
55          MavenProject project = mojo.getProject();
56  
57          // mojo.silent = true;
58          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
59          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
60          artifacts.addAll( directArtifacts );
61  
62          project.setArtifacts( artifacts );
63          project.setDependencyArtifacts( directArtifacts );
64  
65          mojo.execute();
66          String file = null;
67          try
68          {
69              file = mojo.readClasspathFile();
70  
71              fail( "Expected an illegal Argument Exception" );
72          }
73          catch ( IllegalArgumentException e )
74          {
75              // expected to catch this.
76          }
77  
78          mojo.setCpFile( new File( testDir, "buildClasspath.txt" ) );
79          mojo.execute();
80  
81          file = mojo.readClasspathFile();
82          assertNotNull( file );
83          assertTrue( file.length() > 0 );
84  
85          assertTrue( file.indexOf( File.pathSeparator ) >= 0 );
86          assertTrue( file.indexOf( File.separator ) >= 0 );
87  
88          String fileSep = "#####";
89          String pathSep = "%%%%%";
90  
91          mojo.setFileSeparator( fileSep );
92          mojo.setPathSeparator( pathSep );
93          mojo.execute();
94  
95          file = mojo.readClasspathFile();
96          assertNotNull( file );
97          assertTrue( file.length() > 0 );
98  
99          assertFalse( file.indexOf( File.pathSeparator ) >= 0 );
100         assertFalse( file.indexOf( File.separator ) >= 0 );
101         assertTrue( file.indexOf( fileSep ) >= 0 );
102         assertTrue( file.indexOf( pathSep ) >= 0 );
103     }
104 
105     public void testPath() throws Exception
106     {
107         File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
108         BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
109 
110         assertNotNull( mojo );
111         assertNotNull( mojo.getProject() );
112 
113         ArtifactRepository local = new StubArtifactRepository( stubFactory.getWorkingDir().getPath() );
114         mojo.setLocal( local );
115 
116         Artifact artifact = stubFactory.getReleaseArtifact();
117 
118 
119         StringBuffer sb = new StringBuffer();
120         mojo.setPrefix( null );
121         mojo.setStripVersion( false );
122         mojo.appendArtifactPath( artifact, sb );
123         assertEquals( artifact.getFile().getPath(), sb.toString() );
124 
125         mojo.setLocalRepoProperty( "$M2_REPO" );
126         sb.setLength( 0 );
127         mojo.appendArtifactPath( artifact, sb );
128         assertEquals( "$M2_REPO" + File.separator + artifact.getFile().getName(), sb.toString() );
129 
130         mojo.setLocalRepoProperty( "%M2_REPO%" );
131         sb.setLength( 0 );
132         mojo.appendArtifactPath( artifact, sb );
133         assertEquals( "%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString() );
134 
135         mojo.setLocalRepoProperty( "%M2_REPO%" );
136         sb.setLength( 0 );
137         mojo.setPrependGroupId( true );
138         mojo.appendArtifactPath( artifact, sb );
139         assertEquals("If prefix is null, prependGroupId has no impact ", "%M2_REPO%"+File.separator 
140                      + DependencyUtil.getFormattedFileName( artifact, false, false ), sb.toString());
141         
142         mojo.setLocalRepoProperty( "" );
143         mojo.setPrefix( "prefix" );
144         sb.setLength( 0 );
145         mojo.setPrependGroupId( true );
146         mojo.appendArtifactPath( artifact, sb );
147         assertEquals("prefix"+File.separator+DependencyUtil.getFormattedFileName( artifact, false, true ), 
148                      sb.toString());
149         mojo.setPrependGroupId( false );
150         
151         mojo.setLocalRepoProperty( "" );
152         mojo.setPrefix( "prefix" );
153         sb.setLength( 0 );
154         mojo.appendArtifactPath( artifact, sb );
155         assertEquals("prefix"+File.separator+artifact.getFile().getName(),sb.toString());
156       
157         mojo.setPrefix( "prefix" );
158         mojo.setStripVersion( true );
159         sb.setLength( 0 );
160         mojo.appendArtifactPath( artifact, sb );
161         assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ), sb.toString() );
162         
163     }
164 }