001: package org.xorm.tests;
002:
003: import org.xorm.tests.model.*;
004: import org.xorm.XORM;
005: import javax.jdo.PersistenceManager;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.Set;
009: import java.util.HashSet;
010: import org.xorm.ClassMapping;
011: import org.xorm.datastore.*;
012: import org.xorm.query.*;
013:
014: public class TestQuery extends XORMTestCase {
015: public TestQuery(String name) {
016: super (name);
017: }
018:
019: private void printResults(Collection results) {
020: System.out.println("Results:");
021: Iterator i = results.iterator();
022: while (i.hasNext()) {
023: Employee e = (Employee) i.next();
024: System.out.println(e.getFirstName());
025: System.out.println(e.getLastName());
026: if (e.getAddress() != null) {
027: System.out.println(e.getAddress().getZipCode());
028: }
029: }
030: }
031:
032: public void testQuery() {
033: PersistenceManager mgr = factory.getPersistenceManager();
034: mgr.currentTransaction().begin();
035:
036: mgr.getObjectById(XORM.newObjectId(Employee.class, 55), true);
037:
038: // Test field == value
039: Collection results;
040: results = (Collection) mgr.newQuery(Employee.class,
041: "lastName == \"Random11\"").execute();
042: printResults(results);
043:
044: // Test field == value with "this"
045: results = (Collection) mgr.newQuery(Employee.class,
046: "this.lastName == \"Random11\"").execute();
047: printResults(results);
048:
049: // Test field.function(value)
050: results = (Collection) mgr.newQuery(Employee.class,
051: "lastName.startsWith(\"Random9\")").execute();
052: printResults(results);
053:
054: // Test field.field == value
055: results = (Collection) mgr.newQuery(Employee.class,
056: "address.zipCode == \"99033\"").execute();
057: printResults(results);
058:
059: // Test field.field.function(value)
060: results = (Collection) mgr.newQuery(Employee.class,
061: "address.zipCode.endsWith(\"34\")").execute();
062: printResults(results);
063:
064: // Test a combo
065: results = (Collection) mgr
066: .newQuery(Employee.class,
067: "address.zipCode.endsWith(\"34\") || lastName == \"Random22\"")
068: .execute();// && firstName == \"J22\"").execute();
069: printResults(results);
070:
071: // Test full fetch
072: //results = (Collection) mgr.newQuery(Employee.class).execute();
073: //printResults(results);
074:
075: mgr.currentTransaction().commit();
076: mgr.close();
077: }
078:
079: public void doCreate() {
080: PersistenceManager mgr = factory.getPersistenceManager();
081: mgr.currentTransaction().begin();
082:
083: for (int i = 0; i < 100; i++) {
084: Address a = (Address) XORM.newInstance(factory,
085: Address.class);
086: a.setStreet("Street" + i);
087: a.setCity("City" + i);
088: a.setState("State" + i);
089: a.setZipCode(String.valueOf(99000 + i));
090:
091: Employee e = (Employee) XORM.newInstance(factory,
092: Employee.class);
093: e.setFirstName("J" + i);
094: e.setLastName("Random" + i);
095: e.setAddress(a);
096:
097: mgr.makePersistent(e);
098: }
099: mgr.currentTransaction().commit();
100: mgr.close();
101: }
102:
103: public static void main(String[] argv) {
104: TestQuery tq = new TestQuery("TestQuery");
105: tq.setUp();
106: if (argv.length > 0) {
107: if ("create".equals(argv[0])) {
108: tq.doCreate();
109: }
110: } else {
111: tq.testQuery();
112: }
113: tq.tearDown();
114: }
115: }
|