001: package org.compass.core.test.collection;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.LinkedList;
006: import java.util.List;
007:
008: import org.compass.core.CompassHits;
009: import org.compass.core.CompassSession;
010: import org.compass.core.CompassTransaction;
011: import org.compass.core.Property;
012: import org.compass.core.Resource;
013: import org.compass.core.test.AbstractTestCase;
014:
015: /**
016: * @author kimchy
017: */
018: public class CollectionTests extends AbstractTestCase {
019:
020: protected String[] getMappings() {
021: return new String[] { "collection/Collection.cpm.xml" };
022: }
023:
024: public void testSimpleCollectionNull() {
025: CompassSession session = openSession();
026: CompassTransaction transaction = session.beginTransaction();
027:
028: SimpleTypeCollection o = new SimpleTypeCollection();
029: Long id = new Long(1);
030: o.setId(id);
031: o.setValue("test");
032: session.save("simple-type-col", o);
033:
034: o = (SimpleTypeCollection) session.load("simple-type-col", id);
035: assertEquals("test", o.getValue());
036: assertNull(o.getStrings());
037:
038: session.delete("simple-type-col", o);
039: o = (SimpleTypeCollection) session.get("simple-type-col", id);
040: assertNull(o);
041:
042: transaction.commit();
043: }
044:
045: public void testSimpleCollection() {
046: CompassSession session = openSession();
047: CompassTransaction transaction = session.beginTransaction();
048:
049: SimpleTypeCollection o = new SimpleTypeCollection();
050: Long id = new Long(1);
051: Collection stringCol = new ArrayList();
052: stringCol.add("test1");
053: stringCol.add("test2");
054: o.setId(id);
055: o.setValue("test");
056: o.setStrings(stringCol);
057:
058: session.save("simple-type-col", o);
059:
060: o = (SimpleTypeCollection) session.load("simple-type-col", id);
061: assertEquals("test", o.getValue());
062: assertNotNull(o.getStrings());
063: assertEquals(2, o.getStrings().size());
064: ArrayList list = (ArrayList) o.getStrings();
065: assertEquals("test1", list.get(0));
066: assertEquals("test2", list.get(1));
067:
068: Resource r = session.loadResource("simple-type-col", id);
069: assertEquals("2", r
070: .getValue("$/simple-type-col/strings/colSize"));
071:
072: CompassHits result = session.find("mvalue:test1");
073: o = (SimpleTypeCollection) result.data(0);
074: assertEquals("test", o.getValue());
075: assertNotNull(o.getStrings());
076: assertEquals(2, o.getStrings().size());
077: list = (ArrayList) o.getStrings();
078: assertEquals("test1", list.get(0));
079: assertEquals("test2", list.get(1));
080:
081: boolean foundStoredClassType = false;
082: Resource resource = result.resource(0);
083: Property[] properties = resource.getProperties();
084: for (int i = 0; i < properties.length; i++) {
085: String value = properties[i].getStringValue();
086: if (value != null) {
087: if (value.indexOf("list") != -1) {
088: foundStoredClassType = true;
089: }
090: }
091: }
092: if (!foundStoredClassType) {
093: fail("Failed to find stored collection class type");
094: }
095:
096: session.delete("simple-type-col", o);
097: o = (SimpleTypeCollection) session.get("simple-type-col", id);
098: assertNull(o);
099:
100: transaction.commit();
101: }
102:
103: public void testNoMetaDataStored() {
104: CompassSession session = openSession();
105: CompassTransaction transaction = session.beginTransaction();
106:
107: SimpleTypeCollection o = new SimpleTypeCollection();
108: Long id = new Long(1);
109: Collection stringCol = new ArrayList();
110: stringCol.add("test1");
111: stringCol.add("test2");
112: o.setId(id);
113: o.setValue("test");
114: o.setStrings(stringCol);
115:
116: session.save("no-metadata-stored", o);
117:
118: o = (SimpleTypeCollection) session.load("no-metadata-stored",
119: id);
120: assertEquals("test", o.getValue());
121: assertEquals(0, o.getStrings().size());
122: Resource r = session.loadResource("no-metadata-stored", id);
123: assertEquals("0", r
124: .getValue("$/no-metadata-stored/strings/colSize"));
125:
126: transaction.commit();
127: session.close();
128: }
129:
130: public void testExplicitCollection() {
131: CompassSession session = openSession();
132: CompassTransaction transaction = session.beginTransaction();
133:
134: ExplicitTypeCollection o = new ExplicitTypeCollection();
135: Long id = new Long(1);
136: Collection stringCol = new ArrayList();
137: stringCol.add("test1");
138: stringCol.add("test2");
139: o.setId(id);
140: o.setValue("test");
141: o.setStrings(stringCol);
142:
143: session.save(o);
144:
145: o = (ExplicitTypeCollection) session.load(
146: ExplicitTypeCollection.class, id);
147: assertEquals("test", o.getValue());
148: assertNotNull(o.getStrings());
149: assertEquals(2, o.getStrings().size());
150: ArrayList list = (ArrayList) o.getStrings();
151: assertEquals("test1", list.get(0));
152: assertEquals("test2", list.get(1));
153:
154: CompassHits result = session.find("mvalue:test1");
155: o = (ExplicitTypeCollection) result.data(0);
156: assertEquals("test", o.getValue());
157: assertNotNull(o.getStrings());
158: assertEquals(2, o.getStrings().size());
159: list = (ArrayList) o.getStrings();
160: assertEquals("test1", list.get(0));
161: assertEquals("test2", list.get(1));
162:
163: Resource resource = result.resource(0);
164: Property[] properties = resource.getProperties();
165: for (int i = 0; i < properties.length; i++) {
166: String value = properties[i].getStringValue();
167: if (value != null) {
168: if (value.indexOf("ArrayList") != -1) {
169: fail("should not store the collection class");
170: }
171: }
172: }
173:
174: session.delete(o);
175: o = (ExplicitTypeCollection) session.get(
176: ExplicitTypeCollection.class, id);
177: assertNull(o);
178:
179: transaction.commit();
180: }
181:
182: public void testAB() {
183: CompassSession session = openSession();
184: CompassTransaction tr = session.beginTransaction();
185:
186: Long id = new Long(1);
187: A a = new A();
188: a.setId(id);
189: a.setValue("test");
190: B b1 = new B();
191: b1.setValue("test1");
192: B b2 = new B();
193: b2.setValue("test2");
194: Collection bs = new LinkedList();
195: bs.add(b1);
196: bs.add(b2);
197: a.setCb(bs);
198: session.save("a", a);
199:
200: a = (A) session.load("a", id);
201: assertEquals("test", a.getValue());
202: List list = (List) a.getCb();
203: assertNotNull(list);
204: assertEquals(2, list.size());
205: assertEquals("test1", ((B) list.get(0)).getValue());
206: assertEquals("test2", ((B) list.get(1)).getValue());
207:
208: CompassHits result = session.find("value:test1");
209: a = (A) result.data(0);
210: assertEquals("test", a.getValue());
211: list = (List) a.getCb();
212: assertNotNull(list);
213: assertEquals(2, list.size());
214: assertEquals("test1", ((B) list.get(0)).getValue());
215: assertEquals("test2", ((B) list.get(1)).getValue());
216:
217: tr.commit();
218:
219: }
220:
221: public void testABWithNull() {
222: CompassSession session = openSession();
223: CompassTransaction tr = session.beginTransaction();
224:
225: Long id = new Long(1);
226: A a = new A();
227: a.setId(id);
228: a.setValue("test");
229: B b1 = new B();
230: b1.setValue(null);
231: B b2 = new B();
232: b2.setValue(null);
233: Collection bs = new LinkedList();
234: bs.add(b1);
235: bs.add(b2);
236: a.setCb(bs);
237: session.save("a", a);
238:
239: a = (A) session.load("a", id);
240: assertEquals("test", a.getValue());
241: List list = (List) a.getCb();
242: assertNotNull(list);
243: assertEquals(0, list.size());
244:
245: session.delete("a", a);
246: a = (A) session.get("a", id);
247: assertNull(a);
248:
249: tr.commit();
250: }
251:
252: public void testABWithSingleNull() {
253: CompassSession session = openSession();
254: CompassTransaction tr = session.beginTransaction();
255:
256: Long id = new Long(1);
257: A a = new A();
258: a.setId(id);
259: a.setValue("test");
260: B b2 = new B();
261: b2.setValue("test");
262: Collection bs = new LinkedList();
263: bs.add(null);
264: bs.add(b2);
265: a.setCb(bs);
266: session.save("a", a);
267:
268: a = (A) session.load("a", id);
269: assertEquals("test", a.getValue());
270: List list = (List) a.getCb();
271: assertNotNull(list);
272: assertEquals(1, list.size());
273: b2 = (B) list.get(0);
274: assertEquals("test", b2.getValue());
275:
276: session.delete("a", a);
277: a = (A) session.get("a", id);
278: assertNull(a);
279:
280: tr.commit();
281: }
282:
283: public void testABWithBPropertyNull() {
284: CompassSession session = openSession();
285: CompassTransaction tr = session.beginTransaction();
286:
287: Long id = new Long(1);
288: A a = new A();
289: a.setId(id);
290: a.setValue("test");
291: B b1 = new B();
292: b1.setValue("test1");
293: b1.setValue2(null);
294: B b2 = new B();
295: b2.setValue("test2");
296: b2.setValue2("value2");
297: Collection bs = new LinkedList();
298: bs.add(b1);
299: bs.add(b2);
300: a.setCb(bs);
301: session.save("a", a);
302:
303: a = (A) session.load("a", id);
304: assertEquals("test", a.getValue());
305: List list = (List) a.getCb();
306: assertNotNull(list);
307: assertEquals(2, list.size());
308:
309: b1 = (B) list.get(0);
310: assertEquals("test1", b1.getValue());
311: assertEquals(null, b1.getValue2());
312:
313: b2 = (B) list.get(1);
314: assertEquals("test2", b2.getValue());
315: assertEquals("value2", b2.getValue2());
316:
317: session.delete("a", a);
318: a = (A) session.get("a", id);
319: assertNull(a);
320:
321: tr.commit();
322: }
323:
324: public void testXY() {
325: CompassSession session = openSession();
326: CompassTransaction tr = session.beginTransaction();
327:
328: Long xId = new Long(1);
329: Long y1Id = new Long(1);
330: Long y2Id = new Long(2);
331: X x = new X();
332: x.setId(xId);
333: x.setValue("xValue");
334: Y y1 = new Y();
335: y1.setId(y1Id);
336: y1.setValue("yValue");
337: session.save(y1);
338: Y y2 = new Y();
339: y2.setId(y2Id);
340: y2.setValue("yValue");
341: session.save(y2);
342: ArrayList list = new ArrayList();
343: list.add(y1);
344: list.add(y2);
345: x.setCy(list);
346: session.save(x);
347:
348: x = (X) session.load(X.class, xId);
349: assertEquals("xValue", x.getValue());
350: assertNotNull(x.getCy());
351: assertEquals(2, x.getCy().size());
352: Y y = (Y) ((ArrayList) x.getCy()).get(0);
353: assertEquals(1, y.getId().longValue());
354: y = (Y) ((ArrayList) x.getCy()).get(1);
355: assertEquals(2, y.getId().longValue());
356:
357: tr.commit();
358: }
359:
360: public void testThreeLevelDeepComponentCollection() {
361: CompassSession session = openSession();
362: CompassTransaction tr = session.beginTransaction();
363:
364: A a1 = new A();
365: a1.setId(new Long(1));
366: a1.setValue("a1");
367: a1.setCb(new ArrayList());
368:
369: A a21 = new A();
370: a21.setValue("a21");
371: a21.setCb(new ArrayList());
372: a1.getCb().add(a21);
373:
374: A a31 = new A();
375: a31.setValue("a31");
376: a21.getCb().add(a31);
377:
378: A a32 = new A();
379: a32.setValue("a32");
380: a21.getCb().add(a32);
381:
382: A a22 = new A();
383: a22.setValue("a22");
384: a22.setCb(new ArrayList());
385: a1.getCb().add(a22);
386:
387: A a33 = new A();
388: a33.setValue("a33");
389: a22.getCb().add(a33);
390:
391: A a34 = new A();
392: a34.setValue("a34");
393: a22.getCb().add(a34);
394:
395: A a35 = new A();
396: a35.setValue("a35");
397: a22.getCb().add(a35);
398:
399: session.save("a1", a1);
400:
401: a1 = (A) session.load("a1", new Long(1));
402: assertEquals("a1", a1.getValue());
403: assertEquals(2, a1.getCb().size());
404: a21 = (A) ((List) a1.getCb()).get(0);
405: assertEquals("a21", a21.getValue());
406: a22 = (A) ((List) a1.getCb()).get(1);
407: assertEquals("a22", a22.getValue());
408:
409: assertEquals(2, a21.getCb().size());
410: a31 = (A) ((List) a21.getCb()).get(0);
411: assertEquals("a31", a31.getValue());
412: a32 = (A) ((List) a21.getCb()).get(1);
413: assertEquals("a32", a32.getValue());
414:
415: assertEquals(3, a22.getCb().size());
416: a33 = (A) ((List) a22.getCb()).get(0);
417: assertEquals("a33", a33.getValue());
418: a34 = (A) ((List) a22.getCb()).get(1);
419: assertEquals("a34", a34.getValue());
420: a35 = (A) ((List) a22.getCb()).get(2);
421: assertEquals("a35", a35.getValue());
422:
423: tr.commit();
424: session.close();
425: }
426: }
|