001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.session;
020:
021: import static org.easymock.EasyMock.expect;
022: import static org.easymock.EasyMock.isA;
023: import static org.easymock.classextension.EasyMock.createMock;
024: import static org.easymock.classextension.EasyMock.replay;
025: import static org.junit.Assert.assertEquals;
026: import static org.junit.Assert.assertNotSame;
027: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
028: import net.sourceforge.squirrel_sql.client.IApplication;
029: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
030: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
031: import net.sourceforge.squirrel_sql.client.session.event.ISessionListener;
032: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
033: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
034: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
035: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
036: import net.sourceforge.squirrel_sql.fw.sql.SQLConnection;
037: import net.sourceforge.squirrel_sql.fw.util.TaskThreadPool;
038: import net.sourceforge.squirrel_sql.test.TestUtil;
039:
040: import org.easymock.classextension.EasyMock;
041: import org.junit.After;
042: import org.junit.Before;
043: import org.junit.Test;
044:
045: public class SessionTest extends BaseSQuirreLJUnit4TestCase {
046:
047: Session sessionUnderTest = null;
048:
049: // Mock objects
050: IApplication mockApplication = createMock(IApplication.class);
051:
052: SessionManager mockSessionManager = createMock(SessionManager.class);
053:
054: ISQLDriver mockSqlDriver = createMock(ISQLDriver.class);
055:
056: SQLAlias mockSqlAlias = null;
057:
058: SQLConnection mockSqlConnection = null;
059:
060: SquirrelPreferences mockSquirrePrefs = null;
061:
062: IIdentifier mockIidentifier = createMock(IIdentifier.class);
063:
064: IIdentifier mockSqlAliasId = createMock(IIdentifier.class);
065:
066: IIdentifier mockSqlDriverId = createMock(IIdentifier.class);
067:
068: TaskThreadPool mockTaskThreadPool = createMock(TaskThreadPool.class);
069:
070: SessionProperties props = null;
071:
072: static final String FIRST_STMT_SEP = ";";
073: static final String SECOND_STMT_SEP = "FOO";
074: static final String CUSTOM_STMT_SEP = "CustomSeparator";
075:
076: @Before
077: public void setUp() throws Exception {
078: // Simulate the user changing the session properties
079: props = getEasyMockSessionProperties();
080:
081: mockSquirrePrefs = TestUtil
082: .getEasyMockSquirrelPreferences(props);
083: mockSqlAlias = TestUtil.getEasyMockSQLAlias(mockSqlAliasId,
084: mockSqlDriverId);
085: mockSqlConnection = TestUtil.getEasyMockSQLConnection();
086:
087: mockSessionManager
088: .addSessionListener(isA(ISessionListener.class));
089: mockSessionManager
090: .addSessionListener(isA(ISessionListener.class));
091:
092: replay(mockSessionManager);
093:
094: expect(mockApplication.getSessionManager()).andReturn(
095: mockSessionManager).anyTimes();
096: expect(mockApplication.getSquirrelPreferences()).andReturn(
097: mockSquirrePrefs).anyTimes();
098: expect(mockApplication.getThreadPool()).andReturn(
099: mockTaskThreadPool).anyTimes();
100:
101: replay(mockApplication);
102: replay(mockSqlDriver);
103: replay(mockSqlConnection);
104:
105: sessionUnderTest = new Session(mockApplication, mockSqlDriver,
106: mockSqlAlias, mockSqlConnection, "user", "password",
107: mockIidentifier);
108: }
109:
110: @After
111: public void tearDown() throws Exception {
112: sessionUnderTest = null;
113: }
114:
115: // Null tests
116: @Test(expected=IllegalArgumentException.class)
117: public final void testSessionNullApp() {
118: new Session(null, mockSqlDriver, mockSqlAlias,
119: mockSqlConnection, "user", "password", mockIidentifier);
120: }
121:
122: @Test(expected=IllegalArgumentException.class)
123: public final void testSessionNullDriver() {
124: new Session(mockApplication, null, mockSqlAlias,
125: mockSqlConnection, "user", "password", mockIidentifier);
126: }
127:
128: @Test(expected=IllegalArgumentException.class)
129: public final void testSessionNullAlias() {
130: new Session(mockApplication, mockSqlDriver, null,
131: mockSqlConnection, "user", "password", mockIidentifier);
132: }
133:
134: @Test(expected=IllegalArgumentException.class)
135: public final void testSessionNullConnection() {
136: new Session(mockApplication, mockSqlDriver, mockSqlAlias, null,
137: "user", "password", mockIidentifier);
138: }
139:
140: @Test(expected=IllegalArgumentException.class)
141: public final void testSessionNullSessionId() {
142: new Session(mockApplication, mockSqlDriver, mockSqlAlias,
143: mockSqlConnection, "user", "password", null);
144: }
145:
146: @Test(expected=IllegalArgumentException.class)
147: public final void testSessionNullQueryTokenizer() {
148: sessionUnderTest.setQueryTokenizer(null);
149: }
150:
151: // QueryTokenizer tests
152:
153: @Test
154: public final void testGetQueryTokenizer_Default() {
155: // These should be different since we are providing two different
156: // statement separators in SessionProperties
157: IQueryTokenizer qt1 = sessionUnderTest.getQueryTokenizer();
158: IQueryTokenizer qt2 = sessionUnderTest.getQueryTokenizer();
159: assertEquals(FIRST_STMT_SEP, qt1.getSQLStatementSeparator());
160: assertEquals(SECOND_STMT_SEP, qt2.getSQLStatementSeparator());
161: }
162:
163: @Test
164: public final void testGetQueryTokenizer_Custom() {
165: IQueryTokenizer customTokenizer = TestUtil
166: .getEasyMockQueryTokenizer(CUSTOM_STMT_SEP, "--", true,
167: 0);
168:
169: sessionUnderTest.setQueryTokenizer(customTokenizer);
170:
171: IQueryTokenizer retrievedTokenizer = sessionUnderTest
172: .getQueryTokenizer();
173:
174: assertEquals(CUSTOM_STMT_SEP, retrievedTokenizer
175: .getSQLStatementSeparator());
176: }
177:
178: @Test
179: public final void testGetQueryTokenizer_CustomAfterGet() {
180: // This should be a default tokenizer which uses ";" as statement sep
181: IQueryTokenizer initialTokenizer = sessionUnderTest
182: .getQueryTokenizer();
183: assertEquals(FIRST_STMT_SEP, initialTokenizer
184: .getSQLStatementSeparator());
185:
186: IQueryTokenizer customTokenizer = TestUtil
187: .getEasyMockQueryTokenizer(CUSTOM_STMT_SEP, "--", true,
188: 0);
189:
190: // This should override the default tokenizer
191: sessionUnderTest.setQueryTokenizer(customTokenizer);
192:
193: IQueryTokenizer retrievedTokenizer = sessionUnderTest
194: .getQueryTokenizer();
195:
196: assertEquals(CUSTOM_STMT_SEP, retrievedTokenizer
197: .getSQLStatementSeparator());
198:
199: // Check to ensure that the tokenizer received is not the default one that
200: // should have been overridden.
201: assertNotSame(initialTokenizer, retrievedTokenizer);
202: }
203:
204: @Test(expected=IllegalStateException.class)
205: public final void testSetQueryTokenizer() {
206: IQueryTokenizer customTokenizer1 = TestUtil
207: .getEasyMockQueryTokenizer(FIRST_STMT_SEP, "--", true,
208: 0);
209:
210: IQueryTokenizer customTokenizer2 = TestUtil
211: .getEasyMockQueryTokenizer(SECOND_STMT_SEP, "--", true,
212: 0);
213:
214: sessionUnderTest.setQueryTokenizer(customTokenizer1);
215:
216: // this should throw an exception - should not allow multiple custom
217: // tokenizers to be installed for a single session.
218: sessionUnderTest.setQueryTokenizer(customTokenizer2);
219: }
220:
221: private SessionProperties getEasyMockSessionProperties() {
222: // Simulate the user switching the statement separator for the session
223: SessionProperties result = EasyMock
224: .createMock(SessionProperties.class);
225: expect(result.getSQLStatementSeparator()).andReturn(";").once();
226: expect(result.getSQLStatementSeparator()).andReturn("FOO")
227: .once();
228: expect(result.getStartOfLineComment()).andReturn("--")
229: .anyTimes();
230: expect(result.clone()).andReturn(result);
231: expect(result.getRemoveMultiLineComment()).andReturn(true)
232: .anyTimes();
233: replay(result);
234: return result;
235: }
236: }
|