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.kernel;
020:
021: import java.util.Comparator;
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.TreeSet;
029:
030: import org.apache.openjpa.persistence.kernel.common.apps.ProxiesPC;
031: import org.apache.commons.collections.comparators.ComparableComparator;
032: import org.apache.openjpa.persistence.OpenJPAEntityManager;
033: import org.apache.openjpa.util.Proxy;
034:
035: public class TestProxies2 extends BaseKernelTest {
036:
037: private int _oid = 0;
038: private Date _date = null;
039: private java.sql.Date _sqlDate = null;
040: private java.sql.Timestamp _timestamp = null;
041:
042: public TestProxies2(String casename) {
043: super (casename);
044: }
045:
046: public void setUp() {
047: deleteAll(ProxiesPC.class);
048:
049: OpenJPAEntityManager pm = getPM(false, false);
050: startTx(pm);
051:
052: long now = System.currentTimeMillis();
053: _date = new Date(now);
054: _sqlDate = new java.sql.Date(now);
055: _timestamp = new java.sql.Timestamp(now);
056:
057: ProxiesPC pc = new ProxiesPC("main");
058: pc.setDate(_date);
059: pc.setSQLDate(_sqlDate);
060: pc.setTimestamp(_timestamp);
061:
062: pc.getStringSet().add("val1");
063: pc.getStringSet().add("val2");
064: pc.getStringSet().add("val3");
065: pc.getStringSet().add(null);
066:
067: pc.getProxySet().add(new ProxiesPC("set1"));
068: pc.getProxySet().add(new ProxiesPC("set2"));
069: pc.getProxySet().add(new ProxiesPC("set3"));
070:
071: pc.getStringMap().put("key1", "1");
072: pc.getStringMap().put("key2", "2");
073: pc.getStringMap().put("key3", "3");
074: pc.getStringMap().put(null, "null");
075: pc.getStringMap().put("null", null);
076:
077: pc.getProxyMap().put("key1", new ProxiesPC("map1"));
078: pc.getProxyMap().put("key2", new ProxiesPC("map2"));
079: pc.getProxyMap().put("key3", new ProxiesPC("map3"));
080:
081: pc.getList().add("val1");
082: pc.getList().add("val1");
083: pc.getList().add("val2");
084: pc.getList().add("val3");
085: pc.getList().add("val3");
086:
087: pm.persist(pc);
088:
089: _oid = pc.getId();
090:
091: endTx(pm);
092: endEm(pm);
093: }
094:
095: public void testStringSet() {
096: OpenJPAEntityManager pm = getPM(false, false);
097: startTx(pm);
098: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
099:
100: // check that orig values are correct
101: Set set = pc.getStringSet();
102: assertEquals(4, set.size());
103: assertTrue(set.contains("val1"));
104: assertTrue(set.contains("val2"));
105: assertTrue(set.contains("val3"));
106: assertTrue(set.contains(null));
107:
108: // do some mods to try to confuse the proxy
109: set.remove("val1");
110: set.remove("val1");
111: set.add("val4");
112: set.remove("val4");
113: set.add("val5");
114: set.add("val5");
115: endTx(pm);
116: endEm(pm);
117:
118: // re-retrieve and check set
119: pm = getPM(false, false);
120: pc = pm.find(ProxiesPC.class, _oid);
121:
122: set = pc.getStringSet();
123: assertEquals(4, set.size());
124: assertTrue(!set.contains("val1"));
125: assertTrue(set.contains("val2"));
126: assertTrue(set.contains("val3"));
127: assertTrue(!set.contains("val4"));
128: assertTrue(set.contains("val5"));
129: assertTrue(set.contains(null));
130: endEm(pm);
131: }
132:
133: public void testStringMap() {
134: OpenJPAEntityManager pm = getPM(false, false);
135: startTx(pm);
136: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
137:
138: // check that orig values are correct
139: Map map = pc.getStringMap();
140: assertEquals(5, map.size());
141: assertEquals("1", map.get("key1"));
142: assertEquals("2", map.get("key2"));
143: assertEquals("3", map.get("key3"));
144: assertNull(map.get("null"));
145: assertEquals("null", map.get(null));
146:
147: // do some mods to try to confuse the proxy
148: map.put("key1", "1a");
149: map.put("key1", "1b");
150: map.put("key4", "4");
151: map.remove("key4");
152: map.remove("foo");
153: map.put("key5", "5");
154: endTx(pm);
155: endEm(pm);
156:
157: // re-retrieve and check map
158: pm = getPM(false, false);
159: pc = pm.find(ProxiesPC.class, _oid);
160:
161: map = pc.getStringMap();
162: assertEquals(6, map.size());
163: assertEquals("1b", map.get("key1"));
164: assertEquals("5", map.get("key5"));
165: endEm(pm);
166: }
167:
168: public void testProxySet() {
169: OpenJPAEntityManager pm = getPM(false, false);
170: startTx(pm);
171: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
172:
173: // check that orig values are correct
174: Set set = pc.getProxySet();
175: assertEquals(3, set.size());
176: Iterator itr = set.iterator();
177: ProxiesPC set1 = (ProxiesPC) itr.next();
178: ProxiesPC set2 = (ProxiesPC) itr.next();
179: ProxiesPC set3 = (ProxiesPC) itr.next();
180: assertEquals("set1", set1.getName());
181: assertEquals("set2", set2.getName());
182: assertEquals("set3", set3.getName());
183:
184: // do some mods to try to confuse the proxy
185: set.remove(set1);
186: set.remove(set1);
187: ProxiesPC set4 = new ProxiesPC("set4");
188: set.add(set4);
189: set.remove(set4);
190: ProxiesPC set5 = new ProxiesPC("set5");
191: set.add(set5);
192: set.add(set5);
193: endTx(pm);
194: endEm(pm);
195:
196: // re-retrieve and check set
197: pm = getPM(true, false);
198: startTx(pm);
199: pc = pm.find(ProxiesPC.class, _oid);
200: pm.refresh(pc);
201:
202: set = pc.getProxySet();
203: assertEquals(3, set.size());
204: itr = set.iterator();
205: set1 = (ProxiesPC) itr.next();
206: set2 = (ProxiesPC) itr.next();
207: set3 = (ProxiesPC) itr.next();
208: assertEquals("set2", set1.getName());
209: assertEquals("set3", set2.getName());
210: assertEquals("set5", set3.getName());
211: endTx(pm);
212: endEm(pm);
213: }
214:
215: public void testProxyMap() {
216: OpenJPAEntityManager pm = getPM(false, false);
217: startTx(pm);
218: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
219:
220: // check that orig values are correct
221: Map map = pc.getProxyMap();
222: assertEquals("original map size is correct: 3", 3, map.size());
223: ProxiesPC map1 = (ProxiesPC) map.get("key1");
224: ProxiesPC map2 = (ProxiesPC) map.get("key2");
225: ProxiesPC map3 = (ProxiesPC) map.get("key3");
226: assertEquals("map1", map1.getName());
227: assertEquals("map2", map2.getName());
228: assertEquals("map3", map3.getName());
229:
230: // do some mods to try to confuse the proxy
231: ProxiesPC map1a = new ProxiesPC("map1a");
232: map.put("key1", map1a);
233: ProxiesPC map1b = new ProxiesPC("map1b");
234: map.put("key1", map1b);
235: map.put("key4", new ProxiesPC("map4"));
236: map.remove("key4");
237: map.remove("foo");
238: map.put("key5", new ProxiesPC("map5"));
239: endTx(pm);
240: endEm(pm);
241:
242: // re-retrieve and check map
243: pm = getPM(false, false);
244: pc = pm.find(ProxiesPC.class, _oid);
245: startTx(pm);
246: pm.refresh(pc);
247:
248: map = pc.getProxyMap();
249:
250: assertEquals(4, map.size());
251: assertEquals("map1b", ((ProxiesPC) map.get("key1")).getName());
252: assertEquals("map5", ((ProxiesPC) map.get("key5")).getName());
253:
254: endTx(pm);
255: endEm(pm);
256: }
257:
258: public void testReplace() {
259: OpenJPAEntityManager pm = getPM(false, false);
260: startTx(pm);
261: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
262:
263: // totally replace set
264: Set set = new HashSet();
265: set.add("new");
266: pc.setStringSet(set);
267:
268: endTx(pm);
269: endEm(pm);
270:
271: // re-retrieve and check set
272: pm = getPM(false, false);
273: pc = pm.find(ProxiesPC.class, _oid);
274:
275: set = pc.getStringSet();
276: assertEquals(1, set.size());
277: assertTrue(set.contains("new"));
278: endEm(pm);
279: }
280:
281: public void testComparators() {
282: // make sure the system uses the initial field value to find
283: // comparators
284: OpenJPAEntityManager pm = getPM(false, false);
285: startTx(pm);
286: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
287: assertNotNull("pc is null", pc);
288: assertTrue("pc.getComp() is not instanceof Proxy",
289: pc.getComp() instanceof Proxy);
290: assertTrue(
291: "(TreeSet) is not pc.getComp()).comparator() instanceof ComparableComparator",
292: ((TreeSet) pc.getComp()).comparator() instanceof ComparableComparator);
293: pm.evict(pc);
294: endTx(pm);
295:
296: // see if it still saves comparator after transition to hollow
297: // and back
298: assertTrue("pc.getComp() is not instanceof ProxyTreeSet", pc
299: .getComp() instanceof Proxy);
300: Comparator compart = ((TreeSet) pc.getComp()).comparator();
301: assertNotNull("compart is null", compart);
302: assertTrue(
303: "((TreeSet) is not pc.getComp()).comparator()instanceof ComparableComparator",
304: ((TreeSet) pc.getComp()).comparator() instanceof ComparableComparator);
305:
306: endEm(pm);
307: }
308:
309: // FIX ME: Moving fix to essex
310: /*public void testList() {
311: OpenJPAEntityManager pm = getPM(false, false);
312: startTx(pm);
313: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
314:
315: // check that orig values are correct
316: List list = pc.getList();
317: assertEquals(5, list.size());
318: assertEquals("val1", list.get(0));
319: assertEquals("val1", list.get(1));
320: assertEquals("val2", list.get(2));
321: assertEquals("val3", list.get(3));
322: assertEquals("val3", list.get(4));
323:
324: // do some mods to try to confuse the proxy
325: list.remove("val2");
326: list.add("val4");
327: list.remove("val4");
328: list.add("val5");
329: list.add("val6");
330: list.add("val6");
331: endTx(pm);
332: endEm(pm);
333:
334: // re-retrieve and modify again to check holes in ordering
335: pm = getPM(false, false);
336: startTx(pm);
337: pc = (ProxiesPC) pm.find(ProxiesPC.class, _oid);
338:
339: list = pc.getList();
340: assertEquals(7, list.size());
341: assertEquals("val1", list.get(0));
342: assertEquals("val1", list.get(1));
343: assertEquals("val3", list.get(2));
344: assertEquals("val3", list.get(3));
345: assertEquals("val5", list.get(4));
346: assertEquals("val6", list.get(5));
347: assertEquals("val6", list.get(6));
348:
349: list.remove("val5");
350: list.add("val7");
351: endTx(pm);
352: endEm(pm);
353:
354: // re-retrieve and check final contents
355: pm = getPM(false, false);
356: pc = pm.find(ProxiesPC.class, _oid);
357:
358: list = pc.getList();
359: assertEquals(7, list.size());
360: assertEquals("val1", list.get(0));
361: assertEquals("val1", list.get(1));
362: assertEquals("val3", list.get(2));
363: assertEquals("val3", list.get(3));
364: assertEquals("val6", list.get(4));
365: assertEquals("val6", list.get(5));
366: assertEquals("val7", list.get(6));
367: endEm(pm);
368: }
369:
370: public void testListDisablesChangeTracking() {
371: OpenJPAEntityManager pm = getPM(false, false);
372: startTx(pm);
373: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
374:
375: // check that orig values are correct
376: List list = pc.getList();
377: assertEquals(5, list.size());
378: assertEquals("val1", list.get(0));
379: assertEquals("val1", list.get(1));
380: assertEquals("val2", list.get(2));
381: assertEquals("val3", list.get(3));
382: assertEquals("val3", list.get(4));
383:
384: // removing a copy of val3 should disable tracking
385: list.remove("val2");
386: list.remove("val3");
387: list.add("val5");
388: list.add("val5");
389: endTx(pm);
390: endEm(pm);
391:
392: // re-retrieve and change again to check ordering
393: pm = getPM(false, false);
394: startTx(pm);
395: pc = pm.find(ProxiesPC.class, _oid);
396:
397: list = pc.getList();
398: assertEquals(5, list.size());
399: assertEquals("val1", list.get(0));
400: assertEquals("val1", list.get(1));
401: assertEquals("val3", list.get(2));
402: assertEquals("val5", list.get(3));
403: assertEquals("val5", list.get(4));
404:
405: list.remove("val3");
406: list.add("val6");
407: endTx(pm);
408: endEm(pm);
409:
410: // check final contents
411: pm = getPM(false, false);
412: pc = pm.find(ProxiesPC.class, _oid);
413:
414: list = pc.getList();
415: assertEquals(5, list.size());
416: assertEquals("val1", list.get(0));
417: assertEquals("val1", list.get(1));
418: assertEquals("val6", list.get(2));
419: assertEquals("val5", list.get(3));
420: assertEquals("val5", list.get(4));
421: endEm(pm);
422: }
423: */
424: public void testChangeListOrder() {
425: OpenJPAEntityManager pm = getPM(false, false);
426: startTx(pm);
427: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
428:
429: // check that orig values are correct
430: List list = pc.getList();
431: assertEquals(5, list.size());
432: assertEquals("val1", list.get(0));
433: assertEquals("val1", list.get(1));
434: assertEquals("val2", list.get(2));
435: assertEquals("val3", list.get(3));
436: assertEquals("val3", list.get(4));
437:
438: // reorder val2
439: list.remove("val2");
440: list.add("val2");
441: endTx(pm);
442: endEm(pm);
443:
444: // re-retrieve to check ordering
445: pm = getPM(false, false);
446: pc = pm.find(ProxiesPC.class, _oid);
447:
448: list = pc.getList();
449: assertEquals(5, list.size());
450: assertEquals("val1", list.get(0));
451: assertEquals("val1", list.get(1));
452: assertEquals("val3", list.get(2));
453: assertEquals("val3", list.get(3));
454: assertEquals("val2", list.get(4));
455: endEm(pm);
456: }
457:
458: public void testDate() {
459: OpenJPAEntityManager pm = getPM(true, true);
460: startTx(pm);
461: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
462: Date date = pc.getDate();
463: assertNotNull(date);
464:
465: // dates can lose precision, but make sure same day
466: assertEquals(_date.getYear(), date.getYear());
467: assertEquals(_date.getMonth(), date.getMonth());
468: assertEquals(_date.getDate(), date.getDate());
469:
470: // make sure proxied
471: assertTrue(!pm.isDirty(pc));
472: date.setTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
473: assertTrue(pm.isDirty(pc));
474:
475: endTx(pm);
476: assertEquals(date, pc.getDate());
477: endEm(pm);
478: }
479:
480: public void testSQLDate() {
481: OpenJPAEntityManager pm = getPM(true, true);
482: startTx(pm);
483: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
484: java.sql.Date date = pc.getSQLDate();
485: assertNotNull(date);
486:
487: // dates can lose precision, but make sure same day
488: assertEquals(_sqlDate.getYear(), date.getYear());
489: assertEquals(_sqlDate.getMonth(), date.getMonth());
490: assertEquals(_sqlDate.getDate(), date.getDate());
491:
492: // make sure proxied
493: assertTrue(!pm.isDirty(pc));
494: date.setTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
495: assertTrue(pm.isDirty(pc));
496:
497: endTx(pm);
498: assertEquals(date, pc.getSQLDate());
499: endEm(pm);
500: }
501:
502: public void testTimestamp() {
503: OpenJPAEntityManager pm = getPM(true, true);
504: startTx(pm);
505: ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
506: java.sql.Timestamp tstamp = pc.getTimestamp();
507: assertNotNull(tstamp);
508:
509: // dates can lose precision, but make sure same day
510: assertEquals(_timestamp.getYear(), tstamp.getYear());
511: assertEquals(_timestamp.getMonth(), tstamp.getMonth());
512: assertEquals(_timestamp.getDate(), tstamp.getDate());
513:
514: // make sure proxied
515: assertTrue(!pm.isDirty(pc));
516: tstamp.setNanos(100);
517: assertTrue(pm.isDirty(pc));
518:
519: endTx(pm);
520: assertEquals(tstamp, pc.getTimestamp());
521: endEm(pm);
522: }
523: }
|