001: //jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: //Copyright (C) 2004 The jTDS Project
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.test;
019:
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.Properties;
023: import javax.naming.NamingException;
024: import javax.naming.Reference;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: import net.sourceforge.jtds.jdbcx.JtdsDataSource;
030: import net.sourceforge.jtds.jdbc.Messages;
031: import net.sourceforge.jtds.jdbc.Driver;
032:
033: /**
034: * Unit tests for the {@link JtdsDataSource} class.
035: *
036: * @author David D. Kilzer
037: * @version $Id: JtdsDataSourceUnitTest.java,v 1.21 2007/07/08 18:08:54 bheineman Exp $
038: */
039: public class JtdsDataSourceUnitTest extends UnitTestBase {
040:
041: /**
042: * Construct a test suite for this class.
043: * <p/>
044: * The test suite includes the tests in this class as
045: * well as {@link Test_JtdsDataSource_getConnection}.
046: *
047: * @return The test suite to run.
048: */
049: public static Test suite() {
050:
051: TestSuite testSuite = new TestSuite(
052: JtdsDataSourceUnitTest.class);
053:
054: testSuite
055: .addTest(new TestSuite(
056: JtdsDataSourceUnitTest.Test_JtdsDataSource_fields.class,
057: "test_fields_DefaultProperties"));
058:
059: testSuite
060: .addTest(new TestSuite(
061: JtdsDataSourceUnitTest.Test_JtdsDataSource_getReference.class,
062: "test_getReference_DefaultProperties"));
063:
064: testSuite
065: .addTest(new TestSuite(
066: JtdsDataSourceUnitTest.Test_JtdsDataSource_addNonNullProperties.class,
067: "test_addNonNullProperties_DefaultProperties"));
068:
069: testSuite
070: .addTest(new TestSuite(
071: JtdsDataSourceUnitTest.Test_JtdsDataSource_getConnection.class,
072: "test_getConnection"));
073:
074: return testSuite;
075: }
076:
077: /**
078: * Constructor.
079: *
080: * @param name The name of the test.
081: */
082: public JtdsDataSourceUnitTest(String name) {
083: super (name);
084: }
085:
086: /**
087: * Tests that the public constructor works.
088: * <p/>
089: * Needed so that this class has at least one test.
090: */
091: public void testPublicConstructor() {
092: assertNotNull(new JtdsDataSource());
093: }
094:
095: /** Class used to test {@link JtdsDataSource}. */
096: public static class Test_JtdsDataSource_fields extends
097: DefaultPropertiesTestLibrary {
098:
099: /**
100: * Default constructor.
101: */
102: public Test_JtdsDataSource_fields() {
103: setTester(new DefaultPropertiesTester() {
104:
105: public void assertDefaultProperty(String message,
106: String url, Properties properties,
107: String fieldName, String key, String expected) {
108:
109: // Hack for JtdsDataSource.getCacheMetaData()
110: {
111: if ("useMetadataCache".equals(fieldName)) {
112: fieldName = "cacheMetaData";
113: }
114: }
115:
116: JtdsDataSource dataSource = new JtdsDataSource();
117: invokeSetInstanceField(dataSource, fieldName,
118: expected);
119:
120: // Hack for JtdsDataSource.getTds()
121: {
122: if ("tdsVersion".equals(fieldName)) {
123: fieldName = "tds";
124: }
125: }
126:
127: String actual = String
128: .valueOf(invokeInstanceMethod(dataSource,
129: "get" + ucFirst(fieldName),
130: new Class[] {}, new Object[] {}));
131: assertEquals(message, expected, actual);
132: }
133: });
134: }
135: }
136:
137: /** Class used to test {@link JtdsDataSource#getReference()}. */
138: public static class Test_JtdsDataSource_getReference extends
139: DefaultPropertiesTestLibrary {
140:
141: /**
142: * Default constructor.
143: */
144: public Test_JtdsDataSource_getReference() {
145: setTester(new DefaultPropertiesTester() {
146:
147: public void assertDefaultProperty(String message,
148: String url, Properties properties,
149: String fieldName, String key, String expected) {
150:
151: try {
152: // Hack for JtdsDataSource.getCacheMetaData()
153: {
154: if ("useMetadataCache".equals(fieldName)) {
155: fieldName = "cacheMetaData";
156: }
157: }
158: JtdsDataSource dataSource = new JtdsDataSource();
159: invokeSetInstanceField(dataSource, fieldName,
160: expected);
161: Reference reference = dataSource.getReference();
162: assertEquals(message, expected, reference.get(
163: Messages.get(key)).getContent());
164: } catch (NamingException e) {
165: throw new RuntimeException(e.getMessage());
166: }
167: }
168: });
169: }
170: }
171:
172: /** Class used to test <code>JtdsDataSource.addNonNullProperties(Properties, String, String)</code>. */
173: public static class Test_JtdsDataSource_addNonNullProperties extends
174: DefaultPropertiesTestLibrary {
175:
176: /**
177: * Default constructor.
178: */
179: public Test_JtdsDataSource_addNonNullProperties() {
180: setTester(new DefaultPropertiesTester() {
181:
182: public void assertDefaultProperty(String message,
183: String url, Properties properties,
184: String fieldName, String key, String expected) {
185:
186: // Hack for JtdsDataSource.cacheMetaData
187: {
188: if ("useMetadataCache".equals(fieldName)) {
189: fieldName = "cacheMetaData";
190: }
191: }
192:
193: JtdsDataSource dataSource = new JtdsDataSource();
194: dataSource.setServerName("non-null-value");
195: invokeSetInstanceField(dataSource, fieldName,
196: expected);
197:
198: Properties props = new Properties();
199: invokeInstanceMethod(dataSource,
200: "addNonNullProperties", new Class[] {
201: Properties.class, String.class,
202: String.class }, new Object[] {
203: props, "", "" });
204:
205: assertEquals(message, expected, props
206: .getProperty(Messages.get(key)));
207: }
208: });
209: }
210: }
211:
212: public static class Test_JtdsDataSource_getConnection extends
213: UnitTestBase {
214: // TODO Specify host name separately in the properties so that testing can be more accurate
215: public Test_JtdsDataSource_getConnection(String name) {
216: super (name);
217: }
218:
219: /**
220: * Provides a null test suite so that JUnit will not try to instantiate this class directly.
221: *
222: * @return The test suite (always <code>null</code>).
223: */
224: public static final Test suite() {
225: return null;
226: }
227:
228: /**
229: * Test connecting without specifying a host. Should get an SQL state
230: * of 08001 (SQL client unable to establish SQL connection).
231: */
232: public void testNoHost() {
233: JtdsDataSource ds = new JtdsDataSource();
234: ds.setUser(TestBase.props.getProperty(Messages
235: .get(Driver.USER)));
236: ds.setPassword(TestBase.props.getProperty(Messages
237: .get(Driver.PASSWORD)));
238: ds.setDatabaseName(TestBase.props.getProperty(Messages
239: .get(Driver.DATABASENAME)));
240: try {
241: ds.setPortNumber(Integer.parseInt(TestBase.props
242: .getProperty(Messages.get(Driver.PORTNUMBER))));
243: } catch (Exception ex) {
244: // Ignore
245: }
246: try {
247: assertNotNull(ds.getConnection());
248: fail("What the?...");
249: } catch (SQLException ex) {
250: assertEquals("Expecting SQL state 08001. Got "
251: + ex.getSQLState(), "08001", ex.getSQLState());
252: } catch (Throwable t) {
253: t.printStackTrace();
254: fail(t.getClass().getName()
255: + " caught while testing JtdsDataSource.getConnection(): "
256: + t);
257: }
258: }
259:
260: /**
261: * Test connecting without specifying a user. Should get an SQL state
262: * of either 28000 (invalid authorization specification) or 08S01 (bad
263: * host name).
264: */
265: public void testNoUser() {
266: JtdsDataSource ds = new JtdsDataSource();
267: ds.setServerName(TestBase.props.getProperty(Messages
268: .get(Driver.SERVERNAME)));
269: ds.setDatabaseName(TestBase.props.getProperty(Messages
270: .get(Driver.DATABASENAME)));
271: try {
272: ds.setPortNumber(Integer.parseInt(TestBase.props
273: .getProperty(Messages.get(Driver.PORTNUMBER))));
274: } catch (Exception ex) {
275: // Ignore
276: }
277: try {
278: assertNotNull(ds.getConnection());
279: fail("What the?...");
280: } catch (SQLException ex) {
281: String sqlState = ex.getSQLState();
282: if (!"28000".equals(sqlState)
283: && !sqlState.startsWith("08")) {
284: ex.printStackTrace();
285: fail("Expecting SQL state 28000 or 08XXX. Got "
286: + ex.getSQLState());
287: }
288: } catch (Throwable t) {
289: t.printStackTrace();
290: fail(t.getClass().getName()
291: + " caught while testing JtdsDataSource.getConnection(): "
292: + t);
293: }
294: }
295:
296: /**
297: * Test connecting with the settings in connection.properties.
298: * <p>
299: * Should also test bug [1051595] jtdsDataSource connects only to
300: * localhost.
301: */
302: public void testNormal() {
303: JtdsDataSource ds = new JtdsDataSource();
304: ds.setServerName(TestBase.props.getProperty(Messages
305: .get(Driver.SERVERNAME)));
306: ds.setUser(TestBase.props.getProperty(Messages
307: .get(Driver.USER)));
308: ds.setPassword(TestBase.props.getProperty(Messages
309: .get(Driver.PASSWORD)));
310: ds.setDatabaseName(TestBase.props.getProperty(Messages
311: .get(Driver.DATABASENAME)));
312: ds.setTds(TestBase.props.getProperty(Messages
313: .get(Driver.TDS)));
314: ds.setServerType("2".equals(TestBase.props
315: .getProperty(Messages.get(Driver.SERVERTYPE))) ? 2
316: : 1);
317: try {
318: ds.setPortNumber(Integer.parseInt(TestBase.props
319: .getProperty(Messages.get(Driver.PORTNUMBER))));
320: } catch (Exception ex) {
321: // Ignore
322: }
323: try {
324: Connection c = ds.getConnection();
325: assertNotNull(c);
326: c.close();
327: } catch (SQLException ex) {
328: ex.printStackTrace();
329: fail("SQLException caught: " + ex.getMessage()
330: + " SQLState=" + ex.getSQLState());
331: } catch (Throwable t) {
332: t.printStackTrace();
333: fail(t.getClass().getName()
334: + " caught while testing JtdsDataSource.getConnection(): "
335: + t);
336: }
337: }
338:
339: /**
340: * Test connecting without specifying TDS version.
341: * <p/>
342: * Test for bug [1113709] Connecting via DataSource. Issue was caused
343: * by JtdsDataSource setting the default values in the constructor, so
344: * the TDS version was always set to 8.0 unless explicitly modified.
345: * <p/>
346: * The incorrect behavior occured when connecting to Sybase (when the
347: * TDS version should have been 5.0 by default).
348: */
349: public void testDefaultTdsVersion() {
350: JtdsDataSource ds = new JtdsDataSource();
351: ds.setServerName(TestBase.props.getProperty(Messages
352: .get(Driver.SERVERNAME)));
353: ds.setUser(TestBase.props.getProperty(Messages
354: .get(Driver.USER)));
355: ds.setPassword(TestBase.props.getProperty(Messages
356: .get(Driver.PASSWORD)));
357: ds.setDatabaseName(TestBase.props.getProperty(Messages
358: .get(Driver.DATABASENAME)));
359: ds.setServerType("2".equals(TestBase.props
360: .getProperty(Messages.get(Driver.SERVERTYPE))) ? 2
361: : 1);
362: try {
363: ds.setPortNumber(Integer.parseInt(TestBase.props
364: .getProperty(Messages.get(Driver.PORTNUMBER))));
365: } catch (Exception ex) {
366: // Ignore
367: }
368: try {
369: Connection c = ds.getConnection();
370: assertNotNull(c);
371: c.close();
372: } catch (SQLException ex) {
373: ex.printStackTrace();
374: fail("SQLException caught: " + ex.getMessage()
375: + " SQLState=" + ex.getSQLState());
376: } catch (Throwable t) {
377: t.printStackTrace();
378: fail(t.getClass().getName()
379: + " caught while testing JtdsDataSource.getConnection(): "
380: + t);
381: }
382: }
383: }
384: }
|