View Javadoc
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.InputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.codehaus.plexus.util.IOUtil;
29  
30  /**
31   * <p>
32   * Master test for eclipse .classpath and .wtpmodules generation.
33   * </p>
34   * <p>
35   * This test use a 2 modules project with all the mvn dependencies flavours (direct, transitive, with
36   * compile/test/provided/system scope, required and optional, artifacts and modules).
37   * </p>
38   * <p>
39   * In order to fully test the eclipse plugin execution in a such complex environment mvn is executed from a command
40   * line. Mvn is started using a custom settings.xml file, created on the fly. The custom settings.xml only adds a mirror
41   * for the central repository which is actually a local (file://) repository for loading files from
42   * <code>src/test/m2repo</code>
43   * </p>
44   * <p>
45   * The following is the base layout of modules/dependencies. The actual test is to check generated files for module-2
46   * </p>
47   * 
48   * <pre>
49   * 
50   *            +----------------+       +-----------------+       +-----------------+
51   *           /| module 1 (jar) | ----&gt; |   refproject    | ----&gt; | deps-refproject |
52   *          / +----------------+       +-----------------+       +-----------------+  
53   *         /           &circ;
54   *    root             | (depends on)
55   *         \           |
56   *          \ +----------------+       +-----------------+       +-----------------+
57   *           \| module 2 (war) | ----&gt; |     direct      | ----&gt; |   deps-direct   |
58   *            +----------------+       +-----------------+       +-----------------+
59   * 
60   * </pre>
61   * 
62   * @todo a know problem with this approach is that tests are running with the installed version of the plugin! Don't
63   *       enable test in pom.xml at the moment or you will never be able to build.
64   * @author Fabrizio Giustina
65   * @version $Id: EclipsePluginMasterProjectIT.java 1190754 2011-10-29 04:26:32Z hboutemy $
66   */
67  public class EclipsePluginMasterProjectIT
68      extends AbstractEclipsePluginIT
69  {
70  
71      protected File basedir;
72  
73      /**
74       * @see org.apache.maven.plugin.eclipse.it.AbstractEclipsePluginIT#setUp()
75       */
76      protected void setUp()
77          throws Exception
78      {
79          basedir = getTestFile( "target/test-classes/projects/master-test" );
80          super.setUp();
81      }
82  
83      protected void executeMaven2()
84          throws Exception
85      {
86          File pom = new File( basedir, "pom.xml" );
87  
88          Properties properties = new Properties();
89          properties.setProperty( "wtpversion", "R7" );
90  
91          String pluginSpec = getPluginCLISpecification();
92  
93          List goals = new ArrayList();
94  
95          goals.add( pluginSpec + "clean" );
96          goals.add( pluginSpec + "eclipse" );
97  
98          executeMaven( pom, properties, goals );
99  
100     }
101 
102     public void testModule1Project()
103         throws Exception
104     {
105         executeMaven2();
106         assertFileEquals( new File( basedir, "module-1/expected/.project" ), new File( basedir, "module-1/.project" ) );
107     }
108 
109     public void testModule1Classpath()
110         throws Exception
111     {
112         executeMaven2();
113         InputStream fis = new FileInputStream( new File( basedir, "module-1/.classpath" ) );
114         String classpath = IOUtil.toString( fis );
115         IOUtil.close( fis );
116 
117         // direct dependencies, include all
118         assertContains( "Invalid classpath", classpath, "/refproject-compile" );
119         assertContains( "Invalid classpath", classpath, "refproject-sysdep" );
120         assertContains( "Invalid classpath", classpath, "/refproject-test" );
121         assertContains( "Invalid classpath", classpath, "/refproject-optional" );
122         assertContains( "Invalid classpath", classpath, "/refproject-provided" );
123 
124         // transitive dependencies
125         assertContains( "Invalid classpath", classpath, "/deps-refproject-compile" );
126         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-test" );
127         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-optional" );
128         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-provided" );
129 
130     }
131 
132     public void testModule1Wtpmodules()
133         throws Exception
134     {
135         executeMaven2();
136         assertFileEquals( new File( basedir, "module-1/expected/.wtpmodules" ), new File( basedir,
137                                                                                           "module-1/.wtpmodules" ) );
138     }
139 
140     public void testModule2Project()
141         throws Exception
142     {
143         executeMaven2();
144         assertFileEquals( new File( basedir, "module-2/expected/.project" ), new File( basedir, "module-2/.project" ) );
145     }
146 
147     public void testModule2Classpath()
148         throws Exception
149     {
150         executeMaven2();
151         InputStream fis = new FileInputStream( new File( basedir, "module-2/.classpath" ) );
152         String classpath = IOUtil.toString( fis );
153         IOUtil.close( fis );
154 
155         // direct dependencies: include all
156         assertContains( "Invalid classpath", classpath, "/direct-compile" );
157         assertContains( "Invalid classpath", classpath, "/direct-test" );
158         assertContains( "Invalid classpath", classpath, "direct-sysdep" );
159         assertContains( "Invalid classpath", classpath, "/direct-optional" );
160         assertContains( "Invalid classpath", classpath, "/direct-provided" );
161 
162         // referenced project: not required, but it's not a problem to have them included
163         assertContains( "Invalid classpath", classpath, "/module-1" );
164         // assertDoesNotContain( "Invalid classpath", classpath, "/refproject-compile" );
165         // assertDoesNotContain( "Invalid classpath", classpath, "/refproject-sysdep" );
166         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-test" );
167         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-optional" );
168         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-provided" );
169 
170         // transitive dependencies from referenced projects
171         assertContains( "Invalid classpath", classpath, "/deps-direct-compile" );
172         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-test" );
173         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-optional" );
174         // @todo should this be included? see MNG-514
175         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-provided" );
176 
177         // transitive dependencies from referenced projects
178         assertContains( "Invalid classpath", classpath, "/deps-refproject-compile" );
179         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-test" );
180         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-optional" );
181         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-provided" );
182     }
183 
184     public void testModule2Wtpmodules()
185         throws Exception
186     {
187         executeMaven2();
188         InputStream fis = new FileInputStream( new File( basedir, "module-2/.wtpmodules" ) );
189         String wtpmodules = IOUtil.toString( fis );
190         IOUtil.close( fis );
191 
192         // direct dependencies: include only runtime (also optional) dependencies
193         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-compile" );
194         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/direct-test" );
195         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-sysdep" );
196         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-optional" );
197         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/direct-provided" );
198 
199         // referenced project: only runtime deps
200         assertContains( "Invalid wtpmodules", wtpmodules, "/module-1" );
201         assertContains( "Invalid wtpmodules", wtpmodules, "/refproject-compile" );
202         assertContains( "Invalid wtpmodules", wtpmodules, "refproject-sysdep" );
203         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-test" );
204         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-optional" );
205         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-provided" );
206 
207         // transitive dependencies from referenced projects
208         assertContains( "Invalid wtpmodules", wtpmodules, "/deps-direct-compile" );
209         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-test" );
210         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-optional" );
211         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-provided" );
212 
213         // transitive dependencies from referenced projects
214         assertContains( "Invalid wtpmodules", wtpmodules, "/deps-refproject-compile" );
215         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-test" );
216         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-optional" );
217         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-provided" );
218     }
219 
220 }