package org.apache.maven.settings;

import java.io.File;
import java.io.IOException;
import java.io.Reader;

import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileActivationContext;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.project.DefaultMavenProjectBuilder;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.project.harness.PomTestWrapper;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

public class PomConstructionWithSettingsTest     
	extends PlexusTestCase
{
    private static String BASE_DIR = "src/test";

    private static String BASE_POM_DIR = BASE_DIR + "/resources-settings";

    private DefaultMavenProjectBuilder mavenProjectBuilder;

    private File testDirectory;

    protected void setUp()
        throws Exception
    {
        testDirectory = new File( getBasedir(), BASE_POM_DIR );
        mavenProjectBuilder = (DefaultMavenProjectBuilder) lookup( MavenProjectBuilder.class );
    }
    
    public void testA() throws Exception
    {
    	PomTestWrapper pom = buildPom( "settings-no-pom" );
    	System.out.println(pom.getDomainModel().asString());
    }

    private PomTestWrapper buildPom( String pomPath )
    throws Exception
	{
	    File pomFile = new File( testDirectory + File.separator + pomPath , "pom.xml" );
	    File settingsFile = new File( testDirectory + File.separator + pomPath, "settings.xml" );
	    
	    Settings settings = readSettingsFile(settingsFile);
	    
	    ProfileActivationContext pCtx = new ProfileActivationContext(null, true);
	    ProfileManager profileManager = new DefaultProfileManager(getContainer(), pCtx);
	    
	    for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
	    {
	        Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
	
	        profileManager.addProfile( profile );
	    }    
	    
	    ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration();
	    config.setLocalRepository(new DefaultArtifactRepository("default", "", new DefaultRepositoryLayout()));
	
	    config.setGlobalProfileManager(profileManager);
	    return new PomTestWrapper( pomFile, mavenProjectBuilder.build( pomFile, config ) );
	}  
    
    private static Settings readSettingsFile(File settingsFile) 
    	throws IOException, XmlPullParserException
    {
        Settings settings = null;

        Reader reader = null;

        try
        {
            reader = ReaderFactory.newXmlReader( settingsFile );

            SettingsXpp3Reader modelReader = new SettingsXpp3Reader();

            settings = modelReader.read( reader );
        }
        finally
        {
            IOUtil.close( reader );
        }

        return settings;    	
    }
}

@@ -100,16 +100,39 @@
 .Reader;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1575,17 +1575,29 @@
 oid test
-A
+SettingsNoPom
 () throw
@@ -1680,58 +1680,98 @@
    %09
-System.out.println(pom.getDomainModel().asString()
+assertEquals( %22local-profile-prop-value%22, pom.getValue( %22properties/local-profile-prop%22 ) 
 );%0A 
@@ -2503,24 +2503,354 @@
 ;%0A%09    %7D    
+%0A%0A        List%3CString%3E settingsActiveProfileIds = settings.getActiveProfiles();%0A%0A        if ( settingsActiveProfileIds != null )%0A        %7B%0A            for ( String profileId : settingsActiveProfileIds )%0A            %7B%0A                profileManager.getProfileActivationContext().setActive( profileId );%0A            %7D%0A        %7D%09    
 %0A%09    %0A%09    

Test for setting profile properties.