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 java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.maven.it.util.ResourceExtractor;
31  
32  /**
33   * An integration test to ensure any pomfile is only read once.
34   * This is confirmed by adding a Java Agent to the DefaultModelReader and output the options, including the source location
35   *
36   * <a href="https://issues.apache.org/jira/browse/MNG-5669">MNG-5669</a>.
37   *
38   */
39  public class MavenITmng5669ReadPomsOnce
40      extends AbstractMavenIntegrationTestCase
41  {
42  
43      public MavenITmng5669ReadPomsOnce()
44      {
45          super( "[4.0.0-alpha-1,)" );
46      }
47  
48      public void testWithoutBuildConsumer()
49          throws Exception
50      {
51          // prepare JavaAgent
52          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5669-read-poms-once" );
53          Verifier verifier = newVerifier( testDir.getAbsolutePath(), false );
54          Map<String, String> filterProperties =
55              Collections.singletonMap( "${javaAgentJar}",
56                                        verifier.getArtifactPath( "mng-coreit", "javaagent", "1.0-SNAPSHOT", "jar" ) );
57          verifier.filterFile( ".mvn/jvm.config", ".mvn/jvm.config", null, filterProperties );
58  
59          verifier.setForkJvm( true ); // pick up agent
60          verifier.setMavenDebug( false );
61          verifier.setAutoclean( false );
62          verifier.addCliOption( "-q" );
63          verifier.addCliOption( "-U" );
64          verifier.addCliOption( "-Dmaven.experimental.buildconsumer=false" );
65          verifier.executeGoals( Arrays.asList( "verify" ) );
66          verifier.resetStreams();
67  
68          List<String> logTxt = verifier.loadLines( "log.txt", "utf-8" );
69          for ( String line : logTxt )
70          {
71              if ( line.startsWith( "Picked up JAVA_TOOL_OPTIONS:" ) )
72              {
73                  logTxt.remove( line );
74                  break;
75              }
76          }
77          assertEquals( logTxt.toString(), 168, logTxt.size() );
78  
79          // analyze lines. It is a Hashmap, so we can't rely on the order
80          Set<String> uniqueBuildingSources = new HashSet<>( 168 );
81          final String buildSourceKey = "org.apache.maven.model.building.source=";
82          final int keyLength = buildSourceKey.length();
83          for ( String line : logTxt )
84          {
85              int start = line.indexOf( buildSourceKey );
86              if ( start < 0 )
87              {
88                  continue;
89              }
90  
91              int end = line.indexOf( ", ", start );
92              if ( end < 0 )
93              {
94                  end = line.length() - 1; // is the }
95              }
96              uniqueBuildingSources.add( line.substring( start + keyLength, end ) );
97          }
98          assertEquals( uniqueBuildingSources.size(), 167 /* is 168 minus superpom */ );
99      }
100 
101     public void testWithBuildConsumer()
102         throws Exception
103     {
104         // prepare JavaAgent
105         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5669-read-poms-once" );
106         Verifier verifier = newVerifier( testDir.getAbsolutePath(), false );
107         Map<String, String> filterProperties =
108             Collections.singletonMap( "${javaAgentJar}",
109                                       verifier.getArtifactPath( "mng-coreit", "javaagent", "1.0-SNAPSHOT", "jar" ) );
110         verifier.filterFile( ".mvn/jvm.config", ".mvn/jvm.config", null, filterProperties );
111 
112         verifier.setLogFileName( "log-bc.txt" );
113         verifier.setForkJvm( true ); // pick up agent
114         verifier.setMavenDebug( false );
115         verifier.setAutoclean( false );
116         verifier.addCliOption( "-q" );
117         verifier.addCliOption( "-U" );
118         verifier.addCliOption( "-Dmaven.experimental.buildconsumer=true" );
119         verifier.executeGoals( Arrays.asList( "verify" ) );
120         verifier.resetStreams();
121 
122         List<String> logTxt = verifier.loadLines( "log-bc.txt", "utf-8" );
123         for ( String line : logTxt )
124         {
125             if ( line.startsWith( "Picked up JAVA_TOOL_OPTIONS:" ) )
126             {
127                 logTxt.remove( line );
128                 break;
129             }
130         }
131         assertEquals( logTxt.toString(), 168 + 4 /* reactor poms are read twice: file + raw (=XMLFilters) */,
132                       logTxt.size() );
133 
134         // analyze lines. It is a Hashmap, so we can't rely on the order
135         Set<String> uniqueBuildingSources = new HashSet<>( 168 );
136         final String buildSourceKey = "org.apache.maven.model.building.source=";
137         final int keyLength = buildSourceKey.length();
138         for ( String line : logTxt )
139         {
140             int start = line.indexOf( buildSourceKey );
141             if ( start < 0 )
142             {
143                 continue;
144             }
145 
146             int end = line.indexOf( ", ", start );
147             if ( end < 0 )
148             {
149                 end = line.length() - 1; // is the }
150             }
151             uniqueBuildingSources.add( line.substring( start + keyLength, end ) );
152         }
153         assertEquals( uniqueBuildingSources.size(), 167 /* is 168 minus superpom */ );
154     }
155 
156 }