diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java new file mode 100644 index 0000000000..57912605b4 --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java @@ -0,0 +1,133 @@ +package org.apache.maven.embedder.execution; + +/* + * 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 org.apache.maven.embedder.AbstractEmbedderTestCase; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +public abstract class AbstractEmbedderExecutionTestCase + extends AbstractEmbedderTestCase +{ + protected File runWithProject( String goal ) + throws Exception + { + return runWithProject( Collections.singletonList( goal ), null ); + } + + protected File runWithProject( String goal, + Properties properties ) + throws Exception + { + return runWithProject( Collections.singletonList( goal ), properties ); + } + + protected File runWithProject( String[] goals ) + throws Exception + { + return runWithProject( Arrays.asList( goals ), null ); + } + + protected File runWithProject( String[] goals, + Properties properties ) + throws Exception + { + return runWithProject( Arrays.asList( goals ), properties ); + } + + protected File runWithProject( List goals ) + throws Exception + { + return runWithProject( goals, null ); + } + + protected File runWithProject( List goals, + Properties properties ) + throws Exception + { + /* + if ( request.getBaseDirectory() == null || !new File( request.getBaseDirectory() ).exists() ) + { + throw new IllegalStateException( "You must specify a valid base directory in your execution request for this test." ); + } + */ + + File testDirectory = new File( getBasedir(), "src/test/embedder-test-project" ); + + File targetDirectory = new File( getBasedir(), "target/" + getId() ); + + FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest() + .setShowErrors( true ) + .setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_DEBUG ) + .setBaseDirectory( targetDirectory ) + .setGoals( goals ); + + System.out.println( "properties = " + properties ); + + if ( properties != null ) + { + request.setProperties( properties ); + } + + MavenExecutionResult result = maven.execute( request ); + + assertNoExceptions( result ); + + return targetDirectory; + } + + protected abstract String getId(); + + protected void assertNoExceptions( MavenExecutionResult result ) + { + if ( !result.hasExceptions() ) + { + return; + } + + for ( Iterator i = result.getExceptions().iterator(); i.hasNext(); ) + { + Exception exception = (Exception) i.next(); + + exception.printStackTrace( System.err ); + } + + fail( "Encountered Exceptions in MavenExecutionResult during " + getName() ); + } + + protected void assertFileExists( File file ) + { + if ( !file.exists() ) + { + fail( "The specified file '" + file + "' does not exist." ); + } + } +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingEclipsePluginTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingEclipsePluginTest.java new file mode 100644 index 0000000000..59ec2f6e13 --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingEclipsePluginTest.java @@ -0,0 +1,42 @@ +package org.apache.maven.embedder.execution; + +/* + * 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.io.File; + +/** @author Jason van Zyl */ +public class EmbedderUsingEclipsePluginTest + extends AbstractEmbedderExecutionTestCase +{ + protected String getId() + { + return "eclipse-from-embedder"; + } + + public void testRunningEclipsePlugin() + throws Exception + { + File basedir = runWithProject( "eclipse:eclipse" ); + + assertFileExists( new File( basedir, ".classpath" ) ); + + assertFileExists( new File( basedir, ".project" ) ); + } +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingScmPluginTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingScmPluginTest.java new file mode 100644 index 0000000000..1e4084ea4c --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/EmbedderUsingScmPluginTest.java @@ -0,0 +1,47 @@ +package org.apache.maven.embedder.execution; + +/* + * 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.io.File; +import java.util.Properties; + +/** @author Jason van Zyl */ +public class EmbedderUsingScmPluginTest + extends AbstractEmbedderExecutionTestCase +{ + protected String getId() + { + return "scm-plugin-from-embedder"; + } + + public void testRunningScmPlugin() + throws Exception + { + Properties p = new Properties(); + + File outputDirectory = new File( getBasedir(), "target/scm.diff" ); + + p.setProperty( "outputDirectory", outputDirectory.getCanonicalPath() ); + + p.setProperty( "connectionUrl", "scm:svn:http://svn.apache.org/repos/asf/maven/components/trunk/maven-embedder" ); + + File basedir = runWithProject( "scm:diff", p ); + } +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java new file mode 100644 index 0000000000..5a135068aa --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java @@ -0,0 +1,75 @@ +package org.apache.maven.embedder.validation; + +/* + * 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 org.apache.maven.embedder.Configuration; +import org.apache.maven.embedder.ConfigurationValidationResult; +import org.apache.maven.embedder.DefaultConfiguration; +import org.apache.maven.embedder.MavenEmbedder; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.PlexusTestCase; + +import java.io.File; +import java.util.Arrays; + +public class MavenEmbedderCrappySettingsConfigurationTest + extends PlexusTestCase +{ + public void testEmbedderWillStillStartupWhenTheSettingsConfigurationIsCrap() + throws Exception + { + // START SNIPPET: simple-embedder-example + + File projectDirectory = new File( getBasedir(), "src/examples/simple-project" ); + + File user = new File( projectDirectory, "invalid-settings.xml" ); + + Configuration configuration = new DefaultConfiguration() + .setUserSettingsFile( user ) + .setClassLoader( Thread.currentThread().getContextClassLoader() ); + + ConfigurationValidationResult validationResult = MavenEmbedder.validateConfiguration( configuration ); + + assertFalse( validationResult.isValid() ); + + MavenEmbedder embedder = new MavenEmbedder( configuration ); + + assertNotNull( embedder.getLocalRepository().getBasedir() ); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest() + .setBaseDirectory( projectDirectory ) + .setGoals( Arrays.asList( new String[]{"clean", "install"} ) ); + + MavenExecutionResult result = embedder.execute( request ); + + assertNotNull( result.getProject() ); + + MavenProject project = result.getProject(); + + String environment = project.getProperties().getProperty( "environment" ); + + assertEquals( "development", environment ); + + // END SNIPPET: simple-embedder-example + } +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/validation/ValidateConfigurationTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/ValidateConfigurationTest.java new file mode 100644 index 0000000000..ea46bb41aa --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/ValidateConfigurationTest.java @@ -0,0 +1,104 @@ +package org.apache.maven.embedder.validation; + +/* + * 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 org.apache.maven.embedder.Configuration; +import org.apache.maven.embedder.ConfigurationValidationResult; +import org.apache.maven.embedder.DefaultConfiguration; +import org.apache.maven.embedder.MavenEmbedder; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; + +/** @author Jason van Zyl */ +public class ValidateConfigurationTest + extends PlexusTestCase +{ + public void testConfigurationOnlyUserSettingsAreActiveAndItIsValid() + { + File user = new File( getBasedir(), "src/test/resources/settings/valid-settings.xml" ); + + Configuration configuration = new DefaultConfiguration() + .setUserSettingsFile( user ); + + ConfigurationValidationResult result = MavenEmbedder.validateConfiguration( configuration ); + + assertTrue( result.isUserSettingsFilePresent() ); + assertTrue( result.isUserSettingsFileParses() ); + assertNotNull( result.getUserSettings() ); + assertNull( result.getUserSettingsException() ); + assertNull( result.getGlobalSettings() ); + assertNull( result.getGlobalSettingsException() ); + } + + public void testConfigurationOnlyUserSettingsAreActiveAndItIsInvalid() + { + File user = new File( getBasedir(), "src/test/resources/settings/invalid-settings.xml" ); + + Configuration configuration = new DefaultConfiguration() + .setUserSettingsFile( user ); + + ConfigurationValidationResult result = MavenEmbedder.validateConfiguration( configuration ); + + assertTrue( result.isUserSettingsFilePresent() ); + assertFalse( result.isUserSettingsFileParses() ); + assertNull( result.getUserSettings() ); + assertNotNull( result.getUserSettingsException() ); + assertTrue( result.getUserSettingsException() instanceof XmlPullParserException ); + assertNull( result.getGlobalSettings() ); + assertNull( result.getGlobalSettingsException() ); + } + + public void testConfigurationOnlyGlobalSettingsAreActiveAndItIsValid() + { + File global = new File( getBasedir(), "src/test/resources/settings/valid-settings.xml" ); + + Configuration configuration = new DefaultConfiguration() + .setGlobalSettingsFile( global ); + + ConfigurationValidationResult result = MavenEmbedder.validateConfiguration( configuration ); + + assertTrue( result.isGlobalSettingsFilePresent() ); + assertTrue( result.isGlobalSettingsFileParses() ); + assertNotNull( result.getGlobalSettings() ); + assertNull( result.getGlobalSettingsException() ); + assertNull( result.getUserSettings() ); + assertNull( result.getUserSettingsException() ); + } + + public void testConfigurationOnlyGlobalSettingsAreActiveAndItIsInvalid() + { + File global = new File( getBasedir(), "src/test/resources/settings/invalid-settings.xml" ); + + Configuration configuration = new DefaultConfiguration() + .setGlobalSettingsFile( global ); + + ConfigurationValidationResult result = MavenEmbedder.validateConfiguration( configuration ); + + assertTrue( result.isGlobalSettingsFilePresent() ); + assertFalse( result.isGlobalSettingsFileParses() ); + assertNull( result.getGlobalSettings() ); + assertNotNull( result.getGlobalSettingsException() ); + assertTrue( result.getGlobalSettingsException() instanceof XmlPullParserException ); + assertNull( result.getUserSettings() ); + assertNull( result.getUserSettingsException() ); + } +}