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.slice;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.persistence.EntityManager;
025: import javax.persistence.Query;
026:
027: /**
028: * Tests query ordering.
029: *
030: * @author Pinaki Poddar
031: *
032: */
033: public class TestQuery extends SliceTestCase {
034: private int POBJECT_COUNT = 25;
035: private int VALUE_MIN = 100;
036: private int VALUE_MAX = VALUE_MIN + POBJECT_COUNT - 1;
037:
038: protected String getPersistenceUnitName() {
039: return "ordering";
040: }
041:
042: public void setUp() throws Exception {
043: super .setUp(PObject.class, Person.class, Address.class,
044: CLEAR_TABLES);
045: int count = count(PObject.class);
046: if (count == 0) {
047: create(POBJECT_COUNT);
048: }
049: }
050:
051: void create(int N) {
052: EntityManager em = emf.createEntityManager();
053: em.getTransaction().begin();
054: for (int i = 0; i < POBJECT_COUNT; i++) {
055: PObject pc = new PObject();
056: pc.setValue(VALUE_MIN + i);
057: em.persist(pc);
058: String slice = SlicePersistence.getSlice(pc);
059: String expected = (pc.getValue() % 2 == 0) ? "Even" : "Odd";
060: assertEquals(expected, slice);
061: }
062: em.getTransaction().commit();
063: }
064:
065: public void testQueryResultIsOrderedAcrossSlice() {
066: EntityManager em = emf.createEntityManager();
067: em.getTransaction().begin();
068: Query query = em
069: .createQuery("SELECT p.value,p FROM PObject p ORDER BY p.value ASC");
070: List result = query.getResultList();
071: Integer old = Integer.MIN_VALUE;
072: for (Object row : result) {
073: Object[] line = (Object[]) row;
074: int value = ((Integer) line[0]).intValue();
075: PObject pc = (PObject) line[1];
076: assertTrue(value >= old);
077: old = value;
078: assertEquals(value, pc.getValue());
079: }
080: em.getTransaction().rollback();
081: }
082:
083: public void testAggregateQuery() {
084: EntityManager em = emf.createEntityManager();
085: em.getTransaction().begin();
086: Object count = em.createQuery("SELECT COUNT(p) FROM PObject p")
087: .getSingleResult();
088: Object max = em.createQuery(
089: "SELECT MAX(p.value) FROM PObject p").getSingleResult();
090: Object min = em.createQuery(
091: "SELECT MIN(p.value) FROM PObject p").getSingleResult();
092: Object sum = em.createQuery(
093: "SELECT SUM(p.value) FROM PObject p").getSingleResult();
094: em.getTransaction().rollback();
095:
096: assertEquals(POBJECT_COUNT, ((Number) count).intValue());
097: assertEquals(VALUE_MAX, ((Number) max).intValue());
098: assertEquals(VALUE_MIN, ((Number) min).intValue());
099: assertEquals((VALUE_MIN + VALUE_MAX) * POBJECT_COUNT,
100: 2 * ((Number) sum).intValue());
101: }
102:
103: public void testSetMaxResult() {
104: EntityManager em = emf.createEntityManager();
105: int limit = 3;
106: em.getTransaction().begin();
107: List result = em.createQuery(
108: "SELECT p.value,p FROM PObject p ORDER BY p.value ASC")
109: .setMaxResults(limit).getResultList();
110: int i = 0;
111: for (Object row : result) {
112: Object[] line = (Object[]) row;
113: int value = ((Integer) line[0]).intValue();
114: PObject pc = (PObject) line[1];
115: System.err.println(++i + "."
116: + SlicePersistence.getSlice(pc) + ":" + pc.getId()
117: + "," + pc.getValue());
118: }
119: assertEquals(limit, result.size());
120: em.getTransaction().rollback();
121: }
122:
123: public void testHint() {
124: List<String> targets = new ArrayList<String>();
125: targets.add("Even");
126: EntityManager em = emf.createEntityManager();
127: em.getTransaction().begin();
128: Query query = em.createQuery("SELECT p FROM PObject p");
129: query.setHint(ProductDerivation.HINT_TARGET, "Even");
130: List result = query.getResultList();
131: for (Object pc : result) {
132: String slice = SlicePersistence.getSlice(pc);
133: assertTrue(targets.contains(slice));
134: }
135: em.getTransaction().rollback();
136: }
137: }
|