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.FileInputStream;
23  import java.io.InputStreamReader;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.maven.plugin.ide.IdeUtils;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.xml.Xpp3Dom;
32  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
33  
34  /**
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   */
37  public class RadPluginTest
38      extends AbstractEclipsePluginTestCase
39  {
40  
41      private static final String PROJECTS_BASEDIR = "target/test-classes/projects";
42  
43      public void testProject1()
44          throws Exception
45      {
46          testProject( "project-rad-1", new Properties(), "rad-clean", "rad" );
47      }
48  
49      /*
50       * TODO: fix failing test public void testProject2() throws Exception { testProject( "project-rad-2", new
51       * Properties(), "rad-clean", "rad" ); File generatedManifest = getTestFile( PROJECTS_BASEDIR +
52       * "/project-rad-2/src/main/webapp/META-INF/MANIFEST.MF" ); File expectedManifest = getTestFile( PROJECTS_BASEDIR +
53       * "/project-rad-2/src/main/webapp/META-INF/expected_MANIFEST.MF" ); assertFileEquals(
54       * localRepositoryDirectory.getCanonicalPath(), expectedManifest, generatedManifest ); }
55       */
56  
57      public void testProject3()
58          throws Exception
59      {
60          testProject( "project-rad-3", new Properties(), "rad-clean", "rad" );
61          File generatedManifest = getTestFile( PROJECTS_BASEDIR + "/project-rad-3/ejbModule/META-INF/MANIFEST.MF" );
62          File expectedManifest =
63              getTestFile( PROJECTS_BASEDIR + "/project-rad-3/ejbModule/META-INF/expected_MANIFEST.MF" );
64          assertFileEquals( localRepositoryDirectory.getCanonicalPath(), expectedManifest, generatedManifest );
65      }
66  
67      public void testProject4()
68          throws Exception
69      {
70          testProject( "project-rad-4", new Properties(), "rad-clean", "rad" );
71      }
72  
73      public void testProject5()
74          throws Exception
75      {
76          File basedir = getTestFile( "target/test-classes/projects/project-rad-5" );
77  
78          FileUtils.deleteDirectory( new File( basedir, "project-rad-1/META-INF" ) );
79          new File( basedir, "project-rad-1/META-INF" ).mkdirs();
80  
81          File pom0 = new File( basedir, "pom.xml" );
82  
83          MavenProject project = readProject( pom0 );
84  
85          String outputDirPath =
86              IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
87          File outputDir;
88  
89          if ( outputDirPath == null )
90          {
91              outputDir = basedir;
92          }
93          else
94          {
95              outputDir = new File( basedir, outputDirPath );
96              outputDir.mkdirs();
97              new File( outputDir, project.getArtifactId() );
98          }
99  
100         List goals = new ArrayList();
101 
102         String pluginSpec = getPluginCLISpecification();
103 
104         goals.add( pluginSpec + "rad-clean" );
105         goals.add( pluginSpec + "rad" );
106         goals.add( "install" );
107 
108         Properties props = new Properties();
109 
110         executeMaven( pom0, props, goals );
111 
112         // this.maven.execute( Arrays.asList( new MavenProject[] { project, project2, project3 } ), Arrays
113         // .asList( new String[] {
114         // "install",
115         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad-clean",
116         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad" } ), eventMonitor, new ConsoleDownloadMonitor(),
117         // new Properties(), basedir );
118         // this.maven.execute( Arrays.asList( new MavenProject[] { project1, project2, project3 } ), Arrays
119         // .asList( new String[] {
120         // "install",
121         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad-clean",
122         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad" } ), eventMonitor, new ConsoleDownloadMonitor(),
123         // new Properties(), basedir );
124 
125         // jar muss reincoliert sein
126         assertTrue( "Expected file not found: project-rad-1/maven-core-98.0.jar",
127                     new File( basedir, "project-rad-1/maven-core-98.0.jar" ).exists() );
128 
129         Xpp3Dom applicationXml =
130             Xpp3DomBuilder.build( new InputStreamReader(
131                                                          new FileInputStream(
132                                                                               new File( basedir,
133                                                                                         "project-rad-1/META-INF/application.xml" ) ),
134                                                          "UTF-8" ) );
135 
136         Xpp3Dom modulesmapsXml =
137             Xpp3DomBuilder.build( new InputStreamReader(
138                                                          new FileInputStream(
139                                                                               new File( basedir,
140                                                                                         "project-rad-1/META-INF/.modulemaps" ) ),
141                                                          "UTF-8" ) );
142 
143         assertNotNull( modulesmapsXml );
144 
145         Xpp3Dom webappModule = applicationXml.getChildren( "module" )[0];
146         Xpp3Dom ejbModule = applicationXml.getChildren( "module" )[1];
147         if ( webappModule.getChild( "web" ) == null )
148         {
149             webappModule = applicationXml.getChildren( "module" )[1];
150             ejbModule = applicationXml.getChildren( "module" )[0];
151         }
152 
153         assertEquals( "project-rad-5_2.war", webappModule.getChild( "web" ).getChild( "web-uri" ).getValue() );
154         assertEquals( "project-rad-5_2", webappModule.getChild( "web" ).getChild( "context-root" ).getValue() );
155         assertEquals( "project-rad-5_3.jar", ejbModule.getChild( Constants.PROJECT_PACKAGING_EJB ).getValue() );
156 
157         Xpp3Dom websettings =
158             Xpp3DomBuilder.build( new InputStreamReader(
159                                                          new FileInputStream( new File( basedir,
160                                                                                         "project-rad-2/.websettings" ) ),
161                                                          "UTF-8" ) );
162 
163         assertEquals( "project-rad-5_4.jar",
164                       websettings.getChild( "lib-modules" ).getChild( "lib-module" ).getChild( "jar" ).getValue() );
165         assertEquals( "project-rad-5_4",
166                       websettings.getChild( "lib-modules" ).getChild( "lib-module" ).getChild( "project" ).getValue() );
167     }
168 
169     public void testProject5_2()
170         throws Exception
171     {
172         File basedir = getTestFile( "target/test-classes/projects/project-rad-5" );
173 
174         FileUtils.deleteDirectory( new File( basedir, "project-rad-1/META-INF" ) );
175 
176         File pom0 = new File( basedir, "pom.xml" );
177 
178         MavenProject project = readProject( pom0 );
179 
180         String outputDirPath =
181             IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
182         File outputDir;
183 
184         if ( outputDirPath == null )
185         {
186             outputDir = basedir;
187         }
188         else
189         {
190             outputDir = new File( basedir, outputDirPath );
191             outputDir.mkdirs();
192             new File( outputDir, project.getArtifactId() );
193         }
194 
195         List goals = new ArrayList();
196 
197         String pluginSpec = getPluginCLISpecification();
198 
199         goals.add( pluginSpec + "rad-clean" );
200         goals.add( pluginSpec + "rad" );
201 
202         Properties props = new Properties();
203 
204         executeMaven( pom0, props, goals );
205 
206         // this.maven.execute( Arrays.asList( new MavenProject[] { project, project2, project3 } ), Arrays
207         // .asList( new String[] {
208         // "install",
209         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad-clean",
210         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad" } ), eventMonitor, new ConsoleDownloadMonitor(),
211         // new Properties(), basedir );
212         //        
213         // this.maven.execute( Arrays.asList( new MavenProject[] { project1, project2, project3 } ), Arrays
214         // .asList( new String[] {
215         // "install",
216         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad-clean",
217         // "org.apache.maven.plugins:maven-eclipse-plugin:current:rad" } ), eventMonitor, new ConsoleDownloadMonitor(),
218         // new Properties(), basedir );
219 
220         assertTrue( "Expected file not found: project-rad-1/maven-core-98.0.jar",
221                     new File( basedir, "project-rad-1/maven-core-98.0.jar" ).exists() );
222 
223         File modulemaps = new File( basedir, "project-rad-1/META-INF/.modulemaps" );
224 
225         assertNotNull( modulemaps );
226 
227         File application = new File( basedir, "project-rad-1/META-INF/application.xml" );
228 
229         Xpp3Dom applicationXml =
230             Xpp3DomBuilder.build( new InputStreamReader( new FileInputStream( application ), "UTF-8" ) );
231 
232         Xpp3Dom[] children = applicationXml.getChildren( "module" );
233 
234         assertEquals( 2, children.length );
235 
236         boolean ejbVerified = false;
237         boolean warVerified = false;
238 
239         for ( int i = 0; i < children.length; i++ )
240         {
241             Xpp3Dom child = children[i];
242 
243             if ( child.getAttribute( "id" ).startsWith( "WebModule_" ) )
244             {
245                 assertEquals( "project-rad-5_2.war", child.getChild( "web" ).getChild( "web-uri" ).getValue() );
246                 warVerified = true;
247             }
248             else if ( child.getAttribute( "id" ).startsWith( "EjbModule_" ) )
249             {
250                 assertEquals( "project-rad-5_3.jar", child.getChild( Constants.PROJECT_PACKAGING_EJB ).getValue() );
251                 ejbVerified = true;
252             }
253         }
254 
255         assertTrue( warVerified );
256         assertTrue( ejbVerified );
257     }
258 
259     public void testProject6()
260         throws Exception
261     {
262         testProject( "project-rad-6", new Properties(), "rad-clean", "rad" );
263     }
264 
265     /**
266      * Tests warSourceDirectory setting to be reflected in generated .websettings, location of jars in WEB-INF/lib and
267      * generation of MANIFEST.MF at the right place
268      * 
269      * @throws Exception
270      */
271     public void testProject7()
272         throws Exception
273     {
274         testProject( "project-rad-7", new Properties(), "rad-clean", "rad" );
275 
276         /*
277          * testing libs in web content directory
278          */
279         File basedir = getTestFile( "target/test-classes/projects/project-rad-7" );
280         File pom = new File( basedir, "pom.xml" );
281         MavenProject project = readProject( pom );
282         File outputDir;
283         File projectOutputDir = basedir;
284 
285         String outputDirPath =
286             IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
287         if ( outputDirPath == null )
288         {
289             outputDir = basedir;
290         }
291         else
292         {
293             outputDir = new File( basedir, outputDirPath );
294             outputDir.mkdirs();
295             projectOutputDir = new File( outputDir, project.getArtifactId() );
296         }
297 
298         compareDirectoryContent( basedir, projectOutputDir, "WebContent/WEB-INF/lib/" );
299         compareDirectoryContent( basedir, projectOutputDir, "WebContent/META-INF/" );
300         assertFalse( "Default path should not exist because it is overridden!",
301                      new File( basedir + "/src/main/webapp" ).exists() );
302 
303     }
304 
305 }