1   package org.apache.maven.project;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import junit.framework.Test;
28  import junit.framework.TestCase;
29  import junit.framework.TestSuite;
30  
31  import org.apache.maven.MavenUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  public class ProjectInheritanceTest
35      extends TestCase
36  {
37      private String TEST_DOCUMENT = null;
38  
39      private String TEST_DOCUMENT2 = null;
40  
41      private String baseDir;
42  
43      private String FS = null;
44  
45      public ProjectInheritanceTest( String testName )
46      {
47          super( testName );
48      }
49  
50      public static Test suite()
51      {
52          return new TestSuite( ProjectInheritanceTest.class );
53      }
54  
55      protected void setUp()
56          throws Exception
57      {
58          super.setUp();
59          baseDir = System.getProperty( "basedir" );
60          assertNotNull( "The system property basedir was not defined.", baseDir );
61          String fs = System.getProperty( "file.separator" );
62          assertNotNull( "The system property file.separator was not defined.", fs );
63          FS = fs;
64          TEST_DOCUMENT = baseDir + "/src/test/extend/project-child.xml";
65          TEST_DOCUMENT2 = baseDir + "/src/test/extend/child_project/project-child2.xml";
66      }
67  
68      public void testProjectMapping()
69          throws Exception
70      {
71          Project p = MavenUtils.getProject( new File( TEST_DOCUMENT ) );
72  
73          // Make sure the groupId is inherited correctly.
74          assertEquals( "groupId", p.getGroupId() );
75  
76          assertEquals( "Super Extendo", p.getName() );
77          assertEquals( "groupId:super-extendo", p.getId() );
78          assertEquals( "Apache Software Foundation", p.getOrganization().getName() );
79          assertEquals( "http://www.apache.org/", p.getOrganization().getUrl() );
80  
81          //Test siteAddress / siteDirectory
82          assertEquals( "theSiteAddress", p.getSiteAddress() );
83          assertEquals( "theSiteDirectory", p.getSiteDirectory() );
84  
85          // Test Dependencies
86          assertEquals( "child-dep1:child-dep1", ( (Dependency) p.getDependencies().get( 0 ) ).getId() );
87          assertEquals( "1.0", ( (Dependency) p.getDependencies().get( 0 ) ).getVersion() );
88  
89          baseDir = new File( p.getContext().findVariable( "basedir" ).toString() ).getCanonicalPath();
90          // Test Source Directories
91          assertEquals( baseDir + FS + "src" + FS + "java", p.getBuild().getSourceDirectory() );
92          // Unit Test Source Directories
93          assertEquals( baseDir + FS + "src" + FS + "test" + FS + "java", p.getBuild().getUnitTestSourceDirectory() );
94  
95          // Test Aspect Source Directories
96          //assertEquals("src/aspect", p.getBuild().getAspectSourceDirectory());
97  
98          String iuSrc = p.getBuild().getIntegrationUnitTestSourceDirectory();
99          assertTrue( "Should be empty in maven's project descriptor: [" + iuSrc + "]", StringUtils.isEmpty( iuSrc ) );
100 
101         // Unit Test patterns
102         List unitTestIncludes = p.getBuild().getUnitTest().getIncludes();
103         List unitTestExcludes = p.getBuild().getUnitTest().getExcludes();
104 
105         assertEquals( "**/*Test*.java", (String) unitTestIncludes.get( 0 ) );
106         assertEquals( "**/TestAll.java", (String) unitTestExcludes.get( 0 ) );
107         assertEquals( "**/*Abstract*.java", (String) unitTestExcludes.get( 1 ) );
108 
109         SourceModification sm0 = (SourceModification) p.getBuild().getSourceModifications().get( 0 );
110         assertEquals( "java.util.logging.Logger", sm0.getClassName() );
111         assertEquals( "**/Jdk14Logger.java", (String) sm0.getExcludes().get( 0 ) );
112 
113         // Test Versions
114         Version v0 = (Version) p.getVersions().get( 0 );
115         assertEquals( "b1", v0.getId() );
116         assertEquals( "1.0-b1", v0.getName() );
117         assertEquals( "MAVEN_1_0_B1", v0.getTag() );
118 
119         // Test the version map
120         Version v4 = p.getVersionById( "b4" );
121         assertEquals( "b4", v4.getId() );
122         assertEquals( "1.0-b4", v4.getName() );
123         assertEquals( "MAVEN_1_0_B4", v4.getTag() );
124     }
125 
126     public void testProjectMappingExtends()
127         throws Exception
128     {
129         Project p = MavenUtils.getProject( new File( TEST_DOCUMENT2 ) );
130 
131         // Make sure the groupId is inherited correctly.
132         assertEquals( "groupId", p.getGroupId() );
133 
134         assertEquals( "Child Project", p.getName() );
135         assertEquals( "groupId:child", p.getId() );
136 
137         // Test organization inheritance.
138         assertNotNull( p.getOrganization() );
139         assertEquals( "Apache Software Foundation", p.getOrganization().getName() );
140         assertEquals( "http://www.apache.org/", p.getOrganization().getUrl() );
141 
142         // Test siteAddress / siteDirectory
143         assertEquals( "theSiteAddress", p.getSiteAddress() );
144         assertEquals( "theSiteDirectory", p.getSiteDirectory() );
145 
146         boolean found = false;
147         List resources = p.getBuild().getResources();
148         for ( Iterator i = resources.iterator(); i.hasNext(); )
149         {
150             Resource r = (Resource) i.next();
151             File dir = new File( p.getFile().getParent(), "src" + FS + "messages" ).getCanonicalFile();
152             if ( r.getDirectory().equals( dir.getPath() ) )
153             {
154                 assertEquals( "check target path", "org/apache/maven/messages", r.getTargetPath() );
155                 assertEquals( "check includes", Arrays.asList( new String[] { "messages*.properties" } ), r
156                     .getIncludes() );
157                 assertEquals( "check excludes", Collections.EMPTY_LIST, r.getExcludes() );
158                 found = true;
159             }
160         }
161         assertTrue( "Check found resources for src/messages", found );
162     }
163 
164 }