1 package org.apache.maven.plugin.dependency.fromDependencies;
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.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
41 super.setUp( "build-classpath", true );
42 }
43
44
45
46
47
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
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
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 }