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.datacache;
020:
021: import java.util.Collection;
022: import java.util.Date;
023: import java.util.HashMap;
024: import java.util.Map;
025: import javax.persistence.EntityManagerFactory;
026:
027: import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectA;
028: import org.apache.openjpa.persistence.datacache.common.apps.CacheObjectAChild1;
029: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
030:
031: import org.apache.openjpa.kernel.Broker;
032: import org.apache.openjpa.kernel.BrokerFactory;
033: import org.apache.openjpa.kernel.Query;
034: import org.apache.openjpa.kernel.jpql.JPQLParser;
035: import org.apache.openjpa.persistence.JPAFacadeHelper;
036:
037: public class TestMutableParameters extends AbstractTestCase {
038:
039: public TestMutableParameters(String test) {
040: super (test, "datacachecactusapp");
041: }
042:
043: private BrokerFactory _factory;
044: private Date _startDate;
045:
046: public void setUp() throws Exception {
047: deleteAll(CacheObjectA.class);
048: Map propsMap = new HashMap();
049: propsMap.put("openjpa.DataCache", "true");
050: propsMap.put("openjpa.RemoteCommitProvider", "sjvm");
051: propsMap.put("openjpa.BrokerImpl", CacheTestBroker.class
052: .getName());
053:
054: EntityManagerFactory emf = getEmf(propsMap);
055: _factory = JPAFacadeHelper.toBrokerFactory(emf);
056:
057: // create a very early date so that when we mutate it, we
058: // won't need to worry about precision issues.
059: _startDate = new java.text.SimpleDateFormat("dd/MM/yyyy")
060: .parse("01/01/1990");
061: Broker broker = _factory.newBroker();
062: broker.begin();
063: for (int i = 0; i < 50; i++) {
064: CacheObjectAChild1 o = new CacheObjectAChild1("",
065: "JPQL2Queries", i);
066: o.setDate(_startDate);
067: broker.persist(o, null);
068: }
069: broker.commit();
070: broker.close();
071:
072: CacheTestHelper.cacheManager(_factory).getSystemQueryCache()
073: .clear();
074: }
075:
076: public void tearDown() throws Exception {
077: try {
078: _factory.close();
079: } catch (Exception e) {
080: }
081:
082: super .tearDown();
083: }
084:
085: public void testMutatedDateParameter() {
086: Broker broker = _factory.newBroker();
087: Query q = broker.newQuery(JPQLParser.LANG_JPQL,
088: "select a from "
089: + CacheObjectAChild1.class.getSimpleName()
090: + " a where a.date < :p_date");
091: Date d = new Date();
092: Collection c = (Collection) q.execute(new Object[] { d });
093: CacheTestHelper.iterate(c);
094: int initialSize = c.size();
095: CacheTestHelper.assertInCache(this , q, Boolean.TRUE,
096: new Object[] { d });
097: d.setTime(_startDate.getTime());
098: CacheTestHelper.assertInCache(this , q, Boolean.FALSE,
099: new Object[] { d });
100: c = (Collection) q.execute(new Object[] { d });
101:
102: assertFalse(new Integer(initialSize).equals(new Integer(c
103: .size())));
104: }
105:
106: //FIXME Seetha Nov 10,2006
107: //need to find the JPQL query for :p_age contains
108: /*
109: public void testMutatedSetParameter() {
110: mutatedCollectionParameterHelper(true);
111: }
112:
113: public void testMutatedListParameter() {
114: mutatedCollectionParameterHelper(false);
115: }
116:
117: public void mutatedCollectionParameterHelper(boolean set) {
118: Broker broker = _factory.newBroker();
119: Query q =
120: broker.newQuery(JPQLParser.LANG_JPQL,
121: "select a from "+
122: CacheObjectAChild1.class.getSimpleName()+ "a :p_ages.contains (age)");
123: Collection c_param;
124: if (set)
125: c_param = new HashSet();
126: else
127: c_param = new LinkedList();
128: c_param.add(new Long(0));
129: c_param.add(new Long(1));
130: Collection c = (Collection) q.execute(new Object[]{ c_param });
131: CacheTestHelper.iterate(c);
132: int initialSize = c.size();
133: CacheTestHelper.assertInCache(this, q, Boolean.TRUE,
134: new Object[]{ c_param });
135: c_param.add(new Long(2));
136: CacheTestHelper.assertInCache(this, q, Boolean.FALSE,
137: new Object[]{ c_param });
138: c = (Collection) q.execute(new Object[]{ c_param });
139: assertFalse(new Integer(initialSize).equals(new Integer(c.size())));
140: }
141:
142: public void testMutatedSetParameterDates() {
143: mutatedCollectionParameterDatesHelper(true);
144: }
145:
146: public void testMutatedListParameterDates() {
147: mutatedCollectionParameterDatesHelper(false);
148: }
149:
150: public void mutatedCollectionParameterDatesHelper(boolean set) {
151: Broker broker = _factory.newBroker();
152: Query q =
153: broker.newQuery(JPQLParser.LANG_JPQL, CacheObjectAChild1.class,
154: ":p_dates.contains (date)");
155: Collection c_param;
156: if (set)
157: c_param = new HashSet();
158: else
159: c_param = new LinkedList();
160: c_param.add(new Date());
161: c_param.add(new Date(System.currentTimeMillis() - 1000));
162: Collection c = (Collection) q.execute(new Object[]{ c_param });
163: CacheTestHelper.iterate(c);
164: int initialSize = c.size();
165: CacheTestHelper.assertInCache(this, q, Boolean.TRUE,
166: new Object[]{ c_param });
167: c_param.add(new Date(System.currentTimeMillis() - 500));
168: CacheTestHelper.assertInCache(this, q, Boolean.FALSE,
169: new Object[]{ c_param });
170: }*/
171: }
|