1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
26 import junit.framework.TestCase;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.eclipse.EclipseSourceDir;
30 import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
31 import org.apache.maven.plugin.ide.IdeDependency;
32 import org.apache.maven.plugin.logging.SystemStreamLog;
33 import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
34 import org.apache.maven.shared.tools.easymock.TestFileManager;
35 import org.jdom.Attribute;
36 import org.jdom.Document;
37 import org.jdom.JDOMException;
38 import org.jdom.input.SAXBuilder;
39 import org.jdom.xpath.XPath;
40
41 public class EclipseClasspathWriterUnitTest
42 extends TestCase
43 {
44
45 private TestFileManager fileManager = new TestFileManager( "EclipseClasspathWriter.unitTest.", "" );
46
47 protected void tearDown()
48 throws IOException
49 {
50 fileManager.cleanUp();
51 }
52
53 public void testWrite_ShouldMaskOutputDirsNestedWithinAnExistingOutputDir()
54 throws MojoExecutionException, JDOMException, IOException
55 {
56 TestEclipseWriterConfig config = new TestEclipseWriterConfig();
57
58 File basedir = fileManager.createTempDir();
59
60 config.setProjectBaseDir( basedir );
61 config.setEclipseProjectDirectory( basedir );
62
63 String baseOutputDir = "target/classes";
64 String maskedOutputDir = "target/classes/main-resources";
65
66 File buildOutputDir = new File( basedir, baseOutputDir );
67 buildOutputDir.mkdirs();
68
69 config.setBuildOutputDirectory( buildOutputDir );
70
71 new File( basedir, maskedOutputDir ).mkdirs();
72
73 EclipseSourceDir dir =
74 new EclipseSourceDir( "src/main/resources", "target/classes", true, false, null, null, false );
75 EclipseSourceDir testDir =
76 new EclipseSourceDir( "src\\test\\resources", "target/classes/test-resources", true, true, null, null,
77 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 (Object o : javadocUrls.selectNodes(doc)) {
157 Attribute attribute = (Attribute) o;
158 URL jarUrl = new URL(attribute.getValue());
159 URL fileUrl = ((JarURLConnection) jarUrl.openConnection()).getJarFileURL();
160 String host = fileUrl.getHost();
161 assertTrue("Unexpected host: \"" + host + "\"", "".equals(host) || "localhost".equals(host));
162 }
163 }
164
165 private static final class TestLog
166 extends SystemStreamLog
167 {
168 public boolean isDebugEnabled()
169 {
170 return true;
171 }
172 }
173
174 }