diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractEmbedderTestCase.java b/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractEmbedderTestCase.java index 8e7ab0baa6..518ef0a8bf 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractEmbedderTestCase.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractEmbedderTestCase.java @@ -46,10 +46,7 @@ public abstract class AbstractEmbedderTestCase { super.setUp(); - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - - Configuration configuration = new DefaultConfiguration().setClassLoader( classLoader ).setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); - configuration.setUserSettingsFile( MavenEmbedder.DEFAULT_USER_SETTINGS_FILE ); + Configuration configuration = new SimpleConfiguration(); maven = new MavenEmbedder( configuration ); diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java index 68dc056135..0a57461565 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java @@ -21,7 +21,6 @@ package org.apache.maven.embedder; import java.io.File; import java.util.Arrays; -import java.util.Iterator; import java.util.List; import junit.framework.TestCase; @@ -52,12 +51,7 @@ public class MavenEmbedderAligningBasedirTest basedir = new File( "." ).getCanonicalPath(); } - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - - Configuration configuration = new DefaultConfiguration() - .setClassLoader( classLoader ) - .setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); - configuration.setUserSettingsFile( MavenEmbedder.DEFAULT_USER_SETTINGS_FILE ); + Configuration configuration = new SimpleConfiguration(); mavenEmbedder = new MavenEmbedder( configuration ); } @@ -70,7 +64,7 @@ public class MavenEmbedderAligningBasedirTest protected void assertNoExceptions( MavenExecutionResult result ) { - List exceptions = result.getExceptions(); + List exceptions = result.getExceptions(); if ( ( exceptions == null ) || exceptions.isEmpty() ) { // everything is a-ok. @@ -78,10 +72,8 @@ public class MavenEmbedderAligningBasedirTest } System.err.println( "Encountered " + exceptions.size() + " exception(s)." ); - Iterator it = exceptions.iterator(); - while ( it.hasNext() ) + for ( Exception exception : exceptions ) { - Exception exception = (Exception) it.next(); exception.printStackTrace( System.err ); } diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderProjectWithExtensionReadingTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderProjectWithExtensionReadingTest.java index ceffb9f464..a65d5790b9 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderProjectWithExtensionReadingTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderProjectWithExtensionReadingTest.java @@ -81,9 +81,7 @@ public class MavenEmbedderProjectWithExtensionReadingTest public ExtendableMavenEmbedder( ClassLoader classLoader ) throws MavenEmbedderException { - super( new DefaultConfiguration() - .setClassLoader( classLoader ) - .setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ) ); + super( new SimpleConfiguration().setClassLoader( classLoader ) ); } protected Map getPluginExtensionComponents( Plugin plugin ) diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index fbd40f92c1..b3b4ca50a1 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -25,7 +25,6 @@ import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; import java.util.Set; @@ -71,12 +70,7 @@ public class MavenEmbedderTest basedir = new File( "." ).getCanonicalPath(); } - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - - Configuration configuration = new DefaultConfiguration() - .setClassLoader( classLoader ) - .setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); - configuration.setUserSettingsFile( MavenEmbedder.DEFAULT_USER_SETTINGS_FILE ); + Configuration configuration = new SimpleConfiguration(); mavenEmbedder = new MavenEmbedder( configuration ); @@ -92,7 +86,7 @@ public class MavenEmbedderTest protected void assertNoExceptions( MavenExecutionResult result ) { - List exceptions = result.getExceptions(); + List exceptions = result.getExceptions(); if ( ( exceptions == null ) || exceptions.isEmpty() ) { // everything is a-ok. @@ -100,10 +94,8 @@ public class MavenEmbedderTest } System.err.println( "Encountered " + exceptions.size() + " exception(s)." ); - Iterator it = exceptions.iterator(); - while ( it.hasNext() ) + for (Exception exception : exceptions) { - Exception exception = (Exception) it.next(); exception.printStackTrace( System.err ); } @@ -181,11 +173,10 @@ public class MavenEmbedderTest .setGoals( Arrays.asList( new String[]{"validate"} ) ); MavenExecutionResult result = mavenEmbedder.execute( request ); - List exceptions = result.getExceptions(); + List exceptions = result.getExceptions(); assertEquals("Incorrect number of exceptions", 1, exceptions.size()); - Iterator it = exceptions.iterator(); - if( (it.next() instanceof NullPointerException)) + if ( ( exceptions.get( 0 ) instanceof NullPointerException ) ) { fail("Null Pointer on Exception"); } @@ -306,7 +297,7 @@ public class MavenEmbedderTest MavenProject project = result.getProject(); - Artifact p = (Artifact) project.getPluginArtifactMap().get( plugin.getKey() ); + Artifact p = project.getPluginArtifactMap().get( plugin.getKey() ); assertEquals( "2.4.2", p.getVersion() ); /* Add the surefire plugin 2.3 to the pom */ @@ -324,7 +315,7 @@ public class MavenEmbedderTest project = result.getProject(); - p = (Artifact) project.getPluginArtifactMap().get( plugin.getKey() ); + p = project.getPluginArtifactMap().get( plugin.getKey() ); assertEquals( "2.4.3", p.getVersion() ); } @@ -365,7 +356,7 @@ public class MavenEmbedderTest assertEquals( "org.apache.maven", result.getProject().getGroupId() ); - Set artifacts = result.getProject().getArtifacts(); + Set artifacts = result.getProject().getArtifacts(); assertEquals( 1, artifacts.size() ); diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/SimpleConfiguration.java b/maven-embedder/src/test/java/org/apache/maven/embedder/SimpleConfiguration.java new file mode 100644 index 0000000000..2edaa88d78 --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/SimpleConfiguration.java @@ -0,0 +1,49 @@ +package org.apache.maven.embedder; + +/* + * 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; + +/** + * A configuration to be used for unit testing of the embedder. This basically sets some default values. + * + * @author Benjamin Bentmann + */ +public class SimpleConfiguration + extends DefaultConfiguration +{ + + public SimpleConfiguration() + { + String localRepo = System.getProperty( "maven.repo.local", "" ); + + if ( localRepo.length() > 0 ) + { + setLocalRepository( new File( localRepo ).getAbsoluteFile() ); + } + + setClassLoader( Thread.currentThread().getContextClassLoader() ); + + setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); + + setUserSettingsFile( MavenEmbedder.DEFAULT_USER_SETTINGS_FILE ); + } + +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java b/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java index 5b0f9db52e..a01e55fdf4 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java @@ -37,13 +37,7 @@ public class TestComponentOverride protected void setUp() throws Exception { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - - Configuration request = new DefaultConfiguration(); - - request.setClassLoader( loader ); - - request.setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); + Configuration request = new SimpleConfiguration(); File extensions = new File( getBasedir(), "src/test/extensions" ); diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/project/BadModuleNotRecursiveTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/project/BadModuleNotRecursiveTest.java index 8dcde5364f..ec560891d9 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/project/BadModuleNotRecursiveTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/project/BadModuleNotRecursiveTest.java @@ -1,14 +1,12 @@ package org.apache.maven.embedder.project; import java.io.File; -import java.util.Iterator; import junit.framework.TestCase; import org.apache.maven.embedder.Configuration; -import org.apache.maven.embedder.DefaultConfiguration; import org.apache.maven.embedder.MavenEmbedder; -import org.apache.maven.embedder.MavenEmbedderConsoleLogger; +import org.apache.maven.embedder.SimpleConfiguration; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.project.MavenProject; @@ -19,8 +17,7 @@ public class BadModuleNotRecursiveTest public void test() throws Exception { - Configuration configuration = new DefaultConfiguration(); - configuration.setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); + Configuration configuration = new SimpleConfiguration(); MavenEmbedder embedder = new MavenEmbedder( configuration ); File pom = new File( "src/test/projects/bad-module-non-recursive/pom.xml" ).getCanonicalFile(); @@ -39,10 +36,9 @@ public class BadModuleNotRecursiveTest if ( result.hasExceptions() ) { - for ( Iterator it = result.getExceptions().iterator(); it.hasNext(); ) + for ( Exception e : result.getExceptions() ) { - Exception ex = (Exception) it.next(); - ex.printStackTrace(); + e.printStackTrace(); } } 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 index f7e9d9006f..e62839464e 100644 --- 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 @@ -21,12 +21,11 @@ package org.apache.maven.embedder.validation; import java.io.File; import java.util.Arrays; -import java.util.Iterator; 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.embedder.SimpleConfiguration; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; @@ -45,9 +44,8 @@ public class MavenEmbedderCrappySettingsConfigurationTest File user = new File( projectDirectory, "invalid-settings.xml" ); - Configuration configuration = new DefaultConfiguration() - .setUserSettingsFile( user ) - .setClassLoader( Thread.currentThread().getContextClassLoader() ); + Configuration configuration = new SimpleConfiguration() + .setUserSettingsFile( user ); ConfigurationValidationResult validationResult = MavenEmbedder.validateConfiguration( configuration ); @@ -63,9 +61,8 @@ public class MavenEmbedderCrappySettingsConfigurationTest MavenExecutionResult result = embedder.execute( request ); - for ( Iterator i = result.getExceptions().iterator(); i.hasNext(); ) + for ( Exception e : result.getExceptions() ) { - Exception e = (Exception) i.next(); e.printStackTrace(); }