1   package org.apache.maven.plugin.idea;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.idea.stubs.TestCounter;
23  import org.codehaus.plexus.PlexusTestCase;
24  import org.dom4j.Document;
25  import org.dom4j.Element;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * @author Edwin Punzalan
34   */
35  public class IdeaModuleTest
36      extends AbstractIdeaTestCase
37  {
38      public void testJarMinConfig()
39          throws Exception
40      {
41          executeMojo( "src/test/module-plugin-configs/min-plugin-config.xml" );
42      }
43  
44      public void testProvidedDependencies()
45          throws Exception
46      {
47          executeMojo( "src/test/module-plugin-configs/provided-dep-plugin-config.xml" );
48      }
49  
50      public void testExcludeDirectoryConfig()
51          throws Exception
52      {
53          File projectBasedir = new File( getBasedir(), "target/test-harness/" + ( TestCounter.currentCount() + 1 ) );
54  
55          projectBasedir.mkdirs();
56  
57          File excluded = new File( projectBasedir, "excluded" );
58          excluded.mkdirs();
59          File sub = new File( excluded, "sub" );
60          sub.mkdirs();
61          File sub2 = new File( excluded, "sub2" );
62          sub2.mkdirs();
63          File subsub1 = new File( sub, "sub1" );
64          subsub1.mkdirs();
65  
66          Document imlDocument = executeMojo( "src/test/module-plugin-configs/dir-exclusion-plugin-config.xml" );
67  
68          Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
69  
70          boolean excludedDirFound = false;
71          Element content = findElement( component, "content" );
72          for ( Iterator excludes = content.elementIterator( "excludeFolder" ); excludes.hasNext(); )
73          {
74              Element exclude = (Element) excludes.next();
75  
76              if ( "file://$MODULE_DIR$/excluded".equals( exclude.attributeValue( "url" ) ) )
77              {
78                  excludedDirFound = true;
79              }
80          }
81          assertTrue( "Test excluded dir", excludedDirFound );
82      }
83  
84      public void testWarMinConfig()
85          throws Exception
86      {
87          List expectedLibs = new ArrayList();
88          expectedLibs.add( "/WEB-INF/lib/maven-model-2.0.1.jar" );
89          expectedLibs.add( "/WEB-INF/lib/junit-3.8.1.jar" );
90  
91          Document imlDocument = executeMojo( "src/test/module-plugin-configs/min-war-plugin-config.xml" );
92  
93          Element root = imlDocument.getRootElement();
94  
95          assertEquals( "Test Project type", "J2EE_WEB_MODULE", root.attributeValue( "type" ) );
96  
97          Element component = findComponent( root, "WebModuleBuildComponent" );
98  
99          Element setting = findElement( component, "setting" );
100         assertTrue( "Test exploded url setting", "EXPLODED_URL".equals( setting.attributeValue( "name" ) ) );
101         assertTrue( "Test exploded url value",
102                     setting.attributeValue( "value" ).startsWith( "file://$MODULE_DIR$/target/" ) );
103 
104         component = findComponent( root, "WebModuleProperties" );
105 
106         Element deployDescriptor = component.element( "deploymentDescriptor" );
107         assertEquals( "Test deployment descriptor version", "2.3", deployDescriptor.attributeValue( "version" ) );
108         assertEquals( "Test deployment descriptor name", "web.xml", deployDescriptor.attributeValue( "name" ) );
109         assertEquals( "Test deployment descriptor optional", "false", deployDescriptor.attributeValue( "optional" ) );
110         assertEquals( "Test deployment descriptor file", "file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml",
111                       deployDescriptor.attributeValue( "url" ) );
112 
113         Element webroots = component.element( "webroots" );
114         Element webroot = webroots.element( "root" );
115         assertEquals( "Test webroot relative location", "/", webroot.attributeValue( "relative" ) );
116         assertEquals( "Test webroot url", "file://$MODULE_DIR$/src/main/webapp", webroot.attributeValue( "url" ) );
117 
118         List containerElementList = findElementsByName( component, "containerElement" );
119         for ( Iterator containerElements = containerElementList.iterator(); containerElements.hasNext(); )
120         {
121             Element containerElement = (Element) containerElements.next();
122 
123             assertEquals( "Test container element type", "library", containerElement.attributeValue( "type" ) );
124             assertEquals( "Test container element level", "module", containerElement.attributeValue( "level" ) );
125             assertTrue( "Test library url", containerElement.element( "url" ).getText().startsWith( "jar://" ) );
126 
127             Element attribute = findElementByNameAttribute( containerElement, "attribute", "method" );
128             assertEquals( "Test library method", "1", attribute.attributeValue( "value" ) );
129 
130             attribute = findElementByNameAttribute( containerElement, "attribute", "URI" );
131             String attributeValue = attribute.attributeValue( "value" );
132             assertTrue( "Test library URI", expectedLibs.contains( attributeValue ) );
133             expectedLibs.remove( attributeValue );
134         }
135 
136         assertTrue( "All libraries are present", expectedLibs.size() == 0 );
137     }
138 
139     public void testWarConfigWithProvidedDependency()
140         throws Exception
141     {
142         List expectedLibs = new ArrayList();
143         expectedLibs.add( "/WEB-INF/lib/maven-model-2.0.1.jar" );
144         expectedLibs.add( "/WEB-INF/lib/jdbc-stdext-2.0.jar" );
145         expectedLibs.add( "/WEB-INF/lib/junit-3.8.1.jar" );
146 
147         Document imlDocument = executeMojo( "src/test/module-plugin-configs/provided-dep-plugin-config.xml" );
148 
149         Element root = imlDocument.getRootElement();
150 
151         assertEquals( "Test Project type", "J2EE_WEB_MODULE", root.attributeValue( "type" ) );
152 
153         Element component = findComponent( root, "WebModuleBuildComponent" );
154 
155         Element setting = findElement( component, "setting" );
156         assertTrue( "Test exploded url setting", "EXPLODED_URL".equals( setting.attributeValue( "name" ) ) );
157         assertTrue( "Test exploded url value",
158                     setting.attributeValue( "value" ).startsWith( "file://$MODULE_DIR$/target/" ) );
159 
160         component = findComponent( root, "WebModuleProperties" );
161 
162         Element deployDescriptor = component.element( "deploymentDescriptor" );
163         assertEquals( "Test deployment descriptor version", "2.3", deployDescriptor.attributeValue( "version" ) );
164         assertEquals( "Test deployment descriptor name", "web.xml", deployDescriptor.attributeValue( "name" ) );
165         assertEquals( "Test deployment descriptor optional", "false", deployDescriptor.attributeValue( "optional" ) );
166         assertEquals( "Test deployment descriptor file", "file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml",
167                       deployDescriptor.attributeValue( "url" ) );
168 
169         Element webroots = component.element( "webroots" );
170         Element webroot = webroots.element( "root" );
171         assertEquals( "Test webroot relative location", "/", webroot.attributeValue( "relative" ) );
172         assertEquals( "Test webroot url", "file://$MODULE_DIR$/src/main/webapp", webroot.attributeValue( "url" ) );
173 
174         List containerElementList = findElementsByName( component, "containerElement" );
175         for ( Iterator containerElements = containerElementList.iterator(); containerElements.hasNext(); )
176         {
177             Element containerElement = (Element) containerElements.next();
178 
179             assertEquals( "Test container element type", "library", containerElement.attributeValue( "type" ) );
180             assertEquals( "Test container element level", "module", containerElement.attributeValue( "level" ) );
181             assertTrue( "Test library url", containerElement.element( "url" ).getText().startsWith( "jar://" ) );
182 
183             Element attribute = findElementByNameAttribute( containerElement, "attribute", "URI" );
184             String attributeValue = attribute.attributeValue( "value" );
185 
186             attribute = findElementByNameAttribute( containerElement, "attribute", "method" );
187 
188             if ( "/WEB-INF/lib/maven-model-2.0.1.jar".equals( attributeValue ) )
189             {
190                 assertEquals( "Test library method for provided dependency", "0", attribute.attributeValue( "value" ) );
191             }
192             else if ( "/WEB-INF/lib/jdbc-stdext-2.0.jar".equals( attributeValue ) )
193             {
194                 assertEquals( "Test library method for system dependency", "0", attribute.attributeValue( "value" ) );
195             }
196             else if ( "/WEB-INF/lib/junit-3.8.1.jar".equals( attributeValue ) )
197             {
198                 assertEquals( "Test library method for test dependency", "0", attribute.attributeValue( "value" ) );
199             }
200             else
201             {
202                 assertEquals( "Test library method", "1", attribute.attributeValue( "value" ) );
203             }
204 
205             assertTrue( "Test library URI", expectedLibs.contains( attributeValue ) );
206             expectedLibs.remove( attributeValue );
207         }
208 
209         assertTrue( "All libraries are present", expectedLibs.size() == 0 );
210     }
211 
212     public void testEjbMinConfig()
213         throws Exception
214     {
215         List expectedLibs = new ArrayList();
216         expectedLibs.add( "/lib/maven-model-2.0.1.jar" );
217         expectedLibs.add( "/lib/junit-3.8.1.jar" );
218 
219         Document imlDocument = executeMojo( "src/test/module-plugin-configs/min-ejb-plugin-config.xml" );
220 
221         Element root = imlDocument.getRootElement();
222 
223         assertEquals( "Test Project type", "J2EE_EJB_MODULE", root.attributeValue( "type" ) );
224 
225         Element component = findComponent( root, "EjbModuleBuildComponent" );
226 
227         Element setting = findElement( component, "setting" );
228         assertTrue( "Test exploded url setting", "EXPLODED_URL".equals( setting.attributeValue( "name" ) ) );
229         assertTrue( "Test exploded url value",
230                     setting.attributeValue( "value" ).startsWith( "file://$MODULE_DIR$/target/" ) );
231 
232         component = findComponent( root, "EjbModuleProperties" );
233 
234         Element deployDescriptor = component.element( "deploymentDescriptor" );
235         assertEquals( "Test deployment descriptor version", "2.x", deployDescriptor.attributeValue( "version" ) );
236         assertEquals( "Test deployment descriptor name", "ejb-jar.xml", deployDescriptor.attributeValue( "name" ) );
237         assertEquals( "Test deployment descriptor optional", "false", deployDescriptor.attributeValue( "optional" ) );
238         assertEquals( "Test deployment descriptor file", "file://$MODULE_DIR$/src/main/resources/META-INF/ejb-jar.xml",
239                       deployDescriptor.attributeValue( "url" ) );
240 
241         List containerElementList = findElementsByName( component, "containerElement" );
242         for ( Iterator containerElements = containerElementList.iterator(); containerElements.hasNext(); )
243         {
244             Element containerElement = (Element) containerElements.next();
245 
246             assertEquals( "Test container element type", "library", containerElement.attributeValue( "type" ) );
247             assertEquals( "Test container element level", "module", containerElement.attributeValue( "level" ) );
248 
249             Element attribute = findElementByNameAttribute( containerElement, "attribute", "method" );
250             assertEquals( "Test library method", "2", attribute.attributeValue( "value" ) );
251 
252             attribute = findElementByNameAttribute( containerElement, "attribute", "URI" );
253             String attributeValue = attribute.attributeValue( "value" );
254             assertTrue( "Test library URI", expectedLibs.contains( attributeValue ) );
255             expectedLibs.remove( attributeValue );
256         }
257 
258         assertTrue( "All libraries are present", expectedLibs.size() == 0 );
259     }
260 
261     public void testEarMinConfig()
262         throws Exception
263     {
264         Document imlDocument = executeMojo( "src/test/module-plugin-configs/min-ear-plugin-config.xml" );
265 
266         Element root = imlDocument.getRootElement();
267 
268         assertEquals( "Test Project type", "J2EE_APPLICATION_MODULE", root.attributeValue( "type" ) );
269 
270         Element component = findComponent( root, "ApplicationModuleProperties" );
271 
272         Element deployDescriptor = component.element( "deploymentDescriptor" );
273         assertEquals( "Test deployment descriptor version", "1.3", deployDescriptor.attributeValue( "version" ) );
274         assertEquals( "Test deployment descriptor name", "application.xml", deployDescriptor.attributeValue( "name" ) );
275         assertEquals( "Test deployment descriptor optional", "false", deployDescriptor.attributeValue( "optional" ) );
276         assertEquals( "Test deployment descriptor file", "file://$MODULE_DIR$/target/application.xml",
277                       deployDescriptor.attributeValue( "url" ) );
278     }
279 
280     public void testGeneralConfigurations()
281         throws Exception
282     {
283         Document imlDocument = executeMojo( "src/test/module-plugin-configs/general-plugin-config.xml" );
284 
285         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
286 
287         Element content = findElement( component, "content" );
288 
289         List expectedExcludes = new ArrayList();
290         expectedExcludes.add( "file://$MODULE_DIR$/src/main/resources/excluded" );
291         expectedExcludes.add( "file://$MODULE_DIR$/src/main/resources/excluded-too" );
292 
293         List excludeList = content.elements( "excludeFolder" );
294         for ( Iterator excludes = excludeList.iterator(); excludes.hasNext(); )
295         {
296             Element exclude = (Element) excludes.next();
297 
298             String excluded = exclude.attributeValue( "url" );
299 
300             if ( excluded.equals( "file://$MODULE_DIR$/src/main/resources/excluded/sub" ) )
301             {
302                 fail( "A subdirectory of an already excluded directory must be removed" );
303             }
304 
305             if ( expectedExcludes.contains( excluded ) )
306             {
307                 expectedExcludes.remove( excluded );
308             }
309         }
310         assertEquals( "Test all excludes", 0, expectedExcludes.size() );
311 
312         List orderEntryList = findElementsByName( component, "orderEntry" );
313         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
314         {
315             Element orderEntry = (Element) orderEntries.next();
316 
317             if ( "module-library".equals( orderEntry.attributeValue( "type" ) ) )
318             {
319                 Element library = orderEntry.element( "library" );
320 
321                 String name = library.attributeValue( "name" );
322                 assertTrue( "Test usage of fullnames", name.indexOf( ":" ) > 0 );
323             }
324         }
325 
326         File srcFile = new File( PlexusTestCase.getBasedir(),
327                                  "target/local-repo/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1-src.jar" );
328         assertTrue( "Test maven-model source is downloaded", srcFile.exists() );
329         srcFile = new File( PlexusTestCase.getBasedir(), "target/local-repo/junit/junit/3.8.1/junit-3.8.1-src.jar" );
330         assertTrue( "Test junit source is downloaded", srcFile.exists() );
331 
332         File docFile = new File( PlexusTestCase.getBasedir(),
333                                  "target/local-repo/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1-doc.jar" );
334         assertTrue( "Test maven-model javadoc is downloaded", docFile.exists() );
335     }
336 
337     public void testWarConfig()
338         throws Exception
339     {
340         List expectedLibs = new ArrayList();
341         expectedLibs.add( "/WEB-INF/lib/maven-model-2.0.1.jar" );
342         expectedLibs.add( "/WEB-INF/lib/junit-3.8.1.jar" );
343 
344         Document imlDocument = executeMojo( "src/test/module-plugin-configs/war-plugin-config.xml" );
345 
346         Element root = imlDocument.getRootElement();
347 
348         assertEquals( "Test Project type", "J2EE_WEB_MODULE", root.attributeValue( "type" ) );
349 
350         Element component = findComponent( root, "WebModuleBuildComponent" );
351 
352         Element setting = findElement( component, "setting" );
353         assertTrue( "Test exploded url setting", "EXPLODED_URL".equals( setting.attributeValue( "name" ) ) );
354         assertTrue( "Test exploded url value",
355                     setting.attributeValue( "value" ).startsWith( "file://$MODULE_DIR$/target/" ) );
356 
357         component = findComponent( root, "WebModuleProperties" );
358 
359         Element deployDescriptor = component.element( "deploymentDescriptor" );
360         assertEquals( "Test deployment descriptor version", "2.3", deployDescriptor.attributeValue( "version" ) );
361         assertEquals( "Test deployment descriptor name", "web.xml", deployDescriptor.attributeValue( "name" ) );
362         assertEquals( "Test deployment descriptor optional", "false", deployDescriptor.attributeValue( "optional" ) );
363         assertEquals( "Test deployment descriptor file", "file://$MODULE_DIR$/src/main/web/WEB-INF/web.xml",
364                       deployDescriptor.attributeValue( "url" ) );
365 
366         Element webroots = component.element( "webroots" );
367         Element webroot = webroots.element( "root" );
368         assertEquals( "Test webroot relative location", "/", webroot.attributeValue( "relative" ) );
369         assertEquals( "Test webroot url", "file://$MODULE_DIR$/src/main/webapp", webroot.attributeValue( "url" ) );
370 
371         List containerElementList = findElementsByName( component, "containerElement" );
372         for ( Iterator containerElements = containerElementList.iterator(); containerElements.hasNext(); )
373         {
374             Element containerElement = (Element) containerElements.next();
375 
376             assertEquals( "Test container element type", "library", containerElement.attributeValue( "type" ) );
377             assertEquals( "Test container element level", "module", containerElement.attributeValue( "level" ) );
378             assertTrue( "Test library url", containerElement.element( "url" ).getText().startsWith( "jar://" ) );
379 
380             Element attribute = findElementByNameAttribute( containerElement, "attribute", "method" );
381             assertEquals( "Test library method", "1", attribute.attributeValue( "value" ) );
382 
383             attribute = findElementByNameAttribute( containerElement, "attribute", "URI" );
384             String attributeValue = attribute.attributeValue( "value" );
385             assertTrue( "Test library URI", expectedLibs.contains( attributeValue ) );
386             expectedLibs.remove( attributeValue );
387         }
388 
389         assertTrue( "All libraries are present", expectedLibs.size() == 0 );
390     }
391 
392     public void testProjectWithModulesConfigurations()
393         throws Exception
394     {
395         Document imlDocument = executeMojo( "src/test/module-plugin-configs/module-plugin-config.xml" );
396 
397         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
398 
399         boolean moduleFound = false;
400         List orderEntryList = component.elements( "orderEntry" );
401         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
402         {
403             Element orderEntry = (Element) orderEntries.next();
404             if ( "module".equals( orderEntry.attributeValue( "type" ) ) )
405             {
406                 String moduleName = orderEntry.attributeValue( "module-name" );
407                 assertTrue( "Test idea module name", moduleName.startsWith( "plugin-reactor-project-" ) );
408                 moduleFound = true;
409             }
410         }
411         assertTrue( "Test presence of idea module", moduleFound );
412     }
413 
414     public void testProjectWithLibrariesConfigurations()
415         throws Exception
416     {
417         Document imlDocument = executeMojo( "src/test/module-plugin-configs/library-plugin-config.xml" );
418 
419         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
420 
421         boolean libraryFound = false;
422         for ( Iterator orderEntries = component.elementIterator( "orderEntry" ); orderEntries.hasNext(); )
423         {
424             Element orderEntry = (Element) orderEntries.next();
425             Element library = orderEntry.element( "library" );
426             if ( library != null )
427             {
428                 String name = library.attributeValue( "name" );
429                 if ( name != null && name.equals( "test-library" ) )
430                 {
431                     libraryFound = true;
432 
433                     String url = library.element( "CLASSES" ).element( "root" ).attributeValue( "url" );
434                     assertEquals( "Test user provided class path", "file:///user/defined/classes", url );
435 
436                     url = library.element( "SOURCES" ).element( "root" ).attributeValue( "url" );
437                     assertEquals( "Test user provided source path", "file:///user/defined/sources", url );
438                 }
439             }
440         }
441         assertTrue( "Test if configured library was found", libraryFound );
442     }
443 
444     public void testProjectWithLibraryExcludeConfigurations()
445         throws Exception
446     {
447         Document imlDocument = executeMojo( "src/test/module-plugin-configs/library-exclude-plugin-config.xml" );
448 
449         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
450 
451         for ( Iterator orderEntries = component.elementIterator( "orderEntry" ); orderEntries.hasNext(); )
452         {
453             Element orderEntry = (Element) orderEntries.next();
454             Element library = orderEntry.element( "library" );
455             if ( library != null )
456             {
457                 Element classes = library.element( "CLASSES" );
458                 if ( classes != null )
459                 {
460                     Element root = classes.element( "root" );
461                     String url = root.attributeValue( "url" );
462 
463                     if ( url.indexOf( "test-library" ) >= 0 )
464                     {
465                         fail( "test-library must be excluded" );
466                     }
467                 }
468             }
469         }
470     }
471 
472     public void testWarProjectWithModulesConfigurations()
473         throws Exception
474     {
475         Document imlDocument = executeMojo( "src/test/module-plugin-configs/war-module-plugin-config.xml" );
476 
477         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
478 
479         boolean moduleFound = false;
480         List orderEntryList = component.elements( "orderEntry" );
481         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
482         {
483             Element orderEntry = (Element) orderEntries.next();
484             if ( "module".equals( orderEntry.attributeValue( "type" ) ) )
485             {
486                 String moduleName = orderEntry.attributeValue( "module-name" );
487                 assertTrue( "Test idea module name", moduleName.startsWith( "plugin-reactor-project-" ) );
488                 moduleFound = true;
489             }
490         }
491         assertTrue( "Test presence of idea module", moduleFound );
492 
493         component = findComponent( imlDocument.getRootElement(), "WebModuleProperties" );
494 
495         boolean webModuleFound = false;
496         for ( Iterator elements = component.elementIterator( "containerElement" ); elements.hasNext(); )
497         {
498             Element containerElement = (Element) elements.next();
499 
500             if ( "module".equals( containerElement.attributeValue( "type" ) ) )
501             {
502                 String name = containerElement.attributeValue( "name" );
503 
504                 assertTrue( "Module must be from reactor", name.indexOf( "plugin-reactor-project-" ) >= 0 );
505 
506                 assertNull( "Library url for modules must not be present", containerElement.element( "url" ) );
507 
508                 Element method = findElementByNameAttribute( containerElement, "attribute", "method" );
509                 assertEquals( "Test Library module method", "5", method.attributeValue( "value" ) );
510 
511                 Element uri = findElementByNameAttribute( containerElement, "attribute", "URI" );
512                 assertEquals( "Test Library module method", "/WEB-INF/lib/" + name + "-1.0.jar",
513                               uri.attributeValue( "value" ) );
514 
515                 webModuleFound = true;
516             }
517         }
518         assertTrue( "Test WebModuleProperties for module library", webModuleFound );
519     }
520 
521     public void testEjbProjectWithModulesConfigurations()
522         throws Exception
523     {
524         Document imlDocument = executeMojo( "src/test/module-plugin-configs/ejb-module-plugin-config.xml" );
525 
526         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
527 
528         boolean moduleFound = false;
529         List orderEntryList = component.elements( "orderEntry" );
530         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
531         {
532             Element orderEntry = (Element) orderEntries.next();
533             if ( "module".equals( orderEntry.attributeValue( "type" ) ) )
534             {
535                 String moduleName = orderEntry.attributeValue( "module-name" );
536                 assertTrue( "Test idea module name", moduleName.startsWith( "plugin-reactor-project-" ) );
537                 moduleFound = true;
538             }
539         }
540         assertTrue( "Test presence of idea module", moduleFound );
541 
542         component = findComponent( imlDocument.getRootElement(), "EjbModuleProperties" );
543 
544         boolean ejbModuleFound = false;
545         for ( Iterator elements = component.elementIterator( "containerElement" ); elements.hasNext(); )
546         {
547             Element containerElement = (Element) elements.next();
548 
549             if ( "module".equals( containerElement.attributeValue( "type" ) ) )
550             {
551                 String name = containerElement.attributeValue( "name" );
552 
553                 assertTrue( "Module must be from reactor", name.indexOf( "plugin-reactor-project-" ) >= 0 );
554 
555                 assertNull( "Library url for modules must not be present", containerElement.element( "url" ) );
556 
557                 Element method = findElementByNameAttribute( containerElement, "attribute", "method" );
558                 assertEquals( "Test Library module method", "6", method.attributeValue( "value" ) );
559 
560                 Element uri = findElementByNameAttribute( containerElement, "attribute", "URI" );
561                 assertEquals( "Test Library module method", "/lib/plugin-reactor-project-42.jar", uri.attributeValue( "value" ) );
562 
563                 ejbModuleFound = true;
564             }
565         }
566         assertTrue( "Test EjbModuleProperties for module library", ejbModuleFound );
567     }
568 
569     public void testProjectArtifactsWithVersionRange()
570         throws Exception
571     {
572         List expectedDeps = new ArrayList();
573         expectedDeps.add( "/junit/junit/4.0/junit-4.0.jar!/" );
574 
575         Document imlDocument = super.executeMojo( "module", "src/test/module-plugin-configs/artifact-version-range-plugin-config.xml", "iml" );
576 
577         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
578 
579         Element output = findElement( component, "output" );
580         assertEquals( "Module output url created.", "file://$MODULE_DIR$/target/classes",
581                       output.attributeValue( "url" ) );
582 
583         Element outputTest = findElement( component, "output-test" );
584         assertEquals( "Module test-output url created.", "file://$MODULE_DIR$/target/test-classes",
585                       outputTest.attributeValue( "url" ) );
586 
587         Element content = findElement( component, "content" );
588 
589         List excludeList = content.elements( "excludeFolder" );
590         if ( excludeList.size() == 1 )
591         {
592             Element excl = content.element( "excludeFolder" );
593             assertEquals( "Test default exclude folder", "file://$MODULE_DIR$/target", excl.attributeValue( "url" ) );
594         }
595         else
596         {
597             boolean targetIsExcluded = false;
598             for ( Iterator excludes = excludeList.iterator(); excludes.hasNext(); )
599             {
600                 Element excl = (Element) excludes.next();
601 
602                 if ( "file://$MODULE_DIR$/target".equals( excl.attributeValue( "url" ) ) )
603                 {
604                     targetIsExcluded = true;
605                     break;
606                 }
607             }
608 
609             if ( !targetIsExcluded )
610             {
611                 fail( "Default exclude folder 'target' not found" );
612             }
613         }
614 
615         List elementList = findElementsByName( content, "sourceFolder" );
616         for ( Iterator sourceFolders = elementList.iterator(); sourceFolders.hasNext(); )
617         {
618             Element sourceFolder = (Element) sourceFolders.next();
619 
620             String isTestSource = sourceFolder.attributeValue( "isTestSource" ).toLowerCase();
621             if ( "false".equals( isTestSource ) )
622             {
623                 assertTrue( "Main source url",
624                             sourceFolder.attributeValue( "url" ).startsWith( "file://$MODULE_DIR$/src/main" ) );
625             }
626             else if ( "true".equals( isTestSource ) )
627             {
628                 assertTrue( "Test source url",
629                             sourceFolder.attributeValue( "url" ).startsWith( "file://$MODULE_DIR$/src/test" ) );
630             }
631             else
632             {
633                 fail( "Unknown sourceFolder 'isTestSource' attribute value: " + isTestSource );
634             }
635         }
636 
637         List orderEntryList = findElementsByName( component, "orderEntry" );
638 
639         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
640         {
641             Element orderEntry = (Element) orderEntries.next();
642 
643             if ( "module-library".equals( orderEntry.attributeValue( "type" ) ) )
644             {
645                 Element library = (Element) orderEntry.elementIterator( "library" ).next();
646 
647                 Element classes = (Element) library.elementIterator( "CLASSES" ).next();
648 
649                 Element root = (Element) classes.elementIterator( "root" ).next();
650 
651                 String depUrl = root.attributeValue( "url" );
652 
653                 if ( depUrl.endsWith( "/junit/junit/4.0/junit-4.0.jar!/" ) )
654                 {
655                     expectedDeps.remove( "/junit/junit/4.0/junit-4.0.jar!/" );
656                 }
657             }
658         }
659 
660         assertTrue( "All dependencies are present", expectedDeps.size() == 0 );
661     }
662 
663     protected Document executeMojo( String pluginXml )
664         throws Exception
665     {
666         List expectedDeps = new ArrayList();
667         expectedDeps.add( "/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.jar!/" );
668         expectedDeps.add( "/junit/junit/3.8.1/junit-3.8.1.jar!/" );
669 
670         Document imlDocument = super.executeMojo( "module", pluginXml, "iml" );
671 
672         Element component = findComponent( imlDocument.getRootElement(), "NewModuleRootManager" );
673 
674         Element output = findElement( component, "output" );
675         assertEquals( "Module output url created.", "file://$MODULE_DIR$/target/classes",
676                       output.attributeValue( "url" ) );
677 
678         Element outputTest = findElement( component, "output-test" );
679         assertEquals( "Module test-output url created.", "file://$MODULE_DIR$/target/test-classes",
680                       outputTest.attributeValue( "url" ) );
681 
682         Element content = findElement( component, "content" );
683 
684         List excludeList = content.elements( "excludeFolder" );
685         if ( excludeList.size() == 1 )
686         {
687             Element excl = content.element( "excludeFolder" );
688             assertEquals( "Test default exclude folder", "file://$MODULE_DIR$/target", excl.attributeValue( "url" ) );
689         }
690         else
691         {
692             boolean targetIsExcluded = false;
693             for ( Iterator excludes = excludeList.iterator(); excludes.hasNext(); )
694             {
695                 Element excl = (Element) excludes.next();
696 
697                 if ( "file://$MODULE_DIR$/target".equals( excl.attributeValue( "url" ) ) )
698                 {
699                     targetIsExcluded = true;
700                     break;
701                 }
702             }
703 
704             if ( !targetIsExcluded )
705             {
706                 fail( "Default exclude folder 'target' not found" );
707             }
708         }
709 
710         List elementList = findElementsByName( content, "sourceFolder" );
711         for ( Iterator sourceFolders = elementList.iterator(); sourceFolders.hasNext(); )
712         {
713             Element sourceFolder = (Element) sourceFolders.next();
714 
715             String isTestSource = sourceFolder.attributeValue( "isTestSource" ).toLowerCase();
716             if ( "false".equals( isTestSource ) )
717             {
718                 assertTrue( "Main source url",
719                             sourceFolder.attributeValue( "url" ).startsWith( "file://$MODULE_DIR$/src/main" ) );
720             }
721             else if ( "true".equals( isTestSource ) )
722             {
723                 assertTrue( "Test source url",
724                             sourceFolder.attributeValue( "url" ).startsWith( "file://$MODULE_DIR$/src/test" ) );
725             }
726             else
727             {
728                 fail( "Unknown sourceFolder 'isTestSource' attribute value: " + isTestSource );
729             }
730         }
731 
732         List orderEntryList = findElementsByName( component, "orderEntry" );
733 
734         for ( Iterator orderEntries = orderEntryList.iterator(); orderEntries.hasNext(); )
735         {
736             Element orderEntry = (Element) orderEntries.next();
737 
738             if ( "module-library".equals( orderEntry.attributeValue( "type" ) ) )
739             {
740                 Element library = (Element) orderEntry.elementIterator( "library" ).next();
741 
742                 Element classes = (Element) library.elementIterator( "CLASSES" ).next();
743 
744                 Element root = (Element) classes.elementIterator( "root" ).next();
745 
746                 String depUrl = root.attributeValue( "url" );
747 
748                 if ( depUrl.endsWith( "/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.jar!/" ) )
749                 {
750                     expectedDeps.remove( "/org/apache/maven/maven-model/2.0.1/maven-model-2.0.1.jar!/" );
751                 }
752                 else if ( depUrl.endsWith( "/junit/junit/3.8.1/junit-3.8.1.jar!/" ) )
753                 {
754                     expectedDeps.remove( "/junit/junit/3.8.1/junit-3.8.1.jar!/" );
755                 }
756             }
757         }
758 
759         assertTrue( "All dependencies are present", expectedDeps.size() == 0 );
760 
761         return imlDocument;
762     }
763 }