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.deephierarchy;
018:
019: import java.util.ArrayList;
020:
021: import org.compass.core.CompassSession;
022: import org.compass.core.CompassTransaction;
023: import org.compass.core.test.AbstractTestCase;
024:
025: /**
026: * @author kimchy
027: */
028: public class DeepHierarchyTests extends AbstractTestCase {
029:
030: protected String[] getMappings() {
031: return new String[] { "component/deephierarchy/mapping.cpm.xml" };
032: }
033:
034: public void testBLevelNulls() throws Exception {
035: CompassSession session = openSession();
036: CompassTransaction tr = session.beginTransaction();
037:
038: A a = new A();
039: a.id = 1;
040:
041: B b1 = new B();
042: b1.id = 1;
043: a.bs = new ArrayList();
044: a.bs.add(null);
045: a.bs.add(b1);
046:
047: session.save("a", a);
048:
049: // compass will not store nulls within collections
050: a = (A) session.load("a", new Integer(1));
051: assertEquals(1, a.bs.size());
052:
053: tr.commit();
054: session.close();
055: }
056:
057: public void testCLevelNulls1() throws Exception {
058: CompassSession session = openSession();
059: CompassTransaction tr = session.beginTransaction();
060:
061: A a = new A();
062: a.id = 1;
063:
064: B b1 = new B();
065: b1.id = 1;
066: B b2 = new B();
067: b2.id = 2;
068: a.bs = new ArrayList();
069: a.bs.add(b1);
070: a.bs.add(b2);
071:
072: C c1 = new C();
073: c1.id = 1;
074: C c2 = new C();
075: c2.id = 2;
076: b2.cs = new ArrayList();
077: b2.cs.add(c1);
078: b2.cs.add(c2);
079:
080: session.save("a", a);
081:
082: a = (A) session.load("a", new Integer(1));
083: assertEquals(2, a.bs.size());
084: b1 = (B) a.bs.get(0);
085: assertNull(b1.cs);
086: b2 = (B) a.bs.get(1);
087: assertNotNull(b2);
088: assertEquals(2, b2.cs.size());
089: assertEquals(1, ((C) b2.cs.get(0)).id);
090: assertEquals(2, ((C) b2.cs.get(1)).id);
091:
092: tr.commit();
093: session.close();
094: }
095:
096: public void testCLevelNullsInterlieved() throws Exception {
097: CompassSession session = openSession();
098: CompassTransaction tr = session.beginTransaction();
099:
100: A a = new A();
101: a.id = 1;
102:
103: B b1 = new B();
104: b1.id = 1;
105: B b2 = new B();
106: b2.id = 2;
107: a.bs = new ArrayList();
108: a.bs.add(b1);
109: a.bs.add(b2);
110:
111: C c1 = new C();
112: c1.id = 1;
113: b1.cs = new ArrayList();
114: b1.cs.add(null);
115: b1.cs.add(c1);
116:
117: C c2 = new C();
118: c2.id = 2;
119: b2.cs = new ArrayList();
120: b2.cs.add(null);
121: b2.cs.add(c1);
122: b2.cs.add(c2);
123:
124: session.save("a", a);
125:
126: a = (A) session.load("a", new Integer(1));
127: assertEquals(2, a.bs.size());
128: b1 = (B) a.bs.get(0);
129: assertNotNull(b1.cs);
130: assertEquals(1, b1.cs.size());
131: assertEquals(1, ((C) b1.cs.get(0)).id);
132: b2 = (B) a.bs.get(1);
133: assertNotNull(b2);
134: assertEquals(2, b2.cs.size());
135: assertEquals(1, ((C) b2.cs.get(0)).id);
136: assertEquals(2, ((C) b2.cs.get(1)).id);
137:
138: tr.commit();
139: session.close();
140: }
141: }
|