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.cache;
020:
021: import java.util.List;
022: import javax.persistence.EntityManager;
023: import javax.persistence.NamedQuery;
024:
025: import org.apache.openjpa.persistence.cache.common.apps.Entity1;
026: import org.apache.openjpa.persistence.cache.common.apps.Entity2;
027: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
028:
029: @NamedQuery(name="setParam1",query="SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
030: public class TestQueryCache extends AbstractTestCase {
031:
032: EntityManager em;
033:
034: public TestQueryCache(String name) {
035: super (name);
036: System.setProperty("cactus.contextURL",
037: "http://localhost:9000/cachecactus");
038: em = currentEntityManager();
039: }
040:
041: /*public static Test suite()
042: {
043: ServletTestSuite suite = new ServletTestSuite();
044: suite.addTestSuite(TestQueryCache.class);
045: return suite;
046: }*/
047: public void setUp() {
048: System.setProperty("cactus.contextURL",
049: "http://localhost:9000/cactuswebapp");
050:
051: //deleteAll(Entity2.class);
052: deleteAll(Entity1.class);
053:
054: int instNum = 10;
055:
056: startTx(em);
057:
058: //create and persist multiple entity1 instances
059: for (int i = 0; i < instNum; i++) {
060: Entity1 ent = new Entity1(i, "string" + i, i + 2);
061: Entity2 ent2 = new Entity2(i * 2, "ent2" + i, i);
062: ent.setEntity2Field(ent2);
063: em.persist(ent);
064: }
065:
066: endTx(em);
067: endEm(em);
068: }
069:
070: public void testResultList() {
071: em = currentEntityManager();
072: List list = em.createQuery("Select object(o) from Entity1 o")
073: .getResultList();
074:
075: assertEquals(10, list.size());
076:
077: endEm(em);
078: }
079:
080: public void testGetSingleList() {
081: em = currentEntityManager();
082: String curr = 2 + "";
083:
084: Entity1 ret = (Entity1) em.createQuery(
085: "SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
086: .setParameter("pk", curr).getSingleResult();
087:
088: assertNotNull(ret);
089: assertEquals("string2", ret.getStringField());
090: assertEquals(4, ret.getIntField());
091:
092: endEm(em);
093: }
094:
095: public void testExecuteUpdate() {
096: String curr = 2 + "";
097: String curr2 = 22 + "";
098:
099: em = currentEntityManager();
100: startTx(em);
101:
102: Entity1 entity1 = (Entity1) em.createQuery(
103: "SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
104: .setParameter("pk", curr).getSingleResult();
105:
106: int ret = em.createQuery(
107: "Delete FROM Entity1 o WHERE o.pk LIKE :pk")
108: .setParameter("pk", curr).executeUpdate();
109: assertEquals(ret, 1);
110:
111: //cascade remove doesn't remove the entity2
112: int retTmp = em.createQuery(
113: "Delete FROM Entity2 o WHERE o.pk LIKE :pk")
114: .setParameter("pk", entity1.getEntity2Field().getPk())
115: .executeUpdate();
116:
117: int ret2 = em.createQuery(
118: "Delete FROM Entity1 o WHERE o.pk LIKE :pk")
119: .setParameter("pk", curr2).executeUpdate();
120:
121: assertEquals(ret2, 0);
122:
123: endTx(em);
124: endEm(em);
125: }
126:
127: public void testSetMaxResults() {
128: em = currentEntityManager();
129:
130: List l = em.createQuery("Select object(o) from Entity1 o")
131: .setMaxResults(5).getResultList();
132:
133: assertNotNull(l);
134: assertEquals(5, l.size());
135:
136: endEm(em);
137: }
138:
139: public void testSetFirstResults() {
140: em = currentEntityManager();
141:
142: List l = em.createQuery("Select object(o) from Entity1 o")
143: .setFirstResult(3).getResultList();
144:
145: Entity1 ent = (Entity1) l.get(0);
146:
147: assertNotNull(ent);
148: assertEquals("string3", ent.getStringField());
149: assertEquals(5, ent.getIntField());
150:
151: endEm(em);
152: }
153:
154: // Tests Binding an argument to a named parameter.
155: // pk, the named parameter --Not working yet--
156: public void xxxtestSetParameter1() {
157:
158: em = currentEntityManager();
159: String curr = 2 + "";
160:
161: List ret = em.createQuery(
162: "SELECT o FROM Entity1 o WHERE o.pk LIKE :pk")
163: .setParameter("pk", curr).getResultList();
164:
165: assertNotNull(ret);
166: assertEquals(1, ret.size());
167:
168: ret = em.createNamedQuery("setParam1").setParameter("pk", curr)
169: .getResultList();
170:
171: assertNotNull(ret);
172: assertEquals(1, ret.size());
173:
174: endTx(em);
175: }
176:
177: //rest of the interface is tested by the CTS
178: }
|