[MNG-5755] Access toolchains without maven-toolchain-plugin

Added List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements ) to ToolchainManager
This commit is contained in:
Robert Scholte 2015-01-18 16:53:56 +01:00
parent b6ae8ef8ab
commit 4b263106db
4 changed files with 225 additions and 17 deletions

View File

@ -19,7 +19,10 @@ package org.apache.maven.toolchain;
* under the License.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
@ -42,7 +45,8 @@ public class DefaultToolchainManager
@Requirement( role = ToolchainFactory.class )
Map<String, ToolchainFactory> factories;
@Override
public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
{
Map<String, Object> context = retrieveContext( session );
@ -51,28 +55,60 @@ public class DefaultToolchainManager
if ( model != null )
{
try
List<Toolchain> toolchains = selectToolchains( Collections.singletonList( model ), type, null );
if ( !toolchains.isEmpty() )
{
ToolchainFactory fact = factories.get( type );
if ( fact != null )
{
return fact.createToolchain( model );
}
else
{
logger.error( "Missing toolchain factory for type: " + type
+ ". Possibly caused by misconfigured project." );
}
}
catch ( MisconfiguredToolchainException ex )
{
logger.error( "Misconfigured toolchain.", ex );
return toolchains.get( 0 );
}
}
return null;
}
@Override
public List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements )
{
List<ToolchainModel> models = session.getRequest().getToolchains().get( type );
return selectToolchains( models, type, requirements );
}
private List<Toolchain> selectToolchains( List<ToolchainModel> models, String type, Map<String, String> requirements )
{
List<Toolchain> toolchains = new ArrayList<Toolchain>();
if ( models != null )
{
ToolchainFactory fact = factories.get( type );
if ( fact == null )
{
logger.error( "Missing toolchain factory for type: " + type
+ ". Possibly caused by misconfigured project." );
}
else
{
for ( ToolchainModel model : models )
{
try
{
ToolchainPrivate toolchain = fact.createToolchain( model );
if ( requirements == null || toolchain.matchesRequirements( requirements ) )
{
toolchains.add( toolchain );
}
}
catch ( MisconfiguredToolchainException ex )
{
logger.error( "Misconfigured toolchain.", ex );
}
}
}
}
return toolchains;
}
Map<String, Object> retrieveContext( MavenSession session )
{
Map<String, Object> context = null;

View File

@ -19,6 +19,9 @@ package org.apache.maven.toolchain;
* under the License.
*/
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
@ -26,6 +29,7 @@ import org.apache.maven.execution.MavenSession;
* Public API for a toolchain-aware plugin to get expected toolchain instance.
*
* @author mkleint
* @author Robert Scholte
* @since 2.0.9
*/
public interface ToolchainManager
@ -39,4 +43,15 @@ public interface ToolchainManager
* to be used from plugins capable of working with toolchains.
*/
Toolchain getToolchainFromBuildContext( String type, MavenSession context );
/**
* Select all toolchains matching the type and requirements
*
* @param session the maven session, must not be {@code null}
* @param type the type, must not be {@code null}
* @param requirements the requirements, may be {@code null}
* @return the matching toolchains, never {@code null}
* @since 3.2.6
*/
List<Toolchain> getToolchains( MavenSession session, String type, Map<String, String> requirements );
}

View File

@ -34,11 +34,15 @@ public interface ToolchainPrivate
/**
* Let the toolchain decide if it matches requirements defined
* in the toolchain plugin configuration.
* @param requirements Map<String, String> key value pair
* @param requirements Map<String, String> key value pair, may not be {@code null}
* @return {@code true} if the requirements match, otherwise {@code false}
*/
boolean matchesRequirements( Map<String, String> requirements );
/**
*
* @return the original model wrapped by this interface
*/
ToolchainModel getModel();
}

View File

