01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.test.component.cyclic1;
18:
19: import org.compass.core.CompassSession;
20: import org.compass.core.CompassTransaction;
21: import org.compass.core.test.AbstractTestCase;
22:
23: /**
24: * @author kimchy
25: */
26: public class SelfCyclicTests extends AbstractTestCase {
27:
28: protected String[] getMappings() {
29: return new String[] { "component/cyclic1/SelfCycle.cpm.xml" };
30: }
31:
32: public void testSelfCyclic() throws Exception {
33: CompassSession session = openSession();
34: CompassTransaction tr = session.beginTransaction();
35:
36: SelfCycle selfCycle1 = new SelfCycle();
37: selfCycle1.setId(new Long(1));
38: selfCycle1.setValue("value1");
39:
40: SelfCycle selfCycle2 = new SelfCycle();
41: selfCycle2.setValue("value2");
42:
43: SelfCycle selfCycle3 = new SelfCycle();
44: selfCycle3.setValue("value3");
45:
46: SelfCycle selfCycle4 = new SelfCycle();
47: selfCycle4.setValue("value4");
48:
49: SelfCycle selfCycle5 = new SelfCycle();
50: selfCycle5.setValue("value5");
51:
52: selfCycle1.setSelfCycle(selfCycle2);
53: selfCycle2.setSelfCycle(selfCycle3);
54: selfCycle3.setSelfCycle(selfCycle4);
55: selfCycle4.setSelfCycle(selfCycle5);
56:
57: session.save(selfCycle1);
58:
59: selfCycle1 = (SelfCycle) session.load(SelfCycle.class,
60: new Long(1));
61: assertEquals("value1", selfCycle1.getValue());
62: assertEquals("value2", selfCycle1.getSelfCycle().getValue());
63: assertEquals("value3", selfCycle1.getSelfCycle().getSelfCycle()
64: .getValue());
65: assertEquals("value4", selfCycle1.getSelfCycle().getSelfCycle()
66: .getSelfCycle().getValue());
67: // value 5 we will not find since max depth is set to 3
68: assertNull(selfCycle1.getSelfCycle().getSelfCycle()
69: .getSelfCycle().getSelfCycle());
70:
71: tr.commit();
72: session.close();
73: }
74:
75: public void testSameSelfCyclic() throws Exception {
76: CompassSession session = openSession();
77: CompassTransaction tr = session.beginTransaction();
78:
79: SelfCycle selfCycle1 = new SelfCycle();
80: selfCycle1.setId(new Long(1));
81: selfCycle1.setValue("value1");
82:
83: SelfCycle selfCycle2 = new SelfCycle();
84: selfCycle2.setId(new Long(2));
85: selfCycle2.setValue("value2");
86:
87: selfCycle1.setSelfCycle(selfCycle2);
88: selfCycle2.setSelfCycle(selfCycle1);
89:
90: session.save(selfCycle1);
91: session.save(selfCycle2);
92:
93: selfCycle1 = (SelfCycle) session.load(SelfCycle.class,
94: new Long(1));
95: assertEquals(System.identityHashCode(selfCycle1), System
96: .identityHashCode(selfCycle1.getSelfCycle()
97: .getSelfCycle()));
98: }
99: }
|