01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.test.querybuilder.operator;
18:
19: import org.compass.core.CompassSession;
20: import org.compass.core.CompassTransaction;
21: import org.compass.core.test.AbstractTestCase;
22:
23: /**
24: * @author kimchy
25: */
26: public class QueryOperatorTests extends AbstractTestCase {
27:
28: protected String[] getMappings() {
29: return new String[] { "querybuilder/operator/mapping.cpm.xml" };
30: }
31:
32: public void testDefaultOperator() {
33: setUpData();
34:
35: CompassSession session = openSession();
36: CompassTransaction tr = session.beginTransaction();
37:
38: assertEquals(1, session.queryBuilder().queryString("one two")
39: .toQuery().hits().length());
40: assertEquals(0, session.queryBuilder().queryString("one moo")
41: .toQuery().hits().length());
42:
43: tr.commit();
44: session.close();
45: }
46:
47: public void testOrOperator() {
48: setUpData();
49:
50: CompassSession session = openSession();
51: CompassTransaction tr = session.beginTransaction();
52:
53: assertEquals(1, session.queryBuilder().queryString("one two")
54: .useOrDefaultOperator().toQuery().hits().length());
55: assertEquals(2, session.queryBuilder().queryString("one moo")
56: .useOrDefaultOperator().toQuery().hits().length());
57:
58: tr.commit();
59: session.close();
60: }
61:
62: public void testAndOperator() {
63: setUpData();
64:
65: CompassSession session = openSession();
66: CompassTransaction tr = session.beginTransaction();
67:
68: assertEquals(1, session.queryBuilder().queryString("one two")
69: .useAndDefaultOperator().toQuery().hits().length());
70: assertEquals(0, session.queryBuilder().queryString("one moo")
71: .useAndDefaultOperator().toQuery().hits().length());
72:
73: tr.commit();
74: session.close();
75: }
76:
77: private void setUpData() {
78: CompassSession session = openSession();
79: CompassTransaction tr = session.beginTransaction();
80:
81: A a = new A();
82: a.id = 1;
83: a.value1 = "one";
84: a.value2 = "two";
85: session.save("a", a);
86:
87: a.id = 2;
88: a.value1 = "moo";
89: a.value2 = "poo";
90: session.save("a", a);
91:
92: tr.commit();
93: session.close();
94: }
95: }
|