diff options
author | Thomas Watson | 2010-01-26 15:25:31 +0000 |
---|---|---|
committer | Thomas Watson | 2010-01-26 15:25:31 +0000 |
commit | 9ebb8f327ce4f9eee1a386afd3c2a4c33ea29272 (patch) | |
tree | b814e756d9db8a5326be19053f706f96ba175a15 | |
parent | d1015397709fa16837f9a60a90bac791ef2fa4fc (diff) | |
download | rt.equinox.framework-9ebb8f327ce4f9eee1a386afd3c2a4c33ea29272.tar.gz rt.equinox.framework-9ebb8f327ce4f9eee1a386afd3c2a4c33ea29272.tar.xz rt.equinox.framework-9ebb8f327ce4f9eee1a386afd3c2a4c33ea29272.zip |
Bug 300579 - Filter does not allow !, | or & as first character of attr namesR35x_v20100126
3 files changed, 409 insertions, 149 deletions
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/filter/FilterTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/filter/FilterTests.java index dbc36e069..aa1de8b1c 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/filter/FilterTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/filter/FilterTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 IBM Corporation and others. + * Copyright (c) 2008, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,110 +24,192 @@ public abstract class FilterTests extends TestCase { return suite; } + /** + * Fail with cause t. + * + * @param message Failure message. + * @param t Cause of the failure. + */ + public static void fail(String message, Throwable t) { + AssertionFailedError e = new AssertionFailedError(message + ": " + t.getMessage()); + e.initCause(t); + throw e; + } + static final int ISTRUE = 1; static final int ISFALSE = 2; static final int ISILLEGAL = 3; public abstract Filter createFilter(String filterString) throws InvalidSyntaxException; + private Dictionary getProperties() { + Dictionary props = new Hashtable(); + props.put("room", "bedroom"); + props.put("channel", new Object[] {new Integer(34), "101"}); + props.put("status", "(on\\)*"); + List vec = new ArrayList(10); + vec.add(new Long(150)); + vec.add("100"); + props.put("max record time", vec); + props.put("canrecord", "true(x)"); + props.put("shortvalue", new Short((short) 1000)); + props.put("intvalue", new Integer(100000)); + props.put("longvalue", new Long(10000000000L)); + props.put("bytevalue", new Byte((byte) 10)); + props.put("floatvalue", new Float(1.01)); + props.put("doublevalue", new Double(2.01)); + props.put("charvalue", new Character('A')); + props.put("booleanvalue", new Boolean(true)); + props.put("weirdvalue", new Hashtable()); + props.put("primintarrayvalue", new int[] {1, 2, 3}); + props.put("primlongarrayvalue", new long[] {1, 2, 3}); + props.put("primbytearrayvalue", new byte[] {(byte) 1, (byte) 2, (byte) 3}); + props.put("primshortarrayvalue", new short[] {(short) 1, (short) 2, (short) 3}); + props.put("primfloatarrayvalue", new float[] {(float) 1.1, (float) 2.2, (float) 3.3}); + props.put("primdoublearrayvalue", new double[] {1.1, 2.2, 3.3}); + props.put("primchararrayvalue", new char[] {'A', 'b', 'C', 'd'}); + props.put("primbooleanarrayvalue", new boolean[] {false}); + props.put("bigintvalue", new BigInteger("4123456")); + props.put("bigdecvalue", new BigDecimal("4.123456")); + props.put("*", "foo"); + props.put("! ab", "b"); + props.put("| ab", "b"); + props.put("& ab", "b"); + props.put("!", "c"); + props.put("|", "c"); + props.put("&", "c"); + props.put("empty", ""); + props.put("space", new Character(' ')); + return props; + } + public void testFilter() { - Properties props = new Properties(); - props.put("room", "bedroom"); //$NON-NLS-1$ //$NON-NLS-2$ - props.put("channel", new Object[] {new Integer(34), "101"}); //$NON-NLS-1$//$NON-NLS-2$ - props.put("status", "(on\\)*"); //$NON-NLS-1$//$NON-NLS-2$ - Vector vec = new Vector(10, 10); - vec.addElement(new Long(150)); - vec.addElement("100"); //$NON-NLS-1$ - props.put("max record time", vec); //$NON-NLS-1$ - props.put("canrecord", "true(x)"); //$NON-NLS-1$ //$NON-NLS-2$ - props.put("shortvalue", new Short((short) 1000)); //$NON-NLS-1$ - props.put("bytevalue", new Byte((byte) 10)); //$NON-NLS-1$ - props.put("floatvalue", new Float(1.01)); //$NON-NLS-1$ - props.put("doublevalue", new Double(2.01)); //$NON-NLS-1$ - props.put("charvalue", new Character('A')); //$NON-NLS-1$ - props.put("booleanvalue", new Boolean(false)); //$NON-NLS-1$ - props.put("weirdvalue", new Hashtable()); //$NON-NLS-1$ - props.put("primintarrayvalue", new int[] {1, 2, 3}); //$NON-NLS-1$ - props.put("primlongarrayvalue", new long[] {1, 2, 3}); //$NON-NLS-1$ - props.put("primbytearrayvalue", new byte[] {(byte) 1, (byte) 2, (byte) 3}); //$NON-NLS-1$ - props.put("primshortarrayvalue", new short[] {(short) 1, (short) 2, (short) 3}); //$NON-NLS-1$ - props.put("primfloatarrayvalue", new float[] {(float) 1.1, (float) 2.2, (float) 3.3}); //$NON-NLS-1$ - props.put("primdoublearrayvalue", new double[] {1.1, 2.2, 3.3}); //$NON-NLS-1$ - props.put("primchararrayvalue", new char[] {'A', 'b', 'C', 'd'}); //$NON-NLS-1$ - props.put("primbooleanarrayvalue", new boolean[] {false}); //$NON-NLS-1$ - try { - props.put("bigintvalue", new BigInteger("4123456")); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (NoClassDefFoundError e) { - // ignore - } - try { - props.put("bigdecvalue", new BigDecimal("4.123456")); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (NoClassDefFoundError e) { - // ignore - } + Dictionary props = getProperties(); + testFilter("(room=*)", props, ISTRUE); + testFilter("(room=bedroom)", props, ISTRUE); + testFilter("(room~= B E D R O O M )", props, ISTRUE); + testFilter("(room=abc)", props, ISFALSE); + testFilter(" ( room >=aaaa)", props, ISTRUE); + testFilter("(room <=aaaa)", props, ISFALSE); + testFilter(" ( room =b*) ", props, ISTRUE); + testFilter(" ( room =*m) ", props, ISTRUE); + testFilter("(room=bed*room)", props, ISTRUE); + testFilter(" ( room =b*oo*m) ", props, ISTRUE); + testFilter(" ( room =*b*oo*m*) ", props, ISTRUE); + testFilter(" ( room =b*b* *m*) ", props, ISFALSE); + testFilter(" (& (room =bedroom) (channel ~=34))", props, ISTRUE); + testFilter(" (& (room =b*) (room =*x) (channel=34))", props, ISFALSE); + testFilter("(| (room =bed*)(channel=222)) ", props, ISTRUE); + testFilter("(| (room =boom*)(channel=101)) ", props, ISTRUE); + testFilter(" (! (room =ab*b*oo*m*) ) ", props, ISTRUE); + testFilter(" (status =\\(o*\\\\\\)\\*) ", props, ISTRUE); + testFilter(" (canRecord =true\\(x\\)) ", props, ISTRUE); + testFilter("(max Record Time <=140) ", props, ISTRUE); + testFilter("(shortValue >=100) ", props, ISTRUE); + testFilter("(intValue <=100001) ", props, ISTRUE); + testFilter("(longValue >=10000000000) ", props, ISTRUE); + testFilter(" ( & ( byteValue <=100) ( byteValue >=10) ) ", props, ISTRUE); + testFilter("(weirdValue =100) ", props, ISFALSE); + testFilter("(bigIntValue =4123456) ", props, ISTRUE); + testFilter("(bigDecValue =4.123456) ", props, ISTRUE); + testFilter("(floatValue >=1.0) ", props, ISTRUE); + testFilter("(doubleValue <=2.011) ", props, ISTRUE); + testFilter("(charValue ~=a) ", props, ISTRUE); + testFilter("(booleanValue =true) ", props, ISTRUE); + testFilter("(primIntArrayValue =1) ", props, ISTRUE); + testFilter("(primLongArrayValue =2) ", props, ISTRUE); + testFilter("(primByteArrayValue =3) ", props, ISTRUE); + testFilter("(primShortArrayValue =1) ", props, ISTRUE); + testFilter("(primFloatArrayValue =1.1) ", props, ISTRUE); + testFilter("(primDoubleArrayValue =2.2) ", props, ISTRUE); + testFilter("(primCharArrayValue ~=D) ", props, ISTRUE); + testFilter("(primBooleanArrayValue =false) ", props, ISTRUE); + testFilter("(& (| (room =d*m) (room =bed*) (room=abc)) (! (channel=999)))", props, ISTRUE); + testFilter("(room=bedroom)", null, ISFALSE); + testFilter("(*=foo)", props, ISTRUE); + testFilter("(! ab=b)", props, ISTRUE); + testFilter("(| ab=b)", props, ISTRUE); + testFilter("(&=c)", props, ISTRUE); + testFilter("(!=c)", props, ISTRUE); + testFilter("(|=c)", props, ISTRUE); + testFilter("(& ab=b)", props, ISTRUE); + testFilter("(!ab=*)", props, ISFALSE); + testFilter("(|ab=*)", props, ISFALSE); + testFilter("(&ab=*)", props, ISFALSE); + testFilter("(empty=)", props, ISTRUE); + testFilter("(empty=*)", props, ISTRUE); + testFilter("(space= )", props, ISTRUE); + testFilter("(space=*)", props, ISTRUE); + } - testFilter("(room=*)", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room=bedroom)", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room~= B E D R O O M )", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room=abc)", props, ISFALSE); //$NON-NLS-1$ - testFilter(" ( room >=aaaa)", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room <=aaaa)", props, ISFALSE); //$NON-NLS-1$ - testFilter(" ( room =b*) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" ( room =*m) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room=bed*room)", props, ISTRUE); //$NON-NLS-1$ - testFilter(" ( room =b*oo*m) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" ( room =*b*oo*m*) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" ( room =b*b* *m*) ", props, ISFALSE); //$NON-NLS-1$ - testFilter(" (& (room =bedroom) (channel ~= 34))", props, ISTRUE); //$NON-NLS-1$ - testFilter(" (& (room =b*) (room =*x) (channel=34))", props, ISFALSE); //$NON-NLS-1$ - testFilter("(| (room =bed*)(channel=222)) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(| (room =boom*)(channel=101)) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" (! (room =ab*b*oo*m*) ) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" (status =\\(o*\\\\\\)\\*) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" (canRecord =true\\(x\\)) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(max Record Time <=140) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(shortValue >= 100) ", props, ISTRUE); //$NON-NLS-1$ - testFilter(" ( & ( byteValue <= 100 ) ( byteValue >= 10 ) ) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(weirdValue = 100) ", props, ISFALSE); //$NON-NLS-1$ - testFilter("(bigIntValue = 4123456) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(bigDecValue = 4.123456) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(floatValue >= 1.0) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(doubleValue <= 2.011) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(charValue ~= a) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(booleanValue = false) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primIntArrayValue = 1) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primLongArrayValue = 2) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primByteArrayValue = 3) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primShortArrayValue = 1) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primFloatArrayValue = 1.1) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primDoubleArrayValue = 2.2) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primCharArrayValue ~= D) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(primBooleanArrayValue = false) ", props, ISTRUE); //$NON-NLS-1$ - testFilter("(& (| (room =d*m) (room =bed*) (room=abc)) (! (channel=999)))", props, ISTRUE); //$NON-NLS-1$ - testFilter("(room=bedroom)", null, ISFALSE); //$NON-NLS-1$ - - testFilter("", props, ISILLEGAL); //$NON-NLS-1$ - testFilter("()", props, ISILLEGAL); //$NON-NLS-1$ - testFilter("(=foo)", props, ISILLEGAL); //$NON-NLS-1$ - testFilter("(", props, ISILLEGAL); //$NON-NLS-1$ - testFilter("(abc = ))", props, ISILLEGAL); //$NON-NLS-1$ - testFilter("(& (abc = xyz) (& (345))", props, ISILLEGAL); //$NON-NLS-1$ - testFilter(" (room = b**oo!*m*) ) ", props, ISILLEGAL); //$NON-NLS-1$ - testFilter(" (room = b**oo)*m*) ) ", props, ISILLEGAL); //$NON-NLS-1$ - testFilter(" (room = *=b**oo*m*) ) ", props, ISILLEGAL); //$NON-NLS-1$ - testFilter(" (room = =b**oo*m*) ) ", props, ISILLEGAL); //$NON-NLS-1$ + public void testInvalidValues() { + Dictionary props = getProperties(); + testFilter("(intvalue=*)", props, ISTRUE); + testFilter("(intvalue=b)", props, ISFALSE); + testFilter("(intvalue=)", props, ISFALSE); + testFilter("(longvalue=*)", props, ISTRUE); + testFilter("(longvalue=b)", props, ISFALSE); + testFilter("(longvalue=)", props, ISFALSE); + testFilter("(shortvalue=*)", props, ISTRUE); + testFilter("(shortvalue=b)", props, ISFALSE); + testFilter("(shortvalue=)", props, ISFALSE); + testFilter("(bytevalue=*)", props, ISTRUE); + testFilter("(bytevalue=b)", props, ISFALSE); + testFilter("(bytevalue=)", props, ISFALSE); + testFilter("(charvalue=*)", props, ISTRUE); + testFilter("(charvalue=)", props, ISFALSE); + testFilter("(floatvalue=*)", props, ISTRUE); + testFilter("(floatvalue=b)", props, ISFALSE); + testFilter("(floatvalue=)", props, ISFALSE); + testFilter("(doublevalue=*)", props, ISTRUE); + testFilter("(doublevalue=b)", props, ISFALSE); + testFilter("(doublevalue=)", props, ISFALSE); + testFilter("(booleanvalue=*)", props, ISTRUE); + testFilter("(booleanvalue=b)", props, ISFALSE); + testFilter("(booleanvalue=)", props, ISFALSE); + } + public void testIllegal() { + Dictionary props = getProperties(); + testFilter("", props, ISILLEGAL); + testFilter("()", props, ISILLEGAL); + testFilter("(=foo)", props, ISILLEGAL); + testFilter("(", props, ISILLEGAL); + testFilter("(abc = ))", props, ISILLEGAL); + testFilter("(& (abc = xyz) (& (345))", props, ISILLEGAL); + testFilter(" (room = b**oo!*m*) ) ", props, ISILLEGAL); + testFilter(" (room = b**oo)*m*) ) ", props, ISILLEGAL); + testFilter(" (room = *=b**oo*m*) ) ", props, ISILLEGAL); + testFilter(" (room = =b**oo*m*) ) ", props, ISILLEGAL); + } + + public void testScalarSubstring() { + Dictionary props = getProperties(); + testFilter("(shortValue =100*) ", props, ISFALSE); + testFilter("(intValue =100*) ", props, ISFALSE); + testFilter("(longValue =100*) ", props, ISFALSE); + testFilter("( byteValue =1*00 )", props, ISFALSE); + testFilter("(bigIntValue =4*23456) ", props, ISFALSE); + testFilter("(bigDecValue =4*123456) ", props, ISFALSE); + testFilter("(floatValue =1*0) ", props, ISFALSE); + testFilter("(doubleValue =2*011) ", props, ISFALSE); + testFilter("(charValue =a*) ", props, ISFALSE); + testFilter("(booleanValue =t*ue) ", props, ISFALSE); + } + + public void testNormalization() { try { - Filter f1 = createFilter("( a = bedroom )"); //$NON-NLS-1$ - Filter f2 = createFilter(" (a= bedroom ) "); //$NON-NLS-1$ - assertEquals("not equal", "(a= bedroom )", f1.toString()); //$NON-NLS-1$ //$NON-NLS-2$ - assertEquals("not equal", "(a= bedroom )", f2.toString()); //$NON-NLS-1$ //$NON-NLS-2$ - assertEquals("not equal", f1, f2); //$NON-NLS-1$ - assertEquals("not equal", f2, f1); //$NON-NLS-1$ - assertEquals("not equal", f1.hashCode(), f2.hashCode()); //$NON-NLS-1$ + Filter f1 = createFilter("( a = bedroom )"); + Filter f2 = createFilter(" (a= bedroom ) "); + assertEquals("not equal", "(a= bedroom )", f1.toString()); + assertEquals("not equal", "(a= bedroom )", f2.toString()); + assertEquals("not equal", f1, f2); + assertEquals("not equal", f2, f1); + assertEquals("not equal", f1.hashCode(), f2.hashCode()); } catch (InvalidSyntaxException e) { - fail("unexpected invalid syntax: " + e); //$NON-NLS-1$ + fail("unexpected invalid syntax", e); } + } private void testFilter(String query, Dictionary props, int expect) { @@ -137,63 +219,131 @@ public abstract class FilterTests extends TestCase { f1 = createFilter(query); if (expect == ISILLEGAL) { - fail("expected exception"); //$NON-NLS-1$ + fail("expected exception"); } } catch (InvalidSyntaxException e) { if (expect != ISILLEGAL) { - fail("exception: " + e.toString()); //$NON-NLS-1$ + fail("exception", e); } return; } boolean val = f1.match(props); - assertEquals("wrong result", expect == ISTRUE, val); //$NON-NLS-1$ + assertEquals("wrong result", expect == ISTRUE, val); val = f1.match(ref); - assertEquals("wrong result", expect == ISTRUE, val); //$NON-NLS-1$ + assertEquals("wrong result", expect == ISTRUE, val); String normalized = f1.toString(); Filter f2; try { f2 = createFilter(normalized); } catch (InvalidSyntaxException e) { - fail("exception: " + e.toString()); //$NON-NLS-1$ + fail("exception", e); return; } val = f2.match(props); - assertEquals("wrong result", expect == ISTRUE, val); //$NON-NLS-1$ + assertEquals("wrong result", expect == ISTRUE, val); val = f2.match(ref); - assertEquals("wrong result", expect == ISTRUE, val); //$NON-NLS-1$ + assertEquals("wrong result", expect == ISTRUE, val); - assertEquals("normalized not equal", normalized, f2.toString()); //$NON-NLS-1$ + assertEquals("normalized not equal", normalized, f2.toString()); } public void testComparable() { Filter f1 = null; + Object comp42 = new SampleComparable("42"); + Object comp43 = new SampleComparable("43"); + Hashtable hash = new Hashtable(); + + try { + f1 = createFilter("(comparable=42)"); + } catch (InvalidSyntaxException e) { + fail("invalid syntax", e); + } + + hash.put("comparable", comp42); + assertTrue("does not match filter", f1.match(hash)); + assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); + + hash.put("comparable", comp43); + assertFalse("does match filter", f1.match(hash)); + assertFalse("does match filter", f1.match(new DictionaryServiceReference(hash))); + + try { + f1 = createFilter("(comparable<=42)"); + } catch (InvalidSyntaxException e) { + fail("invalid syntax", e); + } + + hash.put("comparable", comp42); + assertTrue("does not match filter", f1.match(hash)); + assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); + + hash.put("comparable", comp43); + assertFalse("does match filter", f1.match(hash)); + assertFalse("does match filter", f1.match(new DictionaryServiceReference(hash))); + try { - f1 = createFilter("(comparable=42)"); //$NON-NLS-1$ + f1 = createFilter("(comparable>=42)"); } catch (InvalidSyntaxException e) { - fail("invalid syntax" + e); //$NON-NLS-1$ + fail("invalid syntax", e); } - Object comp; + + hash.put("comparable", comp42); + assertTrue("does not match filter", f1.match(hash)); + assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); + + hash.put("comparable", comp43); + assertTrue("does not match filter", f1.match(hash)); + assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); + + try { + f1 = createFilter("(comparable=4*2)"); + } catch (InvalidSyntaxException e) { + fail("invalid syntax", e); + } + + hash.put("comparable", comp42); + assertFalse("does match filter", f1.match(hash)); + assertFalse("does match filter", f1.match(new DictionaryServiceReference(hash))); + } + + public void testObject() { + Filter f1 = null; + Object obj42 = new SampleObject("42"); + Object obj43 = new SampleObject("43"); Hashtable hash = new Hashtable(); - comp = new SampleComparable("42"); //$NON-NLS-1$ - hash.put("comparable", comp); //$NON-NLS-1$ - assertTrue("does not match filter", f1.match(hash)); //$NON-NLS-1$ - assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); //$NON-NLS-1$ + try { + f1 = createFilter("(object=42)"); + } catch (InvalidSyntaxException e) { + fail("invalid syntax", e); + } + + hash.put("object", obj42); + assertTrue("does not match filter", f1.match(hash)); + assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); - comp = new Long(42); - hash.put("comparable", comp); //$NON-NLS-1$ - assertTrue("does not match filter", f1.match(hash)); //$NON-NLS-1$ - assertTrue("does not match filter", f1.match(new DictionaryServiceReference(hash))); //$NON-NLS-1$ + hash.put("object", obj43); + assertFalse("does match filter", f1.match(hash)); + assertFalse("does match filter", f1.match(new DictionaryServiceReference(hash))); + try { + f1 = createFilter("(object=4*2)"); + } catch (InvalidSyntaxException e) { + fail("invalid syntax", e); + } + + hash.put("object", obj42); + assertFalse("does match filter", f1.match(hash)); + assertFalse("does match filter", f1.match(new DictionaryServiceReference(hash))); } - private static class SampleComparable implements Comparable { + public static class SampleComparable implements Comparable { private int value = -1; public SampleComparable(String value) { @@ -209,6 +359,25 @@ public abstract class FilterTests extends TestCase { } } + public static class SampleObject { + private int value = -1; + + public SampleObject(String value) { + this.value = Integer.parseInt(value); + } + + public boolean equals(Object o) { + if (o instanceof SampleObject) { + return value == ((SampleObject) o).value; + } + return false; + } + + public String toString() { + return String.valueOf(value); + } + } + private static class DictionaryServiceReference implements ServiceReference { private final Dictionary dictionary; private final String[] keys; @@ -236,14 +405,6 @@ public abstract class FilterTests extends TestCase { this.keys = (String[]) keyList.toArray(new String[keyList.size()]); } - public int compareTo(Object reference) { - throw new UnsupportedOperationException(); - } - - public Bundle getBundle() { - throw new UnsupportedOperationException(); - } - public Object getProperty(String k) { for (int i = 0, length = keys.length; i < length; i++) { String key = keys[i]; @@ -258,6 +419,14 @@ public abstract class FilterTests extends TestCase { return (String[]) keys.clone(); } + public int compareTo(Object reference) { + throw new UnsupportedOperationException(); + } + + public Bundle getBundle() { + throw new UnsupportedOperationException(); + } + public Bundle[] getUsingBundles() { throw new UnsupportedOperationException(); } diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/FilterImpl.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/FilterImpl.java index 6581e20b6..66b0c240f 100644 --- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/FilterImpl.java +++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/FilterImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2009 IBM Corporation and others. + * Copyright (c) 2003, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -756,7 +756,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - int intval2 = Integer.parseInt(((String) value2).trim()); + int intval2; + try { + intval2 = Integer.parseInt(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -795,7 +800,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - long longval2 = Long.parseLong(((String) value2).trim()); + long longval2; + try { + longval2 = Long.parseLong(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -834,7 +844,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - byte byteval2 = Byte.parseByte(((String) value2).trim()); + byte byteval2; + try { + byteval2 = Byte.parseByte(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -873,7 +888,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - short shortval2 = Short.parseShort(((String) value2).trim()); + short shortval2; + try { + shortval2 = Short.parseShort(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -912,7 +932,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - char charval2 = (((String) value2).trim()).charAt(0); + char charval2; + try { + charval2 = ((String) value2).charAt(0); + } catch (IndexOutOfBoundsException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -990,7 +1015,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - float floatval2 = Float.parseFloat(((String) value2).trim()); + float floatval2; + try { + floatval2 = Float.parseFloat(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -1029,7 +1059,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ return false; } - double doubleval2 = Double.parseDouble(((String) value2).trim()); + double doubleval2; + try { + doubleval2 = Double.parseDouble(((String) value2).trim()); + } catch (IllegalArgumentException e) { + return false; + } switch (operation) { case EQUAL : { if (Debug.DEBUG && Debug.DEBUG_FILTER) { @@ -1339,10 +1374,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ } private FilterImpl parse_and() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, filterstring.substring(pos)), filterstring); + pos = lookahead - 1; + return parse_item(); } List operands = new ArrayList(10); @@ -1356,10 +1393,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ } private FilterImpl parse_or() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, filterstring.substring(pos)), filterstring); + pos = lookahead - 1; + return parse_item(); } List operands = new ArrayList(10); @@ -1373,10 +1412,12 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ } private FilterImpl parse_not() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, filterstring.substring(pos)), filterstring); + pos = lookahead - 1; + return parse_item(); } FilterImpl child = parse_filter(); @@ -1549,7 +1590,7 @@ public class FilterImpl implements Filter /* since Framework 1.1 */{ int size = operands.size(); if (size == 0) { - throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_VALUE, filterstring.substring(pos)), filterstring); + return ""; } if (size == 1) { diff --git a/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java b/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java index b565b8651..0b31ec855 100644 --- a/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java +++ b/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java @@ -39,7 +39,7 @@ import javax.security.auth.x500.X500Principal; * * @since 1.3 * @ThreadSafe - * @version $Revision: 7761 $ + * @version $Revision: 8080 $ */ public class FrameworkUtil { /** @@ -919,7 +919,13 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - int intval2 = Integer.parseInt(((String) value2).trim()); + int intval2; + try { + intval2 = Integer.parseInt(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } switch (operation) { case APPROX : case EQUAL : { @@ -939,7 +945,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - long longval2 = Long.parseLong(((String) value2).trim()); + long longval2; + try { + longval2 = Long.parseLong(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } + switch (operation) { case APPROX : case EQUAL : { @@ -959,7 +972,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - byte byteval2 = Byte.parseByte(((String) value2).trim()); + byte byteval2; + try { + byteval2 = Byte.parseByte(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } + switch (operation) { case APPROX : case EQUAL : { @@ -980,7 +1000,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - short shortval2 = Short.parseShort(((String) value2).trim()); + short shortval2; + try { + shortval2 = Short.parseShort(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } + switch (operation) { case APPROX : case EQUAL : { @@ -1001,7 +1028,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - char charval2 = (((String) value2).trim()).charAt(0); + char charval2; + try { + charval2 = ((String) value2).charAt(0); + } + catch (IndexOutOfBoundsException e) { + return false; + } + switch (operation) { case EQUAL : { return charval == charval2; @@ -1046,7 +1080,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - float floatval2 = Float.parseFloat(((String) value2).trim()); + float floatval2; + try { + floatval2 = Float.parseFloat(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } + switch (operation) { case APPROX : case EQUAL : { @@ -1067,7 +1108,14 @@ public class FrameworkUtil { if (operation == SUBSTRING) { return false; } - double doubleval2 = Double.parseDouble(((String) value2).trim()); + double doubleval2; + try { + doubleval2 = Double.parseDouble(((String) value2).trim()); + } + catch (IllegalArgumentException e) { + return false; + } + switch (operation) { case APPROX : case EQUAL : { @@ -1281,11 +1329,12 @@ public class FrameworkUtil { } private FilterImpl parse_and() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException("Missing '(': " - + filterstring.substring(pos), filterstring); + pos = lookahead - 1; + return parse_item(); } List operands = new ArrayList(10); @@ -1300,11 +1349,12 @@ public class FrameworkUtil { } private FilterImpl parse_or() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException("Missing '(': " - + filterstring.substring(pos), filterstring); + pos = lookahead - 1; + return parse_item(); } List operands = new ArrayList(10); @@ -1319,11 +1369,12 @@ public class FrameworkUtil { } private FilterImpl parse_not() throws InvalidSyntaxException { + int lookahead = pos; skipWhiteSpace(); if (filterChars[pos] != '(') { - throw new InvalidSyntaxException("Missing '(': " - + filterstring.substring(pos), filterstring); + pos = lookahead - 1; + return parse_item(); } FilterImpl child = parse_filter(); @@ -1508,8 +1559,7 @@ public class FrameworkUtil { int size = operands.size(); if (size == 0) { - throw new InvalidSyntaxException("Missing value: " - + filterstring.substring(pos), filterstring); + return ""; } if (size == 1) { |