From 4bd0aad1dcfb26d278d8e298a9074ecb9e3fe5de Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sat, 23 May 2009 19:05:00 +0000 Subject: [PATCH] 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 --- .../manager/DefaultProfileManagerTest.java | 2 +- .../profiles/matchers/JdkMatcherTest.java | 26 --- .../project/AbstractMavenProjectTestCase.java | 6 - maven-model-builder/pom.xml | 5 + .../maven/profiles/DefaultProfileManager.java | 79 ++----- .../profiles/ProfileActivationContext.java | 12 ++ .../profiles/ProfileActivationException.java | 1 + .../apache/maven/profiles/ProfileManager.java | 2 +- .../maven/profiles/ProfileManagerInfo.java | 34 --- .../profiles/matchers/DefaultMatcher.java | 37 ---- .../maven/profiles/matchers/FileMatcher.java | 33 --- .../maven/profiles/matchers/JdkMatcher.java | 193 ------------------ .../profiles/matchers/ProfileMatcher.java | 29 --- .../profiles/matchers/PropertyMatcher.java | 62 ------ .../AbstractProfileActivatorTest.java | 98 +++++++++ .../JdkVersionProfileActivatorTest.java | 113 ++++++++++ 16 files changed, 251 insertions(+), 481 deletions(-) delete mode 100644 maven-core/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/DefaultMatcher.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/FileMatcher.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/ProfileMatcher.java delete mode 100644 maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/PropertyMatcher.java create mode 100644 maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/AbstractProfileActivatorTest.java create mode 100644 maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/JdkVersionProfileActivatorTest.java diff --git a/maven-core/src/test/java/org/apache/maven/profiles/manager/DefaultProfileManagerTest.java b/maven-core/src/test/java/org/apache/maven/profiles/manager/DefaultProfileManagerTest.java index 2e969455a9..8bf309c26d 100644 --- a/maven-core/src/test/java/org/apache/maven/profiles/manager/DefaultProfileManagerTest.java +++ b/maven-core/src/test/java/org/apache/maven/profiles/manager/DefaultProfileManagerTest.java @@ -61,7 +61,7 @@ public void testShouldActivateDefaultProfile() defaultActivated.setActivation( defaultActivation ); - Properties props = new Properties(); + Properties props = System.getProperties(); ProfileActivationContext ctx = new ProfileActivationContext( props, false ); ProfileManager profileManager = new DefaultProfileManager( ctx ); diff --git a/maven-core/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java b/maven-core/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java deleted file mode 100644 index e2d8418b2c..0000000000 --- a/maven-core/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java +++ /dev/null @@ -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 )); - } -} diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index 7374b65756..88b4bff7b8 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -21,13 +21,10 @@ 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 @@ protected MavenProject getProjectWithDependencies( File pom ) protected MavenProject getProject( File pom ) throws Exception { - Properties props = System.getProperties(); - ProfileActivationContext ctx = new ProfileActivationContext( props, false ); - ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration(); configuration.setLocalRepository( getLocalRepository() ); diff --git a/maven-model-builder/pom.xml b/maven-model-builder/pom.xml index 8bee5136b5..ed8ea4e4d5 100644 --- a/maven-model-builder/pom.xml +++ b/maven-model-builder/pom.xml @@ -43,6 +43,11 @@ 4.4 test + + org.codehaus.plexus + plexus-container-default + test + diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java index f6356cb5b4..59de9599bd 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java +++ b/maven-model-builder/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java @@ -21,22 +21,20 @@ 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 { @@ -44,11 +42,10 @@ public class DefaultProfileManager private Map profilesById = new LinkedHashMap(); private ProfileActivationContext profileActivationContext; - - private static final ProfileMatcher defaultMatcher = new DefaultMatcher(); - private static final List matchers = - Collections.unmodifiableList( Arrays.asList( new PropertyMatcher(), new FileMatcher(), new JdkMatcher() ) ); + private static final List activators = + Arrays.asList( new PropertyProfileActivator(), new OperatingSystemProfileActivator(), + new FileProfileActivator(), new JdkVersionProfileActivator() ); /** * the properties passed to the profile manager are the props that @@ -174,50 +171,6 @@ public List getActiveProfiles( Model model ) } return allActive; } - - public static Collection getActiveProfiles(List profiles, ProfileManagerInfo profileContextInfo) - { - Properties properties = profileContextInfo.getInterpolatorProperties(); - Collection activeProfileIds = profileContextInfo.getActiveProfileIds(); - Collection inactiveProfileIds = profileContextInfo.getInactiveProfileIds(); - - List matchedProfiles = new ArrayList(); - List defaultProfiles = new ArrayList(); - 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,11 +199,18 @@ private static List getDefaultProfiles(List profiles) private boolean isActive( Profile profile, ProfileActivationContext context ) throws ProfileActivationException { - for(ProfileMatcher matcher : matchers) + for ( ProfileActivator activator : activators ) { - if(matcher.isMatch(profile, context.getExecutionProperties())) + try { - return true; + 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 @@ private void activateAsDefault( String profileId ) profileActivationContext.setActiveByDefault( profileId ); } } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java index 15336f155b..4f2a2434cc 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java +++ b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java @@ -25,7 +25,9 @@ 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 void setActiveByDefaultProfileIds( List activeByDefault ) this.activeByDefault = activeByDefault; } + public List getActiveProfileIds() + { + return getExplicitlyActiveProfileIds(); + } + + public List getInactiveProfileIds() + { + return getExplicitlyInactiveProfileIds(); + } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationException.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationException.java index 56efbdcef4..7af4286716 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationException.java +++ b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileActivationException.java @@ -19,6 +19,7 @@ * under the License. */ +@Deprecated public class ProfileActivationException extends Exception { diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManager.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManager.java index 4c43597dad..1d50f5a058 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManager.java +++ b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManager.java @@ -21,6 +21,7 @@ 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 getProfilesById(); - @Deprecated List getActiveProfiles( Model model ) throws ProfileActivationException; diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java deleted file mode 100644 index a62d778360..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/ProfileManagerInfo.java +++ /dev/null @@ -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 activeProfileIds; - - private Collection inactiveProfileIds; - - public ProfileManagerInfo(Properties interpolatorProperties, Collection activeProfileIds, Collection inactiveProfileIds) - { - this.interpolatorProperties = (interpolatorProperties != null) ? interpolatorProperties : new Properties(); - this.activeProfileIds = (activeProfileIds != null) ? activeProfileIds : new ArrayList(); - this.inactiveProfileIds = (inactiveProfileIds != null) ? inactiveProfileIds : new ArrayList(); - } - - public Properties getInterpolatorProperties() { - return interpolatorProperties; - } - - public Collection getActiveProfileIds() { - return activeProfileIds; - } - - public Collection getInactiveProfileIds() { - return inactiveProfileIds; - } -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/DefaultMatcher.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/DefaultMatcher.java deleted file mode 100644 index 9f78fa43d9..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/DefaultMatcher.java +++ /dev/null @@ -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(); - } - -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/FileMatcher.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/FileMatcher.java deleted file mode 100644 index c3fc5e4b8a..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/FileMatcher.java +++ /dev/null @@ -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; - } -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java deleted file mode 100644 index 8d450312b9..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java +++ /dev/null @@ -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 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 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 valueTokens = new ArrayList( Arrays.asList( value.split( "\\." ) ) ); - List rangeValueTokens = new ArrayList( 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 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 getRange( String range ) - { - List ranges = new ArrayList(); - - 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; - } - } -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/ProfileMatcher.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/ProfileMatcher.java deleted file mode 100644 index c1d3af1b9c..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/ProfileMatcher.java +++ /dev/null @@ -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 ); -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/PropertyMatcher.java b/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/PropertyMatcher.java deleted file mode 100644 index ba6fd3be36..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/profiles/matchers/PropertyMatcher.java +++ /dev/null @@ -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 ip : properties.entrySet() ) - { - if(ip.getKey().equals( name )) - { - return ((String) ip.getValue()).equals(value); - } - } - - return false; - } -} diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/AbstractProfileActivatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/AbstractProfileActivatorTest.java new file mode 100644 index 0000000000..6a3dd3155b --- /dev/null +++ b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/AbstractProfileActivatorTest.java @@ -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 + extends PlexusTestCase +{ + + private Class activatorClass; + + private String roleHint; + + protected T activator; + + public AbstractProfileActivatorTest( Class 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 getActiveProfileIds() + { + return Collections.emptyList(); + } + + public List getInactiveProfileIds() + { + return Collections.emptyList(); + } + + public Properties getExecutionProperties() + { + return executionProperties; + } + + }; + } + +} diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/JdkVersionProfileActivatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/JdkVersionProfileActivatorTest.java new file mode 100644 index 0000000000..68b4c847e3 --- /dev/null +++ b/maven-model-builder/src/test/java/org/apache/maven/model/profile/activator/JdkVersionProfileActivatorTest.java @@ -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 +{ + + 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" ) ) ) ); + } + +}