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.id;
018:
019: import org.compass.core.CompassHits;
020: import org.compass.core.CompassSession;
021: import org.compass.core.CompassTransaction;
022: import org.compass.core.Resource;
023: import org.compass.core.mapping.CompassMapping;
024: import org.compass.core.mapping.ResourcePropertyMapping;
025: import org.compass.core.mapping.osem.ClassMapping;
026: import org.compass.core.spi.InternalCompassSession;
027: import org.compass.core.test.AbstractTestCase;
028:
029: /**
030: * @author kimchy
031: */
032: public class IdTests extends AbstractTestCase {
033:
034: protected String[] getMappings() {
035: return new String[] { "id/id.cpm.xml" };
036: }
037:
038: public void testMultiSaveMultiId() {
039: CompassSession session = openSession();
040:
041: CompassMapping mapping = ((InternalCompassSession) session)
042: .getMapping();
043: ClassMapping firstMapping = (ClassMapping) mapping
044: .getRootMappingByClass(MultipleId.class);
045: ResourcePropertyMapping[] mappings = firstMapping
046: .getResourcePropertyMappings();
047: assertEquals(3, mappings.length);
048:
049: CompassTransaction tr = session.beginTransaction();
050: MultipleId o = new MultipleId();
051: o.setId(new Long(1));
052: o.setId2("2");
053: o.setValue("test");
054:
055: session.save(o);
056: session.save(o);
057:
058: CompassHits results = session.find("mvalue:test");
059: assertEquals(1, results.getLength());
060: o = (MultipleId) results.data(0);
061: assertEquals(new Long(1), o.getId());
062: assertEquals("2", o.getId2());
063: assertEquals("test", o.getValue());
064:
065: tr.commit();
066: session.close();
067: }
068:
069: public void testMultiId() {
070: CompassSession session = openSession();
071: CompassTransaction tr = session.beginTransaction();
072: MultipleId o = new MultipleId();
073: o.setId(new Long(1));
074: o.setId2("2");
075: o.setValue("test");
076:
077: MultipleId ref = o;
078:
079: session.save(o);
080:
081: CompassHits results = session.find("mvalue:test");
082: assertEquals(1, results.getLength());
083: o = (MultipleId) results.data(0);
084: assertEquals(new Long(1), o.getId());
085: assertEquals("2", o.getId2());
086: assertEquals("test", o.getValue());
087:
088: o = session.load(MultipleId.class, 1, "2");
089: assertEquals(new Long(1), o.getId());
090: assertEquals("2", o.getId2());
091: assertEquals("test", o.getValue());
092:
093: o = session.load(MultipleId.class, o);
094: assertEquals(new Long(1), o.getId());
095: assertEquals("2", o.getId2());
096: assertEquals("test", o.getValue());
097:
098: session.delete(o);
099: o = session.get(MultipleId.class, o);
100: assertNull(o);
101:
102: tr.commit();
103:
104: tr = session.beginTransaction();
105: o = session.get(MultipleId.class, ref);
106: assertNull(o);
107: tr.commit();
108: }
109:
110: public void testMultiIdDelete() {
111: CompassSession session = openSession();
112: CompassTransaction tr = session.beginTransaction();
113: MultipleId o = new MultipleId();
114: o.setId(new Long(1));
115: o.setId2("2");
116: o.setValue("test");
117: session.save(o);
118:
119: o = session.load(MultipleId.class, 1, "2");
120: assertEquals(new Long(1), o.getId());
121: assertEquals("2", o.getId2());
122:
123: session.delete("multiple-id", 1, "2");
124: o = session.get(MultipleId.class, 1, "2");
125: assertNull(o);
126:
127: tr.commit();
128: session.close();
129: }
130:
131: public void testSingleId() {
132: CompassSession session = openSession();
133: CompassTransaction tr = session.beginTransaction();
134: SingleId o = new SingleId();
135: o.setId(new Long(1));
136: o.setValue("test");
137: session.save(o);
138:
139: CompassHits results = session.find("mvalue:test");
140: assertEquals(1, results.getLength());
141: o = (SingleId) results.data(0);
142: assertEquals(new Long(1), o.getId());
143: assertEquals("test", o.getValue());
144:
145: results = session.find("$/single-id/id:1");
146: assertEquals(1, results.getLength());
147:
148: o = (SingleId) session.load("single-id", new Long(1));
149: assertEquals(new Long(1), o.getId());
150: assertEquals("test", o.getValue());
151:
152: o = (SingleId) session.load("single-id", o);
153: assertEquals(new Long(1), o.getId());
154: assertEquals("test", o.getValue());
155:
156: SingleId notFound = (SingleId) session.get("single-id",
157: new Long(2));
158: assertNull(notFound);
159:
160: Resource resource = session.loadResource("single-id", new Long(
161: 1));
162: assertEquals("test", resource.getProperty("mvalue")
163: .getStringValue());
164:
165: results = session.find("mvalue:test");
166: assertEquals(1, results.getLength());
167: resource = results.resource(0);
168: assertEquals("test", resource.getProperty("mvalue")
169: .getStringValue());
170:
171: session.delete(o);
172: o = (SingleId) session.get("single-id", o);
173: assertNull(o);
174:
175: tr.commit();
176: }
177:
178: public void testSingleIdDelete() {
179: CompassSession session = openSession();
180: CompassTransaction tr = session.beginTransaction();
181: SingleId o = new SingleId();
182: o.setId(new Long(1));
183: o.setValue("test");
184: session.save(o);
185:
186: o = (SingleId) session.load("single-id", new Long(1));
187: assertEquals(new Long(1), o.getId());
188:
189: // must delete with the alias here, since otherwise it won't be able to
190: // find the mapping
191: session.delete("single-id", new Long(1));
192: o = (SingleId) session.get("single-id", new Long(1));
193: assertNull(o);
194:
195: tr.commit();
196: session.close();
197: }
198:
199: public void testNullValue() {
200: CompassSession session = openSession();
201: CompassTransaction tr = session.beginTransaction();
202: SingleId o = new SingleId();
203: o.setId(new Long(1));
204: o.setValue(null);
205: session.save(o);
206:
207: o = (SingleId) session.load("single-id", new Long(1));
208: assertEquals(new Long(1), o.getId());
209: assertNull(o.getValue());
210:
211: tr.commit();
212: }
213:
214: public void testURIId() {
215: CompassSession session = openSession();
216: CompassTransaction tr = session.beginTransaction();
217:
218: URIId o = new URIId();
219: o.setId("http://test/uri");
220: session.save(o);
221:
222: o = (URIId) session.load(URIId.class, "http://test/uri");
223: assertEquals("http://test/uri", o.getId());
224:
225: o.setId("this is a long string");
226: session.save(o);
227: o = (URIId) session.load(URIId.class, "this is a long string");
228: assertEquals("this is a long string", o.getId());
229:
230: tr.commit();
231: }
232: }
|