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.subindexhash;
018:
019: import org.apache.lucene.index.LuceneSubIndexInfo;
020: import org.compass.core.CompassSession;
021: import org.compass.core.CompassTransaction;
022: import org.compass.core.test.AbstractTestCase;
023:
024: /**
025: * @author kimchy
026: */
027: public class OsemModuloTests extends AbstractTestCase {
028:
029: protected String[] getMappings() {
030: return new String[] { "subindexhash/osem-modulo.cpm.xml" };
031: }
032:
033: public void testSingleObjectA() throws Exception {
034: CompassSession session = openSession();
035:
036: LuceneSubIndexInfo subIndexInfo = LuceneSubIndexInfo
037: .getIndexInfo("index_0", session);
038: assertEquals(0, subIndexInfo.size());
039: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_1",
040: session);
041: assertEquals(0, subIndexInfo.size());
042: try {
043: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_2",
044: session);
045: if (subIndexInfo != null) {
046: fail();
047: }
048: } catch (Exception e) {
049: // all is well
050: }
051: try {
052: subIndexInfo = LuceneSubIndexInfo
053: .getIndexInfo("a", session);
054: if (subIndexInfo != null) {
055: fail();
056: }
057: } catch (Exception e) {
058: // all is well
059: }
060:
061: CompassTransaction tr = session.beginTransaction();
062:
063: Long id = new Long(1);
064: A a = new A();
065: a.setId(id);
066: a.setValue("value1");
067: session.save(a);
068:
069: id = new Long(2);
070: a = new A();
071: a.setId(id);
072: a.setValue("value2");
073: session.save(a);
074:
075: a = (A) session.load("a", new Long(1));
076: assertEquals("value1", a.getValue());
077: a = (A) session.load("a", new Long(2));
078: assertEquals("value2", a.getValue());
079:
080: tr.commit();
081:
082: tr = session.beginTransaction();
083: a = (A) session.load("a", new Long(1));
084: assertEquals("value1", a.getValue());
085: a = (A) session.load("a", new Long(2));
086: assertEquals("value2", a.getValue());
087: tr.commit();
088:
089: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_0",
090: session);
091: assertEquals(1, subIndexInfo.size());
092: assertEquals(1, subIndexInfo.info(0).docCount());
093: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_1",
094: session);
095: assertEquals(1, subIndexInfo.size());
096: assertEquals(1, subIndexInfo.info(0).docCount());
097:
098: session.close();
099: }
100:
101: public void testMultiObjectsToSameModulo() throws Exception {
102: CompassSession session = openSession();
103:
104: LuceneSubIndexInfo subIndexInfo = LuceneSubIndexInfo
105: .getIndexInfo("index_0", session);
106: assertEquals(0, subIndexInfo.size());
107: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_1",
108: session);
109: assertEquals(0, subIndexInfo.size());
110: try {
111: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_2",
112: session);
113: if (subIndexInfo != null) {
114: fail();
115: }
116: } catch (Exception e) {
117: // all is well
118: }
119: try {
120: subIndexInfo = LuceneSubIndexInfo
121: .getIndexInfo("a", session);
122: if (subIndexInfo != null) {
123: fail();
124: }
125: } catch (Exception e) {
126: // all is well
127: }
128:
129: CompassTransaction tr = session.beginTransaction();
130:
131: A a = new A();
132: a.setId(new Long(1));
133: a.setValue("value1");
134: session.save(a);
135:
136: a = new A();
137: a.setId(new Long(2));
138: a.setValue("value2");
139: session.save(a);
140:
141: B b = new B();
142: b.setId(new Long(1));
143: b.setValue("valueb1");
144: session.save("b", b);
145:
146: b = new B();
147: b.setId(new Long(2));
148: b.setValue("valueb2");
149: session.save("b", b);
150:
151: a = (A) session.load("a", new Long(1));
152: assertEquals("value1", a.getValue());
153: a = (A) session.load("a", new Long(2));
154: assertEquals("value2", a.getValue());
155:
156: b = (B) session.load("b", new Long(1));
157: assertEquals("valueb1", b.getValue());
158: b = (B) session.load("b", new Long(2));
159: assertEquals("valueb2", b.getValue());
160:
161: tr.commit();
162:
163: tr = session.beginTransaction();
164:
165: a = (A) session.load("a", new Long(1));
166: assertEquals("value1", a.getValue());
167: a = (A) session.load("a", new Long(2));
168: assertEquals("value2", a.getValue());
169:
170: b = (B) session.load("b", new Long(1));
171: assertEquals("valueb1", b.getValue());
172: b = (B) session.load("b", new Long(2));
173: assertEquals("valueb2", b.getValue());
174:
175: tr.commit();
176:
177: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_0",
178: session);
179: assertEquals(1, subIndexInfo.size());
180: assertEquals(2, subIndexInfo.info(0).docCount());
181: subIndexInfo = LuceneSubIndexInfo.getIndexInfo("index_1",
182: session);
183: assertEquals(1, subIndexInfo.size());
184: assertEquals(2, subIndexInfo.info(0).docCount());
185:
186: session.close();
187: }
188: }
|