View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.lang.reflect.Field;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.LinkedHashSet;
27  import java.util.Set;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.maven.execution.DefaultRuntimeInformation;
32  import org.apache.maven.model.Build;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.model.Resource;
35  import org.apache.maven.plugin.ide.AbstractIdeSupportMojo;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.shared.tools.easymock.TestFileManager;
38  
39  public class EclipsePluginUnitTest
40      extends TestCase
41  {
42  
43      private TestFileManager fileManager = new TestFileManager( "EclipsePlugin.unitTest.", "" );
44  
45      public void tearDown()
46          throws IOException
47      {
48          fileManager.cleanUp();
49      }
50  
51      private EclipsePlugin newMojo()
52          throws Exception
53      {
54          EclipsePlugin mojo = new EclipsePlugin();
55          DefaultRuntimeInformation rti = new DefaultRuntimeInformation();
56          rti.initialize();
57          Field field = AbstractIdeSupportMojo.class.getDeclaredField( "runtimeInformation" );
58          field.setAccessible( true );
59          field.set( mojo, rti );
60          return mojo;
61      }
62  
63      public void testBuildDirectoryList_ShouldUseTestOutputDirFromProjectWhenBuildOutputDirIsStandard()
64          throws Exception
65      {
66          File basedir = fileManager.createTempDir();
67  
68          Build build = new Build();
69  
70          Resource resource = new Resource();
71  
72          String resDir = "src/main/resources";
73          new File( basedir, resDir ).mkdirs();
74  
75          String resOutput = "target/main-output";
76  
77          resource.setDirectory( resDir );
78  
79          build.addTestResource( resource );
80          build.setOutputDirectory( "target/classes" );
81          build.setTestOutputDirectory( resOutput );
82  
83          Model model = new Model();
84          model.setBuild( build );
85  
86          MavenProject project = new MavenProject( model );
87  
88          File pom = new File( basedir, "pom.xml" );
89          project.setFile( pom );
90  
91          EclipsePlugin mojo = newMojo();
92  
93          ArrayList sourceIncludes = new ArrayList();
94          Field field = EclipsePlugin.class.getDeclaredField( "sourceIncludes" );
95          field.setAccessible( true );
96          field.set( mojo, sourceIncludes );
97  
98          ArrayList sourceExcludes = new ArrayList();
99          field = EclipsePlugin.class.getDeclaredField( "sourceExcludes" );
100         field.setAccessible( true );
101         field.set( mojo, sourceExcludes );
102 
103         EclipseSourceDir[] result = mojo.buildDirectoryList( project, basedir, new File( "target/classes" ) );
104 
105         assertEquals( "should have added 1 resource.", 1, result.length );
106 
107         String path = result[0].getOutput();
108 
109         assertEquals( resOutput, path );
110     }
111 
112     public void testExtractResourceDirs_ShouldUseResourceOutput()
113         throws Exception
114     {
115         File basedir = fileManager.createTempDir();
116 
117         Build build = new Build();
118 
119         Resource resource = new Resource();
120 
121         String resDir = "src/main/resources";
122         new File( basedir, resDir ).mkdirs();
123 
124         // assumes base of target/classes.
125         String resOutput = "main-output";
126 
127         resource.setDirectory( resDir );
128         resource.setTargetPath( resOutput );
129         build.addResource( resource );
130 
131         Model model = new Model();
132         model.setBuild( build );
133 
134         MavenProject project = new MavenProject( model );
135 
136         Set result = new LinkedHashSet();
137 
138         EclipsePlugin plugin = newMojo();
139 
140         plugin.extractResourceDirs( result, project.getBuild().getResources(), basedir, basedir, false,
141                                     "target/classes" );
142 
143         Iterator resultIter = result.iterator();
144 
145         assertEquals( "too many resource entries added.", 1, result.size() );
146 
147         String path = ( (EclipseSourceDir) resultIter.next() ).getOutput();
148 
149         String prefix = "target/classes/";
150 
151         assertEquals( prefix + resOutput, path );
152     }
153 
154     public void testExtractResourceDirs_ShouldUseSpecifiedOutputDirectory()
155         throws Exception
156     {
157         File basedir = fileManager.createTempDir();
158 
159         Build build = new Build();
160 
161         Resource resource = new Resource();
162 
163         String resDir = "src/main/resources";
164         new File( basedir, resDir ).mkdirs();
165 
166         String resOutput = "target/main-output";
167 
168         resource.setDirectory( resDir );
169 
170         build.addTestResource( resource );
171 
172         Model model = new Model();
173         model.setBuild( build );
174 
175         MavenProject project = new MavenProject( model );
176 
177         Set result = new LinkedHashSet();
178 
179         EclipsePlugin plugin = newMojo();
180 
181         plugin.extractResourceDirs( result, project.getBuild().getTestResources(), basedir, basedir, false, resOutput );
182 
183         Iterator resultIter = result.iterator();
184 
185         assertEquals( "should have added 1 resource.", 1, result.size() );
186 
187         String path = ( (EclipseSourceDir) resultIter.next() ).getOutput();
188 
189         assertEquals( resOutput, path );
190     }
191 
192     public void testExtractResourceDirs_ShouldIncludeMainAndTestResources()
193         throws Exception
194     {
195         File basedir = fileManager.createTempDir();
196 
197         runResourceExtractionTest( basedir, basedir );
198     }
199 
200     public void testExtractResourceDirs_ShouldIncludeMainAndTestResourcesWhenBaseDirsDiffer()
201         throws Exception
202     {
203         File basedir = fileManager.createTempDir();
204         File projectBasedir = fileManager.createTempDir();
205 
206         runResourceExtractionTest( basedir, projectBasedir );
207     }
208 
209     private void runResourceExtractionTest( File basedir, File workspaceProjectBasedir )
210         throws Exception
211     {
212         Build build = new Build();
213 
214         Resource resource = new Resource();
215 
216         String resDir = "src/main/resources";
217         new File( basedir, resDir ).mkdirs();
218 
219         resource.setDirectory( resDir );
220         build.addResource( resource );
221 
222         Resource testResource = new Resource();
223 
224         String testResDir = "src/test/resources";
225         new File( basedir, testResDir ).mkdirs();
226 
227         testResource.setDirectory( testResDir );
228         build.addTestResource( testResource );
229 
230         Model model = new Model();
231         model.setBuild( build );
232 
233         MavenProject project = new MavenProject( model );
234 
235         Set result = new LinkedHashSet();
236 
237         EclipsePlugin plugin = newMojo();
238 
239         plugin.extractResourceDirs( result, project.getBuild().getResources(), basedir, workspaceProjectBasedir, false,
240                                     "target/classes" );
241 
242         Iterator resultIter = result.iterator();
243 
244         assertEquals( "too many resource entries added.", 1, result.size() );
245 
246         String path = ( (EclipseSourceDir) resultIter.next() ).getPath();
247 
248         if ( !basedir.equals( workspaceProjectBasedir ) )
249         {
250             resDir = resDir.replace( '\\', '/' ).replace( '/', '-' );
251         }
252         assertEquals( resDir, path );
253 
254         plugin.extractResourceDirs( result, project.getBuild().getTestResources(), basedir, workspaceProjectBasedir,
255                                     false, "target/test-classes" );
256 
257         resultIter = result.iterator();
258         resultIter.next();
259 
260         assertEquals( "too many test-resource entries added.", 2, result.size() );
261 
262         path = ( (EclipseSourceDir) resultIter.next() ).getPath();
263 
264         if ( !basedir.equals( workspaceProjectBasedir ) )
265         {
266             testResDir = testResDir.replace( '\\', '/' ).replace( '/', '-' );
267         }
268         assertEquals( testResDir, path );
269     }
270 }