001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.component.cyclic4;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.Resource;
022: import org.compass.core.test.AbstractTestCase;
023:
024: /**
025: * @author kimchy
026: */
027: public class MultiInstanceCyclicTests extends AbstractTestCase {
028:
029: protected String[] getMappings() {
030: return new String[] { "component/cyclic4/mapping.cpm.xml" };
031: }
032:
033: public void testSameIdentityInstance() throws Exception {
034: CompassSession session = openSession();
035: CompassTransaction tr = session.beginTransaction();
036:
037: Child child = new Child(1, "value");
038: Father father = new Father(1, child, child);
039: child.father = father;
040:
041: session.save("father", father);
042: session.save("child", child);
043:
044: father = (Father) session.load("father", new Long(1));
045: assertSame(father.child1, father.child2);
046:
047: Resource resource = session.loadResource("father", new Long(1));
048: assertNotNull(resource.getProperty("$/father/id"));
049: assertNotNull(resource.getProperty("$/father/child1/id"));
050: assertNotNull(resource.getProperty("$/father/child1/value"));
051: assertNotNull(resource.getProperty("$/father/child1/father/id"));
052: assertNotNull(resource.getProperty("$/father/child2/id"));
053: assertNull(resource.getProperty("$/father/child2/value"));
054: assertNull(resource.getProperty("$/father/child2/father/id"));
055:
056: tr.commit();
057: session.close();
058: }
059:
060: public void testSameEqualsInstance() throws Exception {
061: CompassSession session = openSession();
062: CompassTransaction tr = session.beginTransaction();
063:
064: Child child1 = new Child(1, "value");
065: Child child2 = new Child(1, "value");
066: Father father = new Father(1, child1, child2);
067: child1.father = father;
068: child2.father = father;
069:
070: session.save("father", father);
071: session.save("child", child1);
072: session.save("child", child2);
073:
074: father = (Father) session.load("father", new Long(1));
075: assertSame(father.child1, father.child2);
076: assertSame(father, father.child1.father);
077: assertSame(father, father.child2.father);
078:
079: Resource resource = session.loadResource("father", new Long(1));
080: assertNotNull(resource.getProperty("$/father/id"));
081: assertNotNull(resource.getProperty("$/father/child1/id"));
082: assertNotNull(resource.getProperty("$/father/child1/value"));
083: assertNotNull(resource.getProperty("$/father/child1/father/id"));
084: assertNotNull(resource.getProperty("$/father/child2/id"));
085: assertNull(resource.getProperty("$/father/child2/value"));
086: assertNull(resource.getProperty("$/father/child2/father/id"));
087:
088: tr.commit();
089: session.close();
090: }
091:
092: public void testIdentityCollection() throws Exception {
093: CompassSession session = openSession();
094: CompassTransaction tr = session.beginTransaction();
095:
096: Child child1 = new Child(1, "value1");
097: Child child2 = new Child(2, "value2");
098: Father father = new Father(1);
099: father.children.add(child1);
100: father.children.add(child2);
101: father.children.add(child1);
102: child1.father = father;
103: child2.father = father;
104:
105: session.save("father", father);
106: session.save("child", child1);
107: session.save("child", child2);
108:
109: father = (Father) session.load("father", new Long(1));
110: assertEquals(3, father.children.size());
111: assertSame(father.children.get(0), father.children.get(2));
112: assertNotSame(father.children.get(1), father.children.get(2));
113: assertSame(father, ((Child) father.children.get(0)).father);
114: assertSame(father, ((Child) father.children.get(1)).father);
115: assertSame(father, ((Child) father.children.get(2)).father);
116:
117: Resource resource = session.loadResource("father", new Long(1));
118: assertNotNull(resource.getProperty("$/father/id"));
119: assertEquals(3,
120: resource.getProperties("$/father/children/id").length);
121: assertEquals(2, resource
122: .getProperties("$/father/children/value").length);
123: assertEquals(2, resource.getProperties("value").length);
124: // only two here, since we have duplicate childs
125: assertEquals(2, resource
126: .getProperties("$/father/children/father/id").length);
127:
128: tr.commit();
129: session.close();
130: }
131: }
|