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.reference;
018:
019: import org.compass.core.CompassHits;
020: import org.compass.core.CompassSession;
021: import org.compass.core.CompassTransaction;
022: import org.compass.core.mapping.CompassMapping;
023: import org.compass.core.mapping.ResourcePropertyMapping;
024: import org.compass.core.mapping.osem.ClassIdPropertyMapping;
025: import org.compass.core.mapping.osem.ClassMapping;
026: import org.compass.core.mapping.osem.ReferenceMapping;
027: import org.compass.core.spi.InternalCompassSession;
028: import org.compass.core.test.AbstractTestCase;
029:
030: /**
031: * @author kimchy
032: */
033: public class ReferenceTests extends AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "reference/Reference.cpm.xml" };
037: }
038:
039: public void testMappings() {
040:
041: InternalCompassSession session = (InternalCompassSession) openSession();
042:
043: CompassMapping mapping = session.getMapping();
044: ClassMapping xMapping = (ClassMapping) mapping
045: .getRootMappingByAlias("x");
046: ClassMapping yMapping = ((ReferenceMapping) xMapping
047: .getMapping("y")).getRefClassMappings()[0];
048: ClassIdPropertyMapping[] idMappings = yMapping
049: .getClassIdPropertyMappings();
050: assertEquals(1, idMappings.length);
051: ResourcePropertyMapping[] resourcePropertyMappings = idMappings[0]
052: .getResourceIdMappings();
053: assertEquals(1, resourcePropertyMappings.length);
054: assertEquals("$/x/y/id", resourcePropertyMappings[0].getPath()
055: .getPath());
056: assertEquals("id", resourcePropertyMappings[0].getName());
057:
058: ResourcePropertyMapping resourcePropertyMapping = xMapping
059: .getResourcePropertyMappingByDotPath("id");
060: assertNotNull(resourcePropertyMapping);
061: assertEquals("id", resourcePropertyMapping.getName());
062: assertEquals("$/x/id", resourcePropertyMapping.getPath()
063: .getPath());
064:
065: resourcePropertyMapping = xMapping
066: .getResourcePropertyMappingByDotPath("value");
067: assertNotNull(resourcePropertyMapping);
068: assertEquals("value", resourcePropertyMapping.getName());
069: assertEquals("value", resourcePropertyMapping.getPath()
070: .getPath());
071:
072: resourcePropertyMapping = xMapping
073: .getResourcePropertyMappingByDotPath("value.value");
074: assertNotNull(resourcePropertyMapping);
075: assertEquals("value", resourcePropertyMapping.getName());
076: assertEquals("value", resourcePropertyMapping.getPath()
077: .getPath());
078:
079: resourcePropertyMapping = xMapping
080: .getResourcePropertyMappingByDotPath("y.id");
081: assertNotNull(resourcePropertyMapping);
082: assertEquals("id", resourcePropertyMapping.getName());
083: assertEquals("$/x/y/id", resourcePropertyMapping.getPath()
084: .getPath());
085:
086: session.close();
087: }
088:
089: public void testInferMappings() {
090: CompassSession session = openSession();
091: CompassTransaction tr = session.beginTransaction();
092:
093: A a = new A();
094: a.setId(new Long(1));
095: a.setValue("avalue");
096: B b = new B();
097: b.setId(new Long(1));
098: b.setValue("bvalue");
099: a.setB(b);
100:
101: session.save("b", b);
102: session.save("a", a);
103:
104: a = (A) session.load("a", new Long(1));
105: assertEquals("avalue", a.getValue());
106: assertEquals("bvalue", a.getB().getValue());
107:
108: tr.commit();
109: session.close();
110: }
111:
112: public void testXY() {
113: CompassSession session = openSession();
114: CompassTransaction tr = session.beginTransaction();
115:
116: Long xId = new Long(1);
117: Long yId = new Long(2);
118: X x = new X();
119: x.setId(xId);
120: x.setValue("xValue");
121: Y y = new Y();
122: y.setId(yId);
123: y.setValue("yValue");
124: x.setY(y);
125: session.save("y", y);
126: session.save("x", x);
127:
128: x = (X) session.load("x", xId);
129: assertEquals("xValue", x.getValue());
130: assertNotNull(x.getY());
131: assertEquals("yValue", x.getY().getValue());
132:
133: CompassHits hits = session.queryBuilder().term("x.y.id",
134: new Long(2)).hits();
135: assertEquals(1, hits.length());
136:
137: hits = session.queryBuilder().term("x.id", new Long(1)).hits();
138: assertEquals(1, hits.length());
139:
140: tr.commit();
141: }
142:
143: public void testXYRefCompMapping() {
144: CompassSession session = openSession();
145: CompassTransaction tr = session.beginTransaction();
146:
147: Long xId = new Long(1);
148: Long yId = new Long(2);
149: X x = new X();
150: x.setId(xId);
151: x.setValue("xValue");
152: Y y = new Y();
153: y.setId(yId);
154: y.setValue("yValue");
155: x.setY(y);
156: session.save("y1", y);
157: session.save("x1", x);
158:
159: x = (X) session.load("x1", xId);
160: assertEquals("xValue", x.getValue());
161: assertNotNull(x.getY());
162: assertEquals("yValue", x.getY().getValue());
163:
164: CompassHits hits = session.queryBuilder().term("x1.y.id",
165: new Long(2)).hits();
166: assertEquals(1, hits.length());
167:
168: hits = session.queryBuilder().term("x1.id", new Long(1)).hits();
169: assertEquals(1, hits.length());
170:
171: hits = session.queryBuilder().term("value1", "yvalue").hits();
172: assertEquals(1, hits.length());
173: x = (X) hits.data(0);
174: assertEquals("xValue", x.getValue());
175: assertNotNull(x.getY());
176: assertEquals("yValue", x.getY().getValue());
177:
178: hits = session.queryBuilder().term("x1.y.value.value1",
179: "yvalue").hits();
180: assertEquals(1, hits.length());
181:
182: tr.commit();
183: }
184:
185: public void testCyclic() throws Exception {
186: CompassSession session = openSession();
187: CompassTransaction tr = session.beginTransaction();
188:
189: Long id = new Long(1);
190: Cyclic1 cyclic1 = new Cyclic1();
191: cyclic1.setId(id);
192: cyclic1.setValue("cyclic1");
193:
194: Cyclic2 cyclic2 = new Cyclic2();
195: cyclic2.setId(id);
196: cyclic2.setValue("cyclic2");
197:
198: cyclic1.setCyclic2(cyclic2);
199: cyclic2.setCyclic1(cyclic1);
200:
201: session.save(cyclic2);
202: session.save(cyclic1);
203:
204: cyclic1 = (Cyclic1) session.load(Cyclic1.class, id);
205: assertNotNull(cyclic1.getCyclic2());
206: assertEquals("cyclic2", cyclic1.getCyclic2().getValue());
207: cyclic2 = cyclic1.getCyclic2();
208: assertNotNull(cyclic2);
209: assertEquals("cyclic1", cyclic2.getCyclic1().getValue());
210:
211: cyclic2 = (Cyclic2) session.load(Cyclic2.class, id);
212: assertNotNull(cyclic2.getCyclic1());
213: assertEquals("cyclic1", cyclic2.getCyclic1().getValue());
214:
215: tr.commit();
216: session.close();
217: }
218:
219: public void testCyclicNull() throws Exception {
220: CompassSession session = openSession();
221: CompassTransaction tr = session.beginTransaction();
222:
223: Long id = new Long(1);
224: Cyclic1 cyclic1 = new Cyclic1();
225: cyclic1.setId(id);
226: cyclic1.setValue("cyclic1");
227:
228: session.save(cyclic1);
229:
230: cyclic1 = (Cyclic1) session.load(Cyclic1.class, id);
231: assertNull(cyclic1.getCyclic2());
232:
233: tr.commit();
234: session.close();
235: }
236:
237: public void testManyToMany() throws Exception {
238: CompassSession session = openSession();
239: CompassTransaction tr = session.beginTransaction();
240:
241: ManyToMany1 many11 = new ManyToMany1();
242: many11.id = new Long(1);
243: many11.value = "many11";
244:
245: ManyToMany1 many12 = new ManyToMany1();
246: many12.id = new Long(2);
247: many12.value = "many12";
248:
249: ManyToMany2 many21 = new ManyToMany2();
250: many21.id = new Long(1);
251: many21.value = "many21";
252:
253: many11.many2.add(many21);
254: many12.many2.add(many21);
255:
256: many21.many1.add(many11);
257: many21.many1.add(many12);
258:
259: session.save(many11);
260: session.save(many12);
261: session.save(many21);
262:
263: many21 = (ManyToMany2) session.load("many2", new Long(1));
264: assertEquals("many21", many21.value);
265: assertEquals(2, many21.many1.size());
266: assertEquals("many11",
267: ((ManyToMany1) many21.many1.get(0)).value);
268: assertEquals("many12",
269: ((ManyToMany1) many21.many1.get(1)).value);
270:
271: many11 = (ManyToMany1) session.load("many1", new Long(1));
272: assertEquals("many11", many11.value);
273: assertEquals(1, many11.many2.size());
274: assertEquals("many21",
275: ((ManyToMany2) many11.many2.get(0)).value);
276:
277: tr.commit();
278: session.close();
279: }
280:
281: }
|