KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > test > TestExpressions


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: TestExpressions.java,v 1.10 2005/02/21 11:52:33 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query.test;
8
9 import junit.framework.*;
10
11 import com.hp.hpl.jena.graph.query.*;
12
13 /**
14     @author hedgehog
15 */

16 public class TestExpressions extends QueryTestBase
17     {
18     public TestExpressions( String JavaDoc name )
19         { super( name ); }
20     
21     public static TestSuite suite()
22         { return new TestSuite( TestExpressions.class ); }
23    
24     public void testExpressionPatternLanguages()
25         {
26         assertEquals( "http://jena.hpl.hp.com/2003/07/query/RDQL", PatternLiteral.rdql );
27         }
28     
29     public void testBooleanEquality()
30         {
31         assertEquals( Expression.TRUE, Expression.TRUE );
32         assertEquals( Expression.FALSE, Expression.FALSE );
33         assertDiffer( Expression.TRUE, Expression.FALSE );
34         assertDiffer( Expression.FALSE, Expression.TRUE );
35         }
36     
37     public void testDyadicEquality()
38         {
39         Expression A = lit( "Aaa" ), B = lit( "Bee" );
40         assertDiffer( A, B );
41         assertEquals( contains( A, "groo" ), contains( A, "groo" ) );
42         assertEquals( contains( B, "oops" ), contains( B, "oops" ) );
43         assertDiffer( contains( A, "groo" ), contains( A, "glue" ) );
44         assertDiffer( contains( A, "groo" ), contains( B, "groo" ) );
45         assertDiffer( contains( A, "blue" ), startsWith( A, "blue" ) );
46         assertDiffer( contains( A, "blue" ), endsWith( A, "blue" ) );
47         assertDiffer( endsWith( A, "blue" ), startsWith( A, "blue" ) );
48         }
49
50     public Expression contains( Expression L, PatternLiteral R )
51         { return Rewrite.contains( L, R.getPatternString(), R.getPatternModifiers() ); }
52     
53     public Expression contains( Expression L, String JavaDoc R )
54         { return Rewrite.contains( L, R, "" ); }
55     
56     public Expression endsWith( Expression L, String JavaDoc R )
57         { return Rewrite.endsWith( L, R, "" ); }
58     
59     public Expression endsWith( Expression L, PatternLiteral R )
60         { return Rewrite.endsWith( L, R.getPatternString(), R.getPatternModifiers() ); }
61     
62     public Expression startsWith( Expression L, String JavaDoc R )
63         { return Rewrite.startsWith( L, R, "" ); }
64     
65     public Expression startsWith( Expression L, PatternLiteral R )
66         { return Rewrite.startsWith( L, R.getPatternString(), R.getPatternModifiers() ); }
67     
68     public void testLiterals()
69         {
70         assertTrue( Expression.TRUE.isConstant() );
71         assertTrue( Expression.FALSE.isConstant() );
72         assertEquals( Boolean.TRUE, Expression.TRUE.getValue() );
73         assertEquals( Boolean.FALSE, Expression.FALSE.getValue() );
74         }
75     
76     public void testPrepareTRUE()
77         {
78         Valuator t = Expression.TRUE.prepare( emptyMapping );
79         assertEquals( true, t.evalBool( noIVs ) );
80         assertEquals( Boolean.TRUE, t.evalObject( noIVs ) );
81         }
82         
83     public void testPrepareFALSE()
84         {
85         Valuator t = Expression.FALSE.prepare( emptyMapping );
86         assertEquals( false, t.evalBool( noIVs ) );
87         assertEquals( Boolean.FALSE, t.evalObject( noIVs ) );
88         }
89     
90     public void testFixed()
91         {
92         testFixed( "hello" );
93         testFixed( "goodbye" );
94         testFixed( Boolean.TRUE );
95         testFixed( new int[] {17, 27, 42} );
96         }
97
98     protected void testFixed( Object JavaDoc value )
99         {
100         Expression e = lit( value );
101         assertEquals( value, e.getValue() );
102         assertEquals( value, evalObject( e ) );
103         }
104     
105     public void testDyadic()
106         {
107         Expression L = lit( "a" );
108         Expression R = lit( "b" );
109         Expression e = new Dyadic( L, "eh:op", R )
110             {
111             public Object JavaDoc evalObject( Object JavaDoc x, Object JavaDoc y )
112                 { return "" + x + "--" + y; }
113             };
114         assertEquals( 2, e.argCount() );
115         assertSame( L, e.getArg( 0 ) );
116         assertSame( R, e.getArg( 1 ) );
117         assertEquals( "eh:op", e.getFun() );
118         assertEquals( "a--b", evalObject(e) );
119         }
120
121     public void testStartsWith()
122         {
123         assertEquals( true, evalBool( startsWith( lit( "hello" ), "h" ) ) );
124         assertEquals( true, evalBool( startsWith( lit( "hello" ), "he" ) ) );
125         assertEquals( true, evalBool( startsWith( lit( "hello" ), "hel" ) ) );
126         assertEquals( false, evalBool( startsWith( lit( "HELLO" ), "hel" ) ) );
127         assertEquals( false, evalBool( startsWith( lit( "hello" ), "HEL" ) ) );
128         assertEquals( false, evalBool( startsWith( lit( "hello" ), "e" ) ) );
129         assertEquals( false, evalBool( startsWith( lit( "hello" ), "llo" ) ) );
130         assertEquals( false, evalBool( startsWith( lit( "hello" ), "xhe" ) ) );
131         }
132
133     public void testStartsInsensitiveWith()
134         {
135         assertEquals( true, evalBool( startsWith( lit( "hello" ), pli( "H" ) ) ) );
136         assertEquals( true, evalBool( startsWith( lit( "hEllo" ), pli( "he" ) ) ) );
137         assertEquals( true, evalBool( startsWith( lit( "heLlo" ), pli( "hEl" ) ) ) );
138         assertEquals( true, evalBool( startsWith( lit( "HELLO" ), pli( "hel" ) ) ) );
139         assertEquals( true, evalBool( startsWith( lit( "hello" ), pli( "HEL" ) ) ) );
140         assertEquals( false, evalBool( startsWith( lit( "hello" ), pli( "e" ) ) ) );
141         assertEquals( false, evalBool( startsWith( lit( "hello" ), pli( "llo" ) ) ) );
142         assertEquals( false, evalBool( startsWith( lit( "hello" ), pli( "xhe" ) ) ) );
143         }
144
145     public void testIsContains()
146         {
147         assertEquals( true, Rewrite.isContains( pl( "ambulance" ) ) );
148         assertEquals( true, Rewrite.isContains( pl( "tendonitis", "i" ) ) );
149         assertEquals( false, Rewrite.isContains( pl( "finishing", "z" ) ) );
150         }
151     
152     public void testSensitiveContains()
153         {
154         assertEquals( true, evalBool( contains( lit( "hello" ), "h" ) ) );
155         assertEquals( true, evalBool( contains( lit( "hello" ), "e" ) ) );
156         assertEquals( true, evalBool( contains( lit( "hello" ), "ll" ) ) );
157         assertEquals( false, evalBool( contains( lit( "heLLo" ), "ll" ) ) );
158         assertEquals( false, evalBool( contains( lit( "hello" ), "LL" ) ) );
159         assertEquals( false, evalBool( contains( lit( "hello" ), "x" ) ) );
160         assertEquals( false, evalBool( contains( lit( "hello" ), "the" ) ) );
161         assertEquals( false, evalBool( contains( lit( "hello" ), "lot" ) ) );
162         }
163
164     public void testInsensitiveContains()
165         {
166         assertEquals( true, evalBool( contains( lit( "Hello" ), pli( "h" ) ) ) );
167         assertEquals( true, evalBool( contains( lit( "hello" ), pli( "E" ) ) ) );
168         assertEquals( true, evalBool( contains( lit( "heLlo" ), pli( "lL" ) ) ) );
169         assertEquals( false, evalBool( contains( lit( "hello" ), pli( "X" ) ) ) );
170         assertEquals( false, evalBool( contains( lit( "hello" ), pli( "the" ) ) ) );
171         assertEquals( false, evalBool( contains( lit( "hello" ), pli( "lot" ) ) ) );
172         }
173
174
175     public void testEndsWith()
176         {
177         assertEquals( true, evalBool( endsWith( lit( "hello" ), "o" ) ) );
178         assertEquals( true, evalBool( endsWith( lit( "hello" ), "lo" ) ) );
179         assertEquals( true, evalBool( endsWith( lit( "hello" ), "hello" ) ) );
180         assertEquals( false, evalBool( endsWith( lit( "HELLO" ), "hello" ) ) );
181         assertEquals( false, evalBool( endsWith( lit( "hello" ), "HELLO" ) ) );
182         assertEquals( false, evalBool( endsWith( lit( "hello" ), "ll" ) ) );
183         assertEquals( false, evalBool( endsWith( lit( "hello" ), "hel" ) ) );
184         assertEquals( false, evalBool( endsWith( lit( "hello" ), "quantum" ) ) );
185         }
186
187     public void testInsensitiveEndsWith()
188         {
189         assertEquals( true, evalBool( endsWith( lit( "hellO" ), pli( "o" ) ) ) );
190         assertEquals( true, evalBool( endsWith( lit( "hello" ), pli( "lO" ) ) ) );
191         assertEquals( true, evalBool( endsWith( lit( "HeLLo" ), pli( "HELlo" ) ) ) );
192         assertEquals( false, evalBool( endsWith( lit( "hello" ), pli( "ll" ) ) ) );
193         assertEquals( false, evalBool( endsWith( lit( "hello" ), pli( "hel" ) ) ) );
194         assertEquals( false, evalBool( endsWith( lit( "hello" ), pli( "quantum" ) ) ) );
195         }
196
197     private Object JavaDoc evalObject(Expression e)
198         { return e.prepare( emptyMapping ).evalObject( noIVs ); }
199     
200     private boolean evalBool(Expression e)
201         { return e.prepare( emptyMapping ).evalBool( noIVs ); }
202
203     protected Expression lit( Object JavaDoc x )
204         { return new Expression.Fixed( x ); }
205     
206     protected PatternLiteral pl( final String JavaDoc c )
207         { return pl( c, "" ); }
208         
209     protected PatternLiteral pli( String JavaDoc c )
210         { return pl( c, "i" ); }
211     
212     protected PatternLiteral pl( final String JavaDoc c, final String JavaDoc m )
213         {
214         return new PatternLiteral()
215             {
216             public String JavaDoc getPatternString()
217                 { return c; }
218
219             public String JavaDoc getPatternModifiers()
220                 { return m; }
221
222             public String JavaDoc getPatternLanguage()
223                 { return rdql; }
224             };
225         }
226     }
227
228 /*
229     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
230     All rights reserved.
231     
232     Redistribution and use in source and binary forms, with or without
233     modification, are permitted provided that the following conditions
234     are met:
235     
236     1. Redistributions of source code must retain the above copyright
237        notice, this list of conditions and the following disclaimer.
238     
239     2. Redistributions in binary form must reproduce the above copyright
240        notice, this list of conditions and the following disclaimer in the
241        documentation and/or other materials provided with the distribution.
242     
243     3. The name of the author may not be used to endorse or promote products
244        derived from this software without specific prior written permission.
245     
246     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
250     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256 */
Popular Tags