o Refactored profile activators to use problem collector

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@810452 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-09-02 11:12:43 +00:00
parent 102f4ab603
commit 326e14b853
12 changed files with 149 additions and 195 deletions

View File

@ -94,17 +94,9 @@ public class DefaultProfileSelector
{
for ( ProfileActivator activator : activators )
{
try
if ( activator.isActive( profile, context, problems ) )
{
if ( activator.isActive( profile, context ) )
{
return true;
}
}
catch ( ProfileActivationException e )
{
problems.addError( "Invalid activation condition for profile " + profile.getId() + ": "
+ e.getMessage() );
return true;
}
}
return false;

View File

@ -1,73 +0,0 @@
package org.apache.maven.model.profile;
/*
* 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.model.Profile;
/**
* Signals an error in determining the activation status of a profile (e.g. bad syntax of activation condition).
*
* @author Benjamin Bentmann
*/
public class ProfileActivationException
extends Exception
{
/**
* The profile which raised this error, can be {@code null}.
*/
private final Profile profile;
/**
* Creates a new exception with specified detail message and cause for the given profile.
*
* @param message The detail message, may be {@code null}.
* @param profile The profile that caused the error, may be {@code null}.
* @param cause The cause, may be {@code null}.
*/
public ProfileActivationException( String message, Profile profile, Throwable cause )
{
super( message, cause );
this.profile = profile;
}
/**
* Creates a new exception with specified detail message for the given profile.
*
* @param message The detail message, may be {@code null}.
* @param profile The profile that caused the error, may be {@code null}.
*/
public ProfileActivationException( String message, Profile profile )
{
super( message );
this.profile = profile;
}
/**
* Gets the profile that caused this error (if any).
*
* @return The profile that caused this error or {@code null} if not applicable.
*/
public Profile getProfile()
{
return profile;
}
}

View File

@ -24,9 +24,9 @@ import java.io.File;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.path.PathTranslator;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.interpolation.AbstractValueSource;
@ -47,8 +47,7 @@ public class FileProfileActivator
@Requirement
private PathTranslator pathTranslator;
public boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException
public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
{
Activation activation = profile.getActivation();
@ -119,8 +118,9 @@ public class FileProfileActivator
}
catch ( Exception e )
{
throw new ProfileActivationException( "Failed to interpolate file location " + path + " for profile "
+ profile.getId() + ": " + e.getMessage(), profile, e );
problems.addError( "Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
+ e.getMessage(), e );
return false;
}
path = pathTranslator.alignToBaseDirectory( path, basedir );

View File

@ -25,8 +25,8 @@ import java.util.List;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationException;
import org.codehaus.plexus.component.annotations.Component;
/**
@ -39,8 +39,7 @@ public class JdkVersionProfileActivator
implements ProfileActivator
{
public boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException
public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
{
boolean active = false;
@ -56,8 +55,8 @@ public class JdkVersionProfileActivator
if ( version.length() <= 0 )
{
throw new ProfileActivationException( "Failed to determine Java version for profile "
+ profile.getId(), profile );
problems.addError( "Failed to determine Java version for profile " + profile.getId() );
return false;
}
if ( jdk.startsWith( "!" ) )

View File

@ -22,8 +22,8 @@ package org.apache.maven.model.profile.activation;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationOS;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.Os;
@ -37,8 +37,7 @@ public class OperatingSystemProfileActivator
implements ProfileActivator
{
public boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException
public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
{
boolean active = false;

View File

@ -20,8 +20,8 @@ package org.apache.maven.model.profile.activation;
*/
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationException;
/**
* Determines whether a profile should be activated.
@ -37,11 +37,10 @@ public interface ProfileActivator
* @param profile The profile whose activation status should be determined, must not be {@code null}.
* @param context The environmental context used to determine the activation status of the profile, must not be
* {@code null}.
* @param problems The container used to collect problems (e.g. bad syntax) that were encountered, must not be
* {@code null}.
* @return {@code true} if the profile is active, {@code false} otherwise.
* @throws ProfileActivationException If the activation status of the profile could not be determined (e.g. due to
* missing values or bad syntax).
*/
boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException;
boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems );
}

