o Deleted old profile matches

git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@777989 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-05-23 19:05:00 +00:00
parent ff7d13487d
commit 4bd0aad1dc
16 changed files with 251 additions and 481 deletions

View File

@ -61,7 +61,7 @@ public class DefaultProfileManagerTest
defaultActivated.setActivation( defaultActivation );
Properties props = new Properties();
Properties props = System.getProperties();
ProfileActivationContext ctx = new ProfileActivationContext( props, false );
ProfileManager profileManager = new DefaultProfileManager( ctx );

View File

@ -1,26 +0,0 @@
package org.apache.maven.profiles.matchers;
import java.util.Properties;
import junit.framework.TestCase;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
public class JdkMatcherTest extends TestCase
{
public void testJdkMatch()
throws Exception
{
Profile p = new Profile();
Activation a = new Activation();
a.setJdk("(1.3,100)");
p.setActivation(a);
JdkMatcher m = new JdkMatcher();
Properties props = new Properties();
props.setProperty("java.version", "1.5.0_16");
assertTrue(m.isMatch(p, props ));
}
}

View File

@ -21,13 +21,10 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Properties;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileActivationContext;
import org.apache.maven.project.validation.ModelValidationResult;
import org.codehaus.plexus.PlexusTestCase;
@ -146,9 +143,6 @@ public abstract class AbstractMavenProjectTestCase
protected MavenProject getProject( File pom )
throws Exception
{
Properties props = System.getProperties();
ProfileActivationContext ctx = new ProfileActivationContext( props, false );
ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
configuration.setLocalRepository( getLocalRepository() );

View File

@ -43,6 +43,11 @@
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -21,22 +21,20 @@ package org.apache.maven.profiles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.profiles.matchers.DefaultMatcher;
import org.apache.maven.profiles.matchers.FileMatcher;
import org.apache.maven.profiles.matchers.JdkMatcher;
import org.apache.maven.profiles.matchers.ProfileMatcher;
import org.apache.maven.profiles.matchers.PropertyMatcher;
import org.apache.maven.model.profile.activator.FileProfileActivator;
import org.apache.maven.model.profile.activator.JdkVersionProfileActivator;
import org.apache.maven.model.profile.activator.OperatingSystemProfileActivator;
import org.apache.maven.model.profile.activator.ProfileActivator;
import org.apache.maven.model.profile.activator.PropertyProfileActivator;
@Deprecated
public class DefaultProfileManager
implements ProfileManager
{
@ -45,10 +43,9 @@ public class DefaultProfileManager
private ProfileActivationContext profileActivationContext;
private static final ProfileMatcher defaultMatcher = new DefaultMatcher();
private static final List<ProfileMatcher> matchers =
Collections.unmodifiableList( Arrays.asList( new PropertyMatcher(), new FileMatcher(), new JdkMatcher() ) );
private static final List<ProfileActivator> activators =
Arrays.asList( new PropertyProfileActivator(), new OperatingSystemProfileActivator(),
new FileProfileActivator(), new JdkVersionProfileActivator() );
/**
* the properties passed to the profile manager are the props that
@ -175,50 +172,6 @@ public class DefaultProfileManager
return allActive;
}
public static Collection<Profile> getActiveProfiles(List<Profile> profiles, ProfileManagerInfo profileContextInfo)
{
Properties properties = profileContextInfo.getInterpolatorProperties();
Collection<String> activeProfileIds = profileContextInfo.getActiveProfileIds();
Collection<String> inactiveProfileIds = profileContextInfo.getInactiveProfileIds();
List<Profile> matchedProfiles = new ArrayList<Profile>();
List<Profile> defaultProfiles = new ArrayList<Profile>();
for ( Profile profile : profiles )
{
String profileId = profile.getId();
if ( !inactiveProfileIds.contains( profileId ) )
{
if ( activeProfileIds.contains( profileId ) )
{
matchedProfiles.add( profile );
}
else if ( defaultMatcher.isMatch( profile, properties ) )
{
defaultProfiles.add( profile );
}
else
{
for ( ProfileMatcher matcher : matchers )
{
if ( matcher.isMatch( profile, properties ) )
{
matchedProfiles.add( profile );
break;
}
}
}
}
}
if ( matchedProfiles.isEmpty() )
{
matchedProfiles = defaultProfiles;
}
return matchedProfiles;
}
/* (non-Javadoc)
* @see org.apache.maven.project.ProfileManager#addProfiles(java.util.List)
*/
@ -246,13 +199,20 @@ public class DefaultProfileManager
private boolean isActive( Profile profile, ProfileActivationContext context )
throws ProfileActivationException
{
for(ProfileMatcher matcher : matchers)
for ( ProfileActivator activator : activators )
{
if(matcher.isMatch(profile, context.getExecutionProperties()))
try
{
if ( activator.isActive( profile, context ) )
{
return true;
}
}
catch ( org.apache.maven.model.profile.ProfileActivationException e )
{
throw new ProfileActivationException( e.getMessage(), e.getCause() );
}
}
return false;
}
@ -265,4 +225,5 @@ public class DefaultProfileManager
profileActivationContext.setActiveByDefault( profileId );
}
}
}

