KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > lang > T_Like


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.lang.T_Like
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.lang;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.iapi.types.*;
28
29 import org.apache.derby.iapi.services.monitor.Monitor;
30 import org.apache.derby.iapi.reference.Property;
31
32 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
33
34 import org.apache.derby.iapi.error.StandardException;
35
36 import java.util.Properties JavaDoc;
37
38 /**
39     @see Like
40
41     @author ames
42  */

43 public class T_Like extends T_Generic
44 {
45     private static final String JavaDoc testService = "likeTest";
46     boolean didFAIL;
47
48     /*
49     ** Methods required by T_Generic
50     */

51
52     public String JavaDoc getModuleToTestProtocolName() {
53         // actually, we're just testing LIKE; but it is reached through
54
// the ExecutionFactory MODULE, and this wants a MODULE, so...
55
return ExecutionFactory.MODULE;
56     }
57
58     /**
59         @exception T_Fail test failed.
60     */

61     protected void runTests() throws T_Fail
62     {
63         ExecutionFactory f = null;
64         boolean pass = false;
65         didFAIL = false;
66
67         out.println(testService+" underway");
68
69         // don't automatic boot this service if it gets left around
70
if (startParams == null) {
71             startParams = new Properties JavaDoc();
72         }
73         startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
74
75         REPORT("(unitTestMain) Testing " + testService);
76
77         // REMIND: add tests here
78
try {
79             tests();
80         } catch (StandardException e) {
81             FAIL("exception:"+e);
82         }
83         if (didFAIL) throw T_Fail.testFailMsg("see log for details");
84
85         out.println(testService+" complete");
86     }
87
88     // testing support:
89
private void expect(String JavaDoc desc, Boolean JavaDoc test, Boolean JavaDoc result) {
90         boolean pass =
91             ( (test == null && result == null) ||
92               (test != null && result != null && test.equals(result)) );
93
94         if (pass)
95             PASS("TEST ["+desc+"] == result["+result+"] ");
96         else
97             FAIL("TEST ["+desc+"] != result["+result+"] ");
98     }
99
100     // testing mechanism:
101
private void tests() throws StandardException {
102         boolean gotLE = false;
103         Boolean JavaDoc t = null;
104         char[] caNull = null;
105         char[] caHello = "hello".toCharArray();
106         String JavaDoc msg=null;
107         String JavaDoc desc=null;
108
109         REPORT("testing null combinations...");
110         try {
111         expect("null like null escape null", Like.like(caNull, 0, caNull, 0, caNull, 0), null);
112         expect("null like 'hello' escape null", Like.like(caNull, 0, caHello, caHello.length, caNull, 0), null);
113         expect("'hello' like null escape null", Like.like(caHello, caHello.length, caNull, 0, caNull, 0), null);
114         expect("null like null escape '\\'", Like.like(caNull, 0, caNull, 0, "\\".toCharArray(), "\\".toCharArray().length), null);
115
116         // gets back a null before it evaluates the escape
117
expect("null like null escape 'hello'", Like.like(caNull, 0, caNull, 0, caHello, caHello.length), null);
118         // gets back a null before it evaluates the pattern
119
expect("null like 'hello\\' escape '\\'", Like.like(caNull, 0, "hello\\".toCharArray(), "hello\\".toCharArray().length, "\\".toCharArray(), "\\".toCharArray().length), null);
120
121         } catch(StandardException leOuter1) {
122             leOuter1.printStackTrace();
123             FAIL("unexpected exception");
124         }
125
126         REPORT("testing valid match cases...");
127         try {
128         expect("'hello' like 'hello' escape null", Like.like(caHello, caHello.length, caHello, caHello.length, caNull, 0), Boolean.TRUE);
129         expect("'hello' like 'h_llo' escape null", Like.like(caHello, caHello.length, "h_llo".toCharArray(), "h_llo".toCharArray().length, caNull, 0), Boolean.TRUE);
130         expect("'hello' like '_ello' escape null", Like.like(caHello, caHello.length, "_ello".toCharArray(), "_ello".toCharArray().length, caNull, 0), Boolean.TRUE);
131         expect("'hello' like 'hell_' escape null", Like.like(caHello, caHello.length, "hell_".toCharArray(), "hell_".toCharArray().length, caNull, 0), Boolean.TRUE);
132         expect("'hello' like '_____' escape null", Like.like(caHello, caHello.length, "_____".toCharArray(), "_____".toCharArray().length, caNull, 0), Boolean.TRUE);
133         expect("'hello' like 'h___e' escape null", Like.like(caHello, caHello.length, "h___o".toCharArray(), "h___o".toCharArray().length, caNull, 0), Boolean.TRUE);
134         expect("'h' like 'h' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "h".toCharArray(), "h".toCharArray().length, caNull, 0), Boolean.TRUE);
135         expect("'h' like '_' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "_".toCharArray(), "_".toCharArray().length, caNull, 0), Boolean.TRUE);
136         expect("'h' like '%' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "%".toCharArray(), "%".toCharArray().length, caNull, 0), Boolean.TRUE);
137         expect("'h' like '_%' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "_%".toCharArray(), "_%".toCharArray().length, caNull, 0), Boolean.TRUE);
138         expect("'h' like '%_' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "%_".toCharArray(), "%_".toCharArray().length, caNull, 0), Boolean.TRUE);
139         expect("'h' like '%' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "%".toCharArray(), "%".toCharArray().length, caNull, 0), Boolean.TRUE);
140         expect("'' like '%' escape null", Like.like("".toCharArray(), "".toCharArray().length, "%".toCharArray(), "%".toCharArray().length, caNull, 0), Boolean.TRUE);
141         expect("'' like '%%' escape null", Like.like("".toCharArray(), "".toCharArray().length, "%%".toCharArray(), "%%".toCharArray().length, caNull, 0), Boolean.TRUE);
142         expect("'' like '%%%' escape null", Like.like("".toCharArray(), "".toCharArray().length, "%%%".toCharArray(), "%%%".toCharArray().length, caNull, 0), Boolean.TRUE);
143         } catch(StandardException leOuter2) {
144             leOuter2.printStackTrace();
145             FAIL("unexpected exception");
146         }
147
148         REPORT("testing valid nonmatch cases...");
149         try {
150         expect("'hello' like 'hello ' escape null", Like.like(caHello, caHello.length, "hello ".toCharArray(), "hello ".toCharArray().length, caNull, 0), Boolean.FALSE);
151         expect("'hello ' like 'hello' escape null", Like.like("hello ".toCharArray(), "hello ".toCharArray().length, caHello, caHello.length, caNull, 0), Boolean.FALSE);
152         expect("'hello' like 'hellox' escape null", Like.like(caHello, caHello.length, "hellox".toCharArray(), "hellox".toCharArray().length, caNull, 0), Boolean.FALSE);
153         expect("'hellox' like 'hello' escape null", Like.like("hellox".toCharArray(), "hellox".toCharArray().length, caHello, caHello.length, caNull, 0), Boolean.FALSE);
154         expect("'xhellox' like 'hello' escape null", Like.like("xhellox".toCharArray(), "xhellox".toCharArray().length, caHello, caHello.length, caNull, 0), Boolean.FALSE);
155         expect("'hello' like 'xhellox' escape null", Like.like(caHello, caHello.length, "xhellox".toCharArray(), "xhellox".toCharArray().length, null, 0), Boolean.FALSE);
156         expect("'hello' like 'h___' escape null", Like.like(caHello, caHello.length, "h___".toCharArray(), "h___".toCharArray().length, caNull, 0), Boolean.FALSE);
157         expect("'h' like '_%_' escape null", Like.like("h".toCharArray(), "h".toCharArray().length, "_%_".toCharArray(), "_%_".toCharArray().length, caNull, 0), Boolean.FALSE);
158         expect("'' like '_' escape null", Like.like("".toCharArray(), "".toCharArray().length, "_".toCharArray(), "_".toCharArray().length, caNull, 0), Boolean.FALSE);
159         } catch(StandardException leOuter3) {
160             leOuter3.printStackTrace();
161             FAIL("unexpected exception");
162         }
163
164         REPORT("testing error cases...");
165
166         try {
167             msg = null;
168             gotLE=false;
169             desc="null like null escape 'hello'";
170             t=Like.like(caHello, caHello.length, caHello, caHello.length, caHello, caHello.length);
171         } catch (StandardException le) {
172             gotLE=true;
173             msg = le.getMessage();
174         } finally {
175             if (gotLE)
176                 PASS("TEST ["+desc+"] got exception "+msg);
177             else
178                 FAIL("TEST ["+desc+"] didn't get exception");
179         }
180
181         try {
182             msg = null;
183             gotLE=false;
184             desc="'hello' like 'hhh' escape 'h'";
185             t=Like.like(caHello, caHello.length, "hhh".toCharArray(), "hhh".toCharArray().length, "h".toCharArray(), "h".toCharArray().length);
186         } catch (StandardException le) {
187             gotLE=true;
188             msg = le.getMessage();
189         } finally {
190             if (gotLE)
191                 PASS("TEST ["+desc+"] got exception "+msg);
192             else
193                 FAIL("TEST ["+desc+"] didn't get exception");
194         }
195
196         try {
197             msg = null;
198             gotLE=false;
199             desc="'hello' like 'he%' escape 'h'";
200             t=Like.like(caHello, caHello.length, "he%".toCharArray(), "he%".toCharArray().length, "h".toCharArray(), "h".toCharArray().length);
201         } catch (StandardException le) {
202             gotLE=true;
203             msg = le.getMessage();
204         } finally {
205             if (gotLE)
206                 PASS("TEST ["+desc+"] got exception "+msg);
207             else
208                 FAIL("TEST ["+desc+"] didn't get exception");
209         }
210
211     }
212
213     /*
214         override to mark the test as failed when dumping the message.
215      */

216     protected boolean FAIL(String JavaDoc msg) {
217         super.FAIL(msg);
218         return didFAIL = true;
219     }
220 }
221
222
Popular Tags