Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 60415 invoked from network); 18 Mar 2010 08:41:02 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 Mar 2010 08:41:02 -0000 Received: (qmail 79621 invoked by uid 500); 18 Mar 2010 08:41:02 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 79555 invoked by uid 500); 18 Mar 2010 08:41:01 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 79540 invoked by uid 99); 18 Mar 2010 08:41:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Mar 2010 08:41:01 +0000 X-ASF-Spam-Status: No, hits=-1064.4 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Mar 2010 08:40:58 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id BC96C238897D; Thu, 18 Mar 2010 08:40:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r924669 [2/2] - in /felix/trunk/utils: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/felix/ src/main/java/org/apache/felix/utils/ src/main/java/org/apache/felix/utils/collections/ src... Date: Thu, 18 Mar 2010 08:40:38 -0000 To: commits@felix.apache.org From: gnodet@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100318084038.BC96C238897D@eris.apache.org> Added: felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionRange.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionRange.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionRange.java (added) +++ felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionRange.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,436 @@ +/* + * 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. + */ +package org.apache.felix.utils.version; + +import java.io.Serializable; + +import org.osgi.framework.Version; + +public class VersionRange implements Serializable +{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + public static final Version INFINITE_VERSION = new Version( Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, "" ); + public static final VersionRange ANY_VERSION = new VersionRange( false, Version.emptyVersion, INFINITE_VERSION, true ); + + public static final int EXACT = 0; + public static final int MICRO = 1; + public static final int MINOR = 2; + public static final int MAJOR = 3; + public static final int ANY = 40; + + private final boolean openFloor; + private final Version floor; + private final Version ceiling; + private final boolean openCeiling; + + + /** + * Interval constructor + * + * @param openFloor Whether the lower bound of the range is inclusive (false) or exclusive (true). + * @param floor The lower bound version of the range. + * @param ceiling The upper bound version of the range. + * @param openCeiling Whether the upper bound of the range is inclusive (false) or exclusive (true). + */ + public VersionRange( boolean openFloor, Version floor, Version ceiling, boolean openCeiling ) + { + this.openFloor = openFloor; + this.floor = floor; + this.ceiling = ceiling; + this.openCeiling = openCeiling; + checkRange(); + } + + + /** + * atLeast constructor + * + * @param atLeast + */ + public VersionRange( Version atLeast ) + { + this( atLeast, false ); + } + + /** + * atLeast constructor + * + * @param atLeast + */ + public VersionRange( Version atLeast, boolean exact ) + { + + this.openFloor = false; + this.floor = atLeast; + this.ceiling = exact ? atLeast : INFINITE_VERSION; + this.openCeiling = exact ? false : true; + checkRange(); + } + + + public VersionRange( String val ) throws IllegalArgumentException, NumberFormatException + { + this( val, false ); + } + + public VersionRange( String val, boolean exact ) throws IllegalArgumentException, NumberFormatException + { + this( val, exact, true ); + } + + public VersionRange( String val, boolean exact, boolean clean ) throws IllegalArgumentException, NumberFormatException + { + val = val.replaceAll( "\\s", "" ); + val = val.replaceAll( "\"", "" ); + int fst = val.charAt( 0 ); + if ( fst == '[' ) + { + openFloor = false; + } + else if ( fst == '(' ) + { + openFloor = true; + } + else + { + openFloor = false; + floor = VersionTable.getVersion( val, clean ); + ceiling = exact ? floor : INFINITE_VERSION; + openCeiling = exact ? false : true; + return; + } + + int lst = val.charAt( val.length() - 1 ); + if ( lst == ']' ) + { + openCeiling = false; + } + else if ( lst == ')' ) + { + openCeiling = true; + } + else + { + throw new IllegalArgumentException( "illegal version range syntax " + val + + ": range must end in ')' or ']'" ); + } + + String inner = val.substring( 1, val.length() - 1 ); + String[] floorCeiling = inner.split( "," ); + if ( floorCeiling.length != 2 ) + { + throw new IllegalArgumentException( "illegal version range syntax " + "too many commas" ); + } + floor = VersionTable.getVersion( floorCeiling[0], clean ); + ceiling = "*".equals( floorCeiling[1] ) ? INFINITE_VERSION : VersionTable.getVersion( floorCeiling[1], clean ); + checkRange(); + } + + public static VersionRange parseVersionRange( String val ) throws IllegalArgumentException, NumberFormatException + { + if ( val == null || val.trim().length() == 0 ) + { + return ANY_VERSION; + } + + return new VersionRange( val ); + } + + + public Version getCeiling() + { + return ceiling; + } + + + public Version getFloor() + { + return floor; + } + + + public boolean isOpenCeiling() + { + return openCeiling; + } + + + public boolean isOpenFloor() + { + return openFloor; + } + + + public boolean isPointVersion() + { + return !openFloor && !openCeiling && floor.equals( ceiling ); + } + + + /** + * test a version to see if it falls in the range + * + * @param version + * @return + */ + public boolean contains( Version version ) + { + if ( version.equals( INFINITE_VERSION ) ) + { + return ceiling.equals( INFINITE_VERSION ); + } + else + { + return ( version.compareTo( floor ) > 0 && version.compareTo( ceiling ) < 0 ) + || ( !openFloor && version.equals( floor ) ) || ( !openCeiling && version.equals( ceiling ) ); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.aries.application.impl.VersionRange#intersect(VersionRange + * range) + */ + + public VersionRange intersect(VersionRange r) + { + // Use the highest minimum version. + final Version newFloor; + final boolean newOpenFloor; + int minCompare = floor.compareTo(r.getFloor()); + if (minCompare > 0) + { + newFloor = floor; + newOpenFloor = openFloor; + } + else if (minCompare < 0) + { + newFloor = r.getFloor(); + newOpenFloor = r.isOpenFloor(); + } + else + { + newFloor = floor; + newOpenFloor = (openFloor || r.isOpenFloor()); + } + + // Use the lowest maximum version. + final Version newCeiling; + final boolean newOpenCeiling; + // null maximum version means unbounded, so the highest possible value. + int maxCompare = ceiling.compareTo(r.getCeiling()); + if (maxCompare < 0) + { + newCeiling = ceiling; + newOpenCeiling = openCeiling; + } + else if (maxCompare > 0) + { + newCeiling = r.getCeiling(); + newOpenCeiling = r.isOpenCeiling(); + } + else + { + newCeiling = ceiling; + newOpenCeiling = (openCeiling || r.isOpenCeiling()); + } + + VersionRange result; + if (isRangeValid(newOpenFloor, newFloor, newCeiling, newOpenCeiling)) + { + result = new VersionRange(newOpenFloor, newFloor, newCeiling, newOpenCeiling); + } + else + { + result = null; + } + return result; + } + + /** + * Check if the supplied parameters describe a valid version range. + * + * @param floor + * the minimum version. + * @param openFloor + * whether the minimum version is exclusive. + * @param ceiling + * the maximum version. + * @param openCeiling + * whether the maximum version is exclusive. + * @return true is the range is valid; otherwise false. + */ + private static boolean isRangeValid(boolean openFloor, Version floor, Version ceiling, boolean openCeiling) { + boolean result; + int compare = floor.compareTo(ceiling); + if (compare > 0) + { + // Minimum larger than maximum is invalid. + result = false; + } + else if (compare == 0 && (openFloor || openCeiling)) + { + // If floor and ceiling are the same, and either are exclusive, no valid range + // exists. + result = false; + } + else + { + // Range is valid. + result = true; + } + return result; + } + + private void checkRange() + { + if (!isRangeValid(openFloor, floor, ceiling, openCeiling)) + { + throw new IllegalArgumentException("invalid version range: " + makeString(openFloor, floor, ceiling, openCeiling)); + } + } + + + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ( ( ceiling == null ) ? 0 : ceiling.hashCode() ); + result = prime * result + ( ( floor == null ) ? 0 : floor.hashCode() ); + result = prime * result + ( openCeiling ? 1231 : 1237 ); + result = prime * result + ( openFloor ? 1231 : 1237 ); + return result; + } + + + public boolean equals( Object obj ) + { + if ( this == obj ) + return true; + if ( obj == null ) + return false; + if ( getClass() != obj.getClass() ) + return false; + final VersionRange other = ( VersionRange ) obj; + if ( ceiling == null ) + { + if ( other.ceiling != null ) + return false; + } + else if ( !ceiling.equals( other.ceiling ) ) + return false; + if ( floor == null ) + { + if ( other.floor != null ) + return false; + } + else if ( !floor.equals( other.floor ) ) + return false; + if ( openCeiling != other.openCeiling ) + return false; + if ( openFloor != other.openFloor ) + return false; + return true; + } + + + public String toString() + { + if ( ANY_VERSION.equals( this ) ) + { + return makeString( openFloor, Version.emptyVersion, INFINITE_VERSION, openCeiling ); + } + return makeString( openFloor, floor, ceiling, openCeiling ); + } + + + private String makeString( boolean openFloor, Version floor, Version ceiling, boolean openCeiling ) + { + StringBuffer vr = new StringBuffer( 32 ); + if ( INFINITE_VERSION.equals( ceiling ) ) + { + vr.append( Version.emptyVersion.equals( floor ) ? "0" : floor.toString() ); + } + else + { + vr.append( openFloor ? "(" : "[" ); + String floorStr = Version.emptyVersion.equals( floor ) ? "0" : floor.toString(); + String ceilingStr = ceiling.toString(); + vr.append( floorStr ).append( "," ).append( ceilingStr ); + vr.append( openCeiling ? ")" : "]" ); + } + return vr.toString(); + } + + + public static VersionRange newInstance( Version pointVersion, + int lowerBoundRule, + int upperBoundRule ) + { + Version floor = null; + switch ( lowerBoundRule ) + { + case ANY: + floor = VersionTable.getVersion( 0, 0, 0 ); + break; + case MAJOR: + floor = VersionTable.getVersion( pointVersion.getMajor(), 0, 0 ); + break; + case MINOR: + floor = VersionTable.getVersion( pointVersion.getMajor(), pointVersion.getMinor(), 0 ); + break; + case MICRO: + floor = VersionTable.getVersion( pointVersion.getMajor(), pointVersion.getMinor(), pointVersion.getMicro() ); + break; + case EXACT: + floor = pointVersion; + break; + } + + Version ceiling = null; + boolean openCeiling = true; + switch ( upperBoundRule ) + { + case ANY: + ceiling = INFINITE_VERSION; + break; + case MAJOR: + ceiling = VersionTable.getVersion( pointVersion.getMajor() + 1, 0, 0 ); + break; + case MINOR: + ceiling = VersionTable.getVersion( pointVersion.getMajor(), pointVersion.getMinor() + 1, 0 ); + break; + case MICRO: + ceiling = VersionTable.getVersion( pointVersion.getMajor(), pointVersion.getMinor(), pointVersion.getMicro() + 1 ); + break; + case EXACT: + ceiling = pointVersion; + openCeiling = false; + break; + } + + return new VersionRange( false, floor, ceiling, openCeiling ); + } +} Added: felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionTable.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionTable.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionTable.java (added) +++ felix/trunk/utils/src/main/java/org/apache/felix/utils/version/VersionTable.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,91 @@ +/* + * 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. + */ +package org.apache.felix.utils.version; + +import java.util.WeakHashMap; + +import org.osgi.framework.Version; + +/** + * Cache of Versions backed by a WeakHashMap to conserve memory. + * + * VersionTable.getVersion should be used in preference to new Version() or Version.parseVersion. + * + * @author dave + * + */ +public final class VersionTable +{ + private static final WeakHashMap versions = new WeakHashMap(); + + private VersionTable() { } + + public static Version getVersion(String version) + { + return getVersion( version, true ); + } + + public static Version getVersion(String version, boolean clean) + { + if (clean) + { + version = VersionCleaner.clean(version); + } + synchronized( versions ) + { + Version v = (Version) versions.get(version); + if ( v == null ) + { + v = Version.parseVersion(version); + versions.put(version, v); + } + return v; + } + } + + public static Version getVersion(int major, int minor, int micro) + { + return getVersion(major, minor, micro, null); + } + + public static Version getVersion(int major, int minor, int micro, String qualifier) + { + String key; + + if ( qualifier == null || qualifier.length() == 0 ) + { + key = major + "." + minor + "." + micro; + } + else + { + key = major + "." + minor + "." + micro + "." + qualifier; + } + + synchronized( versions ) + { + Version v = (Version) versions.get(key); + if ( v == null ) + { + v = new Version(major, minor, micro, qualifier); + versions.put(key, v); + } + return v; + } + } +} Added: felix/trunk/utils/src/test/java/org/apache/felix/utils/filter/FilterImplTest.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/filter/FilterImplTest.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/test/java/org/apache/felix/utils/filter/FilterImplTest.java (added) +++ felix/trunk/utils/src/test/java/org/apache/felix/utils/filter/FilterImplTest.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,124 @@ +/* + * 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. + */ +package org.apache.felix.utils.filter; + +import java.util.Dictionary; +import java.util.Hashtable; + +import junit.framework.TestCase; +import org.osgi.framework.Version; + +public class FilterImplTest extends TestCase +{ + public void testStandardLDAP() throws Exception + { + FilterImpl filterImpl = FilterImpl.newInstance( + "(&(package=org.eclipse.core.runtime)(version>=0.0.0)(common=split))"); + + Dictionary dict = new Hashtable(); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", "0.0.0"); + dict.put("common", "split"); + + assertTrue(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", "0.0.0"); + dict.put("common", "split-wrong"); + + assertFalse(filterImpl.match(dict)); + } + + public void testNoneStandardLDAPOperators() throws Exception + { + FilterImpl filterImpl = FilterImpl.newInstance( + "(&(package=org.eclipse.core.runtime)(version>=0.0.0)(common=split)(mandatory:<*common,test))"); + + Dictionary dict = new Hashtable(); + dict.put("somethindifferent", "sonstwas"); + assertFalse(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("mandatory:", "common"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("mandatory:", "common,test"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + + filterImpl = FilterImpl.newInstance( + "(&(package=org.eclipse.core.runtime)(version>=0.0.0)(common=split)(mandatory:*>common))"); + dict = new Hashtable(); + dict.put("mandatory:", "common"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("mandatory:", "common,test"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + + filterImpl = FilterImpl.newInstance( + "(&(package=org.eclipse.core.runtime)(version>=0.0.0)(common=split)(mandatory:*>common,test))"); + dict = new Hashtable(); + dict.put("mandatory:", "common"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertFalse(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("mandatory:", "common,test"); + dict.put("package", "org.eclipse.core.runtime"); + dict.put("version", new Version("0.0.0")); + dict.put("common", "split"); + assertTrue(filterImpl.match(dict)); + } + + public void testCaseSensitive() throws Exception + { + FilterImpl filterImpl = FilterImpl.newInstance("(&(package=org.eclipse.core.runtime))"); + + Dictionary dict = new Hashtable(); + dict.put("PACKAGE", "org.eclipse.core.runtime"); + assertTrue(filterImpl.match(dict)); + + dict = new Hashtable(); + dict.put("PACKAGE", "org.eclipse.core.runtime"); + assertFalse(filterImpl.matchCase(dict)); + } + +} \ No newline at end of file Added: felix/trunk/utils/src/test/java/org/apache/felix/utils/manifest/ParserTest.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/manifest/ParserTest.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/test/java/org/apache/felix/utils/manifest/ParserTest.java (added) +++ felix/trunk/utils/src/test/java/org/apache/felix/utils/manifest/ParserTest.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,73 @@ +/* + * 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. + */ +package org.apache.felix.utils.manifest; + +import junit.framework.TestCase; + +public class ParserTest extends TestCase +{ + + public void testSimple() throws Exception { + Clause[] paths = Parser.parseHeader("/foo.xml, /foo/bar.xml"); + assertEquals(2, paths.length); + assertEquals("/foo.xml", paths[0].getName()); + assertEquals(0, paths[0].getAttributes().length); + assertEquals(0, paths[0].getDirectives().length); + assertEquals("/foo/bar.xml", paths[1].getName()); + assertEquals(0, paths[1].getAttributes().length); + assertEquals(0, paths[1].getDirectives().length); + } + + public void testComplex() throws Exception { + Clause[] paths = Parser.parseHeader("OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;some-other-attribute=1"); + assertEquals(2, paths.length); + assertEquals("OSGI-INF/blueprint/comp1_named.xml", paths[0].getName()); + assertEquals(0, paths[0].getAttributes().length); + assertEquals(1, paths[0].getDirectives().length); + assertEquals("true", paths[0].getDirective("ignored-directive")); + assertEquals("OSGI-INF/blueprint/comp2_named.xml", paths[1].getName()); + assertEquals(1, paths[1].getAttributes().length); + assertEquals("1", paths[1].getAttribute("some-other-attribute")); + assertEquals(0, paths[1].getDirectives().length); + } + + public void testPaths() throws Exception { + Clause[] paths = Parser.parseHeader("OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;foo.xml;a=b;1:=2;c:=d;4=5"); + assertEquals(3, paths.length); + assertEquals("OSGI-INF/blueprint/comp1_named.xml", paths[0].getName()); + assertEquals(0, paths[0].getAttributes().length); + assertEquals(1, paths[0].getDirectives().length); + assertEquals("true", paths[0].getDirective("ignored-directive")); + assertEquals("OSGI-INF/blueprint/comp2_named.xml", paths[1].getName()); + assertEquals(2, paths[1].getAttributes().length); + assertEquals("b", paths[1].getAttribute("a")); + assertEquals("5", paths[1].getAttribute("4")); + assertEquals(2, paths[1].getDirectives().length); + assertEquals("d", paths[1].getDirective("c")); + assertEquals("2", paths[1].getDirective("1")); + assertEquals("foo.xml", paths[2].getName()); + assertEquals(2, paths[2].getAttributes().length); + assertEquals("b", paths[2].getAttribute("a")); + assertEquals("5", paths[2].getAttribute("4")); + assertEquals(2, paths[2].getDirectives().length); + assertEquals("d", paths[2].getDirective("c")); + assertEquals("2", paths[2].getDirective("1")); + } + +} Added: felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionCleanerTest.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionCleanerTest.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionCleanerTest.java (added) +++ felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionCleanerTest.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,72 @@ +/* + * 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. + */ +package org.apache.felix.utils.version; + +import junit.framework.TestCase; + +public class VersionCleanerTest extends TestCase { + + public void testConvertVersionToOsgi() + { + String osgiVersion; + + osgiVersion = VersionCleaner.clean( "" ); + assertEquals( "0.0.0", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2.1.0-SNAPSHOT" ); + assertEquals( "2.1.0.SNAPSHOT", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2.1-SNAPSHOT" ); + assertEquals( "2.1.0.SNAPSHOT", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2-SNAPSHOT" ); + assertEquals( "2.0.0.SNAPSHOT", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2" ); + assertEquals( "2.0.0", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2.1" ); + assertEquals( "2.1.0", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2.1.3" ); + assertEquals( "2.1.3", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "2.1.3.4" ); + assertEquals( "2.1.3.4", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "4aug2000r7-dev" ); + assertEquals( "0.0.0.4aug2000r7-dev", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "1.1-alpha-2" ); + assertEquals( "1.1.0.alpha-2", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "1.0-alpha-16-20070122.203121-13" ); + assertEquals( "1.0.0.alpha-16-20070122_203121-13", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "1.0-20070119.021432-1" ); + assertEquals( "1.0.0.20070119_021432-1", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "1-20070119.021432-1" ); + assertEquals( "1.0.0.20070119_021432-1", osgiVersion ); + + osgiVersion = VersionCleaner.clean( "1.4.1-20070217.082013-7" ); + assertEquals( "1.4.1.20070217_082013-7", osgiVersion ); + } + +} Added: felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionRangeTest.java URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionRangeTest.java?rev=924669&view=auto ============================================================================== --- felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionRangeTest.java (added) +++ felix/trunk/utils/src/test/java/org/apache/felix/utils/version/VersionRangeTest.java Thu Mar 18 08:40:37 2010 @@ -0,0 +1,205 @@ +/* + * 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. + */ +package org.apache.felix.utils.version; + +import junit.framework.TestCase; +import org.osgi.framework.Version; + + +public class VersionRangeTest extends TestCase { + + public void testVersionRange() throws Exception { + String version1 = "[1.2.3, 4.5.6]"; + String version2 = "(1, 2]"; + String version3 = "[2,4)"; + String version4 = "(1,2)"; + String version5 = "2"; + String version6 = "2.3"; + String version7 = "[1.2.3.q, 2.3.4.p)"; + String version8 = "1.2.2.5"; + String version9 = "a.b.c"; + String version10 = null; + String version11 = ""; + String version12 = "\"[1.2.3, 4.5.6]\""; + + VersionRange vr = new VersionRange(version1); + assertEquals("The value is wrong", "1.2.3", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "4.5.6", vr.getCeiling().toString()); + assertFalse("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version2); + assertEquals("The value is wrong", "1.0.0", vr.getFloor().toString()); + assertTrue("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "2.0.0", vr.getCeiling().toString()); + assertFalse("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version3); + + assertEquals("The value is wrong", "2.0.0", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "4.0.0", vr.getCeiling().toString()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version4); + + assertEquals("The value is wrong", "1.0.0", vr.getFloor().toString()); + assertTrue("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "2.0.0", vr.getCeiling().toString()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version5); + assertEquals("The value is wrong", "2.0.0", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", VersionRange.INFINITE_VERSION, vr.getCeiling()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version6, true); + assertEquals("The value is wrong", "2.3.0", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "2.3.0", vr.getCeiling().toString()); + assertFalse("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version7); + assertEquals("The value is wrong", "1.2.3.q", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "2.3.4.p", vr.getCeiling().toString()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version8); + assertEquals("The value is wrong", "1.2.2.5", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", VersionRange.INFINITE_VERSION, vr.getCeiling()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + boolean exception = false; + try { + vr = new VersionRange(version9, false, false); + } catch (Exception e) { + exception = true; + } + assertTrue("The value is wrong", exception); + boolean exceptionNull = false; + try { + vr = new VersionRange(version10, false, false); + } catch (Exception e) { + exceptionNull = true; + } + assertTrue("The value is wrong", exceptionNull); + // empty version should be defaulted to >=0.0.0 + vr = VersionRange.parseVersionRange(version11); + assertEquals("The value is wrong", "0.0.0", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", VersionRange.INFINITE_VERSION, vr.getCeiling()); + assertTrue("The value is wrong", vr.isOpenCeiling()); + + vr = new VersionRange(version12); + assertEquals("The value is wrong", "1.2.3", vr.getFloor().toString()); + assertFalse("The value is wrong", vr.isOpenFloor()); + assertEquals("The value is wrong", "4.5.6", vr.getCeiling().toString()); + assertFalse("The value is wrong", vr.isOpenCeiling()); + } + + public void testInvalidVersions() throws Exception { + try { + new VersionRange("a", false, false); + assertTrue("Should have thrown an exception", false); + } catch (IllegalArgumentException e) { + } + + } + + public void testMatches() { + VersionRange vr = new VersionRange("[1.0.0, 2.0.0]"); + + assertFalse(vr.contains(new Version(0, 9, 0))); + assertFalse(vr.contains(new Version(2, 1, 0))); + assertTrue(vr.contains(new Version(2, 0, 0))); + assertTrue(vr.contains(new Version(1, 0, 0))); + assertTrue(vr.contains(new Version(1, 5, 0))); + + vr = new VersionRange("[1.0.0, 2.0.0)"); + + assertFalse(vr.contains(new Version(0, 9, 0))); + assertFalse(vr.contains(new Version(2, 1, 0))); + assertFalse(vr.contains(new Version(2, 0, 0))); + assertTrue(vr.contains(new Version(1, 0, 0))); + assertTrue(vr.contains(new Version(1, 5, 0))); + + vr = new VersionRange("(1.0.0, 2.0.0)"); + + assertFalse(vr.contains(new Version(0, 9, 0))); + assertFalse(vr.contains(new Version(2, 1, 0))); + assertFalse(vr.contains(new Version(2, 0, 0))); + assertFalse(vr.contains(new Version(1, 0, 0))); + assertTrue(vr.contains(new Version(1, 5, 0))); + + vr = new VersionRange("[1.0.0, 1.0.0]"); + assertFalse(vr.contains(new Version(0, 9, 0))); + assertFalse(vr.contains(new Version(2, 0, 0))); + assertTrue(vr.contains(new Version(1, 0, 0))); + assertFalse(vr.contains(new Version(1, 5, 0))); + assertFalse(vr.contains(new Version(1, 9, 9))); + } + + public void testIntersectVersionRange_Valid1() { + VersionRange v1 = new VersionRange("[1.0.0,3.0.0]"); + VersionRange v2 = new VersionRange("[2.0.0,3.0.0)"); + VersionRange result = v1.intersect(v2); + assertNotNull(result); + assertEquals("[2.0.0,3.0.0)", result.toString()); + } + + public void testIntersectVersionRange_Valid2() { + VersionRange v1 = new VersionRange("[1.0.0,3.0.0)"); + VersionRange v2 = new VersionRange("(2.0.0,3.0.0]"); + VersionRange result = v1.intersect(v2); + assertNotNull(result); + assertEquals("(2.0.0,3.0.0)", result.toString()); + } + + public void testIntersectVersionRange_Valid3() { + VersionRange v1 = new VersionRange("[2.0.0,2.0.0]"); + VersionRange v2 = new VersionRange("[1.0.0,3.0.0]"); + VersionRange result = v1.intersect(v2); + assertNotNull(result); + assertEquals("[2.0.0,2.0.0]", result.toString()); + } + + public void testIntersectVersionRange_Invalid1() { + VersionRange v1 = new VersionRange("[1.0.0,2.0.0]"); + VersionRange v2 = new VersionRange("(2.0.0,3.0.0]"); + VersionRange result = v1.intersect(v2); + assertNull(result); + } + + public void testIntersectVersionRange_Invalid2() { + VersionRange v1 = new VersionRange("[1.0.0,2.0.0)"); + VersionRange v2 = new VersionRange("[2.0.0,3.0.0]"); + VersionRange result = v1.intersect(v2); + assertNull(result); + } + + public void testIntersectVersionRange_Invalid3() { + VersionRange v1 = new VersionRange("[1.0.0,1.0.0]"); + VersionRange v2 = new VersionRange("[2.0.0,2.0.0]"); + VersionRange result = v1.intersect(v2); + assertNull(result); + } + +}