You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by if...@apache.org on 2014/07/08 11:52:10 UTC

[2/4] git commit: MPLUGINTESTING-41 bind and manage SessionScope and MojoExecutionScope

MPLUGINTESTING-41 bind and manage SessionScope and MojoExecutionScope

Signed-off-by: Igor Fedorenko <if...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/commit/3c981e52
Tree: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/tree/3c981e52
Diff: http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/diff/3c981e52

Branch: refs/heads/master
Commit: 3c981e52f5aff2118c2f9306c05c7bf87537dbc7
Parents: 1f5a476
Author: Igor Fedorenko <if...@apache.org>
Authored: Tue Jul 8 12:53:52 2014 +0400
Committer: Igor Fedorenko <if...@apache.org>
Committed: Tue Jul 8 13:49:00 2014 +0400

----------------------------------------------------------------------
 .../plugin/testing/AbstractMojoTestCase.java    | 16 +++++++
 .../apache/maven/plugin/testing/MojoRule.java   | 49 +++++++++++++++++++-
 pom.xml                                         |  2 +-
 3 files changed, 65 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/blob/3c981e52/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
----------------------------------------------------------------------
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
index 9b49e71..91edd5e 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/AbstractMojoTestCase.java
@@ -42,6 +42,8 @@ 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.execution.scope.MojoExecutionScoped;
+import org.apache.maven.execution.scope.internal.MojoExecutionScope;
 import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.monitor.logging.DefaultLog;
@@ -78,6 +80,7 @@ import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 
+import com.google.inject.AbstractModule;
 import com.google.inject.Module;
 
 /**
@@ -223,6 +226,19 @@ public abstract class AbstractMojoTestCase
         try
         {
             List<Module> modules = new ArrayList<Module>();
+            modules.add( new AbstractModule()
+            {
+                @Override
+                protected void configure()
+                {
+                    // execution scope bindings (core binds these in plugin realm injector only)
+                    MojoExecutionScope executionScope = new MojoExecutionScope();
+                    bindScope( MojoExecutionScoped.class, executionScope );
+                    bind( MojoExecutionScope.class ).toInstance( executionScope );
+                    bind( MavenProject.class ).toProvider( MojoExecutionScope.<MavenProject> seededKeyProvider() ).in( executionScope );
+                    bind( MojoExecution.class ).toProvider( MojoExecutionScope.<MojoExecution> seededKeyProvider() ).in( executionScope );
+                }
+            } );
             addGuiceModules( modules );
             container = new DefaultPlexusContainer( cc, modules.toArray( new Module[modules.size()] ) );
         }

http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/blob/3c981e52/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java
----------------------------------------------------------------------
diff --git a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java
index 30d2073..a8c2db8 100644
--- a/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java
+++ b/maven-plugin-testing-harness/src/main/java/org/apache/maven/plugin/testing/MojoRule.java
@@ -23,9 +23,13 @@ import java.io.File;
 import java.io.InputStream;
 import java.util.Map;
 
+import org.apache.maven.SessionScope;
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.execution.MojoExecutionEvent;
+import org.apache.maven.execution.MojoExecutionListener;
+import org.apache.maven.execution.scope.internal.MojoExecutionScope;
 import org.apache.maven.plugin.Mojo;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.project.MavenProject;
@@ -333,7 +337,10 @@ public class MojoRule
     public void executeMojo( File basedir, String goal )
         throws Exception
     {
-        lookupConfiguredMojo( basedir, goal ).execute();
+        MavenProject project = readMavenProject( basedir );
+        MavenSession session = newMavenSession( project );
+        MojoExecution execution = newMojoExecution( goal );
+        executeMojo( session, project, execution );
     }
 
     /**
@@ -357,4 +364,44 @@ public class MojoRule
         return getContainer().lookup( role );
     }
 
+    /**
+     * @since 3.2.0
+     */
+    public void executeMojo( MavenSession session, MavenProject project, MojoExecution execution )
+        throws Exception
+    {
+        SessionScope sessionScope = lookup( SessionScope.class );
+        try
+        {
+            sessionScope.enter();
+            sessionScope.seed( MavenSession.class, session );
+
+            MojoExecutionScope executionScope = lookup( MojoExecutionScope.class );
+            try
+            {
+                executionScope.enter();
+
+                executionScope.seed( MavenProject.class, project );
+                executionScope.seed( MojoExecution.class, execution );
+
+                Mojo mojo = lookupConfiguredMojo( session, execution );
+                mojo.execute();
+
+                MojoExecutionEvent event = new MojoExecutionEvent( session, project, execution, mojo );
+                for ( MojoExecutionListener listener : getContainer().lookupList( MojoExecutionListener.class ) )
+                {
+                    listener.afterMojoExecutionSuccess( event );
+                }
+            }
+            finally
+            {
+                executionScope.exit();
+            }
+        }
+        finally
+        {
+            sessionScope.exit();
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/maven-plugin-testing/blob/3c981e52/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 391e03c..a2ebed2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -145,7 +145,7 @@ under the License.
   </distributionManagement>
 
   <properties>
-    <mavenVersion>3.1.1</mavenVersion>
+    <mavenVersion>3.2.1</mavenVersion>
     <plexusVersion>1.5.5</plexusVersion>
     <maven.site.path>plugin-testing-archives/LATEST</maven.site.path>
     <!-- Not sure if this is intentional but MojoRule has an @Override annotation from the interface which is