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.FileNotFoundException;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.codehaus.plexus.util.xml.Xpp3Dom;
31  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  /**
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
37   * @version $Id: EclipsePluginTest.java 628794 2008-02-18 16:09:11Z aheritier $
38   */
39  public class EclipsePluginTest
40      extends AbstractEclipsePluginTestCase
41  {
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46      }
47  
48      public void testProject01()
49          throws Exception
50      {
51          testProject( "project-01" );
52      }
53  
54      public void testProject02()
55          throws Exception
56      {
57          testProject( "project-02" );
58      }
59  
60      public void testProject03()
61          throws Exception
62      {
63          testProject( "project-03" );
64      }
65  
66      public void testProject04()
67          throws Exception
68      {
69          testProject( "project-04" );
70      }
71  
72      public void testProject05()
73          throws Exception
74      {
75          testProject( "project-05" );
76      }
77  
78      public void testProject06()
79          throws Exception
80      {
81          testProject( "project-06" );
82      }
83  
84      /**
85       * @throws Exception
86       */
87      public void testProject07()
88          throws Exception
89      {
90          // Fails because of MECLIPSE-367
91          testProject( "project-07" );
92      }
93  
94      public void testProject08()
95          throws Exception
96      {
97          testProject( "project-08" );
98      }
99  
100     /**
101      * Tests with <code>outputDirectory</code> and <code>outputDir</code>
102      * 
103      * @throws Exception
104      */
105     public void testProject09()
106         throws Exception
107     {
108         testProject( "project-09" );
109     }
110 
111     public void testProject10()
112         throws Exception
113     {
114         testProject( "project-10" );
115     }
116 
117     public void testProject11()
118         throws Exception
119     {
120         testProject( "project-11" );
121     }
122 
123     /**
124      * Ear packaging
125      * 
126      * @throws Exception any exception thrown during test
127      */
128     public void testProject12()
129         throws Exception
130     {
131         testProject( "project-12" );
132     }
133 
134     /**
135      * Dependency range - MECLIPSE-96
136      * 
137      * @throws Exception any exception thrown during test
138      */
139     public void testProject13()
140         throws Exception
141     {
142         testProject( "project-13" );
143     }
144 
145     /**
146      * Additional natures and builders - MECLIPSE-64
147      * 
148      * @throws Exception any exception thrown during test
149      */
150     public void testProject14()
151         throws Exception
152     {
153         testProject( "project-14" );
154     }
155 
156     /**
157      * <code>outputDirectory</code> parameter - MECLIPSE-11
158      * 
159      * @throws Exception any exception thrown during test
160      */
161     public void testProject15()
162         throws Exception
163     {
164         Properties props = new Properties();
165         props.put( "outputDirectory", "bin" );
166         testProject( "project-15", props, "clean", "eclipse" );
167     }
168 
169     /**
170      * UTF8 encoding - MECLIPSE-56
171      * 
172      * @throws Exception any exception thrown during test
173      */
174     public void testProject16()
175         throws Exception
176     {
177         // failing with maven < 2.0.8 due to MNG-2025
178         testProject( "project-16" );
179     }
180 
181     /**
182      * ISO-8859-15 encoding - MECLIPSE-56
183      * 
184      * @throws Exception any exception thrown during test
185      */
186     public void testProject17()
187         throws Exception
188     {
189         // failing with maven < 2.0.8 due to MNG-2025
190         testProject( "project-17" );
191     }
192 
193     /**
194      * relative location of system dependencies - MECLIPSE-89
195      * 
196      * @throws Exception any exception thrown during test
197      */
198     public void testProject18()
199         throws Exception
200     {
201         testProject( "project-18" );
202     }
203 
204     /**
205      * Resource targetPath is relative to the project's output directory - MECLIPSE-77
206      * 
207      * @throws Exception any exception thrown during test
208      */
209     public void testProject19()
210         throws Exception
211     {
212         testProject( "project-19" );
213     }
214 
215     /**
216      * WTP 1.5 changes in wtpmodules.
217      * 
218      * @throws Exception any exception thrown during test
219      */
220     public void testProject20()
221         throws Exception
222     {
223         testProject( "project-20" );
224     }
225 
226     /**
227      * PDE support.
228      * 
229      * @throws Exception any exception thrown during test
230      */
231     public void testProject21()
232         throws Exception
233     {
234         testProject( "project-21" );
235     }
236 
237     /**
238      * PDE support using eclipse-plugin packaging.
239      * 
240      * @throws Exception any exception thrown during test
241      */
242     public void testProject22()
243         throws Exception
244     {
245         testProject( "project-22" );
246     }
247 
248     /**
249      * Additional config files using "additionalConfig" property.
250      * 
251      * @throws Exception any exception thrown during test
252      */
253     public void testProject23()
254         throws Exception
255     {
256         testProject( "project-23" );
257     }
258 
259     /**
260      * Test rewriting of OSGI manifest files.
261      * 
262      * @throws Exception any exception thrown during test
263      */
264     public void testProject24()
265         throws Exception
266     {
267         testProject( "project-24" );
268     }
269 
270     /**
271      * Test source exclude/include.
272      * 
273      * @throws Exception any exception thrown during test
274      */
275     public void testProject25()
276         throws Exception
277     {
278         testProject( "project-25" );
279     }
280 
281     /**
282      * Test different compiler settings for test sources.
283      * 
284      * @throws Exception any exception thrown during test
285      */
286     public void testProject26()
287         throws Exception
288     {
289         testProject( "project-26" );
290     }
291 
292     /**
293      * Test additional project facets specified.
294      * 
295      * @throws Exception any exception thrown during test
296      */
297     public void testProject27()
298         throws Exception
299     {
300         testProject( "project-27" );
301     }
302 
303     /**
304      * MECLIPSE-241 : Compiler settings from parent project aren't used in wtp facet.
305      * 
306      * @throws Exception any exception thrown during test
307      */
308     public void testProject28()
309         throws Exception
310     {
311         testProject( "project-28/module-1" );
312     }
313 
314     /**
315      * MECLIPSE-198 : EJB version is not resloved
316      * 
317      * @throws Exception any exception thrown during test
318      */
319     public void testProject29()
320         throws Exception
321     {
322         testProject( "project-29" );
323     }
324 
325     /**
326      * MECLIPSE-108 : .wtpmodules with version 2.4 for javax.servlet:servlet-api:2.3
327      * 
328      * @throws Exception any exception thrown during test
329      */
330     public void testProject30()
331         throws Exception
332     {
333         testProject( "project-30" );
334     }
335 
336     /**
337      * MECLIPSE-185 : plugin doesn't fail when dependencies are missing
338      * 
339      * @throws Exception any exception thrown during test
340      */
341     public void testProject31()
342         throws Exception
343     {
344         try
345         {
346             testProject( "project-31" );
347 
348             // disabling this test for now. See comments in MECLIPSE-185 - Bfox
349             // fail("Expected to receive a MojoExecutionException");
350         }
351         catch ( MojoExecutionException e )
352         {
353             // expected exception here
354         }
355     }
356 
357     /**
358      * MECLIPSE-109 : .component wb-resource source path incorrect for ear packaging
359      * 
360      * @throws Exception any exception thrown during test
361      */
362     public void testProject32()
363         throws Exception
364     {
365         testProject( "project-32" );
366     }
367 
368     /**
369      * MECLIPSE-287 : dependencies with and without classifiers. MECLIPSE-151 : test jar source attachments.
370      * MECLIPSE-367 : Dependency to artifact with classifier tests not distinguished from the regular artifact
371      * 
372      * @throws Exception any exception thrown during test
373      */
374     public void testProject33()
375         throws Exception
376     {
377         testProject( "project-33" );
378     }
379 
380     public void testProject34()
381         throws Exception
382     {
383         testProject( "project-34" );
384     }
385 
386     public void testProject35()
387         throws Exception
388     {
389         testProject( "project-35" );
390 
391         File basedir = getTestFile( "target/test-classes/projects/project-35" );
392 
393         checkContextRoot( basedir, "multymodule-war", "multymodule-ear", "/somethingVeryDifferent" );
394 
395         FileReader reader =
396             new FileReader( new File( basedir, "multymodule-war/.settings/org.eclipse.wst.common.component" ) );
397         Xpp3Dom warComponent = Xpp3DomBuilder.build( reader );
398         Xpp3Dom[] dependentModules = warComponent.getChild( "wb-module" ).getChildren( "dependent-module" );
399         assertEquals( 2, dependentModules.length );
400         for ( int index = 0; index < dependentModules.length; index++ )
401         {
402             assertEquals( "/WEB-INF/lib", dependentModules[index].getAttribute( "deploy-path" ) );
403         }
404 
405         reader = new FileReader( new File( basedir, "multymodule-ear/.settings/org.eclipse.wst.common.component" ) );
406         Xpp3Dom earComponent = Xpp3DomBuilder.build( reader );
407         dependentModules = earComponent.getChild( "wb-module" ).getChildren( "dependent-module" );
408         assertEquals( 2, dependentModules.length );
409         for ( int index = 0; index < dependentModules.length; index++ )
410         {
411             if ( dependentModules[index].getAttribute( "archiveName" ).endsWith( "war" ) )
412             {
413                 assertEquals( "/", dependentModules[index].getAttribute( "deploy-path" ) );
414                 assertTrue( !dependentModules[index].getAttribute( "archiveName" ).startsWith( ".." ) );
415             }
416             else
417             {
418                 assertEquals( "lib", dependentModules[index].getAttribute( "deploy-path" ) );
419                 assertTrue( dependentModules[index].getAttribute( "archiveName" ).startsWith( ".." ) );
420             }
421         }
422     }
423 
424     public void testProject36()
425         throws Exception
426     {
427         // Install artefacts
428         File basedir = getTestFile( "target/test-classes/projects/project-36" );
429         File pom = new File( basedir, "pom.xml" );
430         List goals = new ArrayList();
431         goals.add( "install" );
432         executeMaven( pom, new Properties(), goals );
433         // Test
434         testProject( "project-36" );
435     }
436 
437     public void testProject37()
438         throws Exception
439     {
440         testProject( "project-37" );
441     }
442 
443     /**
444      * MECLIPSE-56 : problem with encoding of non-ascii characters in pom.xml
445      */
446     public void testMECLIPSE_56_encoding()
447         throws Exception
448     {
449         testProject( "MECLIPSE-56_encoding" );
450     }
451 
452     public void testProject38()
453         throws Exception
454     {
455         testProject( "project-38" );
456     }
457 
458     public void testProject39()
459         throws Exception
460     {
461         checkJRESettingsWithEclipseWorkspace( "project-39", new TempEclipseWorkspace( "eclipseWithDefault13", true ),
462                                               "", null );
463         checkJRESettingsWithEclipseWorkspace( "project-39", new TempEclipseWorkspace( "eclipseWithDefault15", false ),
464                                               "J2SE-1.3", null );
465         checkJRESettingsWithEclipseWorkspace( "project-39", new TempEclipseWorkspace( "rad7WithDefault14", false ),
466                                               "J2SE-1.3", null );
467     }
468 
469     public void testProject40()
470         throws Exception
471     {
472         String jre131 = new java.io.File( "target/test-classes/eclipse/dummyJDK/1.3.1/bin/javac" ).getCanonicalPath();
473         checkJRESettingsWithEclipseWorkspace( "project-40", new TempEclipseWorkspace( "eclipseWithDefault13", true ),
474                                               "JVM 1.3.1", jre131 );
475         checkJRESettingsWithEclipseWorkspace( "project-40", new TempEclipseWorkspace( "eclipseWithDefault15", false ),
476                                               "JVM 1.3.1", jre131 );
477         checkJRESettingsWithEclipseWorkspace( "project-40", new TempEclipseWorkspace( "rad7WithDefault14", false ), "",
478                                               jre131 );
479     }
480 
481     public void testProject41()
482         throws Exception
483     {
484         TempEclipseWorkspace rad7 = new TempEclipseWorkspace( "rad7WithDefault14", false );
485         Properties properties = new Properties();
486         properties.setProperty( "eclipse.workspace", rad7.workspaceLocation.getCanonicalPath() );
487         testProject( "project-41", properties, "clean", "eclipse" );
488 
489     }
490 
491     public void testProject42()
492         throws Exception
493     {
494         TempEclipseWorkspace rad7 = new TempEclipseWorkspace( "rad7WithDefault14", false );
495         Properties properties = new Properties();
496         properties.setProperty( "eclipse.workspace", rad7.workspaceLocation.getCanonicalPath() );
497         testProject( "project-42", properties, "clean", "eclipse" );
498 
499     }
500 
501     /**
502      * [MECLIPSE-79]
503      * 
504      * @since 2.5
505      * @throws Exception
506      */
507     public void testProject43()
508         throws Exception
509     {
510         testProject( "project-43" );
511     }
512 
513     /**
514      * [MECLIPSE-219]
515      * 
516      * @since 2.5
517      * @throws Exception
518      */
519     public void testProject44()
520         throws Exception
521     {
522         testProject( "project-44" );
523     }
524 
525     /**
526      * [MECLIPSE-379] When downloading sources and javadocs dependency classifier is not respected.
527      * 
528      * @since 2.5
529      * @throws Exception
530      */
531     public void testProject45()
532         throws Exception
533     {
534         testProject( "project-45" );
535     }
536 
537     public void testJeeSimple()
538         throws Exception
539     {
540         // Install artefacts
541         File basedir = getTestFile( "target/test-classes/projects/j2ee-simple" );
542         File pom = new File( basedir, "pom.xml" );
543         List goals = new ArrayList();
544         goals.add( "install" );
545         executeMaven( pom, new Properties(), goals );
546         // Test project
547         testProject( "j2ee-simple" );
548         checkContextRoot( basedir, "servlets/servlet", "ear", "servlet" );
549 
550     }
551 
552     private void checkContextRoot( File basedir, String warModule, String earModule, String expectedContextRoot )
553         throws FileNotFoundException, XmlPullParserException, IOException
554     {
555         FileReader reader =
556             new FileReader( new File( basedir, warModule + "/.settings/org.eclipse.wst.common.component" ) );
557         Xpp3Dom warComponent = Xpp3DomBuilder.build( reader );
558         Xpp3Dom[] properties = warComponent.getChild( "wb-module" ).getChildren( "property" );
559         boolean contextRootAvaliable = false;
560         for ( int index = 0; index < properties.length; index++ )
561         {
562             if ( properties[index].getAttribute( "name" ).equals( "context-root" ) )
563             {
564                 assertEquals( "Context root detection in org.eclipse.wst.common.component", expectedContextRoot,
565                               properties[index].getAttribute( "value" ) );
566                 contextRootAvaliable = true;
567             }
568         }
569         assertTrue( "there must be a context root here", contextRootAvaliable );
570 
571         reader = new FileReader( new File( basedir, earModule + "/target/eclipseEar/META-INF/application.xml" ) );
572         Xpp3Dom generatedApplicationXML = Xpp3DomBuilder.build( reader );
573 
574         Xpp3Dom[] modules = generatedApplicationXML.getChildren( "module" );
575         for ( int index = 0; index < modules.length; index++ )
576         {
577             if ( modules[index].getChild( "web" ) != null )
578             {
579                 assertEquals( "Context root detection in target/eclipseEar/META-INF/application.xml",
580                               expectedContextRoot,
581                               modules[index].getChild( "web" ).getChild( "context-root" ).getValue() );
582             }
583         }
584     }
585 
586     private void checkJRESettingsWithEclipseWorkspace( String project, TempEclipseWorkspace workspace,
587                                                        String expectedJRE, String jreExec )
588         throws Exception
589     {
590         Properties properties = new Properties();
591         properties.setProperty( "eclipse.workspace", workspace.workspaceLocation.getCanonicalPath() );
592         if ( jreExec != null )
593         {
594             properties.setProperty( "maven.compiler.executable", jreExec );
595         }
596         testProject( project, properties, "clean", "eclipse" );
597 
598         Xpp3Dom classpath =
599             Xpp3DomBuilder.build( new FileReader( getTestFile( "target/test-classes/projects/" + project +
600                 "/.classpath" ) ) );
601 
602         boolean foundDirectCompileAsProject = false;
603         String foundJRE = "";
604 
605         Xpp3Dom[] classpathentries = classpath.getChildren( "classpathentry" );
606         for ( int index = 0; index < classpathentries.length; index++ )
607         {
608             if ( "/direct-compile".equals( classpathentries[index].getAttribute( "path" ) ) )
609             {
610                 foundDirectCompileAsProject = true;
611             }
612             String path = classpathentries[index].getAttribute( "path" );
613             if ( "con".equals( classpathentries[index].getAttribute( "kind" ) ) && path.lastIndexOf( '/' ) > 0 )
614             {
615                 foundJRE = path.substring( path.lastIndexOf( '/' ) + 1 );
616             }
617         }
618         assertTrue( foundDirectCompileAsProject );
619         assertEquals( expectedJRE, foundJRE );
620     }
621 
622     /*
623      * TODO: Add a test for downloadJavadocs. Currently, eclipse doesn't support having variables in the javadoc path.
624      * This means that the expected .classpath can't match the final result as the result will have the absolute path to
625      * the user's local repo.
626      */
627 }