01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.annotations.test.reference;
18:
19: import java.util.ArrayList;
20:
21: import org.compass.annotations.test.AbstractAnnotationsTestCase;
22: import org.compass.core.CompassHits;
23: import org.compass.core.CompassSession;
24: import org.compass.core.CompassTransaction;
25: import org.compass.core.config.CompassConfiguration;
26:
27: /**
28: * @author kimchy
29: */
30: public class ReferenceTests extends AbstractAnnotationsTestCase {
31:
32: protected void addExtraConf(CompassConfiguration conf) {
33: conf.addClass(A.class).addClass(B.class);
34: }
35:
36: public void testSimpleReference() {
37: CompassSession session = openSession();
38: CompassTransaction tr = session.beginTransaction();
39:
40: A a = new A();
41: a.id = 1;
42: a.value = "avalue";
43:
44: B b = new B();
45: b.id = 1;
46: b.value = "bvalue";
47: a.b = b;
48:
49: B b1 = new B();
50: b1.id = 2;
51: b1.value = "bvalue1";
52:
53: B b2 = new B();
54: b2.id = 3;
55: b2.value = "bvalue2";
56: ArrayList<B> bValues = new ArrayList<B>();
57: bValues.add(b1);
58: bValues.add(b2);
59: a.bValues = bValues;
60:
61: session.save(b);
62: session.save(b1);
63: session.save(b2);
64: session.save(a);
65:
66: a = (A) session.load(A.class, 1);
67: assertEquals("avalue", a.value);
68: assertEquals("bvalue", a.b.value);
69: assertEquals("bvalue1", a.bValues.get(0).value);
70: assertEquals("bvalue2", a.bValues.get(1).value);
71: b = (B) session.load(B.class, 1);
72: assertEquals("bvalue", b.value);
73:
74: CompassHits hits = session.find("bvalue");
75: assertEquals(1, hits.length());
76: b = (B) hits.data(0);
77: assertEquals("bvalue", b.value);
78:
79: hits = session.find("avalue");
80: assertEquals(1, hits.length());
81: a = (A) hits.data(0);
82: assertEquals("avalue", a.value);
83: assertEquals("bvalue", a.b.value);
84:
85: tr.commit();
86: session.close();
87: }
88:
89: }
|