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.it;
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.eclipse.Constants;
29  import org.apache.maven.plugin.ide.IdeUtils;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.xml.Xpp3Dom;
33  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
34  
35  /**
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
37   */
38  public class RadPluginIT
39      extends AbstractEclipsePluginIT
40  {
41  
42      private static final String PROJECTS_BASEDIR = "target/test-classes/projects";
43  
44      public void testProject1()
45          throws Exception
46      {
47          testProject( "project-rad-1", new Properties(), "rad-clean", "rad" );
48      }
49  
50      public void testProject2()
51          throws Exception
52      {
53          testProject( "project-rad-2", new Properties(), "rad-clean", "rad" );
54          File generatedManifest = getTestFile( PROJECTS_BASEDIR + "/project-rad-2/src/main/webapp/META-INF/MANIFEST.MF" );
55          File expectedManifest =
56              getTestFile( PROJECTS_BASEDIR + "/project-rad-2/src/main/webapp/META-INF/expected_MANIFEST.MF" );
57          assertFileEquals( expectedManifest, generatedManifest );
58  
59      }
60  
61      public void testProject3()
62          throws Exception
63      {
64          testProject( "project-rad-3", new Properties(), "rad-clean", "rad" );
65          File generatedManifest = getTestFile( PROJECTS_BASEDIR + "/project-rad-3/ejbModule/META-INF/MANIFEST.MF" );
66          File expectedManifest =
67              getTestFile( PROJECTS_BASEDIR + "/project-rad-3/ejbModule/META-INF/expected_MANIFEST.MF" );
68          assertFileEquals( expectedManifest, generatedManifest );
69      }
70  
71      public void testProject4()
72          throws Exception
73      {
74          testProject( "project-rad-4", new Properties(), "rad-clean", "rad" );
75      }
76  
77      public void testProject5()
78          throws Exception
79      {
80          File basedir = getTestFile( "target/test-classes/projects/project-rad-5" );
81  
82          FileUtils.deleteDirectory( new File( basedir, "project-rad-1/META-INF" ) );
83          new File( basedir, "project-rad-1/META-INF" ).mkdirs();
84  
85          File pom0 = new File( basedir, "pom.xml" );
86  
87          MavenProject project = readProject( pom0 );
88  
89          String outputDirPath =
90              IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
91          File outputDir;
92  
93          if ( outputDirPath == null )
94          {
95              outputDir = basedir;
96          }
97          else
98          {
99              outputDir = new File( basedir, outputDirPath );
100             outputDir.mkdirs();
101             new File( outputDir, project.getArtifactId() );
102         }
103 
104         List goals = new ArrayList();
105 
106         String pluginSpec = getPluginCLISpecification();
107 
108         goals.add( pluginSpec + "rad-clean" );
109         goals.add( pluginSpec + "rad" );
110         goals.add( "install" );
111 
112         Properties props = new Properties();
113 
114         executeMaven( pom0, props, goals );
115 
116         assertTrue( "Expected file not found: project-rad-1/maven-core-98.0.jar",
117                     new File( basedir, "project-rad-1/maven-core-98.0.jar" ).exists() );
118 
119         Xpp3Dom applicationXml =
120             Xpp3DomBuilder.build( new InputStreamReader(
121                                                          new FileInputStream(
122                                                                               new File( basedir,
123                                                                                         "project-rad-1/META-INF/application.xml" ) ),
124                                                          "UTF-8" ) );
125 
126         Xpp3Dom modulesmapsXml =
127             Xpp3DomBuilder.build( new InputStreamReader(
128                                                          new FileInputStream(
129                                                                               new File( basedir,
130                                                                                         "project-rad-1/META-INF/.modulemaps" ) ),
131                                                          "UTF-8" ) );
132 
133         assertNotNull( modulesmapsXml );
134 
135         Xpp3Dom[] children = applicationXml.getChildren( "module" );
136         assertEquals( 2, children.length );
137         // The module children can be in ANY order.
138         boolean ejbVerified = false;
139         boolean warVerified = false;
140         for ( int i = 0; i < children.length; i++ )
141         {
142             Xpp3Dom child = children[i];
143 
144             if ( child.getAttribute( "id" ).startsWith( "WebModule_" ) )
145             {
146                 assertEquals( "project-rad-5_2.war", child.getChild( "web" ).getChild( "web-uri" ).getValue() );
147                 assertEquals( "project-rad-5_2", child.getChild( "web" ).getChild( "context-root" ).getValue() );
148                 warVerified = true;
149             }
150             else if ( child.getAttribute( "id" ).startsWith( "EjbModule_" ) )
151             {
152                 assertEquals( "project-rad-5_3.jar", child.getChild( Constants.PROJECT_PACKAGING_EJB ).getValue() );
153                 ejbVerified = true;
154             }
155         }
156         assertTrue( warVerified );
157         assertTrue( ejbVerified );
158 
159         Xpp3Dom websettings =
160             Xpp3DomBuilder.build( new InputStreamReader(
161                                                          new FileInputStream( new File( basedir,
162                                                                                         "project-rad-2/.websettings" ) ),
163                                                          "UTF-8" ) );
164 
165         assertEquals( "project-rad-5_4.jar",
166                       websettings.getChild( "lib-modules" ).getChild( "lib-module" ).getChild( "jar" ).getValue() );
167         assertEquals( "project-rad-5_4",
168                       websettings.getChild( "lib-modules" ).getChild( "lib-module" ).getChild( "project" ).getValue() );
169     }
170 
171     public void testProject6()
172         throws Exception
173     {
174         testProject( "project-rad-6", new Properties(), "rad-clean", "rad" );
175     }
176 
177     /**
178      * Tests warSourceDirectory setting to be reflected in generated .websettings, location of jars in WEB-INF/lib and
179      * generation of MANIFEST.MF at the right place
180      * 
181      * @throws Exception
182      */
183     public void testProject7()
184         throws Exception
185     {
186         testProject( "project-rad-7", new Properties(), "rad-clean", "rad" );
187 
188         /*
189          * testing libs in web content directory
190          */
191         File basedir = getTestFile( "target/test-classes/projects/project-rad-7" );
192         File pom = new File( basedir, "pom.xml" );
193         MavenProject project = readProject( pom );
194         File outputDir;
195         File projectOutputDir = basedir;
196 
197         String outputDirPath =
198             IdeUtils.getPluginSetting( project, "org.apache.maven.plugins:maven-eclipse-plugin", "outputDir", null );
199         if ( outputDirPath == null )
200         {
201             outputDir = basedir;
202         }
203         else
204         {
205             outputDir = new File( basedir, outputDirPath );
206             outputDir.mkdirs();
207             projectOutputDir = new File( outputDir, project.getArtifactId() );
208         }
209 
210         compareDirectoryContent( basedir, projectOutputDir );
211         assertFalse( "Default path should not exist because it is overridden!",
212                      new File( basedir + "/src/main/webapp" ).exists() );
213 
214     }
215 
216     public void testProject8()
217         throws Exception
218     {
219         testProject( "project-rad-8", new Properties(), "rad-clean", "rad" );
220         File generatedManifest = getTestFile( PROJECTS_BASEDIR + "/project-rad-8/src/main/webapp/META-INF/MANIFEST.MF" );
221         File expectedManifest =
222             getTestFile( PROJECTS_BASEDIR + "/project-rad-8/src/main/webapp/META-INF/expected_MANIFEST.MF" );
223         assertFileEquals( expectedManifest, generatedManifest );
224 
225     }
226 }