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.resource;
018:
019: import java.io.StringReader;
020: import java.util.Calendar;
021:
022: import org.compass.core.CompassHits;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.Property;
026: import org.compass.core.Resource;
027: import org.compass.core.config.CompassEnvironment;
028: import org.compass.core.config.CompassSettings;
029: import org.compass.core.mapping.CompassMapping;
030: import org.compass.core.mapping.ResourceMapping;
031: import org.compass.core.mapping.ResourcePropertyMapping;
032: import org.compass.core.spi.InternalCompassSession;
033: import org.compass.core.test.AbstractTestCase;
034:
035: /**
036: * @author kimchy
037: */
038: public class ResourceTests extends AbstractTestCase {
039:
040: protected String[] getMappings() {
041: return new String[] { "resource/resource.cpm.xml" };
042: }
043:
044: protected void addSettings(CompassSettings settings) {
045: settings
046: .setGroupSettings(
047: CompassEnvironment.Converter.PREFIX,
048: "mydate",
049: new String[] {
050: CompassEnvironment.Converter.TYPE,
051: CompassEnvironment.Converter.Format.FORMAT },
052: new String[] {
053: CompassEnvironment.Converter.DefaultTypes.Simple.DATE,
054: "yyyy-MM-dd" });
055:
056: }
057:
058: public void testMapping() {
059: InternalCompassSession session = (InternalCompassSession) openSession();
060:
061: CompassMapping mapping = session.getMapping();
062: ResourceMapping resourceMapping = mapping
063: .getMappingByAlias("a");
064: ResourcePropertyMapping resourcePropertyMapping = resourceMapping
065: .getResourcePropertyMappingByDotPath("id");
066: assertNotNull(resourcePropertyMapping);
067: assertEquals("id", resourcePropertyMapping.getName());
068: assertEquals("id", resourcePropertyMapping.getPath().getPath());
069:
070: resourceMapping = mapping.getMappingByAlias("b");
071: resourcePropertyMapping = resourceMapping
072: .getResourcePropertyMappingByDotPath("id1");
073: assertNotNull(resourcePropertyMapping);
074: assertEquals("id1", resourcePropertyMapping.getName());
075: assertEquals("id1", resourcePropertyMapping.getPath().getPath());
076: resourcePropertyMapping = resourceMapping
077: .getResourcePropertyMappingByDotPath("id2");
078: assertNotNull(resourcePropertyMapping);
079: assertEquals("id2", resourcePropertyMapping.getName());
080: assertEquals("id2", resourcePropertyMapping.getPath().getPath());
081:
082: session.close();
083: }
084:
085: public void testSingleIdResource() throws Exception {
086: CompassSession session = openSession();
087: CompassTransaction tr = session.beginTransaction();
088:
089: Resource r = getResourceFactory().createResource("a");
090: Property id = getResourceFactory().createProperty("id", "1",
091: Property.Store.YES, Property.Index.UN_TOKENIZED);
092: r.addProperty(id);
093: r.addProperty(getCompass().getResourceFactory().createProperty(
094: "mvalue", "this is a test", Property.Store.YES,
095: Property.Index.TOKENIZED));
096:
097: session.save(r);
098:
099: r = session.getResource("a", "1");
100: assertEquals("this is a test", r.getValue("mvalue"));
101:
102: r = session.getResource("a", (Object) new String[] { "1" });
103: assertEquals("this is a test", r.getValue("mvalue"));
104:
105: r = session.getResource("a", id);
106: assertEquals("this is a test", r.getValue("mvalue"));
107:
108: r = session.getResource("a", (Object) new Property[] { id });
109: assertEquals("this is a test", r.getValue("mvalue"));
110:
111: r = session.getResource("a", r);
112: assertEquals("this is a test", r.getValue("mvalue"));
113:
114: CompassHits hits = session.find("test");
115: assertEquals(1, hits.getLength());
116: assertEquals("this is a test", hits.resource(0).getValue(
117: "mvalue"));
118:
119: session.delete("a", "1");
120: r = session.getResource("a", r);
121: assertNull(r);
122:
123: tr.commit();
124: session.close();
125: }
126:
127: public void testMultipleIdResource() throws Exception {
128: CompassSession session = openSession();
129: CompassTransaction tr = session.beginTransaction();
130:
131: Resource r = getResourceFactory().createResource("b");
132: Property id1 = getResourceFactory().createProperty("id1", "1",
133: Property.Store.YES, Property.Index.UN_TOKENIZED);
134: Property id2 = getResourceFactory().createProperty("id2", "2",
135: Property.Store.YES, Property.Index.UN_TOKENIZED);
136: r.addProperty(id1);
137: r.addProperty(id2);
138: r.addProperty(getResourceFactory().createProperty("mvalue",
139: "this is a test", Property.Store.YES,
140: Property.Index.TOKENIZED));
141:
142: session.save(r);
143:
144: r = session.getResource("b", "1", "2");
145: assertEquals("this is a test", r.getValue("mvalue"));
146:
147: // TODO need to make this work
148: // r = session.getResource("b", id1, id2);
149: r = session.getResource("b",
150: (Object) new Property[] { id1, id2 });
151: assertEquals("this is a test", r.getValue("mvalue"));
152:
153: r = session.getResource("b", r);
154: assertEquals("this is a test", r.getValue("mvalue"));
155:
156: CompassHits hits = session.find("test");
157: assertEquals(1, hits.getLength());
158: assertEquals("this is a test", hits.resource(0).getValue(
159: "mvalue"));
160:
161: tr.commit();
162: session.close();
163: }
164:
165: public void testSimplePropertyMapping() throws Exception {
166: CompassSession session = openSession();
167: CompassTransaction tr = session.beginTransaction();
168:
169: Resource r = getResourceFactory().createResource("c");
170: r.addProperty("id", "1");
171: r.addProperty("value1", "this is a test");
172: r.addProperty("value2", "this is a test");
173: r.addProperty("value3", "this is a test");
174: r.addProperty("value4", "this is a test");
175: Calendar cal = Calendar.getInstance();
176: cal.set(2000, 1, 1, 0, 0, 0);
177: r.addProperty("value5", cal.getTime());
178: r.addProperty("value6", new StringReader("reader"));
179:
180: session.save(r);
181:
182: r = session.getResource("c", "1");
183: Property prop = r.getProperty("value1");
184: assertEquals("this is a test", prop.getStringValue());
185: assertTrue(prop.isIndexed());
186: assertTrue(prop.isStored());
187: assertTrue(prop.isTokenized());
188: assertFalse(prop.isCompressed());
189:
190: prop = r.getProperty("value2");
191: assertEquals("this is a test", prop.getStringValue());
192: assertTrue(prop.isIndexed());
193: assertTrue(prop.isStored());
194: assertTrue(prop.isTokenized());
195: assertFalse(prop.isCompressed());
196:
197: prop = r.getProperty("value3");
198: assertEquals("this is a test", prop.getStringValue());
199: assertTrue(prop.isIndexed());
200: assertTrue(prop.isStored());
201: assertTrue(prop.isTokenized());
202: assertTrue(prop.isCompressed());
203:
204: prop = r.getProperty("value4");
205: assertEquals("this is a test", prop.getStringValue());
206: assertTrue(prop.isIndexed());
207: assertTrue(prop.isStored());
208: assertFalse(prop.isTokenized());
209: assertFalse(prop.isCompressed());
210:
211: prop = r.getProperty("value5");
212: assertEquals("2000-02-01", prop.getStringValue());
213: assertEquals(cal.getTime().toString(), prop.getObjectValue()
214: .toString());
215: assertTrue(prop.isIndexed());
216: assertTrue(prop.isStored());
217: assertTrue(prop.isTokenized());
218: assertFalse(prop.isCompressed());
219:
220: CompassHits hits = session.find("value6:reader");
221: assertEquals(1, hits.length());
222:
223: hits = session.find("value5:2000-02-01");
224: assertEquals(1, hits.length());
225:
226: hits = session.find("value5:[2000-01-01 TO 2000-02-01]");
227: assertEquals(1, hits.length());
228:
229: tr.commit();
230: session.close();
231: }
232:
233: public void testResourceExtends() {
234: CompassSession session = openSession();
235: CompassTransaction tr = session.beginTransaction();
236:
237: Resource r = getResourceFactory().createResource("d");
238: r.addProperty("id", "1");
239: r.addProperty("value1", "this is a test");
240: session.save(r);
241:
242: r = session.getResource("d", "1");
243: assertEquals("this is a test", r.getValue("value1"));
244:
245: tr.commit();
246: session.close();
247: }
248:
249: public void testResourceContractExtends() {
250: CompassSession session = openSession();
251: CompassTransaction tr = session.beginTransaction();
252:
253: Resource r = getResourceFactory().createResource("e");
254: r.addProperty("id", "1");
255: r.addProperty("value1", "test1");
256: r.addProperty("value2", "test2");
257: session.save(r);
258:
259: r = session.getResource("e", "1");
260: assertNull(r.getValue("value1"));
261: assertEquals("test2", r.getValue("value2"));
262:
263: tr.commit();
264: session.close();
265: }
266:
267: public void testResourceExcludeFromAll() {
268: CompassSession session = openSession();
269: CompassTransaction tr = session.beginTransaction();
270:
271: Resource r = getResourceFactory().createResource("f");
272: r.addProperty("id1", "1");
273: r.addProperty("id2", "2");
274: r.addProperty("value1", "test1");
275: r.addProperty("value2", "test2");
276: session.save(r);
277:
278: CompassHits hits = session.find("1");
279: assertEquals(0, hits.length());
280: hits = session.find("2");
281: assertEquals(1, hits.length());
282: hits = session.find("test1");
283: assertEquals(0, hits.length());
284: hits = session.find("test2");
285: assertEquals(1, hits.length());
286:
287: tr.commit();
288: session.close();
289: }
290: }
|