1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39 super.setUp( "build-classpath", true );
40 }
41
42
43
44
45
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
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
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 String propertyValue = project.getProperties().getProperty( "outputProperty" );
105 assertNull( propertyValue );
106 mojo.setOutputProperty( "outputProperty" );
107 mojo.execute();
108 propertyValue = project.getProperties().getProperty( "outputProperty" );
109 assertNotNull( propertyValue );
110
111 }
112
113 public void testPath() throws Exception
114 {
115 File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
116 BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
117
118 assertNotNull( mojo );
119 assertNotNull( mojo.getProject() );
120
121 ArtifactRepository local = new StubArtifactRepository( stubFactory.getWorkingDir().getPath() );
122 mojo.setLocal( local );
123
124 Artifact artifact = stubFactory.getReleaseArtifact();
125
126
127 StringBuilder sb = new StringBuilder();
128 mojo.setPrefix( null );
129 mojo.setStripVersion( false );
130 mojo.appendArtifactPath( artifact, sb );
131 assertEquals( artifact.getFile().getPath(), sb.toString() );
132
133 mojo.setLocalRepoProperty( "$M2_REPO" );
134 sb.setLength( 0 );
135 mojo.appendArtifactPath( artifact, sb );
136 assertEquals( "$M2_REPO" + File.separator + artifact.getFile().getName(), 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.setPrependGroupId( true );
146 mojo.appendArtifactPath( artifact, sb );
147 assertEquals("If prefix is null, prependGroupId has no impact ", "%M2_REPO%"+File.separator
148 + DependencyUtil.getFormattedFileName( artifact, false, false ), sb.toString());
149
150 mojo.setLocalRepoProperty( "" );
151 mojo.setPrefix( "prefix" );
152 sb.setLength( 0 );
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.setLength( 0 );
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.setLength( 0 );
168 mojo.appendArtifactPath( artifact, sb );
169 assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ), sb.toString() );
170
171 }
172 }