View File

@ -22,8 +22,8 @@ package org.apache.maven.model.profile.activation;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationProperty;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.StringUtils;
@ -37,8 +37,7 @@ public class PropertyProfileActivator
implements ProfileActivator
{
public boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException
public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
{
boolean active = false;
@ -53,18 +52,18 @@ public class PropertyProfileActivator
String name = property.getName();
boolean reverseName = false;
if ( name == null )
{
throw new ProfileActivationException( "The property name is required to activate the profile "
+ profile.getId(), profile );
}
if ( name.startsWith( "!" ) )
if ( name != null && name.startsWith( "!" ) )
{
reverseName = true;
name = name.substring( 1 );
}
if ( name == null || name.length() <= 0 )
{
problems.addError( "The property name is required to activate the profile " + profile.getId() );
return false;
}
String sysValue = context.getUserProperties().getProperty( name );
if ( sysValue == null )
{

View File

@ -0,0 +1,68 @@
package org.apache.maven.model.building;
/*
* 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.util.ArrayList;
import java.util.List;
/**
* A simple model problem collector for testing the model building components.
*
* @author Benjamin Bentmann
*/
public class SimpleProblemCollector
implements ModelProblemCollector
{
private List<String> warnings = new ArrayList<String>();
private List<String> errors = new ArrayList<String>();
public void addError( String message )
{
errors.add( message );
}
public void addError( String message, Exception cause )
{
addError( message );
}
public void addWarning( String message )
{
warnings.add( message );
}
public void addWarning( String message, Exception cause )
{
addWarning( message );
}
public List<String> getWarnings()
{
return warnings;
}
public List<String> getErrors()
{
return errors;
}
}

View File

@ -21,6 +21,8 @@ package org.apache.maven.model.profile.activation;
import java.util.Properties;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.SimpleProblemCollector;
import org.apache.maven.model.profile.DefaultProfileActivationContext;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.PlexusTestCase;
@ -77,4 +79,14 @@ public abstract class AbstractProfileActivatorTest<T extends ProfileActivator>
return context.setUserProperties( userProperties ).setSystemProperties( systemProperties );
}
protected void assertActivation( boolean active, Profile profile, ProfileActivationContext context )
{
SimpleProblemCollector problems = new SimpleProblemCollector();
assertEquals( active, activator.isActive( profile, context, problems ) );
assertEquals( problems.getErrors().toString(), 0, problems.getErrors().size() );
assertEquals( problems.getWarnings().toString(), 0, problems.getWarnings().size() );
}
}

View File

@ -61,11 +61,11 @@ public class JdkVersionProfileActivatorTest
{
Profile p = new Profile();
assertFalse( activator.isActive( p, newContext( null, null ) ) );
assertActivation( false, p, newContext( null, null ) );
p.setActivation( new Activation() );
assertFalse( activator.isActive( p, newContext( null, null ) ) );
assertActivation( false, p, newContext( null, null ) );
}
public void testPrefix()
@ -73,13 +73,13 @@ public class JdkVersionProfileActivatorTest
{
Profile profile = newProfile( "1.4" );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.4" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.4" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.4.2" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.4.2" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.3" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.3" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.5" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.5" ) ) );
}
public void testPrefixNegated()
@ -87,13 +87,13 @@ public class JdkVersionProfileActivatorTest
{
Profile profile = newProfile( "!1.4" );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.4" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.4" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.4.2" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.4.2" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.3" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.3" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.5" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.5" ) ) );
}
public void testVersionRange()
@ -101,13 +101,13 @@ public class JdkVersionProfileActivatorTest
{
Profile profile = newProfile( "(1.3,1.6)" );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.5.0_16" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.5.0_16" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.3" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.3" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "1.3.1" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "1.3.1" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "1.6" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "1.6" ) ) );
}
}

View File

