You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/07/01 19:37:42 UTC

[GitHub] [maven] michael-o commented on a diff in pull request #743: [3.9.x] [MNG-7401] [MNG-7474] Keep a single maven session and fix session scope

michael-o commented on code in PR #743:
URL: https://github.com/apache/maven/pull/743#discussion_r912212544


##########
maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilderTest.java:
##########
@@ -0,0 +1,102 @@
+package org.apache.maven.lifecycle.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.maven.execution.AbstractExecutionListener;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.DefaultMavenExecutionResult;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
+import org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder;
+import org.apache.maven.lifecycle.internal.stub.DefaultLifecyclesStub;
+import org.apache.maven.lifecycle.internal.stub.LifecycleTaskSegmentCalculatorStub;
+import org.apache.maven.lifecycle.internal.stub.LoggerStub;
+import org.apache.maven.lifecycle.internal.stub.MojoExecutorStub;
+import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.ContainerConfiguration;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.logging.Logger;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+public class LifecycleModuleBuilderTest extends PlexusTestCase
+{
+    @Override
+    protected void customizeContainerConfiguration( ContainerConfiguration configuration )
+    {
+        configuration.setAutoWiring( true );
+        configuration.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
+
+    }
+
+    public void testCurrentProject() throws Exception
+    {
+        List<MavenProject> currentProjects = new ArrayList<>();
+        MojoExecutorStub mojoExecutor = new MojoExecutorStub()
+        {
+            @Override
+            public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
+                    throws LifecycleExecutionException
+            {
+                super.execute( session, mojoExecutions, projectIndex );
+                currentProjects.add( session.getCurrentProject() );
+            }
+        };
+
+        final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
+        MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
+        mavenExecutionRequest.setExecutionListener( new AbstractExecutionListener() );
+        mavenExecutionRequest.setGoals( Arrays.asList( "clean" ) );
+        mavenExecutionRequest.setDegreeOfConcurrency( 1 );
+        final MavenSession session = new MavenSession( null, null, mavenExecutionRequest, defaultMavenExecutionResult );
+        final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
+        session.setProjectDependencyGraph( dependencyGraphStub );
+        session.setProjects( dependencyGraphStub.getSortedProjects() );
+
+        LifecycleModuleBuilder moduleBuilder = lookup( LifecycleModuleBuilder.class );
+        set( moduleBuilder, "mojoExecutor", mojoExecutor );
+
+        LifecycleStarter ls = lookup( LifecycleStarter.class );
+        ls.execute( session );
+
+        assertNull( session.getCurrentProject() );
+        assertEquals( Arrays.asList( ProjectDependencyGraphStub.A, ProjectDependencyGraphStub.B, ProjectDependencyGraphStub.C,
+                ProjectDependencyGraphStub.X, ProjectDependencyGraphStub.Y, ProjectDependencyGraphStub.Z ), currentProjects );
+    }
+
+    static void set( Object obj, String field, Object v ) throws NoSuchFieldException, IllegalAccessException
+    {
+        Field f = obj.getClass().getDeclaredField( field );
+        f.setAccessible( true );
+        f.set( obj, v );

Review Comment:
   Alright, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org