Package com.avaje.tests.query

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

package com.avaje.tests.query;

import java.util.concurrent.atomic.AtomicInteger;

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.FetchConfig;
import com.avaje.ebean.Query;
import com.avaje.ebean.QueryResultVisitor;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;

public class TestQueryFindVisit 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);

    final AtomicInteger counter = new AtomicInteger(0);

    query.findVisit(new QueryResultVisitor<Customer>() {

      public boolean accept(Customer bean) {
        counter.incrementAndGet();
        return true;
      }
    });

    Assert.assertEquals(2, counter.get());
  }
 
  /**
   * Test the behaviour when an exception is thrown inside the findVisit().
   */
  @Test(expected=IllegalStateException.class)
  public void testVisitThrowingException() {

    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);

    final AtomicInteger counter = new AtomicInteger(0);

    query.findVisit(new QueryResultVisitor<Customer>() {

      public boolean accept(Customer bean) {
        counter.incrementAndGet();
        if (counter.intValue() > 0) {
          throw new IllegalStateException("cause a failure");
        }
        return true;
      }
    });

    Assert.assertFalse("Never get here - exception thrown", true);
  }
}
TOP

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

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.