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.sort;
018:
019: import org.compass.core.CompassHits;
020: import org.compass.core.CompassQuery;
021: import org.compass.core.CompassQuery.SortDirection;
022: import org.compass.core.CompassQuery.SortImplicitType;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.test.AbstractTestCase;
026:
027: public class SortTests extends AbstractTestCase {
028:
029: protected String[] getMappings() {
030: return new String[] { "sort/sort.cpm.xml" };
031: }
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035: CompassSession session = openSession();
036: CompassTransaction tr = session.beginTransaction();
037:
038: A a = new A(new Long(1), "aab test testA", 1, 1.1f, new B(
039: new Integer(2), "aab"));
040: session.save(a);
041: a = new A(new Long(2), "aac test testA", 2, 1.0f, new B(
042: new Integer(1), "aac"));
043: session.save(a);
044: a = new A(new Long(3), "bbc test testB", 10, -1.0f, new B(
045: new Integer(3), "aaa"));
046: session.save(a);
047: a = new A(new Long(4), "zx test testB", -10, 1.3f, new B(
048: new Integer(4), "aad"));
049: session.save(a);
050:
051: tr.commit();
052: session.close();
053: }
054:
055: public void testSortComponent() {
056: CompassSession session = openSession();
057: CompassTransaction tr = session.beginTransaction();
058:
059: CompassHits hits = session.queryBuilder().queryString("test")
060: .toQuery().addSort("a.id", SortDirection.AUTO).hits();
061: assertEquals(4, hits.length());
062: assertAId(1, 0, hits);
063: assertAId(2, 1, hits);
064: assertAId(3, 2, hits);
065: assertAId(4, 3, hits);
066:
067: hits = session.queryBuilder().queryString("test").toQuery()
068: .addSort("a.b.id", SortDirection.AUTO).hits();
069: assertEquals(4, hits.length());
070: assertAId(2, 0, hits);
071: assertAId(1, 1, hits);
072: assertAId(3, 2, hits);
073: assertAId(4, 3, hits);
074:
075: hits = session.queryBuilder().queryString("test").toQuery()
076: .addSort("a.value2", SortDirection.AUTO).hits();
077: assertEquals(4, hits.length());
078: assertAId(1, 0, hits);
079: assertAId(2, 1, hits);
080: assertAId(3, 2, hits);
081: assertAId(4, 3, hits);
082:
083: hits = session.queryBuilder().queryString("test").toQuery()
084: .addSort("a.b.value2", SortDirection.AUTO).hits();
085: assertEquals(4, hits.length());
086: assertAId(3, 0, hits);
087: assertAId(1, 1, hits);
088: assertAId(2, 2, hits);
089: assertAId(4, 3, hits);
090:
091: tr.commit();
092: session.close();
093: }
094:
095: public void testSortRelevance() {
096: CompassSession session = openSession();
097: CompassTransaction tr = session.beginTransaction();
098:
099: CompassQuery query = session.queryBuilder().queryString("test")
100: .toQuery();
101: query.addSort(SortImplicitType.SCORE);
102: CompassHits hits = query.hits();
103: assertEquals(4, hits.length());
104: assertAId(1, 0, hits);
105: assertAId(2, 1, hits);
106: assertAId(3, 2, hits);
107: assertAId(4, 3, hits);
108: tr.commit();
109:
110: session.close();
111: }
112:
113: public void testSortRelevanceReverse() {
114: CompassSession session = openSession();
115: CompassTransaction tr = session.beginTransaction();
116:
117: // TODO WHY is it not reversed?
118: CompassQuery query = session.queryBuilder().queryString("test")
119: .toQuery();
120: query.addSort(SortImplicitType.SCORE, SortDirection.REVERSE);
121: CompassHits hits = query.hits();
122: assertEquals(4, hits.length());
123: assertAId(1, 0, hits);
124: assertAId(2, 1, hits);
125: assertAId(3, 2, hits);
126: assertAId(4, 3, hits);
127: tr.commit();
128:
129: session.close();
130: }
131:
132: public void testSortDoc() {
133: CompassSession session = openSession();
134: CompassTransaction tr = session.beginTransaction();
135:
136: CompassQuery query = session.queryBuilder().queryString("test")
137: .toQuery();
138: query.addSort(SortImplicitType.DOC);
139: CompassHits hits = query.hits();
140: assertEquals(4, hits.length());
141: assertAId(1, 0, hits);
142: assertAId(2, 1, hits);
143: assertAId(3, 2, hits);
144: assertAId(4, 3, hits);
145: tr.commit();
146:
147: session.close();
148: }
149:
150: public void testSortDocReverse() {
151: CompassSession session = openSession();
152: CompassTransaction tr = session.beginTransaction();
153:
154: CompassQuery query = session.queryBuilder().queryString("test")
155: .toQuery();
156: query.addSort(SortImplicitType.DOC, SortDirection.REVERSE);
157: CompassHits hits = query.hits();
158: assertEquals(4, hits.length());
159: assertAId(4, 0, hits);
160: assertAId(3, 1, hits);
161: assertAId(2, 2, hits);
162: assertAId(1, 3, hits);
163: tr.commit();
164:
165: session.close();
166: }
167:
168: public void testSortString() {
169: CompassSession session = openSession();
170: CompassTransaction tr = session.beginTransaction();
171:
172: CompassQuery query = session.queryBuilder().queryString("test")
173: .toQuery();
174: query.addSort("value", CompassQuery.SortPropertyType.STRING);
175: CompassHits hits = query.hits();
176: assertEquals(4, hits.length());
177: assertAId(1, 0, hits);
178: assertAId(2, 1, hits);
179: assertAId(3, 2, hits);
180: assertAId(4, 3, hits);
181: tr.commit();
182:
183: session.close();
184: }
185:
186: public void testSortStringReverse() {
187: CompassSession session = openSession();
188: CompassTransaction tr = session.beginTransaction();
189:
190: CompassQuery query = session.queryBuilder().queryString("test")
191: .toQuery();
192: query.addSort("value", CompassQuery.SortPropertyType.STRING,
193: CompassQuery.SortDirection.REVERSE);
194: CompassHits hits = query.hits();
195: assertEquals(4, hits.length());
196: assertAId(4, 0, hits);
197: assertAId(3, 1, hits);
198: assertAId(2, 2, hits);
199: assertAId(1, 3, hits);
200: tr.commit();
201:
202: session.close();
203: }
204:
205: public void testSortInt() {
206: CompassSession session = openSession();
207: CompassTransaction tr = session.beginTransaction();
208:
209: CompassQuery query = session.queryBuilder().queryString("test")
210: .toQuery();
211: query.addSort("intValue", CompassQuery.SortPropertyType.INT);
212: CompassHits hits = query.hits();
213: assertEquals(4, hits.length());
214: assertAId(4, 0, hits);
215: assertAId(1, 1, hits);
216: assertAId(2, 2, hits);
217: assertAId(3, 3, hits);
218: tr.commit();
219:
220: session.close();
221: }
222:
223: public void testSortIntReverse() {
224: CompassSession session = openSession();
225: CompassTransaction tr = session.beginTransaction();
226:
227: CompassQuery query = session.queryBuilder().queryString("test")
228: .toQuery();
229: query.addSort("intValue", CompassQuery.SortPropertyType.INT,
230: CompassQuery.SortDirection.REVERSE);
231: CompassHits hits = query.hits();
232: assertEquals(4, hits.length());
233: assertAId(3, 0, hits);
234: assertAId(2, 1, hits);
235: assertAId(1, 2, hits);
236: assertAId(4, 3, hits);
237: tr.commit();
238:
239: session.close();
240: }
241:
242: public void testSortFloat() {
243: CompassSession session = openSession();
244: CompassTransaction tr = session.beginTransaction();
245:
246: CompassQuery query = session.queryBuilder().queryString("test")
247: .toQuery();
248: query
249: .addSort("floatValue",
250: CompassQuery.SortPropertyType.FLOAT);
251: CompassHits hits = query.hits();
252: assertEquals(4, hits.length());
253: assertAId(3, 0, hits);
254: assertAId(2, 1, hits);
255: assertAId(1, 2, hits);
256: assertAId(4, 3, hits);
257: tr.commit();
258:
259: session.close();
260: }
261:
262: public void testSortFloatReverse() {
263: CompassSession session = openSession();
264: CompassTransaction tr = session.beginTransaction();
265:
266: CompassQuery query = session.queryBuilder().queryString("test")
267: .toQuery();
268: query.addSort("floatValue",
269: CompassQuery.SortPropertyType.FLOAT,
270: CompassQuery.SortDirection.REVERSE);
271: CompassHits hits = query.hits();
272: assertEquals(4, hits.length());
273: assertAId(4, 0, hits);
274: assertAId(1, 1, hits);
275: assertAId(2, 2, hits);
276: assertAId(3, 3, hits);
277: tr.commit();
278:
279: session.close();
280: }
281:
282: private void assertAId(long id, int hitNum, CompassHits hits) {
283: A a = (A) hits.data(hitNum);
284: assertEquals(id, a.getId().longValue());
285: }
286: }
|