Package com.avaje.tests.query

Source Code of com.avaje.tests.query.TestQueryFindIterate

package com.avaje.tests.query;

import javax.persistence.PersistenceException;

import org.junit.Assert;
import org.junit.Test;

import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.Query;
import com.avaje.ebean.QueryIterator;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;

public class TestQueryFindIterate extends BaseTestCase {

  @Test
  public void test() {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);

    Query<Customer> query = server.find(Customer.class)
        .setAutofetch(false)
        //.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
        .setMaxRows(2);

    int count = 0;

    QueryIterator<Customer> it = query.findIterate();
    try {
      while (it.hasNext()) {
        Customer customer = it.next();
        customer.hashCode();
        count++;
      }
    } finally {
      it.close();
    }

    Assert.assertEquals(2, count);
  }
 
  @Test(expected=PersistenceException.class)
  public void testWithExceptionInQuery() {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);

    // intentionally a query with incorrect type binding
    Query<Customer> query = server.find(Customer.class)
        .setAutofetch(false)
        .where().gt("id","JUNK_NOT_A_LONG")
        .setMaxRows(2);

    // this throws an exception immediately
    QueryIterator<Customer> it = query.findIterate();
    it.hashCode();
    Assert.assertTrue("Never get here as exception thrown", false);
  }
 
 
  @Test(expected=IllegalStateException.class)
  public void testWithExceptionInLoop() {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);

    Query<Customer> query = server.find(Customer.class)
        .setAutofetch(false)
        .where().gt("id", 0)
        .setMaxRows(2);

    QueryIterator<Customer> it = query.findIterate();
    try {
      while (it.hasNext()) {
        Customer customer = it.next();
        if (customer != null) {
          throw new IllegalStateException("cause an exception");
        }
      }
     
    } finally {
      it.close();
    }
  }
}
TOP

Related Classes of com.avaje.tests.query.TestQueryFindIterate

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.