@ -0,0 +1,153 @@
package org.apache.maven.toolchain;
/*
* 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 static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.logging.Logger;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class DefaultToolchainManagerTest
{
// Mocks to inject into toolchainManager
@Mock
private Logger logger;
@InjectMocks
private DefaultToolchainManager toolchainManager;
@Mock
private ToolchainFactory toolchainFactory_basicType;
@Mock
private ToolchainFactory toolchainFactory_rareType;
@Before
public void onSetup() throws Exception
{
toolchainManager = new DefaultToolchainManager();
MockitoAnnotations.initMocks( this );
toolchainManager.factories = new HashMap<String, ToolchainFactory>();
toolchainManager.factories.put( "basic", toolchainFactory_basicType );
toolchainManager.factories.put( "rare", toolchainFactory_rareType );
}
@Test
public void testNoModels()
{
MavenSession session = mock( MavenSession.class );
MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
when( session.getRequest() ).thenReturn( executionRequest );
List<Toolchain> toolchains = toolchainManager.getToolchains( session, "unknown", null );
assertEquals( 0, toolchains.size() );
}
@Test
public void testModelNoFactory()
{
MavenSession session = mock( MavenSession.class );
MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
Map<String, List<ToolchainModel>> toolchainModels = new HashMap<String, List<ToolchainModel>>();
toolchainModels.put( "unknown", Collections.singletonList( new ToolchainModel() ) );
executionRequest.setToolchains( toolchainModels );
when( session.getRequest() ).thenReturn( executionRequest );
List<Toolchain> toolchains = toolchainManager.getToolchains( session, "unknown", null );
assertEquals( 0, toolchains.size() );
verify( logger ).error( "Missing toolchain factory for type: unknown. Possibly caused by misconfigured project." );
}
@Test
public void testModelAndFactory()
{
MavenSession session = mock( MavenSession.class );
MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
Map<String, List<ToolchainModel>> toolchainModels = new HashMap<String, List<ToolchainModel>>();
toolchainModels.put( "basic", Arrays.asList( new ToolchainModel(), new ToolchainModel() ) );
toolchainModels.put( "rare", Collections.singletonList( new ToolchainModel() ) );
executionRequest.setToolchains( toolchainModels );
when( session.getRequest() ).thenReturn( executionRequest );
List<Toolchain> toolchains = toolchainManager.getToolchains( session, "rare", null );
assertEquals( 1, toolchains.size() );
}
@Test
public void testModelsAndFactory()
{
MavenSession session = mock( MavenSession.class );
MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
Map<String, List<ToolchainModel>> toolchainModels = new HashMap<String, List<ToolchainModel>>();
toolchainModels.put( "basic", Arrays.asList( new ToolchainModel(), new ToolchainModel() ) );
toolchainModels.put( "rare", Collections.singletonList( new ToolchainModel() ) );
executionRequest.setToolchains( toolchainModels );
when( session.getRequest() ).thenReturn( executionRequest );
List<Toolchain> toolchains = toolchainManager.getToolchains( session, "basic", null );
assertEquals( 2, toolchains.size() );
}
@Test
public void testRequirements()
throws Exception
{
MavenSession session = mock( MavenSession.class );
MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
Map<String, List<ToolchainModel>> toolchainModels = new HashMap<String, List<ToolchainModel>>();
toolchainModels.put( "basic", Arrays.asList( new ToolchainModel(), new ToolchainModel() ) );
toolchainModels.put( "rare", Collections.singletonList( new ToolchainModel() ) );
executionRequest.setToolchains( toolchainModels );
when( session.getRequest() ).thenReturn( executionRequest );
ToolchainPrivate basicPrivate = mock( ToolchainPrivate.class );
when( basicPrivate.matchesRequirements( anyMap() ) ).thenReturn( false ).thenReturn( true );
when( toolchainFactory_basicType.createToolchain( isA( ToolchainModel.class ) ) ).thenReturn( basicPrivate );
List<Toolchain> toolchains =
toolchainManager.getToolchains( session, "basic", Collections.singletonMap( "key", "value" ) );
assertEquals( 1, toolchains.size() );
}
}