View Javadoc
1   package org.apache.maven.it;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.it.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Properties;
30  
31  /**
32   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-187">MNG-187</a>.
33   *
34   * @author Benjamin Bentmann
35   */
36  public class MavenITmng0187CollectedProjectsTest
37      extends AbstractMavenIntegrationTestCase
38  {
39  
40      public MavenITmng0187CollectedProjectsTest()
41      {
42          super( ALL_MAVEN_VERSIONS );
43      }
44  
45      /**
46       * Verify that MavenProject.getCollectedProjects() provides access to the direct and indirect modules
47       * of the current project.
48       *
49       * @throws Exception in case of failure
50       */
51      public void testit()
52          throws Exception
53      {
54          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-0187" );
55  
56          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
57          verifier.setAutoclean( false );
58          verifier.deleteDirectory( "target" );
59          verifier.deleteDirectory( "sub-1/target" );
60          verifier.deleteDirectory( "sub-1/sub-2/target" );
61          verifier.executeGoal( "validate" );
62          verifier.verifyErrorFreeLog();
63          verifier.resetStreams();
64  
65          Properties props;
66  
67          props = verifier.loadProperties( "target/project.properties" );
68          assertEquals( "2", props.getProperty( "project.collectedProjects.size" ) );
69          assertEquals( Arrays.asList( new String[]{ "sub-1", "sub-2" } ), getProjects( props ) );
70  
71          props = verifier.loadProperties( "sub-1/target/project.properties" );
72          assertEquals( "1", props.getProperty( "project.collectedProjects.size" ) );
73          assertEquals( Arrays.asList( new String[]{ "sub-2" } ), getProjects( props ) );
74  
75          props = verifier.loadProperties( "sub-1/sub-2/target/project.properties" );
76          assertEquals( "0", props.getProperty( "project.collectedProjects.size" ) );
77          assertEquals( Arrays.asList( new String[]{} ), getProjects( props ) );
78      }
79  
80      private List<String> getProjects( Properties props )
81      {
82          List<String> projects = new ArrayList<>();
83  
84          for ( Object o : props.keySet() )
85          {
86              String key = o.toString();
87              if ( key.startsWith( "project.collectedProjects." ) && !key.endsWith( ".size" ) )
88              {
89                  projects.add( props.getProperty( key ) );
90              }
91          }
92  
93          Collections.sort( projects );
94  
95          return projects;
96      }
97  
98  }