Package org.xorm.tests

Source Code of org.xorm.tests.TestQuery

package org.xorm.tests;

import org.xorm.tests.model.*;
import org.xorm.XORM;
import javax.jdo.PersistenceManager;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import org.xorm.ClassMapping;
import org.xorm.datastore.*;
import org.xorm.query.*;

public class TestQuery extends XORMTestCase {
    public TestQuery(String name) {
  super(name);
    }

    private void printResults(Collection results) {
  System.out.println("Results:");
  Iterator i = results.iterator();
  while (i.hasNext()) {
      Employee e = (Employee) i.next();
      System.out.println(e.getFirstName());
      System.out.println(e.getLastName());
      if (e.getAddress() != null) {
    System.out.println(e.getAddress().getZipCode());
      }
  }
    }

    public void testQuery() {
  PersistenceManager mgr = factory.getPersistenceManager();
  mgr.currentTransaction().begin();
 
  mgr.getObjectById(XORM.newObjectId(Employee.class, 55), true);

  // Test field == value
  Collection results;
  results = (Collection) mgr.newQuery(Employee.class, "lastName == \"Random11\"").execute();
  printResults(results);

  // Test field == value with "this"
  results = (Collection) mgr.newQuery(Employee.class, "this.lastName == \"Random11\"").execute();
  printResults(results);

  // Test field.function(value)
  results = (Collection) mgr.newQuery(Employee.class, "lastName.startsWith(\"Random9\")").execute();
  printResults(results);

  // Test field.field == value
  results = (Collection) mgr.newQuery(Employee.class, "address.zipCode == \"99033\"").execute();
  printResults(results);

  // Test field.field.function(value)
  results = (Collection) mgr.newQuery(Employee.class, "address.zipCode.endsWith(\"34\")").execute();
  printResults(results);

  // Test a combo
  results = (Collection) mgr.newQuery(Employee.class, "address.zipCode.endsWith(\"34\") || lastName == \"Random22\"").execute();// && firstName == \"J22\"").execute();
  printResults(results);

  // Test full fetch
  //results = (Collection) mgr.newQuery(Employee.class).execute();
  //printResults(results);

  mgr.currentTransaction().commit();
  mgr.close();
    }

    public void doCreate() {
  PersistenceManager mgr = factory.getPersistenceManager();
  mgr.currentTransaction().begin();

  for (int i = 0; i < 100; i++) {
      Address a = (Address) XORM.newInstance(factory, Address.class);
      a.setStreet("Street" + i);
      a.setCity("City"+i);
      a.setState("State"+i);
      a.setZipCode(String.valueOf(99000+i));
     
      Employee e = (Employee) XORM.newInstance(factory, Employee.class);
      e.setFirstName("J"+i);
      e.setLastName("Random"+i);
      e.setAddress(a);
     
      mgr.makePersistent(e);
  }
  mgr.currentTransaction().commit();
  mgr.close();
    }

    public static void main(String[] argv) {
  TestQuery tq = new TestQuery("TestQuery");
  tq.setUp();
  if (argv.length > 0) {
      if ("create".equals(argv[0])) {
      tq.doCreate();
      }
  } else {
      tq.testQuery();
  }
  tq.tearDown();
    }
}
TOP

Related Classes of org.xorm.tests.TestQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.