View File

@ -25,7 +25,9 @@ import java.util.Collections;
import java.util.List;
import java.util.Properties;
@Deprecated
public class ProfileActivationContext
implements org.apache.maven.model.profile.ProfileActivationContext
{
private boolean isCustomActivatorFailureSuppressed;
@ -148,4 +150,14 @@ public class ProfileActivationContext
this.activeByDefault = activeByDefault;
}
public List<String> getActiveProfileIds()
{
return getExplicitlyActiveProfileIds();
}
public List<String> getInactiveProfileIds()
{
return getExplicitlyInactiveProfileIds();
}
}

View File

@ -19,6 +19,7 @@ package org.apache.maven.profiles;
* under the License.
*/
@Deprecated
public class ProfileActivationException
extends Exception
{

View File

@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
@Deprecated
public interface ProfileManager
{
void addProfile( Profile profile );
@ -33,7 +34,6 @@ public interface ProfileManager
Map<String, Profile> getProfilesById();
@Deprecated
List<Profile> getActiveProfiles( Model model )
throws ProfileActivationException;

View File

@ -1,34 +0,0 @@
package org.apache.maven.profiles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
public class ProfileManagerInfo
{
private Properties interpolatorProperties;
private Collection<String> activeProfileIds;
private Collection<String> inactiveProfileIds;
public ProfileManagerInfo(Properties interpolatorProperties, Collection<String> activeProfileIds, Collection<String> inactiveProfileIds)
{
this.interpolatorProperties = (interpolatorProperties != null) ? interpolatorProperties : new Properties();
this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList<String>();
this.inactiveProfileIds = (inactiveProfileIds != null) ? inactiveProfileIds : new ArrayList<String>();
}
public Properties getInterpolatorProperties() {
return interpolatorProperties;
}
public Collection<String> getActiveProfileIds() {
return activeProfileIds;
}
public Collection<String> getInactiveProfileIds() {
return inactiveProfileIds;
}
}

View File

@ -1,37 +0,0 @@
package org.apache.maven.profiles.matchers;
/*
* 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.Properties;
import org.apache.maven.model.Profile;
public class DefaultMatcher implements ProfileMatcher
{
public boolean isMatch( Profile profile, Properties properties )
{
if(profile.getActivation() == null)
{
return false;
}
return profile.getActivation().isActiveByDefault();
}
}

View File

@ -1,33 +0,0 @@
package org.apache.maven.profiles.matchers;
import java.io.File;
import java.util.Properties;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Profile;
public class FileMatcher implements ProfileMatcher {
public boolean isMatch(Profile profile, Properties properties) {
if (profile == null) {
throw new IllegalArgumentException("profile: null");
}
if(profile.getActivation() == null || profile.getActivation().getFile() == null)
{
return false;
}
ActivationFile f = profile.getActivation().getFile();
if (f.getExists() != null && !new File(f.getExists()).exists()) {
return false;
}
if (f.getMissing() != null && new File(f.getMissing()).exists()) {
return false;
}
return true;
}
}

View File

@ -1,193 +0,0 @@
package org.apache.maven.profiles.matchers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;
import org.apache.maven.model.Profile;
public class JdkMatcher
implements ProfileMatcher
{
private static final String JDK_VERSION = "java.version";
public boolean isMatch(Profile profile,
Properties properties) {
String version = null;
for ( Entry<Object, Object> ip : properties.entrySet() )
{
if(ip.getKey().equals(JDK_VERSION))
{
version = (String) ip.getValue();
break;
}
}
if ( version == null )
{
return false;
}
org.apache.maven.model.Activation activation = profile.getActivation();
if(activation == null || activation.getJdk() == null)
{
return false;
}
String jdk = activation.getJdk();
if ( jdk.startsWith( "!" ) )
{
return !version.startsWith( jdk.replaceFirst( "!", "" ) );
}
else if ( isRange( jdk ) )
{
return isInRange( version, getRange( jdk ) );
}
else
{
return version.startsWith( jdk );
}
}
private static boolean isInRange( String value, List<RangeValue> range )
{
int leftRelation = getRelationOrder( value, range.get( 0 ), true );
if ( leftRelation == 0 )
{
return true;
}
if ( leftRelation < 0 )
{
return false;
}
return getRelationOrder( value, range.get( 1 ), false ) <= 0;
}
private static int getRelationOrder( String value, RangeValue rangeValue, boolean isLeft )
{
if ( rangeValue.value.length() <= 0 )
{
return isLeft ? 1 : -1;
}
List<String> valueTokens = new ArrayList<String>( Arrays.asList( value.split( "\\." ) ) );
List<String> rangeValueTokens = new ArrayList<String>( Arrays.asList( rangeValue.value.split( "\\." ) ) );
int max = Math.max( valueTokens.size(), rangeValueTokens.size() );
addZeroTokens( valueTokens, max );
addZeroTokens( rangeValueTokens, max );
if ( value.equals( rangeValue.value ) )
{
if ( !rangeValue.isClosed() )
{
return isLeft ? -1 : 1;
}
return 0;
}
for ( int i = 0; i < valueTokens.size(); i++ )
{
int x = Integer.parseInt( valueTokens.get( i ) );
int y = Integer.parseInt( rangeValueTokens.get( i ) );
if ( x < y )
{
return -1;
}
else if ( x > y )
{
return 1;
}
}
if ( !rangeValue.isClosed() )
{
return isLeft ? -1 : 1;
}
return 0;
}
private static void addZeroTokens( List<String> tokens, int max )
{
if ( tokens.size() < max )
{
for ( int i = 0; i < ( max - tokens.size() ); i++ )
{
tokens.add( "0" );
}
}
}
private static boolean isRange( String value )
{
return value.contains( "," );
}
private static List<RangeValue> getRange( String range )
{
List<RangeValue> ranges = new ArrayList<RangeValue>();
for ( String token : range.split( "," ) )
{
if ( token.startsWith( "[" ) )
{
ranges.add( new RangeValue( token.replace( "[", "" ), true ) );
}
else if ( token.startsWith( "(" ) )
{
ranges.add( new RangeValue( token.replace( "(", "" ), false ) );
}
else if ( token.endsWith( "]" ) )
{
ranges.add( new RangeValue( token.replace( "]", "" ), true ) );
}
else if ( token.endsWith( ")" ) )
{
ranges.add( new RangeValue( token.replace( ")", "" ), false ) );
}
else if ( token.length() <= 0 )
{
ranges.add( new RangeValue( "", false ) );
}
}
if ( ranges.size() < 2 )
{
ranges.add( new RangeValue( "99999999", false ) );
}
return ranges;
}
private static class RangeValue
{
private String value;
private boolean isClosed;
RangeValue( String value, boolean isClosed )
{
this.value = value.trim();
this.isClosed = isClosed;
}
public String getValue()
{
return value;
}
public boolean isClosed()
{
return isClosed;
}
public String toString()
{
return value;
}
}
}

View File

@ -1,29 +0,0 @@
package org.apache.maven.profiles.matchers;
/*
* 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.Properties;
import org.apache.maven.model.Profile;
public interface ProfileMatcher
{
boolean isMatch( Profile profile, Properties properties );
}

View File

@ -1,62 +0,0 @@
package org.apache.maven.profiles.matchers;
/*
* 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.Properties;
import java.util.Map.Entry;
import org.apache.maven.model.Profile;
public class PropertyMatcher implements ProfileMatcher
{
public boolean isMatch( Profile profile, Properties properties )
{
if (profile == null) {
throw new IllegalArgumentException("profile: null");
}
if(profile.getActivation() == null || profile.getActivation().getProperty() == null)
{
return false;
}
String value = profile.getActivation().getProperty().getValue();
String name = profile.getActivation().getProperty().getName();
if(name == null )
{
return false;
}
if(value == null)
{
return !name.startsWith("!");
}
for ( Entry<Object, Object> ip : properties.entrySet() )
{
if(ip.getKey().equals( name ))
{
return ((String) ip.getValue()).equals(value);
}
}
return false;
}
}

View File

@ -0,0 +1,98 @@
package org.apache.maven.model.profile.activator;
/*
* 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.Collections;
import java.util.List;
import java.util.Properties;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.component.annotations.Component;
/**
* Provides common services to test {@link ProfileActivator} implementations.
*
* @author Benjamin Bentmann
*/
public abstract class AbstractProfileActivatorTest<T extends ProfileActivator>
extends PlexusTestCase
{
private Class<T> activatorClass;
private String roleHint;
protected T activator;
public AbstractProfileActivatorTest( Class<T> activatorClass )
{
if ( activatorClass == null )
{
throw new IllegalArgumentException( "class of profile activator to test is not specified" );
}
this.activatorClass = activatorClass;
roleHint = activatorClass.getAnnotation( Component.class ).hint();
}
@Override
protected void setUp()
throws Exception
{
super.setUp();
activator = activatorClass.cast( lookup( ProfileActivator.class, roleHint ) );
}
@Override
protected void tearDown()
throws Exception
{
activator = null;
super.tearDown();
}
protected ProfileActivationContext newContext( final Properties executionProperties )
{
return new ProfileActivationContext()
{
public List<String> getActiveProfileIds()
{
return Collections.emptyList();
}
public List<String> getInactiveProfileIds()
{
return Collections.emptyList();
}
public Properties getExecutionProperties()
{
return executionProperties;
}
};
}
}

View File

@ -0,0 +1,113 @@
package org.apache.maven.model.profile.activator;
/*
* 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.Properties;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
/**
* Tests {@link JdkVersionProfileActivator}.
*
* @author Benjamin Bentmann
*/
public class JdkVersionProfileActivatorTest
extends AbstractProfileActivatorTest<JdkVersionProfileActivator>
{
public JdkVersionProfileActivatorTest()
{
super( JdkVersionProfileActivator.class );
}
private Profile newProfile( String jdkVersion )
{
Activation a = new Activation();
a.setJdk( jdkVersion );
Profile p = new Profile();
p.setActivation( a );
return p;
}
private Properties newProperties( String javaVersion )
{
Properties props = new Properties();
props.setProperty( "java.version", javaVersion );
return props;
}
public void testNullSafe()
throws Exception
{
Profile p = new Profile();
assertFalse( activator.isActive( p, newContext( new Properties() ) ) );
p.setActivation( new Activation() );
assertFalse( activator.isActive( p, newContext( new Properties() ) ) );
}
public void testPrefix()
throws Exception
{
Profile profile = newProfile( "1.4" );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.4" ) ) ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.4.2" ) ) ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.3" ) ) ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.5" ) ) ) );
}
public void testPrefixNegated()
throws Exception
{
Profile profile = newProfile( "!1.4" );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.4" ) ) ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.4.2" ) ) ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.3" ) ) ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.5" ) ) ) );
}
public void testVersionRange()
throws Exception
{
Profile profile = newProfile( "(1.3,1.6)" );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.5.0_16" ) ) ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.3" ) ) ) );
assertTrue( activator.isActive( profile, newContext( newProperties( "1.3.1" ) ) ) );
assertFalse( activator.isActive( profile, newContext( newProperties( "1.6" ) ) ) );
}
}