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