KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > execute > SQLExecuteHelperTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.execute;
21
22 import org.netbeans.junit.NbTestCase;
23
24 /**
25  *
26  * @author Andrei Badea
27  */

28 public class SQLExecuteHelperTest extends NbTestCase {
29     
30     public SQLExecuteHelperTest(String JavaDoc testName) {
31         super(testName);
32     }
33
34     public void testSplit() {
35         // removing line comments
36
assertSplit("select --line\n from dual", "select from dual");
37         assertSplit("select ----line\n from dual", "select from dual");
38         assertSplit("select --line from dual", "select");
39         
40         // removing block comments
41
assertSplit("select /* block */ from dual", "select from dual");
42         assertSplit("select ///* block */ from dual", "select // from dual");
43         assertSplit("select /* block * block ***/ from dual", "select from dual");
44         assertSplit("select /* block from dual", "select");
45         assertSplit("select a - b / c from dual", "select a - b / c from dual");
46         assertSplit("select 'foo /* bar */ -- baz' from dual", "select 'foo /* bar */ -- baz' from dual");
47         
48         // ; in comments should not be considered a statement separator
49
assertSplit("select --comment; \n foo", "select foo");
50         assertSplit("select /* ; */ foo", "select foo");
51
52         // splitting
53
assertSplit(" ;; ; ", new String JavaDoc[0]);
54         assertSplit("/* comment */ select foo; /* comment */ select bar -- comment", new String JavaDoc[] { "select foo", "select bar" });
55
56         // splitting and start/end positions
57
String JavaDoc test = " select foo ; select /* comment */bar;\n select baz -- comment";
58         // System.out.println(test.substring(12));
59
assertSplit(test, new StatementInfo[] {
60             new StatementInfo("select foo", 2, 0, 2, 12),
61             new StatementInfo("select bar", 18, 0, 18, 41),
62             new StatementInfo("select baz", 46, 1, 3, 56),
63         });
64     }
65     
66     private static void assertSplit(String JavaDoc script, String JavaDoc expected) {
67         assertSplit(script, new String JavaDoc[] { expected });
68     }
69     
70     private static void assertSplit(String JavaDoc script, String JavaDoc[] expected) {
71         StatementInfo[] stmts = (StatementInfo[])SQLExecuteHelper.split(script).toArray(new StatementInfo[0]);
72         assertEquals(expected.length, stmts.length);
73         for (int i = 0; i < expected.length; i++) {
74             assertEquals(expected[i], stmts[i].getSQL());
75         }
76     }
77     
78     private static void assertSplit(String JavaDoc script, StatementInfo[] expected) {
79         StatementInfo[] stmts = (StatementInfo[])SQLExecuteHelper.split(script).toArray(new StatementInfo[0]);
80         assertEquals(expected.length, stmts.length);
81         for (int i = 0; i < expected.length; i++) {
82             assertEquals(expected[i].getSQL(), stmts[i].getSQL());
83             assertEquals(expected[i].getStartOffset(), stmts[i].getStartOffset());
84             assertEquals(expected[i].getStartLine(), stmts[i].getStartLine());
85             assertEquals(expected[i].getStartColumn(), stmts[i].getStartColumn());
86             assertEquals(expected[i].getEndOffset(), stmts[i].getEndOffset());
87         }
88     }
89 }
90
Popular Tags