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.first.cache;
018:
019: import org.compass.core.CompassHits;
020: import org.compass.core.CompassSession;
021: import org.compass.core.CompassTransaction;
022: import org.compass.core.Property;
023: import org.compass.core.Resource;
024: import org.compass.core.cache.first.DefaultFirstLevelCache;
025: import org.compass.core.config.CompassEnvironment;
026: import org.compass.core.config.CompassSettings;
027: import org.compass.core.test.AbstractTestCase;
028:
029: /**
030: * @author kimchy
031: */
032: public class FirstCacheTests extends AbstractTestCase {
033:
034: protected String[] getMappings() {
035: return new String[] { "first/cache/first-cache.cpm.xml" };
036: }
037:
038: protected void addSettings(CompassSettings settings) {
039: super .addSettings(settings);
040: settings.setSetting(CompassEnvironment.Cache.FirstLevel.TYPE,
041: DefaultFirstLevelCache.class.getName());
042: }
043:
044: public void testResourceCache() {
045: CompassSession session = openSession();
046: CompassTransaction tr = session.beginTransaction();
047:
048: Resource r = getResourceFactory().createResource("r");
049: Property id = getResourceFactory().createProperty("id", "1",
050: Property.Store.YES, Property.Index.UN_TOKENIZED);
051: r.addProperty(id);
052: r.addProperty(getResourceFactory().createProperty("mvalue",
053: "this is a test", Property.Store.YES,
054: Property.Index.TOKENIZED));
055:
056: session.save(r);
057:
058: Resource cachedR = session.getResource("r", "1");
059: assertEquals("this is a test", r.getValue("mvalue"));
060:
061: // compass creates a new Resource for the saved resource
062: // so we check for equals on alias and id
063: assertTrue(r.getAlias().equals(cachedR.getAlias()));
064: assertTrue(r.getValue("id").equals(cachedR.getValue("id")));
065: r = cachedR;
066:
067: cachedR = session.getResource("r",
068: (Object) new String[] { "1" });
069: assertEquals("this is a test", r.getValue("mvalue"));
070:
071: assertTrue(r == cachedR);
072:
073: CompassHits hits = session.find("test");
074: assertEquals(1, hits.getLength());
075: assertTrue(r == hits.resource(0));
076:
077: session.evict(r);
078: cachedR = session.getResource("r", "1");
079: assertFalse(r == cachedR);
080:
081: tr.commit();
082: }
083:
084: public void testObjectCached() {
085: CompassSession session = openSession();
086: CompassTransaction tr = session.beginTransaction();
087:
088: Long id = new Long(1);
089: A a = new A();
090: a.setId(id);
091: a.setValue("value");
092:
093: session.save(a);
094:
095: A cachedA = session.get(A.class, id);
096: assertTrue(a == cachedA);
097:
098: CompassHits hits = session.find("value");
099: assertEquals(1, hits.getLength());
100: assertTrue(a == hits.data(0));
101:
102: session.evict(a);
103: cachedA = session.get(A.class, id);
104: assertFalse(a == cachedA);
105:
106: session.save(a);
107: cachedA = session.get(A.class, id);
108: assertTrue(a == cachedA);
109: session.delete("a", id);
110: cachedA = session.get(A.class, id);
111: assertFalse(a == cachedA);
112:
113: tr.commit();
114: }
115: }
|