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 testSimpleBundlesWithType()
138         throws Exception
139     {
140         final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" );
141         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
142                                                                         new String[] {
143                                                                             "test:test:1.0:war"
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.0" ),
152                                                         null,
153                                                         "war",
154                                                         "",
155                                                         new DefaultArtifactHandler() ) );
156     
157         File file = new File( repo.getBasedir() + "/" + path + ".war" );
158         file.getParentFile().mkdirs();
159         buildResourceBundle( "default-simplebundles-create",
160                              null,
161                              new String[] { "SIMPLE.txt" },
162                              file );
163     
164     
165         mojo.execute();
166     
167         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
168         file = new File( file, "SIMPLE.txt" );
169         assertTrue( file.exists() );
170     }
171     
172     public void testSimpleBundlesWithClassifier()
173         throws Exception
174     {
175         final MavenProjectResourcesStub project = createTestProject( "default-simplebundles" );
176         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
177                                                                         new String[] {
178                                                                             "test:test:1.0:jar:test"
179                                                                         } );
180     
181         setupDefaultProject( project );
182     
183         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
184         String path = repo.pathOf( new DefaultArtifact( "test",
185                                                         "test",
186                                                         VersionRange.createFromVersion( "1.0" ),
187                                                         null,
188                                                         "jar",
189                                                         "test",
190                                                         new DefaultArtifactHandler() ) );
191     
192         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
193         file.getParentFile().mkdirs();
194         buildResourceBundle( "default-simplebundles-create",
195                              null,
196                              new String[] { "SIMPLE.txt" },
197                              file );
198     
199     
200         mojo.execute();
201     
202         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
203         file = new File( file, "SIMPLE.txt" );
204         assertTrue( file.exists() );
205     }
206     
207     public void testVelocityUTF8()
208         throws Exception
209     {
210         final MavenProjectResourcesStub project = createTestProject( "default-utf8" );
211         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
212                                                                         new String[] {
213                                                                             "test:test:1.2"
214                                                                         } );
215 
216         setupDefaultProject( project );
217 
218         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
219         String path = repo.pathOf( new DefaultArtifact( "test",
220                                                         "test",
221                                                         VersionRange.createFromVersion( "1.2" ),
222                                                         null,
223                                                         "jar",
224                                                         "",
225                                                         new DefaultArtifactHandler() ) );
226 
227         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
228         file.getParentFile().mkdirs();
229         buildResourceBundle( "default-utf8-create",
230                              "UTF-8",
231                              new String[] { "UTF-8.bin.vm" },
232                              file );
233 
234         mojo.execute();
235 
236         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
237         file = new File( file, "UTF-8.bin" );
238         assertTrue( file.exists() );
239 
240         InputStream in = new FileInputStream( file );
241         byte[] data = IOUtil.toByteArray( in );
242         IOUtil.close( in );
243 
244         byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( "UTF-8" );
245         assertTrue( Arrays.equals( expected, data ) );
246     }
247 
248     public void testVelocityISO88591()
249         throws Exception
250     {
251         final MavenProjectResourcesStub project = createTestProject( "default-iso88591" );
252         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
253                                                                         new String[] {
254                                                                             "test:test:1.3"
255                                                                         } );
256 
257         setupDefaultProject( project );
258 
259         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
260         String path = repo.pathOf( new DefaultArtifact( "test",
261                                                         "test",
262                                                         VersionRange.createFromVersion( "1.3" ),
263                                                         null,
264                                                         "jar",
265                                                         "",
266                                                         new DefaultArtifactHandler() ) );
267 
268         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
269         file.getParentFile().mkdirs();
270         buildResourceBundle( "default-iso88591-create",
271                              "ISO-8859-1",
272                              new String[] { "ISO-8859-1.bin.vm" },
273                              file );
274 
275         mojo.execute();
276 
277         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
278         file = new File( file, "ISO-8859-1.bin" );
279         assertTrue( file.exists() );
280 
281         InputStream in = new FileInputStream( file );
282         byte[] data = IOUtil.toByteArray( in );
283         IOUtil.close( in );
284 
285         byte[] expected = "\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF".getBytes( "ISO-8859-1" );
286         assertTrue( Arrays.equals( expected, data ) );
287     }
288 
289     public void testFilteredBundles()
290         throws Exception
291     {
292         final MavenProjectResourcesStub project = createTestProject( "default-filterbundles" );
293         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
294                                                                         new String[] {
295                                                                             "test:test:1.1"
296                                                                         } );
297 
298         setupDefaultProject( project );
299 
300         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
301         String path = repo.pathOf( new DefaultArtifact( "test",
302                                                         "test",
303                                                         VersionRange.createFromVersion( "1.1" ),
304                                                         null,
305                                                         "jar",
306                                                         "",
307                                                         new DefaultArtifactHandler() ) );
308 
309         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
310         file.getParentFile().mkdirs();
311         buildResourceBundle( "default-filterbundles-create",
312                              null,
313                              new String[] { "FILTER.txt.vm" },
314                              file );
315 
316 
317         mojo.execute();
318         // executing a second time (example: forked lifecycle) should still work
319         mojo.execute();
320 
321         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
322         file = new File( file, "FILTER.txt" );
323         assertTrue( file.exists() );
324 
325         String data = FileUtils.fileRead( file );
326         assertTrue( data.indexOf( "2007" ) != -1 );
327         assertTrue( data.indexOf( "default-filterbundles" ) != -1 );
328     }
329 
330     public void testFilteredBundlesWithProjectProperties()
331       throws Exception
332     {
333         final MavenProjectResourcesStub project = createTestProject( "default-filterbundles-two" );
334         final ProcessRemoteResourcesMojo mojo =
335             lookupProcessMojoWithSettings( project, new String[]{"test-filtered-bundles:test-filtered-bundles:2"} );
336 
337         mojo.includeProjectProperties = true;
338         setupDefaultProject( project );
339 
340         project.addProperty( "testingPropertyOne", "maven" );
341         project.addProperty( "testingPropertyTwo", "rules" );
342 
343         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
344         String path = repo.pathOf( new DefaultArtifact( "test-filtered-bundles", "test-filtered-bundles",
345                                                         VersionRange.createFromVersion( "2" ), null, "jar", "",
346                                                         new DefaultArtifactHandler() ) );
347 
348         File file = new File( repo.getBasedir() + "/" + path + ".jar" );
349         file.getParentFile().mkdirs();
350         buildResourceBundle( "default-filterbundles-two-create", null, new String[]{"PROPERTIES.txt.vm"}, file );
351 
352         mojo.execute();
353         // executing a second time (example: forked lifecycle) should still work
354         mojo.execute();
355 
356         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
357         file = new File( file, "PROPERTIES.txt" );
358 
359         assertTrue( file.exists() );
360 
361         String data = FileUtils.fileRead( file );
362         assertTrue( data.indexOf( "maven" ) != -1 );
363         assertTrue( data.indexOf( "rules" ) != -1 );
364     }
365 
366     protected void buildResourceBundle( String id,
367                                        String sourceEncoding,
368                                        String resourceNames[],
369                                        File jarName )
370     throws Exception
371     {
372         final MavenProjectResourcesStub project = createTestProject( id );
373 
374         final File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
375         final BundleRemoteResourcesMojo mojo = lookupBundleMojoWithSettings( project , resourceDir, sourceEncoding );
376 
377         setupDefaultProject( project );
378 
379         for ( int x = 0; x < resourceNames.length; x++ )
380         {
381             File resource = new File( resourceDir, resourceNames[x] );
382             URL source = getClass().getResource( "/" + resourceNames[x] );
383 
384             FileUtils.copyURLToFile( source, resource );
385         }
386 
387         mojo.execute();
388 
389         File xmlFile = new File( project.getBasedir() + "/target/classes/META-INF/maven/remote-resources.xml" );
390         assertTrue( xmlFile.exists() );
391 
392         String data = FileUtils.fileRead( xmlFile );
393         for ( int x = 0; x < resourceNames.length; x++ )
394         {
395             assertTrue( data.indexOf( resourceNames[x] ) != -1 );
396         }
397 
398         if ( null != jarName )
399         {
400             JarOutputStream jar = new JarOutputStream( new FileOutputStream( jarName ) );
401             jar.putNextEntry( new ZipEntry( "META-INF/maven/remote-resources.xml" ) );
402             jar.write( data.getBytes() );
403             jar.closeEntry();
404 
405             for ( int x = 0; x < resourceNames.length; x++ )
406             {
407                 File resource = new File( resourceDir, resourceNames[x] );
408                 InputStream in = new FileInputStream( resource );
409                 jar.putNextEntry( new ZipEntry( resourceNames[x] ) );
410                 IOUtil.copy( in, jar );
411                 IOUtil.close( in );
412                 jar.closeEntry();
413             }
414             jar.close();
415         }
416     }
417 
418 
419 
420     protected MavenProjectResourcesStub createTestProject( final String testName )
421     throws Exception
422     {
423         // this will automatically create the isolated
424         // test environment
425         return new MavenProjectResourcesStub( testName );
426     }
427     protected void setupDefaultProject( final MavenProjectResourcesStub project )
428     throws Exception
429     {
430         // put this on the root dir
431         project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE );
432         project.setInceptionYear( "2007" );
433         // start creating the environment
434         project.setupBuildEnvironment();
435     }
436 
437 
438     protected BundleRemoteResourcesMojo lookupBundleMojo()
439     throws Exception
440     {
441         File pomFile = new File( getBasedir(), DEFAULT_BUNDLE_POM_PATH );
442         BundleRemoteResourcesMojo mojo = (BundleRemoteResourcesMojo) lookupMojo( "bundle", pomFile );
443 
444         assertNotNull( mojo );
445 
446         return mojo;
447     }
448     protected BundleRemoteResourcesMojo lookupBundleMojoWithDefaultSettings( final MavenProject project )
449         throws Exception
450     {
451         File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
452         return lookupBundleMojoWithSettings( project, resourceDir, null );
453     }
454     protected BundleRemoteResourcesMojo lookupBundleMojoWithSettings( final MavenProject project,
455                                                                       File resourceDir, String sourceEncoding )
456     throws Exception
457     {
458         final BundleRemoteResourcesMojo mojo = lookupBundleMojo();
459 
460         setVariableValueToObject( mojo, "resourcesDirectory", resourceDir );
461         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
462         setVariableValueToObject( mojo, "sourceEncoding", sourceEncoding );
463         return mojo;
464     }
465 
466     protected ProcessRemoteResourcesMojo lookupProcessMojo()
467         throws Exception
468     {
469         File pomFile = new File( getBasedir(), DEFAULT_PROCESS_POM_PATH );
470         ProcessRemoteResourcesMojo mojo = (ProcessRemoteResourcesMojo) lookupMojo( "process", pomFile );
471 
472         assertNotNull( mojo );
473 
474         return mojo;
475     }
476 
477 
478     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
479                                                                  String bundles[] )
480         throws Exception
481     {
482         return lookupProcessMojoWithSettings( project, new ArrayList( Arrays.asList( bundles ) ) );
483     }
484 
485     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
486                                                                  ArrayList bundles )
487         throws Exception
488     {
489         final ProcessRemoteResourcesMojo mojo = lookupProcessMojo();
490 
491         MavenSession session = new MavenSession( container,
492                                     null, //Settings settings,
493                                     null, //ArtifactRepository localRepository,
494                                     null, //EventDispatcher eventDispatcher,
495                                     new ReactorManager(new ArrayList()),
496                                     Arrays.asList( new String[] {"install"} ),
497                                     project.getBasedir().toString(),
498                                     new Properties(),
499                                     Calendar.getInstance().getTime() );
500 
501         setVariableValueToObject( mojo, "project", project );
502         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
503         setVariableValueToObject( mojo, "resourceBundles", bundles );
504         setVariableValueToObject( mojo, "mavenSession", session );
505         setVariableValueToObject( mojo, "remoteArtifactRepositories", project.getRemoteArtifactRepositories() );
506         setVariableValueToObject( mojo, "resources", project.getResources() );
507         return mojo;
508     }
509 
510     protected ProcessRemoteResourcesMojo lookupProcessMojoWithDefaultSettings( final MavenProject project )
511         throws Exception
512     {
513         return lookupProcessMojoWithSettings( project, new ArrayList() );
514     }
515 }