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, false );
78  
79          EclipseSourceDir[] dirs = { dir, testDir };
80  
81          config.setSourceDirs( dirs );
82  
83          config.setEclipseProjectName( "test-project" );
84  
85          TestLog log = new TestLog();
86  
87          EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter();
88          classpathWriter.init( log, config );
89          classpathWriter.write();
90  
91          SAXBuilder builder = new SAXBuilder( false );
92  
93          Document doc = builder.build( new File( basedir, ".classpath" ) );
94  
95          XPath resourcePath = XPath.newInstance( "//classpathentry[@path='src/main/resources']" );
96  
97          assertTrue( "resources classpath entry not found.", resourcePath.selectSingleNode( doc ) != null );
98  
99          XPath testResourcePath = XPath.newInstance( "//classpathentry[@path='src/test/resources']" );
100 
101         assertTrue( "test resources (minus custom output dir) classpath entry not found.",
102                     testResourcePath.selectSingleNode( doc ) != null );
103 
104         XPath stdOutputPath = XPath.newInstance( "//classpathentry[@kind='output' && @path='target/classes']" );
105 
106         assertTrue( "standard output classpath entry not found.", stdOutputPath.selectSingleNode( doc ) != null );
107 
108     }
109 
110     public void testWrite_ShouldGenerateValidJavadocURLs()
111         throws MojoExecutionException, JDOMException, IOException
112     {
113         TestEclipseWriterConfig config = new TestEclipseWriterConfig();
114 
115         File basedir = fileManager.createTempDir();
116 
117         File repoDir = new File( basedir, "repo" );
118         config.setLocalRepository( new StubArtifactRepository( repoDir.getPath() ) );
119 
120         config.setProjectBaseDir( basedir );
121         config.setEclipseProjectDirectory( basedir );
122 
123         String baseOutputDir = "target/classes";
124         String maskedOutputDir = "target/classes/main-resources";
125 
126         File buildOutputDir = new File( basedir, baseOutputDir );
127         buildOutputDir.mkdirs();
128 
129         config.setBuildOutputDirectory( buildOutputDir );
130 
131         new File( basedir, maskedOutputDir ).mkdirs();
132 
133         config.setEclipseProjectName( "test-project" );
134 
135         IdeDependency dependency = new IdeDependency();
136         dependency.setFile( new File( repoDir, "g/a/v/a-v.jar" ) );
137         dependency.setGroupId( "g" );
138         dependency.setArtifactId( "a" );
139         dependency.setVersion( "v" );
140         dependency.setAddedToClasspath( true );
141         dependency.setJavadocAttachment( new File( System.getProperty( "user.home" ), ".m2/some.jar" ) );
142 
143         config.setDeps( new IdeDependency[] { dependency } );
144 
145         TestLog log = new TestLog();
146 
147         EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter();
148         classpathWriter.init( log, config );
149         classpathWriter.write();
150 
151         SAXBuilder builder = new SAXBuilder( false );
152 
153         Document doc = builder.build( new File( basedir, ".classpath" ) );
154 
155         XPath javadocUrls = XPath.newInstance( "//attribute/@value" );
156         for ( Iterator it = javadocUrls.selectNodes( doc ).iterator(); it.hasNext(); )
157         {
158             Attribute attribute = (Attribute) it.next();
159             URL jarUrl = new URL( attribute.getValue() );
160             URL fileUrl = ( (JarURLConnection) jarUrl.openConnection() ).getJarFileURL();
161             String host = fileUrl.getHost();
162             assertTrue( "Unexpected host: \"" + host + "\"", "".equals( host ) || "localhost".equals( host ) );
163         }
164     }
165 
166     private static final class TestLog
167         extends SystemStreamLog
168     {
169         public boolean isDebugEnabled()
170         {
171             return true;
172         }
173     }
174 
175 }