Repository: maven Updated Branches: refs/heads/master 2d0ec9426 -> 3e133902f
Additional unittests for DefaultToolchainManagerPrivate Remove unnecessary null checks for Lists Improve javadoc Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/3e133902 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/3e133902 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/3e133902 Branch: refs/heads/master Commit: 3e133902fb5598568ffcf6ccc525748fb7d8eba7 Parents: 2d0ec94 Author: Robert Scholte <[email protected]> Authored: Sun Jan 11 22:02:14 2015 +0100 Committer: Robert Scholte <[email protected]> Committed: Sun Jan 11 22:02:14 2015 +0100 ---------------------------------------------------------------------- .../DefaultToolchainManagerPrivate.java | 10 +-- .../toolchain/ToolchainManagerPrivate.java | 6 +- .../building/ToolchainsBuildingException.java | 2 +- .../DefaultToolchainManagerPrivateTest.java | 87 ++++++++++++++++++-- .../org/apache/maven/toolchain/global.xml | 35 ++++++++ .../org/apache/maven/toolchain/user.xml | 35 ++++++++ 6 files changed, 158 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivate.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivate.java b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivate.java index e2eacea..aeb8294 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivate.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivate.java @@ -86,15 +86,11 @@ public class DefaultToolchainManagerPrivate } else { - List<ToolchainModel> lst = effectiveToolchains.getToolchains(); - if ( lst != null ) + for ( ToolchainModel toolchainModel : effectiveToolchains.getToolchains() ) { - for ( ToolchainModel toolchainModel : lst ) + if ( type.equals( toolchainModel.getType() ) ) { - if ( type.equals( toolchainModel.getType() ) ) - { - toRet.add( fact.createToolchain( toolchainModel ) ); - } + toRet.add( fact.createToolchain( toolchainModel ) ); } } http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/main/java/org/apache/maven/toolchain/ToolchainManagerPrivate.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/ToolchainManagerPrivate.java b/maven-core/src/main/java/org/apache/maven/toolchain/ToolchainManagerPrivate.java index 67131ac..407c8be 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/ToolchainManagerPrivate.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/ToolchainManagerPrivate.java @@ -33,7 +33,9 @@ public interface ToolchainManagerPrivate /** * Retrieves the toolchains of given type from the user settings. - * + * + * @param type the type, must not be {@code null} + * @param context the Maven session, must not be {@code null} * @since 3.0 (addition of the <code>MavenSession</code> parameter) */ ToolchainPrivate[] getToolchainsForType( String type, MavenSession context ) @@ -42,6 +44,8 @@ public interface ToolchainManagerPrivate /** * Stores the toolchain into build context. * + * @param toolchain the toolchain to store, must not be {@code null} + * @param context the Maven session, must not be {@code null} * @since 2.0.9 */ void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession context ); http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/main/java/org/apache/maven/toolchain/building/ToolchainsBuildingException.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/building/ToolchainsBuildingException.java b/maven-core/src/main/java/org/apache/maven/toolchain/building/ToolchainsBuildingException.java index f8108a3..b3ec9a3 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/building/ToolchainsBuildingException.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/building/ToolchainsBuildingException.java @@ -39,7 +39,7 @@ public class ToolchainsBuildingException /** * Creates a new exception with the specified problems. * - * @param problems The problems that causes this exception, may be {@code null}. + * @param problems The problems that causes this exception, must not be {@code null}. */ public ToolchainsBuildingException( List<Problem> problems ) { http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivateTest.java ---------------------------------------------------------------------- diff --git a/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivateTest.java b/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivateTest.java index 15bcc07..090eefb 100644 --- a/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivateTest.java +++ b/maven-core/src/test/java/org/apache/maven/toolchain/DefaultToolchainManagerPrivateTest.java @@ -20,6 +20,8 @@ package org.apache.maven.toolchain; */ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isA; import static org.mockito.Mockito.mock; @@ -27,12 +29,14 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; import java.util.HashMap; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; import org.apache.maven.toolchain.building.DefaultToolchainsBuildingResult; +import org.apache.maven.toolchain.building.ToolchainsBuildingException; import org.apache.maven.toolchain.building.ToolchainsBuildingRequest; import org.apache.maven.toolchain.building.ToolchainsBuildingResult; import org.apache.maven.toolchain.model.PersistedToolchains; @@ -40,6 +44,7 @@ import org.apache.maven.toolchain.model.ToolchainModel; import org.codehaus.plexus.logging.Logger; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -70,13 +75,7 @@ public class DefaultToolchainManagerPrivateTest MockitoAnnotations.initMocks( this ); toolchainManager.factories = new HashMap<String, ToolchainFactory>(); - - ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class ); - when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain ); toolchainManager.factories.put( "basic", toolchainFactory_basicType ); - - ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class ); - when( toolchainFactory_rareType.createDefaultToolchain() ).thenReturn( rareToolchain ); toolchainManager.factories.put( "rare", toolchainFactory_rareType ); } @@ -93,6 +92,8 @@ public class DefaultToolchainManagerPrivateTest when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult ); ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class ); when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain ); + ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class ); + when( toolchainFactory_rareType.createDefaultToolchain() ).thenReturn( rareToolchain ); // execute ToolchainPrivate[] toolchains = toolchainManager.getToolchainsForType( "basic", session ); @@ -113,6 +114,10 @@ public class DefaultToolchainManagerPrivateTest ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( new PersistedToolchains(), null ); when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult ); + ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class ); + when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain ); + ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class ); + when( toolchainFactory_rareType.createDefaultToolchain() ).thenReturn( rareToolchain ); // execute ToolchainPrivate[] toolchains = toolchainManager.getToolchainsForType( "unknown", session ); @@ -149,8 +154,74 @@ public class DefaultToolchainManagerPrivateTest // verify verify( logger, never() ).error( anyString() ); - // there's always a default in case the requirement doesn't match(?) - assertEquals( 3, toolchains.length ); + assertEquals( 2, toolchains.length ); + } + + @SuppressWarnings( "unchecked" ) + @Test( expected = MisconfiguredToolchainException.class ) + public void testMisconfiguredToolchain() + throws Exception + { + // prepare + MavenSession session = mock( MavenSession.class ); + MavenExecutionRequest req = new DefaultMavenExecutionRequest(); + when( session.getRequest() ).thenReturn( req ); + + when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenThrow( ToolchainsBuildingException.class ); + + // execute + toolchainManager.getToolchainsForType( "basic", session ); + + // verify + fail( "Should exit with a MisconfiguredToolchainException" ); + } + + @Test + public void testGlobalToolchainsFile() + throws Exception + { + // prepare + MavenSession session = mock( MavenSession.class ); + MavenExecutionRequest req = new DefaultMavenExecutionRequest(); + File globalFile = new File( "target/test-classes/org/apache/maven/toolchain/global.xml" ); + req.setGlobalToolchainsFile( globalFile ); + when( session.getRequest() ).thenReturn( req ); + + ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( new PersistedToolchains(), null ); + when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult ); + + // execute + toolchainManager.getToolchainsForType( "basic", session ); + + // verify + ArgumentCaptor<ToolchainsBuildingRequest> argument = ArgumentCaptor.forClass(ToolchainsBuildingRequest.class); + verify( toolchainsBuilder ).build( argument.capture() ); + assertNull( argument.getValue().getUserToolchainsSource() ); + assertEquals( globalFile.getAbsolutePath(), argument.getValue().getGlobalToolchainsSource().getLocation() ); + } + + @Test + public void testUserToolchainsFile() + throws Exception + { + // prepare + MavenSession session = mock( MavenSession.class ); + MavenExecutionRequest req = new DefaultMavenExecutionRequest(); + File userFile = new File( "target/test-classes/org/apache/maven/toolchain/user.xml" ); + req.setUserToolchainsFile( userFile ); + when( session.getRequest() ).thenReturn( req ); + + ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( new PersistedToolchains(), null ); + when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult ); + + // execute + toolchainManager.getToolchainsForType( "basic", session ); + + // verify + ArgumentCaptor<ToolchainsBuildingRequest> argument = ArgumentCaptor.forClass(ToolchainsBuildingRequest.class); + verify( toolchainsBuilder ).build( argument.capture() ); + assertNull( argument.getValue().getGlobalToolchainsSource() ); + assertEquals( userFile.getAbsolutePath(), argument.getValue().getUserToolchainsSource().getLocation() ); } } http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/test/resources/org/apache/maven/toolchain/global.xml ---------------------------------------------------------------------- diff --git a/maven-core/src/test/resources/org/apache/maven/toolchain/global.xml b/maven-core/src/test/resources/org/apache/maven/toolchain/global.xml new file mode 100644 index 0000000..cd9be74 --- /dev/null +++ b/maven-core/src/test/resources/org/apache/maven/toolchain/global.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF8"?> + +<!-- +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. +--> + +<toolchains> + <toolchain> + <type>basic</type> + <configuration> + <global>true</global> + </configuration> + </toolchain> + <toolchain> + <type>rare</type> + <configuration> + <global>true</global> + </configuration> + </toolchain> +</toolchains> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven/blob/3e133902/maven-core/src/test/resources/org/apache/maven/toolchain/user.xml ---------------------------------------------------------------------- diff --git a/maven-core/src/test/resources/org/apache/maven/toolchain/user.xml b/maven-core/src/test/resources/org/apache/maven/toolchain/user.xml new file mode 100644 index 0000000..d11181f --- /dev/null +++ b/maven-core/src/test/resources/org/apache/maven/toolchain/user.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF8"?> + +<!-- +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. +--> + +<toolchains> + <toolchain> + <type>basic</type> + <configuration> + <user>true</user> + </configuration> + </toolchain> + <toolchain> + <type>rare</type> + <configuration> + <user>true</user> + </configuration> + </toolchain> +</toolchains> \ No newline at end of file
