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.idea.stubs.TestCounter;
27 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28 import org.apache.maven.plugin.Mojo;
29 import org.codehaus.plexus.PlexusTestCase;
30
31 import java.io.File;
32 import java.util.Iterator;
33 import java.util.List;
34
35
36
37
38 public abstract class AbstractIdeaTestCase
39 extends AbstractMojoTestCase
40 {
41 protected Mojo mojo;
42
43 protected Document executeMojo( String goal, String pluginXml, String targetExtension )
44 throws Exception
45 {
46 File pluginXmlFile = new File( getBasedir(), pluginXml );
47
48 mojo = lookupMojo( goal, pluginXmlFile );
49
50 assertNotNull( "Get mojo instance using " + pluginXmlFile.getAbsolutePath() , mojo );
51
52 mojo.execute();
53
54 int testCounter = TestCounter.currentCount();
55
56 File outputFile = new File( PlexusTestCase.getBasedir(), "target/test-harness/" + testCounter +
57 "/plugin-test-" + testCounter + "." + targetExtension );
58
59 assertTrue( "Target file was created", outputFile.exists() );
60
61 return readXmlDocument( outputFile );
62 }
63
64 protected Document readXmlDocument( File xmlFile )
65 throws DocumentException
66 {
67 SAXReader reader = new SAXReader();
68
69 return reader.read( xmlFile );
70 }
71
72 protected Element findComponent( Element module, String name )
73 throws Exception
74 {
75 return findElementByNameAttribute( module, "component", name );
76 }
77
78 protected Element findElementByNameAttribute( Element element, String elementName, String nameAttribute )
79 throws Exception
80 {
81 Element e = null;
82
83 for ( Iterator children = element.elementIterator( elementName ); children.hasNext(); )
84 {
85 Element child = (Element) children.next();
86 if ( nameAttribute == null )
87 {
88 e = child;
89 }
90 else if ( nameAttribute.equals( child.attributeValue( "name" ) ) )
91 {
92 e = child;
93 }
94 }
95
96 if ( e == null)
97 {
98 if ( nameAttribute == null )
99 {
100 fail( "Element " + elementName + " not found." );
101 }
102 else
103 {
104 fail( "Attribute " + nameAttribute + " not found in elements " + elementName + "." );
105 }
106 }
107
108 return e;
109 }
110
111 protected List findElementsByName( Element element, String elementName )
112 {
113 return element.elements( elementName );
114 }
115
116 protected Element findElement( Element component, String name )
117 {
118 Element element = component.element( name );
119
120 if ( element == null )
121 {
122 fail( "Element " + name + " not found." );
123 }
124
125 return element;
126 }
127 }