1 package org.apache.maven.plugins.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.execution.MavenSession;
27 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
28 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
29 import org.apache.maven.project.MavenProject;
30 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
31 import org.sonatype.aether.util.DefaultRepositorySystemSession;
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;
69 try
70 {
71 mojo.readClasspathFile();
72
73 fail( "Expected an illegal Argument Exception" );
74 }
75 catch ( IllegalArgumentException e )
76 {
77
78 }
79
80 mojo.setOutputFile( 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.contains( File.pathSeparator ) );
88 assertTrue( file.contains( File.separator ) );
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.contains( File.pathSeparator ) );
102 assertFalse( file.contains( File.separator ) );
103 assertTrue( file.contains( fileSep ) );
104 assertTrue( file.contains( pathSep ) );
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()
116 throws Exception
117 {
118 File testPom = new File( getBasedir(), "target/test-classes/unit/build-classpath-test/plugin-config.xml" );
119 BuildClasspathMojo mojo = (BuildClasspathMojo) lookupMojo( "build-classpath", testPom );
120
121 assertNotNull( mojo );
122 assertNotNull( mojo.getProject() );
123
124 MavenSession session = newMavenSession( mojo.getProject() );
125 setVariableValueToObject( mojo, "session", session );
126
127 DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
128 repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
129
130 Artifact artifact = stubFactory.getReleaseArtifact();
131
132 StringBuilder sb = new StringBuilder();
133 mojo.setPrefix( null );
134 mojo.setStripVersion( false );
135 mojo.appendArtifactPath( artifact, sb );
136 assertEquals( artifact.getFile().getPath(), 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.appendArtifactPath( artifact, sb );
146 assertEquals( "%M2_REPO%" + File.separator + artifact.getFile().getName(), sb.toString() );
147
148 mojo.setLocalRepoProperty( "%M2_REPO%" );
149 sb.setLength( 0 );
150 mojo.setPrependGroupId( true );
151 mojo.appendArtifactPath( artifact, sb );
152 assertEquals( "If prefix is null, prependGroupId has no impact ",
153 "%M2_REPO%" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, false ),
154 sb.toString() );
155
156 mojo.setLocalRepoProperty( "" );
157 mojo.setPrefix( "prefix" );
158 sb.setLength( 0 );
159 mojo.setPrependGroupId( true );
160 mojo.appendArtifactPath( artifact, sb );
161 assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, false, true ),
162 sb.toString() );
163 mojo.setPrependGroupId( false );
164
165 mojo.setLocalRepoProperty( "" );
166 mojo.setPrefix( "prefix" );
167 sb.setLength( 0 );
168 mojo.appendArtifactPath( artifact, sb );
169 assertEquals( "prefix" + File.separator + artifact.getFile().getName(), sb.toString() );
170
171 mojo.setPrefix( "prefix" );
172 mojo.setStripVersion( true );
173 sb.setLength( 0 );
174 mojo.appendArtifactPath( artifact, sb );
175 assertEquals( "prefix" + File.separator + DependencyUtil.getFormattedFileName( artifact, true ),
176 sb.toString() );
177
178 }
179 }