View Javadoc

1   package org.apache.maven.plugin.resources.remote;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.DefaultArtifact;
23  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.execution.ReactorManager;
28  import org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.FileOutputStream;
36  import java.io.InputStream;
37  import java.net.URL;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.Calendar;
41  import java.util.Properties;
42  import java.util.jar.JarOutputStream;
43  import java.util.zip.ZipEntry;
44  import org.codehaus.plexus.util.IOUtil;
45  
46  
47  /**
48   * RemoteResources plugin Test Case
49   */
50  
51  public class RemoteResourcesMojoTest
52      extends AbstractMojoTestCase
53  {
54      static final String DEFAULT_BUNDLE_POM_PATH = "target/test-classes/unit/rrmojotest/bundle-plugin-config.xml";
55      static final String DEFAULT_PROCESS_POM_PATH = "target/test-classes/unit/rrmojotest/process-plugin-config.xml";
56  
57      public void setUp()
58          throws Exception
59      {
60          super.setUp();
61      }
62  
63      public void tearDown()
64          throws Exception
65      {
66  
67      }
68  
69      /**
70       * check test environment
71       *
72       * @throws Exception if any exception occurs
73       */
74      public void testTestEnvironment()
75          throws Exception
76      {
77          // Perform lookup on the Mojo to make sure everything is ok
78          lookupProcessMojo();
79      }
80  
81  
82      public void testNoBundles()
83          throws Exception
84      {
85          final MavenProjectResourcesStub project = createTestProject( "default-nobundles" );
86          final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithDefaultSettings( project );
87  
88          setupDefaultProject( project );
89  
90          mojo.execute();
91      }
92  
93      public void testCreateBundle()
94          throws Exception
95      {
96          buildResourceBundle( "default-createbundle",
97                              null,
98                              new String[] { "SIMPLE.txt" },
99                              null );
100     }
101 
102     public void testSimpleBundles()
103         throws Exception
104     {
105         final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" );
106         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
107                                                                         new String[] {
108                                                                             "test:test:1.0"
109                                                                         } );
110 
111         setupDefaultProject( project );
112 
113         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
114         String path = repo.pathOf( new DefaultArtifact( "test",
115                                                         "test",
116                                                         VersionRange.createFromVersion( "1.0" ),
117                                                         null,
118                                                         "jar",
119                                                         "",
120                                                         new DefaultArtifactHandler() ) );
121 
122         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
123         file.getParentFile().mkdirs();
124         buildResourceBundle( "default-simplebundles-create",
125                              null,
126                              new String[] { "SIMPLE.txt" },
127                              file );
128 
129 
130         mojo.execute();
131 
132         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
133         file = new File( file, "SIMPLE.txt" );
134         assertTrue( file.exists() );
135     }
136 
137     public void testVelocityUTF8()
138         throws Exception
139     {
140         final MavenProjectResourcesStub project = createTestProject( "default-utf8" );
141         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
142                                                                         new String[] {
143                                                                             "test:test:1.2"
144                                                                         } );
145 
146         setupDefaultProject( project );
147 
148         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
149         String path = repo.pathOf( new DefaultArtifact( "test",
150                                                         "test",
151                                                         VersionRange.createFromVersion( "1.2" ),
152                                                         null,
153                                                         "jar",
154                                                         "",
155                                                         new DefaultArtifactHandler() ) );
156 
157         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
158         file.getParentFile().mkdirs();
159         buildResourceBundle( "default-utf8-create",
160                              "UTF-8",
161                              new String[] { "UTF-8.bin.vm" },
162                              file );
163 
164         mojo.execute();
165 
166         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
167         file = new File( file, "UTF-8.bin" );
168         assertTrue( file.exists() );
169 
170         InputStream in = new FileInputStream( file );
171         byte[] data = IOUtil.toByteArray( in );
172         IOUtil.close( in );
173 
174         byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( "UTF-8" );
175         assertTrue( Arrays.equals( expected, data ) );
176     }
177 
178     public void testVelocityISO88591()
179         throws Exception
180     {
181         final MavenProjectResourcesStub project = createTestProject( "default-iso88591" );
182         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
183                                                                         new String[] {
184                                                                             "test:test:1.3"
185                                                                         } );
186 
187         setupDefaultProject( project );
188 
189         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
190         String path = repo.pathOf( new DefaultArtifact( "test",
191                                                         "test",
192                                                         VersionRange.createFromVersion( "1.3" ),
193                                                         null,
194                                                         "jar",
195                                                         "",
196                                                         new DefaultArtifactHandler() ) );
197 
198         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
199         file.getParentFile().mkdirs();
200         buildResourceBundle( "default-iso88591-create",
201                              "ISO-8859-1",
202                              new String[] { "ISO-8859-1.bin.vm" },
203                              file );
204 
205         mojo.execute();
206 
207         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
208         file = new File( file, "ISO-8859-1.bin" );
209         assertTrue( file.exists() );
210 
211         InputStream in = new FileInputStream( file );
212         byte[] data = IOUtil.toByteArray( in );
213         IOUtil.close( in );
214 
215         byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( "ISO-8859-1" );
216         assertTrue( Arrays.equals( expected, data ) );
217     }
218 
219     public void testFilteredBundles()
220         throws Exception
221     {
222         final MavenProjectResourcesStub project = createTestProject( "default-filterbundles" );
223         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
224                                                                         new String[] {
225                                                                             "test:test:1.1"
226                                                                         } );
227 
228         setupDefaultProject( project );
229 
230         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
231         String path = repo.pathOf( new DefaultArtifact( "test",
232                                                         "test",
233                                                         VersionRange.createFromVersion( "1.1" ),
234                                                         null,
235                                                         "jar",
236                                                         "",
237                                                         new DefaultArtifactHandler() ) );
238 
239         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
240         file.getParentFile().mkdirs();
241         buildResourceBundle( "default-filterbundles-create",
242                              null,
243                              new String[] { "FILTER.txt.vm" },
244                              file );
245 
246 
247         mojo.execute();
248         // executing a second time (example: forked lifecycle) should still work
249         mojo.execute();
250 
251         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
252         file = new File( file, "FILTER.txt" );
253         assertTrue( file.exists() );
254 
255         String data = FileUtils.fileRead( file );
256         assertTrue( data.indexOf( "2007" ) != -1 );
257         assertTrue( data.indexOf( "default-filterbundles" ) != -1 );
258     }
259 
260     public void testFilteredBundlesWithProjectProperties()
261       throws Exception
262     {
263         final MavenProjectResourcesStub project = createTestProject( "default-filterbundles-two" );
264         final ProcessRemoteResourcesMojo mojo =
265             lookupProcessMojoWithSettings( project, new String[]{"test-filtered-bundles:test-filtered-bundles:2"} );
266 
267         mojo.includeProjectProperties = true;
268         setupDefaultProject( project );
269 
270         project.addProperty( "testingPropertyOne", "maven" );
271         project.addProperty( "testingPropertyTwo", "rules" );
272 
273         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
274         String path = repo.pathOf( new DefaultArtifact( "test-filtered-bundles", "test-filtered-bundles",
275                                                         VersionRange.createFromVersion( "2" ), null, "jar", "",
276                                                         new DefaultArtifactHandler() ) );
277 
278         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
279         file.getParentFile().mkdirs();
280         buildResourceBundle( "default-filterbundles-two-create", null, new String[]{"PROPERTIES.txt.vm"}, file );
281 
282         mojo.execute();
283         // executing a second time (example: forked lifecycle) should still work
284         mojo.execute();
285 
286         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
287         file = new File( file, "PROPERTIES.txt" );
288 
289         assertTrue( file.exists() );
290 
291         String data = FileUtils.fileRead( file );
292         assertTrue( data.indexOf( "maven" ) != -1 );
293         assertTrue( data.indexOf( "rules" ) != -1 );
294     }
295 
296     protected void buildResourceBundle( String id,
297                                        String sourceEncoding,
298                                        String resourceNames[],
299                                        File jarName )
300     throws Exception
301     {
302         final MavenProjectResourcesStub project = createTestProject( id );
303 
304         final File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
305         final BundleRemoteResourcesMojo mojo = lookupBundleMojoWithSettings( project , resourceDir, sourceEncoding );
306 
307         setupDefaultProject( project );
308 
309         for ( int x = 0; x < resourceNames.length; x++ )
310         {
311             File resource = new File( resourceDir, resourceNames[x] );
312             URL source = getClass().getResource( "/" + resourceNames[x] );
313 
314             FileUtils.copyURLToFile( source, resource );
315         }
316 
317         mojo.execute();
318 
319         File xmlFile = new File( project.getBasedir() + "/target/classes/META-INF/maven/remote-resources.xml" );
320         assertTrue( xmlFile.exists() );
321 
322         String data = FileUtils.fileRead( xmlFile );
323         for ( int x = 0; x < resourceNames.length; x++ )
324         {
325             assertTrue( data.indexOf( resourceNames[x] ) != -1 );
326         }
327 
328         if ( null != jarName )
329         {
330             JarOutputStream jar = new JarOutputStream( new FileOutputStream( jarName ) );
331             jar.putNextEntry( new ZipEntry( "META-INF/maven/remote-resources.xml" ) );
332             jar.write( data.getBytes() );
333             jar.closeEntry();
334 
335             for ( int x = 0; x < resourceNames.length; x++ )
336             {
337                 File resource = new File( resourceDir, resourceNames[x] );
338                 InputStream in = new FileInputStream( resource );
339                 jar.putNextEntry( new ZipEntry( resourceNames[x] ) );
340                 IOUtil.copy( in, jar );
341                 IOUtil.close( in );
342                 jar.closeEntry();
343             }
344             jar.close();
345         }
346     }
347 
348 
349 
350     protected MavenProjectResourcesStub createTestProject( final String testName )
351     throws Exception
352     {
353         // this will automatically create the isolated
354         // test environment
355         return new MavenProjectResourcesStub( testName );
356     }
357     protected void setupDefaultProject( final MavenProjectResourcesStub project )
358     throws Exception
359     {
360         // put this on the root dir
361         project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE );
362         project.setInceptionYear( "2007" );
363         // start creating the environment
364         project.setupBuildEnvironment();
365     }
366 
367 
368     protected BundleRemoteResourcesMojo lookupBundleMojo()
369     throws Exception
370     {
371         File pomFile = new File( getBasedir(), DEFAULT_BUNDLE_POM_PATH );
372         BundleRemoteResourcesMojo mojo = (BundleRemoteResourcesMojo) lookupMojo( "bundle", pomFile );
373 
374         assertNotNull( mojo );
375 
376         return mojo;
377     }
378     protected BundleRemoteResourcesMojo lookupBundleMojoWithDefaultSettings( final MavenProject project )
379         throws Exception
380     {
381         File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
382         return lookupBundleMojoWithSettings( project, resourceDir, null );
383     }
384     protected BundleRemoteResourcesMojo lookupBundleMojoWithSettings( final MavenProject project,
385                                                                       File resourceDir, String sourceEncoding )
386     throws Exception
387     {
388         final BundleRemoteResourcesMojo mojo = lookupBundleMojo();
389 
390         setVariableValueToObject( mojo, "resourcesDirectory", resourceDir );
391         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
392         setVariableValueToObject( mojo, "sourceEncoding", sourceEncoding );
393         return mojo;
394     }
395 
396     protected ProcessRemoteResourcesMojo lookupProcessMojo()
397         throws Exception
398     {
399         File pomFile = new File( getBasedir(), DEFAULT_PROCESS_POM_PATH );
400         ProcessRemoteResourcesMojo mojo = (ProcessRemoteResourcesMojo) lookupMojo( "process", pomFile );
401 
402         assertNotNull( mojo );
403 
404         return mojo;
405     }
406 
407 
408     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
409                                                                  String bundles[] )
410         throws Exception
411     {
412         return lookupProcessMojoWithSettings( project, new ArrayList( Arrays.asList( bundles ) ) );
413     }
414 
415     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
416                                                                  ArrayList bundles )
417         throws Exception
418     {
419         final ProcessRemoteResourcesMojo mojo = lookupProcessMojo();
420 
421         MavenSession session = new MavenSession( container,
422                                     null, //Settings settings,
423                                     null, //ArtifactRepository localRepository,
424                                     null, //EventDispatcher eventDispatcher,
425                                     new ReactorManager(new ArrayList()),
426                                     Arrays.asList( new String[] {"install"} ),
427                                     project.getBasedir().toString(),
428                                     new Properties(),
429                                     Calendar.getInstance().getTime() );
430 
431         setVariableValueToObject( mojo, "project", project );
432         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
433         setVariableValueToObject( mojo, "resourceBundles", bundles );
434         setVariableValueToObject( mojo, "mavenSession", session );
435         setVariableValueToObject( mojo, "remoteArtifactRepositories", project.getRemoteArtifactRepositories() );
436         setVariableValueToObject( mojo, "resources", project.getResources() );
437         return mojo;
438     }
439 
440     protected ProcessRemoteResourcesMojo lookupProcessMojoWithDefaultSettings( final MavenProject project )
441         throws Exception
442     {
443         return lookupProcessMojoWithSettings( project, new ArrayList() );
444     }
445 }