[MNG-5754] Toolchains should be read during initialization

DefaultToolchainManagerPrivate now uses toolchains from MavenExecutionRequest
This commit is contained in:
Robert Scholte 2015-01-17 17:24:00 +01:00
parent 4f05b59de9
commit b6ae8ef8ab
2 changed files with 19 additions and 112 deletions

View File

@ -19,20 +19,13 @@ package org.apache.maven.toolchain;
* under the License. * under the License.
*/ */
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.maven.building.FileSource;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
import org.apache.maven.toolchain.building.ToolchainsBuildingException;
import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.ToolchainModel; import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
/** /**
* @author mkleint * @author mkleint
@ -44,38 +37,10 @@ public class DefaultToolchainManagerPrivate
implements ToolchainManagerPrivate implements ToolchainManagerPrivate
{ {
@Requirement @Override
private org.apache.maven.toolchain.building.ToolchainsBuilder toolchainsBuilder;
public ToolchainPrivate[] getToolchainsForType( String type, MavenSession context ) public ToolchainPrivate[] getToolchainsForType( String type, MavenSession context )
throws MisconfiguredToolchainException throws MisconfiguredToolchainException
{ {
DefaultToolchainsBuildingRequest buildRequest = new DefaultToolchainsBuildingRequest();
File globalToolchainsFile = context.getRequest().getGlobalToolchainsFile();
if ( globalToolchainsFile != null && globalToolchainsFile.isFile() )
{
buildRequest.setGlobalToolchainsSource( new FileSource( globalToolchainsFile ) );
}
File userToolchainsFile = context.getRequest().getUserToolchainsFile();
if ( userToolchainsFile != null && userToolchainsFile.isFile() )
{
buildRequest.setUserToolchainsSource( new FileSource( userToolchainsFile ) );
}
ToolchainsBuildingResult buildResult;
try
{
buildResult = toolchainsBuilder.build( buildRequest );
}
catch ( ToolchainsBuildingException e )
{
throw new MisconfiguredToolchainException( e.getMessage(), e );
}
PersistedToolchains effectiveToolchains = buildResult.getEffectiveToolchains();
List<ToolchainPrivate> toRet = new ArrayList<ToolchainPrivate>(); List<ToolchainPrivate> toRet = new ArrayList<ToolchainPrivate>();
ToolchainFactory fact = factories.get( type ); ToolchainFactory fact = factories.get( type );
@ -86,9 +51,11 @@ public class DefaultToolchainManagerPrivate
} }
else else
{ {
for ( ToolchainModel toolchainModel : effectiveToolchains.getToolchains() ) List<ToolchainModel> availableToolchains = context.getRequest().getToolchains().get( type );
if ( availableToolchains != null )
{ {
if ( type.equals( toolchainModel.getType() ) ) for ( ToolchainModel toolchainModel : availableToolchains )
{ {
toRet.add( fact.createToolchain( toolchainModel ) ); toRet.add( fact.createToolchain( toolchainModel ) );
} }
@ -105,6 +72,7 @@ public class DefaultToolchainManagerPrivate
return toRet.toArray( new ToolchainPrivate[toRet.size()] ); return toRet.toArray( new ToolchainPrivate[toRet.size()] );
} }
@Override
public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session ) public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
{ {
Map<String, Object> context = retrieveContext( session ); Map<String, Object> context = retrieveContext( session );

View File

@ -20,31 +20,25 @@ package org.apache.maven.toolchain;
*/ */
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.File; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession; 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;
import org.apache.maven.toolchain.model.ToolchainModel; import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -52,9 +46,6 @@ import org.mockito.MockitoAnnotations;
public class DefaultToolchainManagerPrivateTest public class DefaultToolchainManagerPrivateTest
{ {
// Mocks to inject into toolchainManager // Mocks to inject into toolchainManager
@Mock
private org.apache.maven.toolchain.building.ToolchainsBuilder toolchainsBuilder;
@Mock @Mock
private Logger logger; private Logger logger;
@ -88,8 +79,6 @@ public class DefaultToolchainManagerPrivateTest
MavenExecutionRequest req = new DefaultMavenExecutionRequest(); MavenExecutionRequest req = new DefaultMavenExecutionRequest();
when( session.getRequest() ).thenReturn( req ); when( session.getRequest() ).thenReturn( req );
ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( new PersistedToolchains(), null );
when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult );
ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class ); ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class );
when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain ); when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain );
ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class ); ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class );
@ -112,8 +101,6 @@ public class DefaultToolchainManagerPrivateTest
MavenExecutionRequest req = new DefaultMavenExecutionRequest(); MavenExecutionRequest req = new DefaultMavenExecutionRequest();
when( session.getRequest() ).thenReturn( req ); when( session.getRequest() ).thenReturn( req );
ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( new PersistedToolchains(), null );
when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult );
ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class ); ToolchainPrivate basicToolchain = mock( ToolchainPrivate.class );
when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain ); when( toolchainFactory_basicType.createDefaultToolchain() ).thenReturn( basicToolchain );
ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class ); ToolchainPrivate rareToolchain = mock( ToolchainPrivate.class );
@ -135,19 +122,21 @@ public class DefaultToolchainManagerPrivateTest
MavenSession session = mock( MavenSession.class ); MavenSession session = mock( MavenSession.class );
MavenExecutionRequest req = new DefaultMavenExecutionRequest(); MavenExecutionRequest req = new DefaultMavenExecutionRequest();
when( session.getRequest() ).thenReturn( req ); when( session.getRequest() ).thenReturn( req );
Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>();
req.setToolchains( groupedToolchains );
PersistedToolchains effectiveToolchains = new PersistedToolchains(); List<ToolchainModel> basicToolchains = new ArrayList<ToolchainModel>();
ToolchainModel basicToolchainModel = new ToolchainModel(); ToolchainModel basicToolchainModel = new ToolchainModel();
basicToolchainModel.setType( "basic" ); basicToolchainModel.setType( "basic" );
effectiveToolchains.addToolchain( basicToolchainModel ); basicToolchains.add( basicToolchainModel );
effectiveToolchains.addToolchain( basicToolchainModel ); basicToolchains.add( basicToolchainModel );
groupedToolchains.put( "basic", basicToolchains );
List<ToolchainModel> rareToolchains = new ArrayList<ToolchainModel>();
ToolchainModel rareToolchainModel = new ToolchainModel(); ToolchainModel rareToolchainModel = new ToolchainModel();
rareToolchainModel.setType( "rare" ); rareToolchainModel.setType( "rare" );
effectiveToolchains.addToolchain( rareToolchainModel ); rareToolchains.add( rareToolchainModel );
groupedToolchains.put( "rare", rareToolchains );
ToolchainsBuildingResult toolchainsResult = new DefaultToolchainsBuildingResult( effectiveToolchains, null );
when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenReturn( toolchainsResult );
// execute // execute
ToolchainPrivate[] toolchains = toolchainManager.getToolchainsForType( "basic", session ); ToolchainPrivate[] toolchains = toolchainManager.getToolchainsForType( "basic", session );
@ -166,62 +155,12 @@ public class DefaultToolchainManagerPrivateTest
MavenSession session = mock( MavenSession.class ); MavenSession session = mock( MavenSession.class );
MavenExecutionRequest req = new DefaultMavenExecutionRequest(); MavenExecutionRequest req = new DefaultMavenExecutionRequest();
when( session.getRequest() ).thenReturn( req ); when( session.getRequest() ).thenReturn( req );
when(toolchainFactory_basicType.createDefaultToolchain()).thenThrow( MisconfiguredToolchainException.class );
when( toolchainsBuilder.build( isA( ToolchainsBuildingRequest.class ) ) ).thenThrow( ToolchainsBuildingException.class );
// execute // execute
toolchainManager.getToolchainsForType( "basic", session ); toolchainManager.getToolchainsForType( "basic", session );
// verify // verify
fail( "Should exit with a MisconfiguredToolchainException" ); 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() );
}
} }