/*
 * Copyright 2013 Andrew Everitt, Andrew Heckford, Daniele Masato
 *
 * Licensed 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.
 */
package uk.org.raje.maven.plugin.msbuild;

import java.io.File;

import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.project.DefaultMavenProjectBuilder;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.DefaultContext;

/**
 * Abstract unit test base class to extend AbstractMojoTestCase and add 
 * common functions that we use.
 */
public abstract class AbstractMSBuildMojoTestCase extends AbstractMojoTestCase
{

    /**
     * Workaround for parent class lookupMojo and lookupConfiguredMojo.
     * @param name the name of the Mojo to lookup
     * @param pomPath where to find the POM file
     * @return a configured MSBuild Mojo for testing
     * @throws Exception if we can't find the Mojo or the POM is malformed
     */
    protected final Mojo lookupConfiguredMojo( String name, String pomPath ) throws Exception
    {
        File pom = new File( getClass().getResource( pomPath ).getPath() );
        assertTrue( pom.exists() );


        // The following is an attempt to create a MavenProject
        // This fails because the Context is not properly initialised
        // I am committing this to record how far this attempt got
        ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
        Context context = new DefaultContext();

        MavenProjectBuilder projectBuilder = new DefaultMavenProjectBuilder();
        ( ( DefaultMavenProjectBuilder ) projectBuilder ).initialize();
        ( ( DefaultMavenProjectBuilder ) projectBuilder ).contextualize( context );
        MavenProject mavenProject = projectBuilder.build( pom, projectBuilderConfiguration );
        assertNotNull( mavenProject );
        
        // Used lookupMojo as it sets up most of what we need and reads configuration
        // variables from the poms.
        // It doesn't set a MavenProject so we have to do that manually
        // lookupConfiguredMojo doesn't work properly, configuration variables are no expanded
        // as we expect and it fails to setup a Log.
        Mojo mojo = lookupMojo( name, pom );
        //Mojo mojo = super.lookupConfiguredMojo( mavenProject, name );
        assertNotNull( mojo );

        setVariableValueToObject( mojo, "mavenProject", mavenProject );
        
        return mojo;
    }
}

@@ -717,208 +717,201 @@
 ven.
-plugin.Mojo;%0Aimport org.apache.maven.plugin.testing.AbstractMojoTestCase;%0Aimport org.apache.maven.project.DefaultMavenProjectBuilder;%0Aimport org.apache.maven.project.DefaultProjectBuilderConfiguration
+execution.DefaultMavenExecutionRequest;%0Aimport org.apache.maven.execution.MavenExecutionRequest;%0Aimport org.apache.maven.plugin.Mojo;%0Aimport org.apache.maven.plugin.testing.AbstractMojoTestCase
 ;%0Aim
@@ -982,29 +982,24 @@
 ven.project.
-Maven
 ProjectBuild
@@ -1050,117 +1050,17 @@
 uild
-erConfiguration;%0Aimport org.codehaus.plexus.context.Context;%0Aimport org.codehaus.plexus.context.DefaultContex
+ingReques
 t;%0A%0A
@@ -1783,17 +1783,16 @@
 s() );%0A%0A
-%0A
         
@@ -1812,349 +1812,149 @@
 ing 
-is an attempt to create a MavenProject%0A        // This fails because the Context is not properly initialised%0A        // I am committing this to record how far this attempt got%0A        ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();%0A        Context context = new DefaultContex
+4 lines are simply to get a MavenProject object%0A        MavenExecutionRequest executionRequest = new DefaultMavenExecutionReques
 t();%0A
-%0A
         
 Mave
@@ -1945,29 +1945,24 @@
 ();%0A        
-Maven
 ProjectBuild
@@ -1965,44 +1965,57 @@
 uild
-er projectBuilder = new DefaultMaven
+ingRequest buildingRequest = executionRequest.get
 Proj
@@ -2022,18 +2022,26 @@
 ectBuild
-er
+ingRequest
 ();%0A    
@@ -2044,32 +2044,16 @@
         
-( ( DefaultMaven
 ProjectB
@@ -2050,34 +2050,32 @@
   ProjectBuilder
- )
  projectBuilder 
@@ -2078,104 +2078,43 @@
 der 
-).initialize();%0A        ( ( DefaultMavenProjectBuilder ) projectBuilder ).contextualize( context
+= this.lookup( ProjectBuilder.class
  );%0A
@@ -2180,36 +2180,37 @@
 om, 
-projectBuilderConfiguration 
+buildingRequest ).getProject(
 );%0A 

Revert "Attempt to convert this to be compatible with Maven 2 API."