View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.writers;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.JarURLConnection;
24  import java.net.URL;
25  import java.util.Iterator;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.EclipseSourceDir;
31  import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
32  import org.apache.maven.plugin.ide.IdeDependency;
33  import org.apache.maven.plugin.logging.SystemStreamLog;
34  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
35  import org.apache.maven.shared.tools.easymock.TestFileManager;
36  import org.jdom.Attribute;
37  import org.jdom.Document;
38  import org.jdom.JDOMException;
39  import org.jdom.input.SAXBuilder;
40  import org.jdom.xpath.XPath;
41  
42  public class EclipseClasspathWriterUnitTest
43      extends TestCase
44  {
45  
46      private TestFileManager fileManager = new TestFileManager( "EclipseClasspathWriter.unitTest.", "" );
47  
48      protected void tearDown()
49          throws IOException
50      {
51          fileManager.cleanUp();
52      }
53  
54      public void testWrite_ShouldMaskOutputDirsNestedWithinAnExistingOutputDir()
55          throws MojoExecutionException, JDOMException, IOException
56      {
57          TestEclipseWriterConfig config = new TestEclipseWriterConfig();
58  
59          File basedir = fileManager.createTempDir();
60  
61          config.setProjectBaseDir( basedir );
62          config.setEclipseProjectDirectory( basedir );
63  
64          String baseOutputDir = "target/classes";
65          String maskedOutputDir = "target/classes/main-resources";
66  
67          File buildOutputDir = new File( basedir, baseOutputDir );
68          buildOutputDir.mkdirs();
69  
70          config.setBuildOutputDirectory( buildOutputDir );
71  
72          new File( basedir, maskedOutputDir ).mkdirs();
73  
74          EclipseSourceDir dir =
75              new EclipseSourceDir( "src/main/resources", "target/classes", true, false, null, null, false );
76          EclipseSourceDir testDir =
77              new EclipseSourceDir( "src\\test\\resources", "target/classes/test-resources", true, true, null, null,
78                                    false );
79  
80          EclipseSourceDir[] dirs = { dir, testDir };
81  
82          config.setSourceDirs( dirs );
83  
84          config.setEclipseProjectName( "test-project" );
85  
86          TestLog log = new TestLog();
87  
88          EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter();
89          classpathWriter.init( log, config );
90          classpathWriter.write();
91  
92          SAXBuilder builder = new SAXBuilder( false );
93  
94          Document doc = builder.build( new File( basedir, ".classpath" ) );
95  
96          XPath resourcePath = XPath.newInstance( "//classpathentry[@path='src/main/resources']" );
97  
98          assertTrue( "resources classpath entry not found.", resourcePath.selectSingleNode( doc ) != null );
99  
100         XPath testResourcePath = XPath.newInstance( "//classpathentry[@path='src/test/resources']" );
101 
102         assertTrue( "test resources (minus custom output dir) classpath entry not found.",
103                     testResourcePath.selectSingleNode( doc ) != null );
104 
105         XPath stdOutputPath = XPath.newInstance( "//classpathentry[@kind='output' && @path='target/classes']" );
106 
107         assertTrue( "standard output classpath entry not found.", stdOutputPath.selectSingleNode( doc ) != null );
108 
109     }
110 
111     public void testWrite_ShouldGenerateValidJavadocURLs()
112         throws MojoExecutionException, JDOMException, IOException
113     {
114         TestEclipseWriterConfig config = new TestEclipseWriterConfig();
115 
116         File basedir = fileManager.createTempDir();
117 
118         File repoDir = new File( basedir, "repo" );
119         config.setLocalRepository( new StubArtifactRepository( repoDir.getPath() ) );
120 
121         config.setProjectBaseDir( basedir );
122         config.setEclipseProjectDirectory( basedir );
123 
124         String baseOutputDir = "target/classes";
125         String maskedOutputDir = "target/classes/main-resources";
126 
127         File buildOutputDir = new File( basedir, baseOutputDir );
128         buildOutputDir.mkdirs();
129 
130         config.setBuildOutputDirectory( buildOutputDir );
131 
132         new File( basedir, maskedOutputDir ).mkdirs();
133 
134         config.setEclipseProjectName( "test-project" );
135 
136         IdeDependency dependency = new IdeDependency();
137         dependency.setFile( new File( repoDir, "g/a/v/a-v.jar" ) );
138         dependency.setGroupId( "g" );
139         dependency.setArtifactId( "a" );
140         dependency.setVersion( "v" );
141         dependency.setAddedToClasspath( true );
142         dependency.setJavadocAttachment( new File( System.getProperty( "user.home" ), ".m2/some.jar" ) );
143 
144         config.setDeps( new IdeDependency[] { dependency } );
145 
146         TestLog log = new TestLog();
147 
148         EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter();
149         classpathWriter.init( log, config );
150         classpathWriter.write();
151 
152         SAXBuilder builder = new SAXBuilder( false );
153 
154         Document doc = builder.build( new File( basedir, ".classpath" ) );
155 
156         XPath javadocUrls = XPath.newInstance( "//attribute/@value" );
157         for ( Iterator it = javadocUrls.selectNodes( doc ).iterator(); it.hasNext(); )
158         {
159             Attribute attribute = (Attribute) it.next();
160             URL jarUrl = new URL( attribute.getValue() );
161             URL fileUrl = ( (JarURLConnection) jarUrl.openConnection() ).getJarFileURL();
162             String host = fileUrl.getHost();
163             assertTrue( "Unexpected host: \"" + host + "\"", "".equals( host ) || "localhost".equals( host ) );
164         }
165     }
166 
167     private static final class TestLog
168         extends SystemStreamLog
169     {
170         public boolean isDebugEnabled()
171         {
172             return true;
173         }
174     }
175 
176 }