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.querybuilder.range1;
018:
019: import java.util.Date;
020:
021: import org.compass.core.CompassHits;
022: import org.compass.core.CompassSession;
023: import org.compass.core.CompassTransaction;
024: import org.compass.core.test.AbstractTestCase;
025:
026: /**
027: * @author kimchy
028: */
029: public class RangeQueryBuilderTests extends AbstractTestCase {
030:
031: protected String[] getMappings() {
032: return new String[] { "querybuilder/range1/mapping.cpm.xml" };
033: }
034:
035: public void testIntExactNoPadding() {
036: CompassSession session = openSession();
037: CompassTransaction tr = session.beginTransaction();
038:
039: A a = new A();
040: a.id = 1;
041: a.value = 1;
042: session.save("a", a);
043:
044: CompassHits hits = session.find("value:1");
045: assertEquals(1, hits.length());
046:
047: tr.commit();
048: session.close();
049: }
050:
051: public void testIntRangeNoPadding() {
052: CompassSession session = openSession();
053: CompassTransaction tr = session.beginTransaction();
054:
055: A a = new A();
056: a.id = 1;
057: a.value = 1;
058: session.save("a", a);
059:
060: a.id = 2;
061: a.value = 2;
062: session.save("a", a);
063:
064: a.id = 3;
065: a.value = 11;
066: session.save("a", a);
067:
068: a.id = 4;
069: a.value = 22;
070: session.save("a", a);
071:
072: CompassHits hits = session.find("value:[1 TO 3]");
073: assertEquals(2, hits.length());
074:
075: tr.commit();
076: session.close();
077: }
078:
079: public void testDateRangeUsingNow() {
080: CompassSession session = openSession();
081: CompassTransaction tr = session.beginTransaction();
082:
083: A a = new A();
084: a.id = 1;
085: a.date = new Date();
086: session.save("a", a);
087:
088: CompassHits hits = session.find("date:[now-1hour now+1hour]");
089: assertEquals(1, hits.length());
090:
091: hits = session.find("date:[now-2hour now-1hour]");
092: assertEquals(0, hits.length());
093:
094: tr.commit();
095: session.close();
096: }
097:
098: public void testDateRangeUsingTwoDifferentFormats() {
099: CompassSession session = openSession();
100: CompassTransaction tr = session.beginTransaction();
101:
102: A a = new A();
103: a.id = 1;
104: a.date2 = new Date();
105: session.save("a", a);
106:
107: // verify that the default format works
108: CompassHits hits = session
109: .find("date2:[2000-01-01 TO 2010-01-01]");
110: assertEquals(1, hits.length());
111:
112: hits = session.queryBuilder().between("a.date2", "2000-01-01",
113: "2010-01-01", true).hits();
114: assertEquals(1, hits.length());
115:
116: // verify that the other format works as well
117: hits = session.find("date2:[01-01-2000 TO 01-01-2010]");
118: assertEquals(1, hits.length());
119:
120: hits = session.queryBuilder().between("a.date2", "01-01-2000 ",
121: "01-01-2010", true).hits();
122: assertEquals(1, hits.length());
123:
124: tr.commit();
125: session.close();
126: }
127: }
|