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.cascade.simple;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.test.AbstractTestCase;
022:
023: /**
024: * Simple tests for cascading operation between two root objects (A and B)
025: * with component mapping.
026: *
027: * @author kimchy
028: */
029: public class SimpleCascadeTests extends AbstractTestCase {
030:
031: protected String[] getMappings() {
032: return new String[] { "component/cascade/simple/mapping.cpm.xml" };
033: }
034:
035: public void testCascadeAll() {
036: CompassSession session = openSession();
037: CompassTransaction tr = session.beginTransaction();
038:
039: B b = new B(1, "bvalue");
040: A a = new A(1, "avalue", b);
041: // this should cause cascading for b as well
042: session.create("aAll", a);
043:
044: a = (A) session.load("aAll", "1");
045: session.load("b", "1");
046:
047: a.b.value = "bupdated";
048: session.save("aAll", a);
049: b = (B) session.load("b", "1");
050: assertEquals("bupdated", b.value);
051:
052: session.delete("aAll", a);
053: assertNull(session.get("aAll", "1"));
054: assertNull(session.get("b", "1"));
055:
056: tr.commit();
057: session.close();
058: }
059:
060: public void testCascadeCreate() {
061: CompassSession session = openSession();
062: CompassTransaction tr = session.beginTransaction();
063:
064: B b = new B(1, "bvalue");
065: A a = new A(1, "avalue", b);
066: // this should cause cascading for b as well
067: session.create("aCreate", a);
068:
069: a = (A) session.load("aCreate", "1");
070: session.load("b", "1");
071:
072: a.b.value = "bupdated";
073: // this will not cause b to get cascaded
074: session.save("aCreate", a);
075: b = (B) session.load("b", "1");
076: assertEquals("bvalue", b.value);
077:
078: session.delete("aCreate", a);
079: assertNull(session.get("aAll", "1"));
080: assertNotNull(session.get("b", "1"));
081:
082: tr.commit();
083: session.close();
084: }
085:
086: public void testCascadeSave() {
087: CompassSession session = openSession();
088: CompassTransaction tr = session.beginTransaction();
089:
090: B b = new B(1, "bvalue");
091: A a = new A(1, "avalue", b);
092: // this should not cause cascading for b as well
093: session.create("aSave", a);
094: assertNull(session.get("b", "1"));
095: session.create("b", b);
096:
097: a = (A) session.load("aSave", "1");
098: session.load("b", "1");
099:
100: a.b.value = "bupdated";
101: // this will cause b to get cascaded
102: session.save("aSave", a);
103: b = (B) session.load("b", "1");
104: assertEquals("bupdated", b.value);
105:
106: session.delete("aSave", a);
107: assertNull(session.get("aAll", "1"));
108: assertNotNull(session.get("b", "1"));
109:
110: tr.commit();
111: session.close();
112: }
113:
114: public void testCascadeDelete() {
115: CompassSession session = openSession();
116: CompassTransaction tr = session.beginTransaction();
117:
118: B b = new B(1, "bvalue");
119: A a = new A(1, "avalue", b);
120: // this should not cause cascading for b as well
121: session.create("aDelete", a);
122: assertNull(session.get("b", "1"));
123: session.create("b", b);
124:
125: a = (A) session.load("aDelete", "1");
126: session.load("b", "1");
127:
128: a.b.value = "bupdated";
129: // this will not cause b to get cascaded
130: session.save("aDelete", a);
131: b = (B) session.load("b", "1");
132: assertEquals("bvalue", b.value);
133:
134: session.delete("aDelete", a);
135: assertNull(session.get("aAll", "1"));
136: assertNull(session.get("b", "1"));
137:
138: tr.commit();
139: session.close();
140: }
141: }
|