1 package org.apache.maven.plugin.idea;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.dom4j.Document;
23 import org.dom4j.Element;
24 import org.dom4j.DocumentException;
25 import org.dom4j.io.SAXReader;
26 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
27 import org.apache.maven.plugin.Mojo;
28 import org.apache.maven.project.MavenProject;
29
30 import java.io.File;
31 import java.util.Iterator;
32 import java.util.List;
33
34
35
36
37 public abstract class AbstractIdeaTestCase
38 extends AbstractMojoTestCase
39 {
40 protected Mojo mojo;
41
42 protected Document executeMojo( String goal, String pluginXml, String targetExtension )
43 throws Exception
44 {
45 File pluginXmlFile = new File( getBasedir(), pluginXml );
46
47 mojo = lookupMojo( goal, pluginXmlFile );
48
49 assertNotNull( "Get mojo instance using " + pluginXmlFile.getAbsolutePath() , mojo );
50
51 mojo.execute();
52
53 MavenProject executedProject = (MavenProject) getVariableValueFromObject( mojo, "executedProject" );
54
55 File outputFile =
56 new File( executedProject.getBasedir(), executedProject.getArtifactId() + '.' + targetExtension );
57
58 assertTrue( "Target file was created", outputFile.exists() );
59
60 return readXmlDocument( outputFile );
61 }
62
63 protected Document readXmlDocument( File xmlFile )
64 throws DocumentException
65 {
66 SAXReader reader = new SAXReader();
67
68 return reader.read( xmlFile );
69 }
70
71 protected Element findComponent( Element module, String name )
72 throws Exception
73 {
74 return findElementByNameAttribute( module, "component", name );
75 }
76
77 protected Element findElementByNameAttribute( Element element, String elementName, String nameAttribute )
78 throws Exception
79 {
80 Element e = null;
81
82 for ( Iterator children = element.elementIterator( elementName ); children.hasNext(); )
83 {
84 Element child = (Element) children.next();
85 if ( nameAttribute == null )
86 {
87 e = child;
88 }
89 else if ( nameAttribute.equals( child.attributeValue( "name" ) ) )
90 {
91 e = child;
92 }
93 }
94
95 if ( e == null)
96 {
97 if ( nameAttribute == null )
98 {
99 fail( "Element " + elementName + " not found." );
100 }
101 else
102 {
103 fail( "Attribute " + nameAttribute + " not found in elements " + elementName + "." );
104 }
105 }
106
107 return e;
108 }
109
110 protected List findElementsByName( Element element, String elementName )
111 {
112 return element.elements( elementName );
113 }
114
115 protected Element findElement( Element component, String name )
116 {
117 Element element = component.element( name );
118
119 if ( element == null )
120 {
121 fail( "Element " + name + " not found." );
122 }
123
124 return element;
125 }
126 }