View Javadoc
1   package org.apache.maven.plugin.eclipse.writers;
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 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          // create the config and the logger
72          TestEclipseWriterConfig config = new TestEclipseWriterConfig();
73          TestLog log = new TestLog();
74  
75          // setup the eclipse project
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          // create the .project file and start writing the contents
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();// name
93  
94          writer.startElement( "linkedResources" );
95          writer.startElement( "link" );
96  
97          writer.startElement( "name" );
98          writer.writeText( "linkTest" );
99          writer.endElement();// name
100 
101         writer.startElement( "type" );
102         writer.writeText( "2" );
103         writer.endElement();// type
104 
105         writer.startElement( "location" );
106         writer.writeText( basedir + "/dummyName" );
107         writer.endElement(); // location
108 
109         writer.endElement();// link
110         writer.endElement();// linkedResources
111         writer.endElement();// projectDescription
112 
113         IOUtil.close( w );
114         // parse the file we just created in order to keep manually-added linkedResources
115         // pre setup
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         // call the projectwriter to write the .project file
126         projectWriter.init( log, config );
127         projectWriter.write();
128         // post check..compare the pre values to make sure the writer preserves the LinkedResources
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 }