01: /*
02: * Copyright (C) 2007 Rob Manning
03: * manningr@users.sourceforge.net
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19: package net.sourceforge.squirrel_sql.plugins.mysql.tokenizer;
20:
21: import static org.junit.Assert.*;
22:
23: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
24: import net.sourceforge.squirrel_sql.fw.preferences.IQueryTokenizerPreferenceBean;
25:
26: import org.easymock.classextension.EasyMock;
27: import org.junit.After;
28: import org.junit.Before;
29: import org.junit.Test;
30:
31: public class MysqlQueryTokenizerTest extends BaseSQuirreLJUnit4TestCase {
32:
33: private static final String storedProcSQLWithSep = "CREATE PROCEDURE sp_ins (P VARCHAR(10)) \n"
34: + "BEGIN \n"
35: + "SET @x=CHAR_LENGTH(P); \n"
36: + "SET @y = HEX(P); \n"
37: + "INSERT INTO sp1(id,txt) VALUES(@x,@y); \n"
38: + "END \n"
39: + "| \n";
40:
41: private static final String storedProcSQLWithoutSep = "CREATE PROCEDURE sp_ins (P VARCHAR(10)) \n"
42: + "BEGIN \n"
43: + "SET @x=CHAR_LENGTH(P); \n"
44: + "SET @y = HEX(P); \n"
45: + "INSERT INTO sp1(id,txt) VALUES(@x,@y); \n" + "END \n";
46:
47: MysqlQueryTokenizer tokenizerUnderTest = null;
48:
49: // Mock Objects
50: IQueryTokenizerPreferenceBean prefsBean = EasyMock
51: .createMock(IQueryTokenizerPreferenceBean.class);
52:
53: @Before
54: public void setUp() throws Exception {
55: EasyMock.expect(prefsBean.getStatementSeparator()).andReturn(
56: ";").anyTimes();
57: EasyMock.expect(prefsBean.getProcedureSeparator()).andReturn(
58: "|").anyTimes();
59: EasyMock.expect(prefsBean.getLineComment()).andReturn("--")
60: .anyTimes();
61: EasyMock.expect(prefsBean.isRemoveMultiLineComments())
62: .andReturn(false).anyTimes();
63: EasyMock.replay(prefsBean);
64: tokenizerUnderTest = new MysqlQueryTokenizer(prefsBean);
65: }
66:
67: @After
68: public void tearDown() throws Exception {
69: tokenizerUnderTest = null;
70: }
71:
72: @Test
73: public final void testSetScriptToTokenizeWithSep() {
74: tokenizerUnderTest.setScriptToTokenize(storedProcSQLWithSep);
75: assertEquals(1, tokenizerUnderTest.getQueryCount());
76: }
77:
78: @Test
79: public final void testSetScriptToTokenizeWithoutSep() {
80: tokenizerUnderTest.setScriptToTokenize(storedProcSQLWithoutSep);
81: assertEquals(1, tokenizerUnderTest.getQueryCount());
82: }
83:
84: @Test
85: public final void testSetScriptToTokenizeMultipleProcs() {
86: StringBuilder script = new StringBuilder();
87: for (int i = 0; i < 5; i++) {
88: script.append(storedProcSQLWithSep);
89: script.append("\n");
90: }
91: tokenizerUnderTest.setScriptToTokenize(script.toString());
92: assertEquals(5, tokenizerUnderTest.getQueryCount());
93: }
94:
95: }
|