001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.io.StringReader;
009: import java.util.Random;
010:
011: import org.h2.test.TestBase;
012: import org.h2.util.ScriptReader;
013:
014: /**
015: * Tests the script reader tool that breaks up SQL scripts in statements.
016: */
017: public class TestScriptReader extends TestBase {
018:
019: public void test() throws Exception {
020: testCommon();
021: testRandom();
022: }
023:
024: private void testRandom() throws Exception {
025: int len = getSize(1000, 10000);
026: Random random = new Random(10);
027: for (int i = 0; i < len; i++) {
028: int l = random.nextInt(10);
029: String[] sql = new String[l];
030: StringBuffer buff = new StringBuffer();
031: for (int j = 0; j < l; j++) {
032: sql[j] = randomStatement(random);
033: buff.append(sql[j]);
034: if (j < l - 1) {
035: buff.append(";");
036: }
037: }
038: String s = buff.toString();
039: StringReader reader = new StringReader(s);
040: ScriptReader source = new ScriptReader(reader);
041: for (int j = 0; j < l; j++) {
042: String e = source.readStatement();
043: String c = sql[j];
044: if (c.length() == 0 && j == l - 1) {
045: c = null;
046: }
047: check(e, c);
048: }
049: check(source.readStatement(), null);
050: }
051: }
052:
053: private String randomStatement(Random random) {
054: StringBuffer buff = new StringBuffer();
055: int len = random.nextInt(5);
056: for (int i = 0; i < len; i++) {
057: switch (random.nextInt(10)) {
058: case 0: {
059: int l = random.nextInt(4);
060: String[] ch = new String[] { "\n", "\r", " ", "*", "a",
061: "0" };
062: for (int j = 0; j < l; j++) {
063: buff.append(ch[random.nextInt(ch.length)]);
064: }
065: break;
066: }
067: case 1: {
068: buff.append('\'');
069: int l = random.nextInt(4);
070: String[] ch = new String[] { ";", "\n", "\r", "--",
071: "//", "/", "-", "*", "/*", "*/", "\"" };
072: for (int j = 0; j < l; j++) {
073: buff.append(ch[random.nextInt(ch.length)]);
074: }
075: buff.append('\'');
076: break;
077: }
078: case 2: {
079: buff.append('"');
080: int l = random.nextInt(4);
081: String[] ch = new String[] { ";", "\n", "\r", "--",
082: "//", "/", "-", "*", "/*", "*/", "\'" };
083: for (int j = 0; j < l; j++) {
084: buff.append(ch[random.nextInt(ch.length)]);
085: }
086: buff.append('"');
087: break;
088: }
089: case 3: {
090: buff.append('-');
091: if (random.nextBoolean()) {
092: String[] ch = new String[] { "\n", "\r", "*", "a",
093: " " };
094: int l = 1 + random.nextInt(4);
095: for (int j = 0; j < l; j++) {
096: buff.append(ch[random.nextInt(ch.length)]);
097: }
098: } else {
099: buff.append('-');
100: String[] ch = new String[] { ";", "-", "//", "/*",
101: "*/", "a" };
102: int l = random.nextInt(4);
103: for (int j = 0; j < l; j++) {
104: buff.append(ch[random.nextInt(ch.length)]);
105: }
106: buff.append('\n');
107: }
108: break;
109: }
110: case 4: {
111: buff.append('/');
112: if (random.nextBoolean()) {
113: String[] ch = new String[] { "\n", "\r", "a", " ",
114: "- " };
115: int l = 1 + random.nextInt(4);
116: for (int j = 0; j < l; j++) {
117: buff.append(ch[random.nextInt(ch.length)]);
118: }
119: } else {
120: buff.append('*');
121: String[] ch = new String[] { ";", "-", "//", "/* ",
122: "--", "\n", "\r", "a" };
123: int l = random.nextInt(4);
124: for (int j = 0; j < l; j++) {
125: buff.append(ch[random.nextInt(ch.length)]);
126: }
127: buff.append("*/");
128: }
129: break;
130: }
131: }
132: }
133: return buff.toString();
134: }
135:
136: private void testCommon() throws Exception {
137: String s = "a;';';\";\";--;\n;/*;\n*/;//;\na;";
138: StringReader reader = new StringReader(s);
139: ScriptReader source = new ScriptReader(reader);
140: check(source.readStatement(), "a");
141: check(source.readStatement(), "';'");
142: check(source.readStatement(), "\";\"");
143: check(source.readStatement(), "--;\n");
144: check(source.readStatement(), "/*;\n*/");
145: check(source.readStatement(), "//;\na");
146: check(source.readStatement(), null);
147: source.close();
148: }
149:
150: }
|