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.array;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.test.AbstractTestCase;
022:
023: /**
024: * @author kimchy
025: */
026: public class ArrayTests extends AbstractTestCase {
027:
028: protected String[] getMappings() {
029: return new String[] { "array/Array.cpm.xml" };
030: }
031:
032: public void testSimpleArray() {
033: CompassSession session = openSession();
034: CompassTransaction tr = session.beginTransaction();
035:
036: Long id = new Long(1);
037: SimpleArray sa = new SimpleArray();
038: sa.setId(id);
039: sa.setValue("test");
040: sa.setStrings(new String[] { "test1", "test2" });
041: session.save(sa);
042:
043: sa = (SimpleArray) session.load(SimpleArray.class, id);
044: assertEquals("test", sa.getValue());
045: assertEquals(2, sa.getStrings().length);
046: assertEquals("test1", sa.getStrings()[0]);
047: assertEquals("test2", sa.getStrings()[1]);
048:
049: session.delete(sa);
050: sa = (SimpleArray) session.get(SimpleArray.class, id);
051: assertNull(sa);
052:
053: tr.commit();
054: }
055:
056: public void testAB() {
057: CompassSession session = openSession();
058: CompassTransaction tr = session.beginTransaction();
059:
060: Long id = new Long(1);
061: A a = new A();
062: a.setId(id);
063: a.setValue("test");
064: B b1 = new B();
065: b1.setValue("test1");
066: B b2 = new B();
067: b2.setValue("test2");
068: a.setArrB(new B[] { b1, b2 });
069:
070: session.save(a);
071:
072: a = (A) session.load(A.class, id);
073: assertEquals("test", a.getValue());
074: assertEquals(2, a.getArrB().length);
075: assertEquals("test1", a.getArrB()[0].getValue());
076: assertEquals("test2", a.getArrB()[1].getValue());
077:
078: session.delete(a);
079:
080: tr.commit();
081: }
082:
083: public void testABWithNull() {
084: CompassSession session = openSession();
085: CompassTransaction tr = session.beginTransaction();
086:
087: Long id = new Long(1);
088: A a = new A();
089: a.setId(id);
090: a.setValue("test");
091: B b1 = new B();
092: b1.setValue(null);
093: B b2 = new B();
094: b2.setValue(null);
095: a.setArrB(new B[] { b1, b2 });
096:
097: session.save(a);
098:
099: a = (A) session.load(A.class, id);
100: assertEquals("test", a.getValue());
101: assertEquals(2, a.getArrB().length);
102: assertNull(a.getArrB()[0]);
103: assertNull(a.getArrB()[1]);
104:
105: tr.commit();
106: }
107:
108: public void testABSingleNullB() {
109: CompassSession session = openSession();
110: CompassTransaction tr = session.beginTransaction();
111:
112: Long id = new Long(1);
113: A a = new A();
114: a.setId(id);
115: a.setValue("test");
116: B b2 = new B();
117: b2.setValue("test");
118: a.setArrB(new B[] { null, b2 });
119:
120: session.save(a);
121:
122: a = (A) session.load(A.class, id);
123: assertEquals("test", a.getValue());
124: assertEquals(1, a.getArrB().length);
125: assertEquals("test", a.getArrB()[0].getValue());
126:
127: tr.commit();
128: }
129:
130: public void testABWithPropertyNull() {
131: CompassSession session = openSession();
132: CompassTransaction tr = session.beginTransaction();
133:
134: Long id = new Long(1);
135: A a = new A();
136: a.setId(id);
137: a.setValue("test");
138: B b1 = new B();
139: b1.setValue("test1");
140: b1.setValue2(null);
141: B b2 = new B();
142: b2.setValue(null);
143: b2.setValue2("test2");
144: a.setArrB(new B[] { b1, b2 });
145:
146: session.save(a);
147:
148: a = (A) session.load(A.class, id);
149: assertEquals("test", a.getValue());
150: assertEquals(2, a.getArrB().length);
151: assertEquals("test1", a.getArrB()[0].getValue());
152: assertNull(a.getArrB()[0].getValue2());
153: assertNull(a.getArrB()[1].getValue());
154: assertEquals("test2", a.getArrB()[1].getValue2());
155:
156: session.delete(a);
157:
158: tr.commit();
159: }
160:
161: public void testXY() {
162: CompassSession session = openSession();
163: CompassTransaction tr = session.beginTransaction();
164:
165: Long xId = new Long(1);
166: Long y1Id = new Long(1);
167: Long y2Id = new Long(2);
168: X x = new X();
169: x.setId(xId);
170: x.setValue("xValue");
171: Y y1 = new Y();
172: y1.setId(y1Id);
173: y1.setValue("yValue");
174: session.save(y1);
175: Y y2 = new Y();
176: y2.setId(y2Id);
177: y2.setValue("yValue");
178: session.save(y2);
179: x.setCy(new Y[] { y1, y2 });
180: session.save(x);
181:
182: x = (X) session.load(X.class, xId);
183: assertEquals("xValue", x.getValue());
184: assertNotNull(x.getCy());
185: assertEquals(2, x.getCy().length);
186: Y y = x.getCy()[0];
187: assertEquals(1, y.getId().longValue());
188: y = x.getCy()[1];
189: assertEquals(2, y.getId().longValue());
190:
191: tr.commit();
192: }
193:
194: }
|