001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: TestExpressions.java,v 1.15 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query.test;
008:
009: import junit.framework.*;
010:
011: import com.hp.hpl.jena.graph.query.*;
012: import com.hp.hpl.jena.graph.test.NodeCreateUtils;
013:
014: /**
015: @author hedgehog
016: */
017: public class TestExpressions extends QueryTestBase {
018: public TestExpressions(String name) {
019: super (name);
020: }
021:
022: public static TestSuite suite() {
023: return new TestSuite(TestExpressions.class);
024: }
025:
026: public void testExpressionPatternLanguages() {
027: assertEquals("http://jena.hpl.hp.com/2003/07/query/RDQL",
028: PatternLiteral.rdql);
029: }
030:
031: public void testBooleanEquality() {
032: assertEquals(Expression.TRUE, Expression.TRUE);
033: assertEquals(Expression.FALSE, Expression.FALSE);
034: assertDiffer(Expression.TRUE, Expression.FALSE);
035: assertDiffer(Expression.FALSE, Expression.TRUE);
036: }
037:
038: public void testDyadicEquality() {
039: Expression A = lit("Aaa"), B = lit("Bee");
040: assertDiffer(A, B);
041: assertEquals(contains(A, "groo"), contains(A, "groo"));
042: assertEquals(contains(B, "oops"), contains(B, "oops"));
043: assertDiffer(contains(A, "groo"), contains(A, "glue"));
044: assertDiffer(contains(A, "groo"), contains(B, "groo"));
045: assertDiffer(contains(A, "blue"), startsWith(A, "blue"));
046: assertDiffer(contains(A, "blue"), endsWith(A, "blue"));
047: assertDiffer(endsWith(A, "blue"), startsWith(A, "blue"));
048: }
049:
050: public Expression contains(Expression L, PatternLiteral R) {
051: return Rewrite.contains(L, R.getPatternString(), R
052: .getPatternModifiers());
053: }
054:
055: public Expression contains(Expression L, String R) {
056: return Rewrite.contains(L, R, "");
057: }
058:
059: public Expression endsWith(Expression L, String R) {
060: return Rewrite.endsWith(L, R, "");
061: }
062:
063: public Expression endsWith(Expression L, PatternLiteral R) {
064: return Rewrite.endsWith(L, R.getPatternString(), R
065: .getPatternModifiers());
066: }
067:
068: public Expression startsWith(Expression L, String R) {
069: return Rewrite.startsWith(L, R, "");
070: }
071:
072: public Expression startsWith(Expression L, PatternLiteral R) {
073: return Rewrite.startsWith(L, R.getPatternString(), R
074: .getPatternModifiers());
075: }
076:
077: public void testLiterals() {
078: assertTrue(Expression.TRUE.isConstant());
079: assertTrue(Expression.FALSE.isConstant());
080: assertEquals(Boolean.TRUE, Expression.TRUE.getValue());
081: assertEquals(Boolean.FALSE, Expression.FALSE.getValue());
082: }
083:
084: public void testPrepareTRUE() {
085: Valuator t = Expression.TRUE.prepare(emptyMapping);
086: assertEquals(true, t.evalBool(noIVs));
087: assertEquals(Boolean.TRUE, t.evalObject(noIVs));
088: }
089:
090: public void testPrepareFALSE() {
091: Valuator t = Expression.FALSE.prepare(emptyMapping);
092: assertEquals(false, t.evalBool(noIVs));
093: assertEquals(Boolean.FALSE, t.evalObject(noIVs));
094: }
095:
096: public void testFixed() {
097: testFixed("hello");
098: testFixed("goodbye");
099: testFixed(Boolean.TRUE);
100: testFixed(new int[] { 17, 27, 42 });
101: }
102:
103: protected void testFixed(Object value) {
104: Expression e = lit(value);
105: assertEquals(value, e.getValue());
106: assertEquals(value, evalObject(e));
107: }
108:
109: public void testDyadic() {
110: Expression L = lit("a");
111: Expression R = lit("b");
112: Expression e = new Dyadic(L, "eh:op", R) {
113: public Object evalObject(Object x, Object y) {
114: return "" + x + "--" + y;
115: }
116: };
117: assertEquals(2, e.argCount());
118: assertSame(L, e.getArg(0));
119: assertSame(R, e.getArg(1));
120: assertEquals("eh:op", e.getFun());
121: assertEquals("a--b", evalObject(e));
122: }
123:
124: public void testStartsWith() {
125: assertEquals(true, evalBool(startsWith(lit("hello"), "h")));
126: assertEquals(true, evalBool(startsWith(lit("hello"), "he")));
127: assertEquals(true, evalBool(startsWith(lit("hello"), "hel")));
128: assertEquals(false, evalBool(startsWith(lit("HELLO"), "hel")));
129: assertEquals(false, evalBool(startsWith(lit("hello"), "HEL")));
130: assertEquals(false, evalBool(startsWith(lit("hello"), "e")));
131: assertEquals(false, evalBool(startsWith(lit("hello"), "llo")));
132: assertEquals(false, evalBool(startsWith(lit("hello"), "xhe")));
133: }
134:
135: public void testStartsInsensitiveWith() {
136: assertEquals(true, evalBool(startsWith(lit("hello"), pli("H"))));
137: assertEquals(true,
138: evalBool(startsWith(lit("hEllo"), pli("he"))));
139: assertEquals(true,
140: evalBool(startsWith(lit("heLlo"), pli("hEl"))));
141: assertEquals(true,
142: evalBool(startsWith(lit("HELLO"), pli("hel"))));
143: assertEquals(true,
144: evalBool(startsWith(lit("hello"), pli("HEL"))));
145: assertEquals(false,
146: evalBool(startsWith(lit("hello"), pli("e"))));
147: assertEquals(false, evalBool(startsWith(lit("hello"),
148: pli("llo"))));
149: assertEquals(false, evalBool(startsWith(lit("hello"),
150: pli("xhe"))));
151: }
152:
153: public void testIsContains() {
154: assertEquals(true, Rewrite.isContains(pl("ambulance")));
155: assertEquals(true, Rewrite.isContains(pl("tendonitis", "i")));
156: assertEquals(false, Rewrite.isContains(pl("finishing", "z")));
157: }
158:
159: public void testSensitiveContains() {
160: assertEquals(true, evalBool(contains(lit("hello"), "h")));
161: assertEquals(true, evalBool(contains(lit("hello"), "e")));
162: assertEquals(true, evalBool(contains(lit("hello"), "ll")));
163: assertEquals(false, evalBool(contains(lit("heLLo"), "ll")));
164: assertEquals(false, evalBool(contains(lit("hello"), "LL")));
165: assertEquals(false, evalBool(contains(lit("hello"), "x")));
166: assertEquals(false, evalBool(contains(lit("hello"), "the")));
167: assertEquals(false, evalBool(contains(lit("hello"), "lot")));
168: }
169:
170: public void testInsensitiveContains() {
171: assertEquals(true, evalBool(contains(lit("Hello"), pli("h"))));
172: assertEquals(true, evalBool(contains(lit("hello"), pli("E"))));
173: assertEquals(true, evalBool(contains(lit("heLlo"), pli("lL"))));
174: assertEquals(false, evalBool(contains(lit("hello"), pli("X"))));
175: assertEquals(false,
176: evalBool(contains(lit("hello"), pli("the"))));
177: assertEquals(false,
178: evalBool(contains(lit("hello"), pli("lot"))));
179: }
180:
181: public void testLangedLiteralsEndsWith() {
182: assertEquals(true,
183: evalBool(endsWith(litString("'spoo'en"), "o")));
184: assertEquals(true,
185: evalBool(endsWith(litString("'spoo'go"), "o")));
186: assertEquals(false, evalBool(endsWith(litString("'spot'go"),
187: "o")));
188: assertEquals(false, evalBool(endsWith(litString("'spot'en"),
189: "o")));
190: }
191:
192: public void testTypedLiteralsEndsWith() {
193: assertEquals(true, evalBool(endsWith(
194: litString("'spoo'xsd:mint"), "o")));
195: assertEquals(true, evalBool(endsWith(
196: litString("'spoo'xsd:gloo"), "o")));
197: assertEquals(false, evalBool(endsWith(
198: litString("'spot'xsd:slat"), "o")));
199: assertEquals(false, evalBool(endsWith(
200: litString("'spot'xsd:do"), "o")));
201: }
202:
203: public void testLangedLiteralsContains() {
204: assertEquals(true, evalBool(contains(litString("'spoo'en"),
205: "po")));
206: assertEquals(true, evalBool(contains(litString("'spoo'go"),
207: "sp")));
208: assertEquals(false, evalBool(contains(litString("'spot'go"),
209: "go")));
210: assertEquals(false, evalBool(contains(litString("'spot'en"),
211: "en")));
212: }
213:
214: public void testTypedLiteralsContains() {
215: assertEquals(true, evalBool(contains(
216: litString("'spoo'xsd:mint"), "sp")));
217: assertEquals(true, evalBool(contains(
218: litString("'spoo'xsd:gloo"), "po")));
219: assertEquals(false, evalBool(contains(
220: litString("'spot'xsd:slat"), "sl")));
221: assertEquals(false, evalBool(contains(
222: litString("'spot'xsd:do"), "do")));
223: }
224:
225: public void testEndsWith() {
226: assertEquals(true, evalBool(endsWith(lit("hello"), "o")));
227: assertEquals(true, evalBool(endsWith(lit("hello"), "lo")));
228: assertEquals(true, evalBool(endsWith(lit("hello"), "hello")));
229: assertEquals(false, evalBool(endsWith(lit("HELLO"), "hello")));
230: assertEquals(false, evalBool(endsWith(lit("hello"), "HELLO")));
231: assertEquals(false, evalBool(endsWith(lit("hello"), "ll")));
232: assertEquals(false, evalBool(endsWith(lit("hello"), "hel")));
233: assertEquals(false, evalBool(endsWith(lit("hello"), "quantum")));
234: }
235:
236: public void testInsensitiveEndsWith() {
237: assertEquals(true, evalBool(endsWith(lit("hellO"), pli("o"))));
238: assertEquals(true, evalBool(endsWith(lit("hello"), pli("lO"))));
239: assertEquals(true,
240: evalBool(endsWith(lit("HeLLo"), pli("HELlo"))));
241: assertEquals(false, evalBool(endsWith(lit("hello"), pli("ll"))));
242: assertEquals(false,
243: evalBool(endsWith(lit("hello"), pli("hel"))));
244: assertEquals(false, evalBool(endsWith(lit("hello"),
245: pli("quantum"))));
246: }
247:
248: private Object evalObject(Expression e) {
249: return e.prepare(emptyMapping).evalObject(noIVs);
250: }
251:
252: private boolean evalBool(Expression e) {
253: return e.prepare(emptyMapping).evalBool(noIVs);
254: }
255:
256: protected Expression litString(String s) {
257: return lit(NodeCreateUtils.create(s));
258: }
259:
260: protected Expression lit(Object x) {
261: return new Expression.Fixed(x);
262: }
263:
264: protected PatternLiteral pl(final String c) {
265: return pl(c, "");
266: }
267:
268: protected PatternLiteral pli(String c) {
269: return pl(c, "i");
270: }
271:
272: protected PatternLiteral pl(final String c, final String m) {
273: return new PatternLiteral() {
274: public String getPatternString() {
275: return c;
276: }
277:
278: public String getPatternModifiers() {
279: return m;
280: }
281:
282: public String getPatternLanguage() {
283: return rdql;
284: }
285: };
286: }
287: }
288:
289: /*
290: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
291: All rights reserved.
292:
293: Redistribution and use in source and binary forms, with or without
294: modification, are permitted provided that the following conditions
295: are met:
296:
297: 1. Redistributions of source code must retain the above copyright
298: notice, this list of conditions and the following disclaimer.
299:
300: 2. Redistributions in binary form must reproduce the above copyright
301: notice, this list of conditions and the following disclaimer in the
302: documentation and/or other materials provided with the distribution.
303:
304: 3. The name of the author may not be used to endorse or promote products
305: derived from this software without specific prior written permission.
306:
307: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
308: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
309: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
310: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
311: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
312: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
313: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
314: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
315: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
316: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
317: */
|