001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.query;
020:
021: import java.util.List;
022: import javax.persistence.EntityManager;
023: import javax.persistence.Query;
024:
025: import junit.textui.TestRunner;
026: import org.apache.openjpa.persistence.OpenJPAQuery;
027: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
028:
029: /**
030: * Test that we can exclude subclass instances from query results.
031: *
032: * @author Abe White
033: */
034: public class TestQueryExcludingSubclasses extends SingleEMFTestCase {
035:
036: public void setUp() {
037: setUp(ManyOneEntity.class, ManyOneEntitySub.class);
038:
039: ManyOneEntity e1 = new ManyOneEntity();
040: e1.setName("e1");
041: ManyOneEntity e2 = new ManyOneEntity();
042: e2.setName("e2");
043: ManyOneEntity invalid = new ManyOneEntity();
044: invalid.setName("invalid");
045: ManyOneEntitySub esub1 = new ManyOneEntitySub();
046: esub1.setName("esub1");
047: ManyOneEntitySub esub2 = new ManyOneEntitySub();
048: esub2.setName("esub2");
049: ManyOneEntitySub invalidsub = new ManyOneEntitySub();
050: invalidsub.setName("invalidsub");
051:
052: EntityManager em = emf.createEntityManager();
053: em.getTransaction().begin();
054: em.persist(e1);
055: em.persist(e2);
056: em.persist(invalid);
057: em.persist(esub1);
058: em.persist(esub2);
059: em.persist(invalidsub);
060: em.getTransaction().commit();
061: em.close();
062: }
063:
064: public void testQuery() {
065: EntityManager em = emf.createEntityManager();
066: Query q = em.createQuery("select e from ManyOneEntity e "
067: + "where e.name like 'e%' order by e.name asc");
068: List<ManyOneEntity> res = (List<ManyOneEntity>) q
069: .getResultList();
070: assertEquals(4, res.size());
071: for (int i = 0; i < 2; i++) {
072: assertEquals(ManyOneEntity.class, res.get(i).getClass());
073: assertEquals("e" + (i + 1), res.get(i).getName());
074: }
075: for (int i = 0; i < 2; i++) {
076: assertEquals(ManyOneEntitySub.class, res.get(i + 2)
077: .getClass());
078: assertEquals("esub" + (i + 1), res.get(i + 2).getName());
079: }
080: em.close();
081: }
082:
083: public void testQueryExcludingSubclasses() {
084: EntityManager em = emf.createEntityManager();
085: Query q = em.createQuery("select e from ManyOneEntity e "
086: + "where e.name like 'e%' order by e.name asc");
087: ((OpenJPAQuery) q).setSubclasses(false);
088: List<ManyOneEntity> res = (List<ManyOneEntity>) q
089: .getResultList();
090: assertEquals(2, res.size());
091: for (int i = 0; i < res.size(); i++) {
092: assertEquals(ManyOneEntity.class, res.get(i).getClass());
093: assertEquals("e" + (i + 1), res.get(i).getName());
094: }
095: em.close();
096: }
097:
098: public static void main(String[] args) {
099: TestRunner.run(TestQueryExcludingSubclasses.class);
100: }
101: }
|