From 023a96155bef4114fbc8e7aae10ecb9ef244368d Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Wed, 4 Mar 2009 18:50:28 +0000 Subject: [PATCH] Support for jdk activation ranges. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@750099 13f79535-47bb-0310-9956-ffa450edef68 --- .../project/builder/profile/JdkMatcher.java | 154 ++++++++++++++++-- .../builder/profile/JdkMatcherTest.java | 111 +++++++++++++ 2 files changed, 250 insertions(+), 15 deletions(-) diff --git a/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java b/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java index 8886372c8c..a04b186d23 100644 --- a/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java +++ b/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java @@ -5,46 +5,170 @@ import org.apache.maven.shared.model.InterpolatorProperty; import org.apache.maven.shared.model.ModelProperty; import org.apache.maven.project.builder.ProjectUri; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -public class JdkMatcher implements ActiveProfileMatcher { - //TODO: Ranges - public boolean isMatch(ModelContainer modelContainer, List properties) { - if(modelContainer == null ) +public class JdkMatcher + implements ActiveProfileMatcher +{ + // TODO: Ranges + public boolean isMatch( ModelContainer modelContainer, List properties ) + { + if ( modelContainer == null ) { - throw new IllegalArgumentException("modelContainer: null"); + throw new IllegalArgumentException( "modelContainer: null" ); } - if(properties == null) + if ( properties == null ) { return false; } - for(InterpolatorProperty property : properties) + for ( InterpolatorProperty property : properties ) { - if(property.getKey().equals("${java.specification.version}")) + if ( property.getKey().equals( "${java.specification.version}" ) ) { String version = property.getValue(); - for(ModelProperty modelProperty : modelContainer.getProperties()) + for ( ModelProperty modelProperty : modelContainer.getProperties() ) { - if(modelProperty.getUri().equals(ProjectUri.Profiles.Profile.Activation.jdk)) + if ( modelProperty.getUri().equals( ProjectUri.Profiles.Profile.Activation.jdk ) ) { - if(modelProperty.getValue().startsWith("!")) + if ( modelProperty.getResolvedValue().startsWith( "!" ) ) { - return !version.equals(modelProperty.getValue().replaceFirst("!", "")); + return !version.equals( modelProperty.getResolvedValue().replaceFirst( "!", "" ) ); + } + else if ( isRange( modelProperty.getResolvedValue()) ) + { + return isInRange( version, getRange( modelProperty.getResolvedValue())); } else { - return version.equals(modelProperty.getValue()); + return version.equals( modelProperty.getResolvedValue() ); } } } - return false; + return false; } } return false; } -} + 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) + { + List valueTokens = Arrays.asList(value.split( "." )); + List rangeValueTokens = Arrays.asList(rangeValue.value.split( "." )); + + int max = Math.max( valueTokens.size(), rangeValueTokens.size() ); + addZeroTokens(valueTokens, max); + addZeroTokens(rangeValueTokens, max); + + if(value.equals( rangeValue.value ) ) + { + return (rangeValue.isClosed()) ? 0 : -1; + } + + for( int i = 0; i < valueTokens.size() ; i++) + { + int x = Integer.getInteger( valueTokens.get( i ) ); + int y = Integer.getInteger( rangeValueTokens.get( i ) ); + if( x < y) + { + return -1; + } + else if( x > y) + { + return 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 ) ); + } + + } + 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; + } + } +} diff --git a/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/JdkMatcherTest.java b/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/JdkMatcherTest.java index b1d4673bc3..9ad6c4c664 100644 --- a/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/JdkMatcherTest.java +++ b/maven-project-builder/src/test/java/org/apache/maven/project/builder/profile/JdkMatcherTest.java @@ -83,7 +83,118 @@ public class JdkMatcherTest { JdkMatcher matcher = new JdkMatcher(); assertFalse(matcher.isMatch(modelContainer, props)); } + + @org.junit.Test + public void jdkVersionRange_ClosedEdge() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , "[1.5,")); + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertTrue(matcher.isMatch(modelContainer, props)); + } + + @org.junit.Test + public void jdkVersionRange_OpenEdge() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , "(1.5,")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertFalse(matcher.isMatch(modelContainer, props)); + } + + @org.junit.Test + public void jdkVersionRange_OpenEdge2() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , "(1.4,")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertTrue(matcher.isMatch(modelContainer, props)); + } + + @org.junit.Test + public void jdkVersionRange_OpenEdgeWithPadding() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , "(1.5.0,")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertTrue(matcher.isMatch(modelContainer, props)); + } + @org.junit.Test + public void jdkVersionRange_OpenRightEdge() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , ", 1.6)")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertTrue(matcher.isMatch(modelContainer, props)); + } + + @org.junit.Test + public void jdkVersionRange_OpenRightEdge2() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , ",1.5)")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertFalse(matcher.isMatch(modelContainer, props)); + } + + @org.junit.Test + public void jdkVersionRange_OpenRightEdgeWithWhiteSpace() { + List modelProperties = new ArrayList(); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.xUri, null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.xUri , null)); + modelProperties.add(new ModelProperty(ProjectUri.Profiles.Profile.Activation.jdk , ", 1.5)")); + + ModelContainer modelContainer = new DefaultModelContainer(modelProperties); + + List props = new ArrayList(); + props.add(new InterpolatorProperty("${java.specification.version}" , "1.5")); + + JdkMatcher matcher = new JdkMatcher(); + assertFalse(matcher.isMatch(modelContainer, props)); + } + @org.junit.Test public void jdkVersionNotFound() { List modelProperties = new ArrayList();