Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakValueHashMap.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakValueHashMap.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakValueHashMap.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakValueHashMap.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,572 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/* + * TestWeakHashMap.java + * + */ + +package org.apache.jdo.test; + +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.util.WeakValueHashMap; + +/** + * + * @author Markus Fuchs + */ +public class Test_WeakValueHashMap extends AbstractTest { + + static boolean verbose = Boolean.getBoolean("verbose"); + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_WeakValueHashMap.class); + } + + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void testContainsKey() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + y11 = null; + y12 = null; + + if (verbose) System.out.println("\nTest ContainsKey:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertTrue("FAILURE: map does not contain x11", map.containsKey(new X("x11", y11))); + assertTrue("FAILURE: map does not contain x12", map.containsKey(new X("x12"))); + assertTrue("FAILURE: map does not contain x13", map.containsKey(new X("x13"))); + assertTrue("FAILURE: map does not contain x14", map.containsKey(new X("x14"))); + assertTrue("FAILURE: map does not contain null", map.containsKey(null)); + + // now gc + gc(); + + if (verbose) System.out.println("After GC:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertTrue("FAILURE: map does not contain x11", map.containsKey(new X("x11"))); + assertTrue("FAILURE: map contains x12", !map.containsKey(new X("x12"))); + assertTrue("FAILURE: map contains x13", !map.containsKey(new X("x13"))); + assertTrue("FAILURE: map does not contain x14", map.containsKey(new X("x14"))); + assertTrue("FAILURE: map does not contain null", map.containsKey(null)); + + map.clear(); + } + + /** */ + public void testContainsValue() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + y11 = null; + y12 = null; + + if (verbose) System.out.println("\nTest ContainsValue:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertTrue("FAILURE: map does not contain y11", map.containsValue(new Y("y11"))); + assertTrue("FAILURE: map does not contain y12", map.containsValue(new Y("y12", x12))); + assertTrue("FAILURE: map does not contain y13", map.containsValue(new Y("y13"))); + assertTrue("FAILURE: map does not contain y14", map.containsValue(new Y("y14"))); + assertTrue("FAILURE: map does not contain null", map.containsValue(new Y("y14"))); + + // now gc + gc(); + + if (verbose) System.out.println("After GC:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertTrue("FAILURE: map does not contain y11", map.containsValue(new Y("y11"))); + assertTrue("FAILURE: map contains y12", !map.containsValue(new Y("y12", x12))); + assertTrue("FAILURE: map contains y13", !map.containsValue(new Y("y13"))); + assertTrue("FAILURE: map does not contain y14", map.containsValue(new Y("y14"))); + assertTrue("FAILURE: map does not contain null", map.containsValue(null)); + + map.clear(); + } + + /** */ + public void testGet() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + + if (verbose) System.out.println("\nTest Get:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertEquals("Get (x11) returns wrong value", y11, map.get(new X("x11"))); + assertEquals("Get (x12) returns wrong value", y12, map.get(new X("x12"))); + assertEquals("Get (x13) returns wrong value", new Y("y13"), map.get(new X("x13"))); + assertNull("Get (x14) returns wrong value", map.get(new X("x14"))); + assertEquals("Get (null) returns wrong value", y14, map.get(null)); + + y11 = null; + y12 = null; + + // now gc + gc(); + + if (verbose) System.out.println("After GC:"); + if (verbose) printEntries(map); + + // check whether instances are included in map + assertNotNull("Get (x11) returns wrong value", map.get(new X("x11"))); + assertNull("Get (x12) returns wrong value",map.get(new X("x12"))); + assertNull("Get (x13) returns wrong value", map.get(new X("x13"))); + assertNull("Get (x14) returns wrong value", map.get(new X("x14"))); + assertNotNull("Get (null) returns wrong value", map.get(null)); + + map.clear(); + } + + /** */ + public void testSize() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + y11 = null; + y12 = null; + + int size1, size2, size3; + + if (verbose) System.out.println("\nTest Size:"); + if (verbose) printEntries(map); + + size1 = map.size(); + size2 = map.entrySet().size(); + size3 = map.values().size(); + + if (verbose) System.out.println("Size: " + size1); + + assertEquals("FAILURE: unexpected size of map", 5, size1); + assertEquals("FAILURE: unexpected size of entrySet", 5, size2); + assertEquals("FAILURE: unexpected size of values", 5, size3); + + // now gc + gc(); + + if (verbose) System.out.println("After GC:"); + if (verbose) printEntries(map); + + size1 = map.size(); + size2 = map.entrySet().size(); + size3 = map.values().size(); + + if (verbose) System.out.println("After GC, size: " + size1); + + assertEquals("FAILURE: unexpected size of map", 3, size1); + assertEquals("FAILURE: unexpected size of entrySet", 3, size2); + assertEquals("FAILURE: unexpected size of values", 3, size3); + + map.clear(); + } + + /** */ + public void testEntrySet() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + y11 = null; + y12 = null; + + if (verbose) System.out.println("\nTest EntrySet:"); + if (verbose) printEntries(map); + + Set entrySet = map.entrySet(); + + if (verbose) System.out.println("size: " + entrySet.size()); + assertEquals("Unexpected size of entrySet", 5, entrySet.size()); + if (verbose) System.out.println("isEmpty: " + entrySet.isEmpty()); + assertFalse("Unexpected empty entrySet", entrySet.isEmpty()); + if (verbose) System.out.println("hashCode: " + entrySet.hashCode()); + + entrySet.remove(new Entry((Object) x12, (Object) new Y("y12", x12))); + + if (verbose) System.out.println("After remove(x12, y12):"); + if (verbose) printEntries(map); + + if (verbose) System.out.println("size: " + entrySet.size()); + assertEquals("Unexpected size of entrySet", 4, entrySet.size()); + if (verbose) System.out.println("isEmpty: " + entrySet.isEmpty()); + assertFalse("Unexpected empty entrySet", entrySet.isEmpty()); + if (verbose) System.out.println("hashCode: " + entrySet.hashCode()); + + // Remove a specific entry, then print the map; the entry must be + // gone from there. + for (Iterator i = entrySet.iterator(); i.hasNext();) { + Map.Entry ent = (Map.Entry) i.next(); + if (ent.getKey() == x11) { + i.remove(); + } + } + + if (verbose) System.out.println("After Iterator.remove:"); + if (verbose) printEntries(map); + + if (verbose) System.out.println("size: " + entrySet.size()); + assertEquals("Unexpected size of entrySet", 3, entrySet.size()); + if (verbose) System.out.println("isEmpty: " + entrySet.isEmpty()); + assertFalse("Unexpected empty entrySet", entrySet.isEmpty()); + if (verbose) System.out.println("hashCode: " + entrySet.hashCode()); + + entrySet.clear(); + if (verbose) System.out.println("After clear:"); + + if (verbose) System.out.println("size: " + entrySet.size()); + assertEquals("Unexpected size of entrySet", 0, entrySet.size()); + if (verbose) System.out.println("isEmpty: " + entrySet.isEmpty()); + assertTrue("Unexpected non empty entrySet", entrySet.isEmpty()); + if (verbose) System.out.println("hashCode: " + entrySet.hashCode()); + } + + /** */ + public void testValues() { + Map map = new WeakValueHashMap(); + + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + y11 = null; + y12 = null; + + Collection values = map.values(); + + if (verbose) System.out.println("\nTest Values:"); + if (verbose) printValues(map); + assertEquals("Unexpected size of values", 5, values.size()); + + // now gc + gc(); + + if (verbose) System.out.println("After GC:"); + if (verbose) printValues(map); + assertEquals("Unexpected size of values", 3, values.size()); + + values.remove(new Y("y11")); + + if (verbose) System.out.println("After remove(y11):"); + if (verbose) printValues(map); + assertEquals("Unexpected size of values", 2, values.size()); + + map.clear(); + } + + /** */ + public void testAll() { + Map weakMap = new WeakValueHashMap(); + + // WeakHashMap + Y y11 = new Y("y11"); + X x11 = new X("x11", y11); + X x12 = new X("x12"); + Y y12 = new Y("y12", x12); + X x14 = new X("x14"); + Y y14 = new Y("y14"); + weakMap.put(x11, y11); + weakMap.put(x12, y12); + weakMap.put(new X("x13"), new Y("y13")); + weakMap.put(x14, null); + weakMap.put(null, y14); + + // Regular HashMap + Map map = new HashMap(); + map.put(x11, y11); + map.put(x12, y12); + map.put(new X("x13"), new Y("y13")); + map.put(x14, null); + map.put(null, y14); + + if (verbose) System.out.println("\nTest equals:"); + if (verbose) System.out.println("Weak map Map1:"); if (verbose) printEntries(weakMap); + if (verbose) System.out.println("Regular map Map2:"); if (verbose) printEntries(map); + if (verbose) System.out.println("Map2.equals(Map1): " + map.equals(weakMap)); + assertTrue("map.equals(weakMap) should return true", map.equals(weakMap)); + if (verbose) System.out.println("Map1.equals(Map2): " + weakMap.equals(map)); + assertTrue("weakMap.equals(map) should return true", weakMap.equals(map)); + + weakMap.put(new X("x15"), new Y("y15")); + map.put(new X("x16"), new Y("y16")); + + if (verbose) System.out.println("\nTest equals:"); + if (verbose) System.out.println("Weak map Map1:"); if (verbose) printEntries(weakMap); + if (verbose) System.out.println("Regular map Map2:"); if (verbose) printEntries(map); + if (verbose) System.out.println("Map2.equals(Map1): " + map.equals(weakMap)); + assertFalse("map.equals(weakMap) should return false", map.equals(weakMap)); + if (verbose) System.out.println("Map1.equals(Map2): " + weakMap.equals(map)); + assertFalse("weakMap.equals(map) should return false", weakMap.equals(map)); + + weakMap.remove(new X("x15")); + map.clear(); + map.put(new X("x15"), new Y("y15")); + map.put(new X("x16"), new Y("y16")); + + if (verbose) System.out.println("\nTest putAll:"); + if (verbose) System.out.println("Weak map Map1:"); if (verbose) printEntries(weakMap); + if (verbose) System.out.println("Regular map Map2:"); if (verbose) printEntries(map); + weakMap.putAll(map); + if (verbose) System.out.println("Map1.putAll(Map2):"); if (verbose) printEntries(weakMap); + assertEquals("Unexpected size of Weak map", 7, weakMap.size()); + weakMap.remove(new X("x15")); + weakMap.remove(new X("x16")); + + if (verbose) System.out.println("\nTest putAll:"); + if (verbose) System.out.println("Weak map Map1:"); if (verbose) printEntries(weakMap); + if (verbose) System.out.println("Regular map Map2:"); if (verbose) printEntries(map); + map.putAll(weakMap); + if (verbose) System.out.println("Map2.putAll(Map1):"); if (verbose) printEntries(map); + assertEquals("Unexpected size of map", 7, map.size()); + + weakMap.clear(); + } + + /* Internal class for entries */ + private static class Entry implements Map.Entry { + private Object key; + private Object value; + + Entry(Object key, Object value) { + this.key = key; + this.value = value; + } + + public Object getKey() { + return key; + } + + public Object getValue() { + return value; + } + + public Object setValue(Object value) { + Object oldValue = this.value; + this.value = value; + return oldValue; + } + + private static boolean valEquals(Object o1, Object o2) { + return (o1 == null) ? (o2 == null) : o1.equals(o2); + } + + public boolean equals(Object o) { + if (!(o instanceof Map.Entry)) return false; + Map.Entry e = (Map.Entry) o; + return (valEquals(key, e.getKey()) + && valEquals(value, e.getValue())); + } + + public int hashCode() { + Object k; + return (((key == null) ? 0 : key.hashCode()) + ^ ((value == null) ? 0 : value.hashCode())); + } + + } + + private static class X { + private String name; + private Y y; + public X(String name) { this.name = name; } + public X(String name, Y y) { this.name = name; this.y = y; } + public String toString() { return "(" + this.name + "|" + y + ")"; } + public int hashCode() { return (name == null) ? 0 : name.hashCode(); } + public boolean equals(Object o) + { + // compare by name only + if ((o == null) || !(o instanceof X)) + return false; + else if (name == null) + return ((X)o).name == null; + else + return name.equals(((X)o).name); + } + } + + private static class Y implements Comparable { + private String name; + private X x; + public Y(String name) { this.name = name; } + public Y(String name, X x) { this.name = name; this.x = x; } + public String toString() { return "(" + this.name + "|" + x + ")"; } + public int hashCode() { return (name == null) ? 0 : name.hashCode(); } + public int compareTo(Object o) { + if (o != null + && o instanceof Y) { + return toString().compareTo(o.toString()); + } else { + return 1; + } + } + public boolean equals(Object o) + { + // compare by name only + if ((o == null) || !(o instanceof Y)) + return false; + else if (name == null) + return ((Y)o).name == null; + else + return name.equals(((Y)o).name); + } + } + + /** + * A sorted set of instances of map entries. + */ + private static class XTreeSet extends TreeSet { + private static final Comparator c = new Comparator() { + public int compare(Object o1, Object o2) { + if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } else { + Object s1 = ((Map.Entry)o1).getKey(); + Object s2 = ((Map.Entry)o2).getKey(); + if (s1 == null) { + return -1; + } else if (s2 == null) { + return 1; + } else { + return (s1.toString()).compareTo(s2.toString()); + } + } + } + + public boolean equals(Object obj) { + return obj.equals(this); + } + }; + + XTreeSet(Collection col) { + super(c); + this.addAll(col); + } + } + + private static void printEntries(Map map) { + Set entrySet = map.entrySet(); + TreeSet sorted = new XTreeSet(entrySet); + for (Iterator i = sorted.iterator(); i.hasNext();) { + Map.Entry ent = (Map.Entry) i.next(); + System.out.println("Key: " + ent.getKey() + + " Val: " + ent.getValue()); + } + } + + private static void printValues(Map map) { + TreeSet values = new TreeSet(map.values()); + for (Iterator i = values.iterator(); i.hasNext();) { + System.out.println("Val: " + i.next()); + } + } + + private static void gc() { + //System.out.print("gc: "); + Runtime rt = Runtime.getRuntime(); + long oldfree; + long newfree = rt.freeMemory(); + do { + oldfree = newfree; + rt.runFinalization(); rt.gc(); + newfree = rt.freeMemory(); + //System.out.print('.'); + } while (newfree > oldfree); + //System.out.println(); + } + +} + +
Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/AdvancedTest.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/AdvancedTest.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/AdvancedTest.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/AdvancedTest.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,1856 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/* + * AdvancedTest.java + * + * Created on April 10, 2000 + */ + +package org.apache.jdo.test.query; + +import java.util.*; +import java.io.*; +import java.io.PrintStream; + +import javax.jdo.*; + +import org.apache.jdo.pc.xempdept.Department; +import org.apache.jdo.pc.xempdept.Employee; +import org.apache.jdo.pc.xempdept.PrimitiveTypes; +import org.apache.jdo.pc.xempdept.Project; + +/** + * + * @author Michael Bouschen + */ +public class AdvancedTest + extends PositiveTest +{ + public AdvancedTest(PersistenceManagerFactory pmf, PrintStream log) + { + super(pmf, log); + } + + protected boolean isTestMethodName(String methodName) + { + return methodName.startsWith("advanced"); + } + + // ========================================================================== + // Test methods + // ========================================================================== + + /** + * Testcase: check for null values == + */ + public void advanced001() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname == null"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: check for null values != + */ + public void advanced002() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname != null"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 8; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: serialize/deserialize non compiled query + */ + public void advanced003() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("empid == 3"); + setEmployeeCandidates(query); + + // serialize query object + ByteArrayOutputStream bout = new ByteArrayOutputStream (); + ObjectOutputStream oout = new ObjectOutputStream (bout); + oout.writeObject (query); + oout.flush (); + byte[] bytes = bout.toByteArray(); + oout.close (); + + // deserialize query object + ByteArrayInputStream bin = new ByteArrayInputStream (bytes); + ObjectInputStream oin = new ObjectInputStream (bin); + query = (Query)oin.readObject (); + oin.close (); + + // init and execute query + query = pm.newQuery(query); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: serialize/deserialize compiled query + * checks bug report 4518967 + */ + public void advanced004() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("empid == 3"); + setEmployeeCandidates(query); + query.compile(); + + // serialize query object + ByteArrayOutputStream bout = new ByteArrayOutputStream (); + ObjectOutputStream oout = new ObjectOutputStream (bout); + oout.writeObject (query); + oout.flush (); + byte[] bytes = bout.toByteArray(); + oout.close (); + + // deserialize query object + ByteArrayInputStream bin = new ByteArrayInputStream (bytes); + ObjectInputStream oin = new ObjectInputStream (bin); + query = (Query)oin.readObject (); + oin.close (); + + // init and execute query + query = pm.newQuery(query); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: serialize/deserialize compiled query + * (using more compilated query) + * checks bug report 4518967 + */ + public void advanced005() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.declareParameters("Project p"); + query.declareVariables("Project project"); + query.setFilter("projects.contains(project) & project.projid == p.projid"); + setEmployeeCandidates(query); + query.compile(); + + // serialize query object + ByteArrayOutputStream bout = new ByteArrayOutputStream (); + ObjectOutputStream oout = new ObjectOutputStream (bout); + oout.writeObject (query); + oout.flush (); + byte[] bytes = bout.toByteArray(); + oout.close (); + + // deserialize query object + ByteArrayInputStream bin = new ByteArrayInputStream (bytes); + ObjectInputStream oin = new ObjectInputStream (bin); + query = (Query)oin.readObject (); + oin.close (); + + // init and execute query + Project p = getProjectById(1L); + + query = pm.newQuery(query); + setEmployeeCandidates(query); + Object result = query.execute(p); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: re-execute the same query w/o recompile + * checks bug report 4349720 + */ + public void advanced006() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("empid == 6");; + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + checkQueryResult(oids, expected); + + // rexecute the query + + result = query.execute(); + oids = getResultOids(result); + + // check new query result + + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: use ESCuxxxx chars in string literal + */ + public void advanced007() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname == \"\u0066irstEngOne\"");; + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: use ESCxxx chars in string literal + */ + public void advanced008() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname == \"\146irstEngOne\"");; + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries startsWith + */ + public void advanced009() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname.startsWith(\"firstEng\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries endsWith + */ + public void advanced010() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname.endsWith(\"e\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries startsWith with non constant arguments + */ + public void advanced011() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname.startsWith(\"first\" + department.name)"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries endsWith with non constant arguments + */ + public void advanced012() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("firstname.endsWith(department.name + \"Five\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries startsWith with simple navigation + * checks bug report 4418353 + */ + public void advanced015() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("department.name.startsWith(\"Eng\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries endsWith with simple navigation + * checks bug report 4418353 + */ + public void advanced016() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("department.name.endsWith(\"ing\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries startsWith with collection navigation + * checks bug report 4418353 + */ + public void advanced017() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.declareImports("import org.apache.jdo.pc.xempdept.Project"); + query.declareVariables("Project p"); + query.setFilter("projects.contains(p) & p.name.startsWith(\"E\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: wildcard queries endsWith with collection navigation + * checks bug report 4418353 + */ + public void advanced018() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.declareImports("import org.apache.jdo.pc.xempdept.Project"); + query.declareVariables("Project p"); + query.setFilter("projects.contains(p) & p.name.endsWith(\"g Project\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: no filter specified + */ + public void advanced019() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + key = new Department.Oid(); + key.deptid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: filter specified as empty string + */ + public void advanced020() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter(" "); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + key = new Department.Oid(); + key.deptid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize boolean constant == boolean constant + */ + public void advanced021() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("true == !true"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize boolean constant != boolean constant + */ + public void advanced022() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("true != (employees == null)"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize boolean constant == expr + */ + public void advanced023() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("true == (empid == 1)"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize expr == boolean constant + * checks bug report 4444393 + */ + public void advanced024() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("employees.isEmpty() == false"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize boolean constant != expr + */ + public void advanced025() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("false != firstname.startsWith(\"firstEng\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize expr != boolean constant + */ + public void advanced026() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("employees.isEmpty() != true"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize expr && true + */ + public void advanced027() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("(empid == 1) && true"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize expr & false + */ + public void advanced028() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("lastname.endsWith(\"bla\") & false"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: true & expr + */ + public void advanced029() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("true & team.isEmpty()"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: false && expr + */ + public void advanced030() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("(team == null) && (empid == 1)"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + /** + * Testcase: optimize expr | true + */ + public void advanced031() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("!employees.isEmpty() | true"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + key = new Department.Oid(); + key.deptid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize expr || false + */ + public void advanced032() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("(empid == 1) || false"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: true || expr + */ + public void advanced033() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("true || firstname.startsWith(\"bla\")"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 4; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 6; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 8; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: false | expr + */ + public void advanced034() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("false | (empid == 2)"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: !true + */ + public void advanced035() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("!true"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: invert inner equality comparison, + * because of outer equality comparison with constant + */ + public void advanced036() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("(empid != 1) == false"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: invert inner logical not, + * because of outer equality comparison with constant + */ + public void advanced037() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("!team.isEmpty() == false"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + key = new Employee.Oid(); + key.empid = 3; + expected.add(key); + key = new Employee.Oid(); + key.empid = 5; + expected.add(key); + key = new Employee.Oid(); + key.empid = 7; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: multiple boolean constant expressions + */ + public void advanced038() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter(" ((empid == 1) && !(false)) | (false && true) || ((empid == 2) == true)"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 1; + expected.add(key); + key = new Employee.Oid(); + key.empid = 2; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize constant == constant for non boolean constants + * checks bug report 4481691 + */ + public void advanced039() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("1 == 1"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Department.Oid key = new Department.Oid(); + key.deptid = 1; + expected.add(key); + key = new Department.Oid(); + key.deptid = 2; + expected.add(key); + key = new Department.Oid(); + key.deptid = 3; + expected.add(key); + key = new Department.Oid(); + key.deptid = 4; + expected.add(key); + key = new Department.Oid(); + key.deptid = 11; + expected.add(key); + key = new Department.Oid(); + key.deptid = 12; + expected.add(key); + key = new Department.Oid(); + key.deptid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: optimize constant != constant for non boolean constants + * checks bug report 4481691 + */ + public void advanced040() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(Department.class); + query.setFilter("1 != 1"); + setDepartmentCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + + + /** + * Testcase: Method getPersistenceManager should return null + * if called for a deserialized, unbound query object. + * checks bug report 4547932 + */ + public void advanced041() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("empid == 3"); + + // serialize query object + ByteArrayOutputStream bout = new ByteArrayOutputStream (); + ObjectOutputStream oout = new ObjectOutputStream (bout); + oout.writeObject (query); + oout.flush (); + byte[] bytes = bout.toByteArray(); + oout.close (); + + // deserialize query object + ByteArrayInputStream bin = new ByteArrayInputStream (bytes); + ObjectInputStream oin = new ObjectInputStream (bin); + query = (Query)oin.readObject (); + oin.close (); + + PersistenceManager otherPM = query.getPersistenceManager(); + if (otherPM != null) + throw new Exception("Expected null PM for deserialized, unbound query object, pm == " + pm); + tx.commit(); + } + + /** + * Testcase: filter uses boolean fields (booleanField == true) + */ + public void advanced042() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("booleanNull == true"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 1; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 3; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 5; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 11; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: use of hex literal + * checks bug report 4818832 + */ + public void advanced043() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("intNotNull == 0x7fffffff"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 10; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: use of oct literal + * checks bug report 4818832 + */ + public void advanced044() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + // create query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("intNotNull == 017777777777"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 10; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: filter using Integer.MIN_VALUE + * checks bug report 4818832 + */ + public void advanced045() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("longNotNull == -2147483648"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: navigation through a null valued relationship field. + * checks bug report 4833898 + */ + public void advanced046() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("this.department.deptid > 3 || this.empid == 100"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 8; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: navigation through a null valued relationship field. + * checks bug report 4833898 + */ + public void advanced047() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(Employee.class); + query.setFilter("this.empid == 100 | this.department.deptid > 3"); + setEmployeeCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + Employee.Oid key = new Employee.Oid(); + key.empid = 8; + expected.add(key); + key = new Employee.Oid(); + key.empid = 9; + expected.add(key); + key = new Employee.Oid(); + key.empid = 11; + expected.add(key); + key = new Employee.Oid(); + key.empid = 12; + expected.add(key); + key = new Employee.Oid(); + key.empid = 13; + expected.add(key); + key = new Employee.Oid(); + key.empid = 100; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } +} + Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/ArithmeticTest.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/ArithmeticTest.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/ArithmeticTest.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/query/ArithmeticTest.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,324 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/* + * ArithmeticTest.java + * + * Created on April 10, 2000 + */ + +package org.apache.jdo.test.query; + +import java.util.*; +import java.io.PrintStream; + +import javax.jdo.*; + +import org.apache.jdo.pc.xempdept.PrimitiveTypes; + +/** + * + * @author Michael Bouschen + */ +public class ArithmeticTest + extends PositiveTest +{ + public ArithmeticTest(PersistenceManagerFactory pmf, PrintStream log) + { + super(pmf, log); + } + + protected boolean isTestMethodName(String methodName) + { + return methodName.startsWith("arithmetic"); + } + + // ========================================================================== + // Test methods + // ========================================================================== + + /** + * Testcase: arithmetic operation + * field == literal + literal + */ + public void arithmetic001() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("intNotNull == ( 1 + 2 )"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 3; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: arithmetic operation + * field - literal > literal + */ + public void arithmetic002() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("floatNotNull - 10000.0 > 10000.0"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 14; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: arithmetic operation + * field * literal > literal + */ + public void arithmetic003() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("doubleNotNull * 10 > 45.0"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 5; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 14; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: arithmetic operation + * field / literal < literal + */ + public void arithmetic004() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("doubleNull > 0.0 && (doubleNull / 10.0 < 0.5)"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 1; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 2; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 3; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 4; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 10; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 12; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: unary operation + + */ + public void arithmetic005() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("+byteNotNull == 1"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: unary operation - + * check bug report 4437010 + */ + public void arithmetic006() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("-byteNull == -1"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: complement operation ~ + */ + public void arithmetic007() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("~shortNotNull == -2"); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 1; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + + /** + * Testcase: string concatenation operator + + */ + public void arithmetic008() + throws Exception + { + Transaction tx = pm.currentTransaction(); + tx.begin(); + + // create and execute query + Query query = pm.newQuery(); + query.setClass(PrimitiveTypes.class); + query.setFilter("stringNull == \"yet \" + \"another string\""); + setPrimitiveTypesCandidates(query); + Object result = query.execute(); + List oids = getResultOids(result); + Collections.sort(oids); + + // check query result + + Collection expected = new ArrayList(); + PrimitiveTypes.Oid key = new PrimitiveTypes.Oid(); + key.id = 12; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 13; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 14; + expected.add(key); + key = new PrimitiveTypes.Oid(); + key.id = 15; + expected.add(key); + checkQueryResult(oids, expected); + + tx.commit(); + } + +} +