@ -66,11 +66,11 @@ public class PropertyProfileActivatorTest
{
Profile p = new Profile();
assertFalse( activator.isActive( p, newContext( null, null ) ) );
assertActivation( false, p, newContext( null, null ) );
p.setActivation( new Activation() );
assertFalse( activator.isActive( p, newContext( null, null ) ) );
assertActivation( false, p, newContext( null, null ) );
}
public void testWithNameOnly_UserProperty()
@ -78,11 +78,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", null );
assertTrue( activator.isActive( profile, newContext( newProperties( "prop", "value" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "prop", "value" ), null ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "prop", "" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "prop", "" ), null ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "other", "value" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "other", "value" ), null ) );
}
public void testWithNameOnly_SystemProperty()
@ -90,11 +90,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", null );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "prop", "value" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "prop", "value" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "prop", "" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "prop", "" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "other", "value" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "other", "value" ) ) );
}
public void testWithNegatedNameOnly_UserProperty()
@ -102,11 +102,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "!prop", null );
assertFalse( activator.isActive( profile, newContext( newProperties( "prop", "value" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "prop", "value" ), null ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "prop", "" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "prop", "" ), null ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "other", "value" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "other", "value" ), null ) );
}
public void testWithNegatedNameOnly_SystemProperty()
@ -114,11 +114,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "!prop", null );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "prop", "value" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "prop", "value" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "prop", "" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "prop", "" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "other", "value" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "other", "value" ) ) );
}
public void testWithValue_UserProperty()
@ -126,11 +126,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", "value" );
assertTrue( activator.isActive( profile, newContext( newProperties( "prop", "value" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "prop", "value" ), null ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "prop", "other" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "prop", "other" ), null ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "prop", "" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "prop", "" ), null ) );
}
public void testWithValue_SystemProperty()
@ -138,11 +138,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", "value" );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "prop", "value" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "prop", "value" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "prop", "other" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "prop", "other" ) ) );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "other", "" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "other", "" ) ) );
}
public void testWithNegatedValue_UserProperty()
@ -150,11 +150,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", "!value" );
assertFalse( activator.isActive( profile, newContext( newProperties( "prop", "value" ), null ) ) );
assertActivation( false, profile, newContext( newProperties( "prop", "value" ), null ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "prop", "other" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "prop", "other" ), null ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "prop", "" ), null ) ) );
assertActivation( true, profile, newContext( newProperties( "prop", "" ), null ) );
}
public void testWithNegatedValue_SystemProperty()
@ -162,11 +162,11 @@ public class PropertyProfileActivatorTest
{
Profile profile = newProfile( "prop", "!value" );
assertFalse( activator.isActive( profile, newContext( null, newProperties( "prop", "value" ) ) ) );
assertActivation( false, profile, newContext( null, newProperties( "prop", "value" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "prop", "other" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "prop", "other" ) ) );
assertTrue( activator.isActive( profile, newContext( null, newProperties( "other", "" ) ) ) );
assertActivation( true, profile, newContext( null, newProperties( "other", "" ) ) );
}
public void testWithValue_UserPropertyDominantOverSystemProperty()
@ -177,9 +177,9 @@ public class PropertyProfileActivatorTest
Properties props1 = newProperties( "prop", "value" );
Properties props2 = newProperties( "prop", "other" );
assertTrue( activator.isActive( profile, newContext( props1, props2 ) ) );
assertActivation( true, profile, newContext( props1, props2 ) );
assertFalse( activator.isActive( profile, newContext( props2, props1 ) ) );
assertActivation( false, profile, newContext( props2, props1 ) );
}
}

View File

@ -20,13 +20,12 @@ package org.apache.maven.model.validation;
*/
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.SimpleProblemCollector;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.PlexusTestCase;
@ -40,46 +39,6 @@ public class DefaultModelValidatorTest
private DefaultModelValidator validator;
private static class SimpleProblemCollector
implements ModelProblemCollector
{
private List<String> warnings = new ArrayList<String>();
private List<String> errors = new ArrayList<String>();
public void addError( String message )
{
errors.add( message );
}
public void addError( String message, Exception cause )
{
addError( message );
}
public void addWarning( String message )
{
warnings.add( message );
}
public void addWarning( String message, Exception cause )
{
addWarning( message );
}
public List<String> getWarnings()
{
return warnings;
}
public List<String> getErrors()
{
return errors;
}
}
private Model read( String pom )
throws Exception
{