KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestPattern


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import org.h2.expression.CompareLike;
8 import org.h2.test.TestBase;
9 import org.h2.value.CompareMode;
10
11 /**
12  * @author Thomas
13  */

14 public class TestPattern extends TestBase {
15     
16     public void test() throws Exception JavaDoc {
17         CompareMode mode = new CompareMode(null, null);
18         CompareLike comp = new CompareLike(mode, null, null, null);
19         test(comp, "B", "%_");
20         test(comp, "A", "A%");
21         test(comp, "A", "A%%");
22
23         for(int i=0; i<10; i++) {
24             String JavaDoc pattern=getRandomPattern();
25             String JavaDoc value=getRandomValue();
26             test(comp, value, pattern);
27         }
28     }
29     
30     void test(CompareLike comp, String JavaDoc value, String JavaDoc pattern) throws Exception JavaDoc {
31         // TODO test: need another regexp implementation (this one doesn't work for gcj)
32
// String regexp = initPatternRegexp(pattern);
33
// boolean resultRegexp = value.matches(regexp);
34
// boolean result =
35
comp.test(pattern, value, 'X');
36         
37 // if(result != resultRegexp) {
38
// throw new Exception("Error: >"+value+"< LIKE >"+pattern+"< result="+result+" resultReg="+resultRegexp);
39
// }
40
}
41
42     static String JavaDoc getRandomValue() {
43         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
44         int len = (int)(Math.random() * 10);
45         String JavaDoc s = "ABCDEFGHIJKLMNOP";
46         for(int i=0; i<len; i++) {
47             buff.append(s.charAt((int)(Math.random()*s.length())));
48         }
49         return buff.toString();
50     }
51
52     static String JavaDoc getRandomPattern() {
53         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
54         int len = (int)(Math.random() * 4);
55         //String s = "ABC%_\\";
56
String JavaDoc s = "AB_";
57         for(int i=0; i<len; i++) {
58             buff.append(s.charAt((int)(Math.random()*s.length())));
59         }
60         return buff.toString();
61     }
62
63 // private String initPatternRegexp(String pattern) {
64
// int len = pattern.length();
65
// StringBuffer buff = new StringBuffer();
66
// for (int i = 0; i < len; i++) {
67
// char c = pattern.charAt(i);
68
// if (escape != null && escape.charValue() == c) {
69
// if (i >= len) {
70
// throw Message.internal("escape can't be last char");
71
// }
72
// c = pattern.charAt(++i);
73
// buff.append('\\');
74
// buff.append(c);
75
// } else if (c == '%') {
76
// buff.append(".*");
77
// } else if (c == '_') {
78
// buff.append('.');
79
// } else if(c=='\\'){
80
// buff.append("\\\\");
81
// } else {
82
// buff.append(c);
83
// }
84
// // TODO regexp: there are other chars that need escaping
85
// }
86
// String regexp = buff.toString();
87
//// System.out.println("regexp = " + regexp);
88
// return regexp;
89
// }
90

91 }
92
Popular Tags