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;
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.ResourcePropertyLookup;
024: import org.compass.core.mapping.ResourcePropertyMapping;
025: import org.compass.core.mapping.osem.ClassIdPropertyMapping;
026: import org.compass.core.mapping.osem.ClassMapping;
027: import org.compass.core.spi.InternalCompassSession;
028: import org.compass.core.test.AbstractTestCase;
029:
030: /**
031: * @author kimchy
032: */
033: public class ComponentTests extends AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "component/C.cpm.xml",
037: "component/SimpleRoot.cpm.xml",
038: "component/SimpleComponent.cpm.xml",
039: "component/id-component.cpm.xml" };
040: }
041:
042: public void testMappings() {
043: InternalCompassSession session = (InternalCompassSession) openSession();
044:
045: CompassMapping mapping = session.getMapping();
046: ClassMapping firstMapping = (ClassMapping) mapping
047: .getRootMappingByClass(CFirst.class);
048: String[] propertyNames = firstMapping
049: .getResourcePropertyNames();
050: assertEquals(2, propertyNames.length);
051: ClassIdPropertyMapping[] idMappings = firstMapping
052: .getClassIdPropertyMappings();
053: assertEquals(1, idMappings.length);
054: ResourcePropertyMapping[] resourcePropertyMappings = idMappings[0]
055: .getResourceIdMappings();
056: assertEquals(1, resourcePropertyMappings.length);
057: assertEquals("$/first/id", resourcePropertyMappings[0]
058: .getPath().getPath());
059: assertEquals("id", resourcePropertyMappings[0].getName());
060: ResourcePropertyLookup lookup = mapping
061: .getResourcePropertyLookup("value");
062: assertEquals(7, lookup.getResourcePropertyMappings().length);
063:
064: ResourcePropertyMapping resourcePropertyMapping = firstMapping
065: .getResourcePropertyMappingByDotPath("id");
066: assertNotNull(resourcePropertyMapping);
067: assertEquals("id", resourcePropertyMapping.getName());
068: assertEquals("$/first/id", resourcePropertyMapping.getPath()
069: .getPath());
070:
071: resourcePropertyMapping = firstMapping
072: .getResourcePropertyMappingByDotPath("value");
073: assertNotNull(resourcePropertyMapping);
074: assertEquals("value", resourcePropertyMapping.getName());
075: assertEquals("$/first/value", resourcePropertyMapping.getPath()
076: .getPath());
077:
078: resourcePropertyMapping = firstMapping
079: .getResourcePropertyMappingByDotPath("value.value");
080: assertNotNull(resourcePropertyMapping);
081: assertEquals("value", resourcePropertyMapping.getName());
082: assertEquals("value", resourcePropertyMapping.getPath()
083: .getPath());
084:
085: resourcePropertyMapping = firstMapping
086: .getResourcePropertyMappingByDotPath("second.value");
087: assertNotNull(resourcePropertyMapping);
088: assertEquals("value", resourcePropertyMapping.getName());
089: assertEquals("$/first/second/value", resourcePropertyMapping
090: .getPath().getPath());
091:
092: resourcePropertyMapping = firstMapping
093: .getResourcePropertyMappingByDotPath("second.value.value");
094: assertNotNull(resourcePropertyMapping);
095: assertEquals("value", resourcePropertyMapping.getName());
096: assertEquals("value", resourcePropertyMapping.getPath()
097: .getPath());
098:
099: resourcePropertyMapping = firstMapping
100: .getResourcePropertyMappingByDotPath("second.third.value");
101: assertNotNull(resourcePropertyMapping);
102: assertEquals("value", resourcePropertyMapping.getName());
103: assertEquals("$/first/second/third/value",
104: resourcePropertyMapping.getPath().getPath());
105:
106: resourcePropertyMapping = firstMapping
107: .getResourcePropertyMappingByDotPath("second.third.value.value");
108: assertNotNull(resourcePropertyMapping);
109: assertEquals("value", resourcePropertyMapping.getName());
110: assertEquals("value", resourcePropertyMapping.getPath()
111: .getPath());
112:
113: session.close();
114: }
115:
116: public void testRoot() {
117: CompassSession session = openSession();
118: CompassTransaction tr = session.beginTransaction();
119:
120: Long id = new Long(1);
121:
122: SimpleRoot root = new SimpleRoot();
123: root.setId(id);
124: root.setValue("test");
125: SimpleComponent first = new SimpleComponent();
126: first.setValue("test1");
127: root.setFirstComponent(first);
128: SimpleComponent second = new SimpleComponent();
129: second.setValue("test2");
130: root.setSecondComponent(second);
131: session.save("sr", root);
132:
133: root = (SimpleRoot) session.load("sr", id);
134: assertEquals("test", root.getValue());
135: assertNotNull(root.getFirstComponent());
136: assertEquals("test1", root.getFirstComponent().getValue());
137: assertNotNull(root.getSecondComponent());
138: assertEquals("test2", root.getSecondComponent().getValue());
139:
140: tr.commit();
141: }
142:
143: public void testIdComp() {
144: CompassSession session = openSession();
145: CompassTransaction tr = session.beginTransaction();
146:
147: Long id = new Long(1);
148:
149: SimpleRootId root = new SimpleRootId();
150: root.setId(id);
151: root.setValue("test");
152: SimpleComponentId first = new SimpleComponentId();
153: first.setId(new Long(1));
154: first.setValue("test1");
155: root.setFirstComponent(first);
156: SimpleComponentId second = new SimpleComponentId();
157: second.setId(new Long(2));
158: second.setValue("test2");
159: root.setSecondComponent(second);
160: session.save("id-sr", root);
161:
162: root = new SimpleRootId();
163: root.setId(new Long(2));
164: root.setValue("test");
165: first = new SimpleComponentId();
166: first.setId(new Long(3));
167: first.setValue("test1");
168: root.setFirstComponent(first);
169: second = new SimpleComponentId();
170: second.setId(new Long(4));
171: second.setValue("test2");
172: root.setSecondComponent(second);
173: session.save("id-sr", root);
174:
175: root = (SimpleRootId) session.load("id-sr", id);
176: assertEquals("test", root.getValue());
177: assertNotNull(root.getFirstComponent());
178: assertEquals("test1", root.getFirstComponent().getValue());
179: assertNotNull(root.getSecondComponent());
180: assertEquals("test2", root.getSecondComponent().getValue());
181:
182: CompassHits hits = session.find("id-sc:2");
183: assertEquals(1, hits.getLength());
184: root = (SimpleRootId) hits.data(0);
185: assertEquals("test", root.getValue());
186: assertNotNull(root.getFirstComponent());
187: assertEquals("test1", root.getFirstComponent().getValue());
188: assertNotNull(root.getSecondComponent());
189: assertEquals("test2", root.getSecondComponent().getValue());
190:
191: tr.commit();
192: }
193:
194: public void testCXNull() {
195: CompassSession session = openSession();
196: CompassTransaction tr = session.beginTransaction();
197:
198: Long id = new Long(1);
199:
200: CFirst first = new CFirst();
201: first.setId(id);
202: first.setValue("test");
203: CSecond second = new CSecond();
204: second.setValue("test1");
205: first.setSecond(second);
206: session.save(first);
207:
208: first = (CFirst) session.load(CFirst.class, id);
209: assertEquals("test", first.getValue());
210: assertNotNull(first.getSecond());
211: assertEquals("test1", first.getSecond().getValue());
212: assertNull(first.getSecond().getThird());
213:
214: tr.commit();
215: }
216:
217: public void testCX() {
218: CompassSession session = openSession();
219: CompassTransaction tr = session.beginTransaction();
220:
221: Long id = new Long(1);
222:
223: CFirst first = new CFirst();
224: first.setId(id);
225: first.setValue("test");
226: CSecond second = new CSecond();
227: second.setValue("test1");
228: CThird third = new CThird();
229: third.setValue("test2");
230: CFourth fourth = new CFourth();
231: fourth.setValue("test3");
232: third.setFourth(fourth);
233: second.setThird(third);
234: first.setSecond(second);
235: session.save(first);
236:
237: first = (CFirst) session.load(CFirst.class, id);
238: assertEquals("test", first.getValue());
239: assertNotNull(first.getSecond());
240: assertEquals("test1", first.getSecond().getValue());
241: assertNotNull(first.getSecond().getThird());
242: assertEquals("test2", first.getSecond().getThird().getValue());
243: assertNotNull(first.getSecond().getThird().getFourth());
244: assertEquals("test3", first.getSecond().getThird().getFourth()
245: .getValue());
246:
247: CompassHits hits = session.find("test");
248: assertEquals(1, hits.getLength());
249: hits = session.find("test1");
250: assertEquals(1, hits.getLength());
251: hits = session.find("test2");
252: assertEquals(1, hits.getLength());
253: hits = session.find("test3");
254: assertEquals(1, hits.getLength());
255:
256: tr.commit();
257: }
258: }
|