Search in sources :

Example 1 with AbstractPerson

use of org.apache.cayenne.testdo.inheritance_people.AbstractPerson in project cayenne by apache.

the class SingleTableInheritanceIT method testPaginatedQueries.

@Test
public void testPaginatedQueries() throws Exception {
    create5PersonDataSet();
    SelectQuery select = new SelectQuery(AbstractPerson.class);
    select.addOrdering("db:" + AbstractPerson.PERSON_ID_PK_COLUMN, SortOrder.ASCENDING);
    select.setPageSize(3);
    List<AbstractPerson> results = context.performQuery(select);
    assertEquals(5, results.size());
    assertTrue(results.get(0) instanceof Employee);
    // this is where things would blow up per CAY-1142
    assertTrue(results.get(1) instanceof Manager);
    assertTrue(results.get(3) instanceof Manager);
    assertTrue(results.get(4) instanceof Employee);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 2 with AbstractPerson

use of org.apache.cayenne.testdo.inheritance_people.AbstractPerson in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicSharedCache.

@Test
public void testPolymorphicSharedCache() throws SQLException {
    tPerson.insert(1, "P1", "EM");
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", 1), false, ObjectIdQuery.CACHE);
    AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
    assertTrue(ap1 instanceof Manager);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use different context to ensure we hit shared cache
            AbstractPerson ap2 = (AbstractPerson) Cayenne.objectForQuery(context2, q1);
            assertTrue(ap2 instanceof Manager);
        }
    });
}
Also used : ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 3 with AbstractPerson

use of org.apache.cayenne.testdo.inheritance_people.AbstractPerson in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicLocalCache.

@Test
public void testPolymorphicLocalCache() throws SQLException {
    tPerson.insert(1, "P1", "EM");
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", 1), false, ObjectIdQuery.CACHE);
    AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
    assertTrue(ap1 instanceof Manager);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use same context to ensure we hit local cache
            // note that this does not guarantee test correctness. If local
            // cache polymorphic ID lookup is broken, shared cache will pick
            // it up
            AbstractPerson ap2 = (AbstractPerson) Cayenne.objectForQuery(context1, q1);
            assertTrue(ap2 instanceof Manager);
        }
    });
}
Also used : ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 4 with AbstractPerson

use of org.apache.cayenne.testdo.inheritance_people.AbstractPerson in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicSharedCache_AfterCayenneInsert.

@Test
public void testPolymorphicSharedCache_AfterCayenneInsert() throws SQLException {
    // see CAY-2101... we are trying to get a snapshot from a new object in the shared cache, and then read this
    // object via a relationship, so that shared cache is consulted
    Employee e = context1.newObject(Employee.class);
    e.setName("E1");
    e.setSalary(1234.01f);
    context1.commitChanges();
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", Cayenne.intPKForObject(e)), false, ObjectIdQuery.CACHE);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use different context to ensure we hit shared cache
            AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context2, q1);
            assertTrue(ap1 instanceof Employee);
        }
    });
}
Also used : Employee(org.apache.cayenne.testdo.inheritance_people.Employee) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 5 with AbstractPerson

use of org.apache.cayenne.testdo.inheritance_people.AbstractPerson in project cayenne by apache.

the class SingleTableInheritanceIT method testRelationshipAbstractFromSuperPrefetchingJoint.

@Test
public void testRelationshipAbstractFromSuperPrefetchingJoint() {
    context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (4, 'BB', 3)"));
    SelectQuery query = new SelectQuery(AbstractPerson.class);
    query.addPrefetch(AbstractPerson.NOTES.joint());
    final AbstractPerson person = (AbstractPerson) Cayenne.objectForQuery(context, query);
    assertTrue(person instanceof Employee);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, person.getNotes().size());
            String[] names = new String[2];
            names[0] = person.getNotes().get(0).getNotes();
            names[1] = person.getNotes().get(1).getNotes();
            List<String> nameSet = Arrays.asList(names);
            assertTrue(nameSet.contains("AA"));
            assertTrue(nameSet.contains("BB"));
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Aggregations

AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)6 Test (org.junit.Test)6 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)5 Employee (org.apache.cayenne.testdo.inheritance_people.Employee)4 ObjectId (org.apache.cayenne.ObjectId)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 Manager (org.apache.cayenne.testdo.inheritance_people.Manager)3 List (java.util.List)2 SQLTemplate (org.apache.cayenne.query.SQLTemplate)2 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)2