1 package org.apache.maven.plugin.eclipse.writers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.Reader;
29 import java.io.Writer;
30
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
33 import org.apache.maven.plugin.logging.SystemStreamLog;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.shared.tools.easymock.TestFileManager;
36 import org.codehaus.plexus.util.IOUtil;
37 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
38 import org.codehaus.plexus.util.xml.XMLWriter;
39 import org.codehaus.plexus.util.xml.Xpp3Dom;
40 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
41 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
42 import org.jdom.JDOMException;
43 import junit.framework.TestCase;
44
45 public class EclipseProjectWriterTest
46 extends TestCase
47 {
48 private TestFileManager fileManager = new TestFileManager( "EclipseProjectWriter.unitTest.", "" );
49
50 public EclipseProjectWriterTest( String name )
51 {
52 super( name );
53 }
54
55 protected void setUp()
56 throws Exception
57 {
58
59 }
60
61 protected void tearDown()
62 throws Exception
63 {
64 fileManager.cleanUp();
65 }
66
67 public void testWrite_preservingLinkedResources()
68 throws MojoExecutionException, JDOMException, IOException, XmlPullParserException
69 {
70
71
72 TestEclipseWriterConfig config = new TestEclipseWriterConfig();
73 TestLog log = new TestLog();
74
75
76 File basedir = fileManager.createTempDir();
77 config.setProjectBaseDir( basedir );
78 config.setEclipseProjectDirectory( basedir );
79 config.setEclipseProjectName( "test-project" );
80 MavenProject project = new MavenProject();
81 config.setProject( project );
82 EclipseProjectWriter projectWriter = new EclipseProjectWriter();
83
84 File dotProject = new File( config.getEclipseProjectDirectory(), ".project" );
85 Writer w = new OutputStreamWriter( new FileOutputStream( dotProject ), "UTF-8" );
86 XMLWriter writer = new PrettyPrintXMLWriter( w );
87
88 writer.startElement( "projectDescription" );
89
90 writer.startElement( "name" );
91 writer.writeText( "test-project" );
92 writer.endElement();
93
94 writer.startElement( "linkedResources" );
95 writer.startElement( "link" );
96
97 writer.startElement( "name" );
98 writer.writeText( "linkTest" );
99 writer.endElement();
100
101 writer.startElement( "type" );
102 writer.writeText( "2" );
103 writer.endElement();
104
105 writer.startElement( "location" );
106 writer.writeText( basedir + "/dummyName" );
107 writer.endElement();
108
109 writer.endElement();
110 writer.endElement();
111 writer.endElement();
112
113 IOUtil.close( w );
114
115
116 Reader reader;
117 reader = new InputStreamReader( new FileInputStream( dotProject ), "UTF-8" );
118 Xpp3Dom dom = Xpp3DomBuilder.build( reader );
119 Xpp3Dom linkedResourcesElement = dom.getChild( "linkedResources" );
120 Xpp3Dom[] existingLinks = linkedResourcesElement.getChildren( "link" );
121 String existingName = existingLinks[0].getChild( "name" ).getValue();
122 String existingType = existingLinks[0].getChild( "type" ).getValue();
123 String existingLocation = existingLinks[0].getChild( "location" ).getValue();
124 reader.close();
125
126 projectWriter.init( log, config );
127 projectWriter.write();
128
129 reader = new InputStreamReader( new FileInputStream( dotProject ), "UTF-8" );
130 Xpp3Dom linkedResourcesElement1 = dom.getChild( "linkedResources" );
131 assertNotNull( "No linkedResources present", linkedResourcesElement1 );
132 String currentName = existingLinks[0].getChild( "name" ).getValue();
133 String currentType = existingLinks[0].getChild( "type" ).getValue();
134 String currentLocation = existingLinks[0].getChild( "location" ).getValue();
135 assertEquals( "link name is not equal", existingName, currentName );
136 assertEquals( "link type is not equal", existingType, currentType );
137 assertEquals( "link location is not equal", existingLocation, currentLocation );
138
139 reader.close();
140
141 }
142
143 private static final class TestLog
144 extends SystemStreamLog
145 {
146 public boolean isDebugEnabled()
147 {
148 return true;
149 }
150 }
151
152 }