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  
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 }