1 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 ; 37 38 43 public class T_Like extends T_Generic 44 { 45 private static final String testService = "likeTest"; 46 boolean didFAIL; 47 48 51 52 public String getModuleToTestProtocolName() { 53 return ExecutionFactory.MODULE; 56 } 57 58 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 if (startParams == null) { 71 startParams = new Properties (); 72 } 73 startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString()); 74 75 REPORT("(unitTestMain) Testing " + testService); 76 77 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 private void expect(String desc, Boolean test, Boolean 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 private void tests() throws StandardException { 102 boolean gotLE = false; 103 Boolean t = null; 104 char[] caNull = null; 105 char[] caHello = "hello".toCharArray(); 106 String msg=null; 107 String 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 expect("null like null escape 'hello'", Like.like(caNull, 0, caNull, 0, caHello, caHello.length), null); 118 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 216 protected boolean FAIL(String msg) { 217 super.FAIL(msg); 218 return didFAIL = true; 219 } 220 } 221 222 | Popular Tags |