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 693338 2008-09-09 01:36:25Z baerrach $
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, "module-1/.wtpmodules" ) );
137     }
138 
139     public void testModule2Project()
140         throws Exception
141     {
142         executeMaven2();
143         assertFileEquals( new File( basedir, "module-2/expected/.project" ), new File( basedir, "module-2/.project" ) );
144     }
145 
146     public void testModule2Classpath()
147         throws Exception
148     {
149         executeMaven2();
150         InputStream fis = new FileInputStream( new File( basedir, "module-2/.classpath" ) );
151         String classpath = IOUtil.toString( fis );
152         IOUtil.close( fis );
153 
154         // direct dependencies: include all
155         assertContains( "Invalid classpath", classpath, "/direct-compile" );
156         assertContains( "Invalid classpath", classpath, "/direct-test" );
157         assertContains( "Invalid classpath", classpath, "direct-sysdep" );
158         assertContains( "Invalid classpath", classpath, "/direct-optional" );
159         assertContains( "Invalid classpath", classpath, "/direct-provided" );
160 
161         // referenced project: not required, but it's not a problem to have them included
162         assertContains( "Invalid classpath", classpath, "/module-1" );
163         // assertDoesNotContain( "Invalid classpath", classpath, "/refproject-compile" );
164         // assertDoesNotContain( "Invalid classpath", classpath, "/refproject-sysdep" );
165         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-test" );
166         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-optional" );
167         assertDoesNotContain( "Invalid classpath", classpath, "/refproject-provided" );
168 
169         // transitive dependencies from referenced projects
170         assertContains( "Invalid classpath", classpath, "/deps-direct-compile" );
171         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-test" );
172         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-optional" );
173         // @todo should this be included? see MNG-514
174         assertDoesNotContain( "Invalid classpath", classpath, "/deps-direct-provided" );
175 
176         // transitive dependencies from referenced projects
177         assertContains( "Invalid classpath", classpath, "/deps-refproject-compile" );
178         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-test" );
179         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-optional" );
180         assertDoesNotContain( "Invalid classpath", classpath, "/deps-refproject-provided" );
181     }
182 
183     public void testModule2Wtpmodules()
184         throws Exception
185     {
186         executeMaven2();
187         InputStream fis = new FileInputStream( new File( basedir, "module-2/.wtpmodules" ) );
188         String wtpmodules = IOUtil.toString( fis );
189         IOUtil.close( fis );
190 
191         // direct dependencies: include only runtime (also optional) dependencies
192         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-compile" );
193         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/direct-test" );
194         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-sysdep" );
195         assertContains( "Invalid wtpmodules", wtpmodules, "/direct-optional" );
196         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/direct-provided" );
197 
198         // referenced project: only runtime deps
199         assertContains( "Invalid wtpmodules", wtpmodules, "/module-1" );
200         assertContains( "Invalid wtpmodules", wtpmodules, "/refproject-compile" );
201         assertContains( "Invalid wtpmodules", wtpmodules, "refproject-sysdep" );
202         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-test" );
203         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-optional" );
204         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/refproject-provided" );
205 
206         // transitive dependencies from referenced projects
207         assertContains( "Invalid wtpmodules", wtpmodules, "/deps-direct-compile" );
208         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-test" );
209         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-optional" );
210         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-direct-provided" );
211 
212         // transitive dependencies from referenced projects
213         assertContains( "Invalid wtpmodules", wtpmodules, "/deps-refproject-compile" );
214         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-test" );
215         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-optional" );
216         assertDoesNotContain( "Invalid wtpmodules", wtpmodules, "/deps-refproject-provided" );
217     }
218 
219 }