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
24 import junit.framework.TestCase;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.eclipse.EclipseSourceDir;
28 import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
29 import org.apache.maven.plugin.logging.SystemStreamLog;
30 import org.apache.maven.shared.tools.easymock.TestFileManager;
31 import org.jdom.Document;
32 import org.jdom.JDOMException;
33 import org.jdom.input.SAXBuilder;
34 import org.jdom.xpath.XPath;
35
36 public class EclipseClasspathWriterUnitTest
37 extends TestCase
38 {
39
40 private TestFileManager fileManager = new TestFileManager( "EclipseClasspathWriter.unitTest.", "" );
41
42 protected void tearDown()
43 throws IOException
44 {
45 fileManager.cleanUp();
46 }
47
48 public void testWrite_ShouldMaskOutputDirsNestedWithinAnExistingOutputDir()
49 throws MojoExecutionException, JDOMException, IOException
50 {
51 TestEclipseWriterConfig config = new TestEclipseWriterConfig();
52
53 File basedir = fileManager.createTempDir();
54
55 config.setProjectBaseDir( basedir );
56 config.setEclipseProjectDirectory( basedir );
57
58 String baseOutputDir = "target/classes";
59 String maskedOutputDir = "target/classes/main-resources";
60
61 File buildOutputDir = new File( basedir, baseOutputDir );
62 buildOutputDir.mkdirs();
63
64 config.setBuildOutputDirectory( buildOutputDir );
65
66 new File( basedir, maskedOutputDir ).mkdirs();
67
68 EclipseSourceDir dir =
69 new EclipseSourceDir( "src/main/resources", "target/classes", true, false, null, null, false );
70 EclipseSourceDir testDir =
71 new EclipseSourceDir( "src/test/resources", "target/classes/test-resources", true, true, null, null, false );
72
73 EclipseSourceDir[] dirs = { dir, testDir };
74
75 config.setSourceDirs( dirs );
76
77 config.setEclipseProjectName( "test-project" );
78
79 TestLog log = new TestLog();
80
81 EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter();
82 classpathWriter.init( log, config );
83 classpathWriter.write();
84
85 SAXBuilder builder = new SAXBuilder( false );
86
87 Document doc = builder.build( new File( basedir, ".classpath" ) );
88
89 XPath resourcePath = XPath.newInstance( "//classpathentry[@path='src/main/resources']" );
90
91 assertTrue( "resources classpath entry not found.", resourcePath.selectSingleNode( doc ) != null );
92
93 XPath testResourcePath = XPath.newInstance( "//classpathentry[@path='src/test/resources']" );
94
95 assertTrue( "test resources (minus custom output dir) classpath entry not found.",
96 testResourcePath.selectSingleNode( doc ) != null );
97
98 XPath stdOutputPath = XPath.newInstance( "//classpathentry[@kind='output' && @path='target/classes']" );
99
100 assertTrue( "standard output classpath entry not found.", stdOutputPath.selectSingleNode( doc ) != null );
101
102 }
103
104 private static final class TestLog
105 extends SystemStreamLog
106 {
107 public boolean isDebugEnabled()
108 {
109 return true;
110 }
111 }
112
113 }