1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.eclipse;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.LinkedHashSet;
25 import java.util.Set;
26
27 import junit.framework.TestCase;
28
29 import org.apache.maven.model.Build;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.Resource;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.shared.tools.easymock.TestFileManager;
35
36 public class EclipsePluginUnitTest
37 extends TestCase
38 {
39
40 private TestFileManager fileManager = new TestFileManager( "EclipsePlugin.unitTest.", "" );
41
42 public void tearDown()
43 throws IOException
44 {
45 fileManager.cleanUp();
46 }
47
48 public void testBuildDirectoryList_ShouldUseTestOutputDirFromProjectWhenBuildOutputDirIsStandard()
49 throws MojoExecutionException
50 {
51 File basedir = fileManager.createTempDir();
52
53 Build build = new Build();
54
55 Resource resource = new Resource();
56
57 String resDir = "src/main/resources";
58 new File( basedir, resDir ).mkdirs();
59
60 String resOutput = "target/main-output";
61
62 resource.setDirectory( resDir );
63
64 build.addTestResource( resource );
65 build.setOutputDirectory( "target/classes" );
66 build.setTestOutputDirectory( resOutput );
67
68 Model model = new Model();
69 model.setBuild( build );
70
71 MavenProject project = new MavenProject( model );
72
73 File pom = new File( basedir, "pom.xml" );
74 project.setFile( pom );
75
76 EclipseSourceDir[] result =
77 new EclipsePlugin().buildDirectoryList( project, basedir, new File( "target/classes" ) );
78
79 assertEquals( "should have added 1 resource.", 1, result.length );
80
81 String path = result[0].getOutput();
82
83 assertTrue( "output directory should end with: " + resOutput, path.endsWith( resOutput ) );
84 }
85
86 public void testExtractResourceDirs_ShouldUseResourceOutput()
87 throws MojoExecutionException
88 {
89 File basedir = fileManager.createTempDir();
90
91 Build build = new Build();
92
93 Resource resource = new Resource();
94
95 String resDir = "src/main/resources";
96 new File( basedir, resDir ).mkdirs();
97
98
99 String resOutput = "main-output";
100
101 resource.setDirectory( resDir );
102 resource.setTargetPath( resOutput );
103 build.addResource( resource );
104
105 Model model = new Model();
106 model.setBuild( build );
107
108 MavenProject project = new MavenProject( model );
109
110 Set result = new LinkedHashSet();
111
112 EclipsePlugin plugin = new EclipsePlugin();
113
114 plugin.extractResourceDirs( result, project.getBuild().getResources(), project, basedir, basedir, false,
115 "target/classes" );
116
117 Iterator resultIter = result.iterator();
118
119 assertEquals( "too many resource entries added.", 1, result.size() );
120
121 String path = ( (EclipseSourceDir) resultIter.next() ).getOutput();
122
123 String prefix = "target/classes/";
124
125 assertTrue( "output directory should end with: " + prefix + resOutput + "\nWas: " + path,
126 path.endsWith( prefix + resOutput ) );
127 }
128
129 public void testExtractResourceDirs_ShouldUseSpecifiedOutputDirectory()
130 throws MojoExecutionException
131 {
132 File basedir = fileManager.createTempDir();
133
134 Build build = new Build();
135
136 Resource resource = new Resource();
137
138 String resDir = "src/main/resources";
139 new File( basedir, resDir ).mkdirs();
140
141 String resOutput = "target/main-output";
142
143 resource.setDirectory( resDir );
144
145 build.addTestResource( resource );
146
147 Model model = new Model();
148 model.setBuild( build );
149
150 MavenProject project = new MavenProject( model );
151
152 Set result = new LinkedHashSet();
153
154 EclipsePlugin plugin = new EclipsePlugin();
155
156 plugin.extractResourceDirs( result, project.getBuild().getTestResources(), project, basedir, basedir, false,
157 resOutput );
158
159 Iterator resultIter = result.iterator();
160
161 assertEquals( "should have added 1 resource.", 1, result.size() );
162
163 String path = ( (EclipseSourceDir) resultIter.next() ).getOutput();
164
165 assertTrue( "output directory should end with: " + resOutput, path.endsWith( resOutput ) );
166 }
167
168 public void testExtractResourceDirs_ShouldIncludeMainAndTestResources()
169 throws MojoExecutionException
170 {
171 File basedir = fileManager.createTempDir();
172
173 runResourceExtractionTest( basedir, basedir );
174 }
175
176 public void testExtractResourceDirs_ShouldIncludeMainAndTestResourcesWhenBaseDirsDiffer()
177 throws MojoExecutionException
178 {
179 File basedir = fileManager.createTempDir();
180 File projectBasedir = fileManager.createTempDir();
181
182 runResourceExtractionTest( basedir, projectBasedir );
183 }
184
185 private void runResourceExtractionTest( File basedir, File workspaceProjectBasedir )
186 throws MojoExecutionException
187 {
188 Build build = new Build();
189
190 Resource resource = new Resource();
191
192 String resDir = "src/main/resources";
193 new File( basedir, resDir ).mkdirs();
194
195 resource.setDirectory( resDir );
196 build.addResource( resource );
197
198 Resource testResource = new Resource();
199
200 String testResDir = "src/test/resources";
201 new File( basedir, testResDir ).mkdirs();
202
203 testResource.setDirectory( testResDir );
204 build.addTestResource( testResource );
205
206 Model model = new Model();
207 model.setBuild( build );
208
209 MavenProject project = new MavenProject( model );
210
211 Set result = new LinkedHashSet();
212
213 EclipsePlugin plugin = new EclipsePlugin();
214
215 plugin.extractResourceDirs( result, project.getBuild().getResources(), project, basedir,
216 workspaceProjectBasedir, false, "target/classes" );
217
218 Iterator resultIter = result.iterator();
219
220 assertEquals( "too many resource entries added.", 1, result.size() );
221
222 String path = ( (EclipseSourceDir) resultIter.next() ).getPath();
223
224 if ( basedir.equals( workspaceProjectBasedir ) )
225 {
226 assertTrue( "resource dir path: " + path + " does not end with: " + resDir, path.endsWith( resDir ) );
227 }
228 else
229 {
230 resDir = resDir.replace( '\\', '/' ).replace( '/', '-' );
231
232 assertTrue( "resource dir path: " + path + " does not end with: " + resDir, path.endsWith( resDir ) );
233 }
234
235 plugin.extractResourceDirs( result, project.getBuild().getTestResources(), project, basedir,
236 workspaceProjectBasedir, false, "target/test-classes" );
237
238 resultIter = result.iterator();
239 resultIter.next();
240
241 assertEquals( "too many test-resource entries added.", 2, result.size() );
242
243 path = ( (EclipseSourceDir) resultIter.next() ).getPath();
244
245 if ( basedir.equals( workspaceProjectBasedir ) )
246 {
247 assertTrue( "test-resource dir path: " + path + " does not end with: " + testResDir,
248 path.endsWith( testResDir ) );
249 }
250 else
251 {
252 testResDir = testResDir.replace( '\\', '/' ).replace( '/', '-' );
253
254 assertTrue( "test-resource dir path: " + path + " does not end with: " + testResDir,
255 path.endsWith( testResDir ) );
256 }
257
258 }
259 }