001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.lang.T_Like
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.lang;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Generic;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import org.apache.derby.iapi.types.*;
028:
029: import org.apache.derby.iapi.services.monitor.Monitor;
030: import org.apache.derby.iapi.reference.Property;
031:
032: import org.apache.derby.iapi.sql.execute.ExecutionFactory;
033:
034: import org.apache.derby.iapi.error.StandardException;
035:
036: import java.util.Properties;
037:
038: /**
039: @see Like
040:
041: @author ames
042: */
043: public class T_Like extends T_Generic {
044: private static final String testService = "likeTest";
045: boolean didFAIL;
046:
047: /*
048: ** Methods required by T_Generic
049: */
050:
051: public String getModuleToTestProtocolName() {
052: // actually, we're just testing LIKE; but it is reached through
053: // the ExecutionFactory MODULE, and this wants a MODULE, so...
054: return ExecutionFactory.MODULE;
055: }
056:
057: /**
058: @exception T_Fail test failed.
059: */
060: protected void runTests() throws T_Fail {
061: ExecutionFactory f = null;
062: boolean pass = false;
063: didFAIL = false;
064:
065: out.println(testService + " underway");
066:
067: // don't automatic boot this service if it gets left around
068: if (startParams == null) {
069: startParams = new Properties();
070: }
071: startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
072:
073: REPORT("(unitTestMain) Testing " + testService);
074:
075: // REMIND: add tests here
076: try {
077: tests();
078: } catch (StandardException e) {
079: FAIL("exception:" + e);
080: }
081: if (didFAIL)
082: throw T_Fail.testFailMsg("see log for details");
083:
084: out.println(testService + " complete");
085: }
086:
087: // testing support:
088: private void expect(String desc, Boolean test, Boolean result) {
089: boolean pass = ((test == null && result == null) || (test != null
090: && result != null && test.equals(result)));
091:
092: if (pass)
093: PASS("TEST [" + desc + "] == result[" + result + "] ");
094: else
095: FAIL("TEST [" + desc + "] != result[" + result + "] ");
096: }
097:
098: // testing mechanism:
099: private void tests() throws StandardException {
100: boolean gotLE = false;
101: Boolean t = null;
102: char[] caNull = null;
103: char[] caHello = "hello".toCharArray();
104: String msg = null;
105: String desc = null;
106:
107: REPORT("testing null combinations...");
108: try {
109: expect("null like null escape null", Like.like(caNull, 0,
110: caNull, 0, caNull, 0), null);
111: expect("null like 'hello' escape null", Like.like(caNull,
112: 0, caHello, caHello.length, caNull, 0), null);
113: expect("'hello' like null escape null", Like.like(caHello,
114: caHello.length, caNull, 0, caNull, 0), null);
115: expect("null like null escape '\\'", Like.like(caNull, 0,
116: caNull, 0, "\\".toCharArray(),
117: "\\".toCharArray().length), null);
118:
119: // gets back a null before it evaluates the escape
120: expect("null like null escape 'hello'", Like.like(caNull,
121: 0, caNull, 0, caHello, caHello.length), null);
122: // gets back a null before it evaluates the pattern
123: expect("null like 'hello\\' escape '\\'", Like.like(caNull,
124: 0, "hello\\".toCharArray(),
125: "hello\\".toCharArray().length, "\\".toCharArray(),
126: "\\".toCharArray().length), null);
127:
128: } catch (StandardException leOuter1) {
129: leOuter1.printStackTrace();
130: FAIL("unexpected exception");
131: }
132:
133: REPORT("testing valid match cases...");
134: try {
135: expect("'hello' like 'hello' escape null", Like.like(
136: caHello, caHello.length, caHello, caHello.length,
137: caNull, 0), Boolean.TRUE);
138: expect("'hello' like 'h_llo' escape null", Like.like(
139: caHello, caHello.length, "h_llo".toCharArray(),
140: "h_llo".toCharArray().length, caNull, 0),
141: Boolean.TRUE);
142: expect("'hello' like '_ello' escape null", Like.like(
143: caHello, caHello.length, "_ello".toCharArray(),
144: "_ello".toCharArray().length, caNull, 0),
145: Boolean.TRUE);
146: expect("'hello' like 'hell_' escape null", Like.like(
147: caHello, caHello.length, "hell_".toCharArray(),
148: "hell_".toCharArray().length, caNull, 0),
149: Boolean.TRUE);
150: expect("'hello' like '_____' escape null", Like.like(
151: caHello, caHello.length, "_____".toCharArray(),
152: "_____".toCharArray().length, caNull, 0),
153: Boolean.TRUE);
154: expect("'hello' like 'h___e' escape null", Like.like(
155: caHello, caHello.length, "h___o".toCharArray(),
156: "h___o".toCharArray().length, caNull, 0),
157: Boolean.TRUE);
158: expect("'h' like 'h' escape null", Like
159: .like("h".toCharArray(), "h".toCharArray().length,
160: "h".toCharArray(),
161: "h".toCharArray().length, caNull, 0),
162: Boolean.TRUE);
163: expect("'h' like '_' escape null", Like
164: .like("h".toCharArray(), "h".toCharArray().length,
165: "_".toCharArray(),
166: "_".toCharArray().length, caNull, 0),
167: Boolean.TRUE);
168: expect("'h' like '%' escape null", Like
169: .like("h".toCharArray(), "h".toCharArray().length,
170: "%".toCharArray(),
171: "%".toCharArray().length, caNull, 0),
172: Boolean.TRUE);
173: expect("'h' like '_%' escape null", Like.like("h"
174: .toCharArray(), "h".toCharArray().length, "_%"
175: .toCharArray(), "_%".toCharArray().length, caNull,
176: 0), Boolean.TRUE);
177: expect("'h' like '%_' escape null", Like.like("h"
178: .toCharArray(), "h".toCharArray().length, "%_"
179: .toCharArray(), "%_".toCharArray().length, caNull,
180: 0), Boolean.TRUE);
181: expect("'h' like '%' escape null", Like
182: .like("h".toCharArray(), "h".toCharArray().length,
183: "%".toCharArray(),
184: "%".toCharArray().length, caNull, 0),
185: Boolean.TRUE);
186: expect("'' like '%' escape null", Like
187: .like("".toCharArray(), "".toCharArray().length,
188: "%".toCharArray(),
189: "%".toCharArray().length, caNull, 0),
190: Boolean.TRUE);
191: expect("'' like '%%' escape null", Like.like(""
192: .toCharArray(), "".toCharArray().length, "%%"
193: .toCharArray(), "%%".toCharArray().length, caNull,
194: 0), Boolean.TRUE);
195: expect("'' like '%%%' escape null", Like.like(""
196: .toCharArray(), "".toCharArray().length, "%%%"
197: .toCharArray(), "%%%".toCharArray().length, caNull,
198: 0), Boolean.TRUE);
199: } catch (StandardException leOuter2) {
200: leOuter2.printStackTrace();
201: FAIL("unexpected exception");
202: }
203:
204: REPORT("testing valid nonmatch cases...");
205: try {
206: expect("'hello' like 'hello ' escape null", Like.like(
207: caHello, caHello.length, "hello ".toCharArray(),
208: "hello ".toCharArray().length, caNull, 0),
209: Boolean.FALSE);
210: expect("'hello ' like 'hello' escape null", Like.like(
211: "hello ".toCharArray(),
212: "hello ".toCharArray().length, caHello,
213: caHello.length, caNull, 0), Boolean.FALSE);
214: expect("'hello' like 'hellox' escape null", Like.like(
215: caHello, caHello.length, "hellox".toCharArray(),
216: "hellox".toCharArray().length, caNull, 0),
217: Boolean.FALSE);
218: expect("'hellox' like 'hello' escape null", Like.like(
219: "hellox".toCharArray(),
220: "hellox".toCharArray().length, caHello,
221: caHello.length, caNull, 0), Boolean.FALSE);
222: expect("'xhellox' like 'hello' escape null", Like.like(
223: "xhellox".toCharArray(),
224: "xhellox".toCharArray().length, caHello,
225: caHello.length, caNull, 0), Boolean.FALSE);
226: expect("'hello' like 'xhellox' escape null", Like.like(
227: caHello, caHello.length, "xhellox".toCharArray(),
228: "xhellox".toCharArray().length, null, 0),
229: Boolean.FALSE);
230: expect("'hello' like 'h___' escape null", Like.like(
231: caHello, caHello.length, "h___".toCharArray(),
232: "h___".toCharArray().length, caNull, 0),
233: Boolean.FALSE);
234: expect("'h' like '_%_' escape null", Like.like("h"
235: .toCharArray(), "h".toCharArray().length, "_%_"
236: .toCharArray(), "_%_".toCharArray().length, caNull,
237: 0), Boolean.FALSE);
238: expect("'' like '_' escape null", Like
239: .like("".toCharArray(), "".toCharArray().length,
240: "_".toCharArray(),
241: "_".toCharArray().length, caNull, 0),
242: Boolean.FALSE);
243: } catch (StandardException leOuter3) {
244: leOuter3.printStackTrace();
245: FAIL("unexpected exception");
246: }
247:
248: REPORT("testing error cases...");
249:
250: try {
251: msg = null;
252: gotLE = false;
253: desc = "null like null escape 'hello'";
254: t = Like.like(caHello, caHello.length, caHello,
255: caHello.length, caHello, caHello.length);
256: } catch (StandardException le) {
257: gotLE = true;
258: msg = le.getMessage();
259: } finally {
260: if (gotLE)
261: PASS("TEST [" + desc + "] got exception " + msg);
262: else
263: FAIL("TEST [" + desc + "] didn't get exception");
264: }
265:
266: try {
267: msg = null;
268: gotLE = false;
269: desc = "'hello' like 'hhh' escape 'h'";
270: t = Like.like(caHello, caHello.length, "hhh".toCharArray(),
271: "hhh".toCharArray().length, "h".toCharArray(), "h"
272: .toCharArray().length);
273: } catch (StandardException le) {
274: gotLE = true;
275: msg = le.getMessage();
276: } finally {
277: if (gotLE)
278: PASS("TEST [" + desc + "] got exception " + msg);
279: else
280: FAIL("TEST [" + desc + "] didn't get exception");
281: }
282:
283: try {
284: msg = null;
285: gotLE = false;
286: desc = "'hello' like 'he%' escape 'h'";
287: t = Like.like(caHello, caHello.length, "he%".toCharArray(),
288: "he%".toCharArray().length, "h".toCharArray(), "h"
289: .toCharArray().length);
290: } catch (StandardException le) {
291: gotLE = true;
292: msg = le.getMessage();
293: } finally {
294: if (gotLE)
295: PASS("TEST [" + desc + "] got exception " + msg);
296: else
297: FAIL("TEST [" + desc + "] didn't get exception");
298: }
299:
300: }
301:
302: /*
303: override to mark the test as failed when dumping the message.
304: */
305: protected boolean FAIL(String msg) {
306: super .FAIL(msg);
307: return didFAIL = true;
308: }
309: }
|