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.util.Properties;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024:
025: import net.sourceforge.jtds.jdbc.DefaultProperties;
026: import net.sourceforge.jtds.jdbc.Driver;
027: import net.sourceforge.jtds.jdbc.Messages;
028:
029: /**
030: * Library for testing default properties.
031: * <p/>
032: * Uses a {@link DefaultPropertiesTester} object to test different methods
033: * in different classes.
034: * <p/>
035: * To extend this class, the programmer must implement the following items:
036: * <ol>
037: * <li>Set the <code>#tester</code> field in a <code>public</code> default
038: * constructor that takes no arguments.</li>
039: * <li>A <code>public static Test suite()</code> method that takes one or more
040: * arguments. (The {@link #suite()} method in this class should
041: * <em>not</em> be overridden.)</li>
042: * </ol>
043: *
044: * @author David D. Kilzer
045: * @version $Id: DefaultPropertiesTestLibrary.java,v 1.26 2007/07/12 20:34:47 bheineman Exp $
046: */
047: public abstract class DefaultPropertiesTestLibrary extends TestCase {
048:
049: /** Test JDBC URL for SQL Server. */
050: private static final String URL_SQLSERVER = "jdbc:jtds:"
051: + DefaultProperties.SERVER_TYPE_SQLSERVER + "://servername";
052: /** Test JDBC URL for Sybase. */
053: private static final String URL_SYBASE = "jdbc:jtds:"
054: + DefaultProperties.SERVER_TYPE_SYBASE + "://servername";
055:
056: /** Object used to run all of the tests. */
057: private DefaultPropertiesTester tester;
058: /** If true, only run tests for SQL Server, not Sybase. */
059: private boolean onlySqlServerTests = false;
060: /** If true, only run tests for TDS 7.0. */
061: private boolean onlyTds70Tests = false;
062:
063: /**
064: * Provides a null test suite so that JUnit will not try to instantiate
065: * this class directly.
066: *
067: * @return The test suite (always <code>null</code>).
068: */
069: public static final Test suite() {
070: return null;
071: }
072:
073: /**
074: * Default constructor.
075: * <p/>
076: * The extender of this class is required to set the <code>#tester</code>
077: * field in a <code>public</code> default constructor.
078: */
079: public DefaultPropertiesTestLibrary() {
080: }
081:
082: /**
083: * Test the <code>serverType</code> property.
084: * <p/>
085: * Different values are set depending on whether SQL Server or
086: * Sybase is used.
087: */
088: public void test_serverType() {
089: String fieldName = "serverType";
090: String messageKey = Driver.SERVERTYPE;
091: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
092: fieldName, String.valueOf(Driver.SQLSERVER));
093: if (!isOnlySqlServerTests()) {
094: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
095: fieldName, String.valueOf(Driver.SYBASE));
096: }
097: }
098:
099: /**
100: * Test the <code>tds</code> (version) property.
101: */
102: public void test_tds() {
103: String fieldName = "tdsVersion";
104: String messageKey = Driver.TDS;
105: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
106: fieldName, DefaultProperties.TDS_VERSION_80);
107: if (!isOnlySqlServerTests()) {
108: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
109: fieldName, DefaultProperties.TDS_VERSION_50);
110: }
111: }
112:
113: /**
114: * Test the <code>portNumber</code> property.
115: * <p/>
116: * Different values are set depending on whether SQL Server or
117: * Sybase is used.
118: */
119: public void test_portNumber() {
120: String fieldName = "portNumber";
121: String messageKey = Driver.PORTNUMBER;
122: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
123: fieldName, DefaultProperties.PORT_NUMBER_SQLSERVER);
124: if (!isOnlySqlServerTests()) {
125: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
126: fieldName, DefaultProperties.PORT_NUMBER_SYBASE);
127: }
128: }
129:
130: /**
131: * Test the <code>databaseName</code> property.
132: */
133: public void test_databaseName() {
134: String fieldName = "databaseName";
135: String messageKey = Driver.DATABASENAME;
136: String expectedValue = DefaultProperties.DATABASE_NAME;
137: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
138: fieldName, expectedValue);
139: if (!isOnlySqlServerTests()) {
140: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
141: fieldName, expectedValue);
142: }
143: }
144:
145: /**
146: * Test the <code>appName</code> property.
147: */
148: public void test_appName() {
149: String fieldName = "appName";
150: String messageKey = Driver.APPNAME;
151: String expectedValue = DefaultProperties.APP_NAME;
152: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
153: fieldName, expectedValue);
154: if (!isOnlySqlServerTests()) {
155: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
156: fieldName, expectedValue);
157: }
158: }
159:
160: /**
161: * Test the <code>bindAddress</code> property.
162: */
163: public void test_bindAddress() {
164: String fieldName = "bindAddress";
165: String messageKey = Driver.BINDADDRESS;
166: String expectedValue = DefaultProperties.BIND_ADDRESS;
167: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
168: fieldName, expectedValue);
169: if (!isOnlySqlServerTests()) {
170: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
171: fieldName, expectedValue);
172: }
173: }
174:
175: /**
176: * Test the <code>lastUpdateCount</code> property.
177: */
178: public void test_lastUpdateCount() {
179: String fieldName = "lastUpdateCount";
180: String messageKey = Driver.LASTUPDATECOUNT;
181: String expectedValue = DefaultProperties.LAST_UPDATE_COUNT;
182: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
183: fieldName, expectedValue);
184: if (!isOnlySqlServerTests()) {
185: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
186: fieldName, expectedValue);
187: }
188: }
189:
190: /**
191: * Test the <code>lobBuffer</code> property.
192: */
193: public void test_lobBuffer() {
194: String fieldName = "lobBuffer";
195: String messageKey = Driver.LOBBUFFER;
196: String expectedValue = DefaultProperties.LOB_BUFFER_SIZE;
197: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
198: fieldName, expectedValue);
199: if (!isOnlySqlServerTests()) {
200: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
201: fieldName, expectedValue);
202: }
203: }
204:
205: /**
206: * Test the <code>loginTimeout</code> property.
207: */
208: public void test_loginTimeout() {
209: String fieldName = "loginTimeout";
210: String messageKey = Driver.LOGINTIMEOUT;
211: String expectedValue = DefaultProperties.LOGIN_TIMEOUT;
212: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
213: fieldName, expectedValue);
214: if (!isOnlySqlServerTests()) {
215: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
216: fieldName, expectedValue);
217: }
218: }
219:
220: /**
221: * Test the <code>socketTimeout</code> property.
222: */
223: public void test_socketTimeout() {
224: String fieldName = "socketTimeout";
225: String messageKey = Driver.SOTIMEOUT;
226: String expectedValue = DefaultProperties.SOCKET_TIMEOUT;
227: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
228: fieldName, expectedValue);
229: if (!isOnlySqlServerTests()) {
230: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
231: fieldName, expectedValue);
232: }
233: }
234:
235: /**
236: * Test the <code>macAddress</code> property.
237: */
238:
239: public void test_macAddress() {
240: String fieldName = "macAddress";
241: String messageKey = Driver.MACADDRESS;
242: String expectedValue = DefaultProperties.MAC_ADDRESS;
243: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
244: fieldName, expectedValue);
245: if (!isOnlySqlServerTests()) {
246: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
247: fieldName, expectedValue);
248: }
249: }
250:
251: /**
252: * Test the <code>namedPipe</code> property.
253: */
254: public void test_namedPipe() {
255: String fieldName = "namedPipe";
256: String messageKey = Driver.NAMEDPIPE;
257: String expectedValue = DefaultProperties.NAMED_PIPE;
258: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
259: fieldName, expectedValue);
260: if (!isOnlySqlServerTests()) {
261: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
262: fieldName, expectedValue);
263: }
264: }
265:
266: /**
267: * Test the <code>packetSize</code> property.
268: */
269: public void test_packetSize() {
270:
271: String fieldName = "packetSize";
272: String messageKey = Driver.PACKETSIZE;
273:
274: if (!isOnlyTds70Tests()) {
275: String expectedValue = DefaultProperties.PACKET_SIZE_42;
276: assertDefaultPropertyByTdsVersion(URL_SQLSERVER,
277: DefaultProperties.TDS_VERSION_42, messageKey,
278: fieldName, expectedValue);
279: expectedValue = DefaultProperties.PACKET_SIZE_50;
280: assertDefaultPropertyByTdsVersion(URL_SYBASE,
281: DefaultProperties.TDS_VERSION_50, messageKey,
282: fieldName, expectedValue);
283: }
284:
285: String expectedValue = DefaultProperties.PACKET_SIZE_70_80;
286: assertDefaultPropertyByTdsVersion(URL_SQLSERVER,
287: DefaultProperties.TDS_VERSION_70, messageKey,
288: fieldName, expectedValue);
289: if (!isOnlyTds70Tests()) {
290: assertDefaultPropertyByTdsVersion(URL_SQLSERVER,
291: DefaultProperties.TDS_VERSION_80, messageKey,
292: fieldName, expectedValue);
293: }
294: }
295:
296: /**
297: * Test the <code>prepareSql</code> property.
298: */
299: public void test_prepareSql() {
300: String fieldName = "prepareSql";
301: String messageKey = Driver.PREPARESQL;
302: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
303: fieldName, DefaultProperties.PREPARE_SQLSERVER);
304: if (!isOnlySqlServerTests()) {
305: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
306: fieldName, DefaultProperties.PREPARE_SYBASE);
307: }
308: }
309:
310: /**
311: * Test the <code>progName</code> property.
312: */
313: public void test_progName() {
314: String fieldName = "progName";
315: String messageKey = Driver.PROGNAME;
316: String expectedValue = DefaultProperties.PROG_NAME;
317: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
318: fieldName, expectedValue);
319: if (!isOnlySqlServerTests()) {
320: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
321: fieldName, expectedValue);
322: }
323: }
324:
325: /**
326: * Test the <code>sendStringParametersAsUnicode</code> property.
327: */
328: public void test_sendStringParametersAsUnicode() {
329: String fieldName = "sendStringParametersAsUnicode";
330: String messageKey = Driver.SENDSTRINGPARAMETERSASUNICODE;
331: String expectedValue = DefaultProperties.USE_UNICODE;
332: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
333: fieldName, expectedValue);
334: if (!isOnlySqlServerTests()) {
335: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
336: fieldName, expectedValue);
337: }
338: }
339:
340: /**
341: * Test the <code>batchSize</code> property.
342: */
343: public void test_batchSize() {
344: String fieldName = "batchSize";
345: String messageKey = Driver.BATCHSIZE;
346: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
347: fieldName, DefaultProperties.BATCH_SIZE_SQLSERVER);
348: if (!isOnlySqlServerTests()) {
349: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
350: fieldName, DefaultProperties.BATCH_SIZE_SYBASE);
351: }
352: }
353:
354: /**
355: * Test the <code>bufferDir</code> property.
356: */
357: public void test_bufferDir() {
358: String fieldName = "bufferDir";
359: String messageKey = Driver.BUFFERDIR;
360: String expectedValue = DefaultProperties.BUFFER_DIR;
361: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
362: fieldName, expectedValue);
363: if (!isOnlySqlServerTests()) {
364: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
365: fieldName, expectedValue);
366: }
367: }
368:
369: /**
370: * Test the <code>bufferMaxMemory</code> property.
371: */
372: public void test_bufferMaxMemory() {
373: String fieldName = "bufferMaxMemory";
374: String messageKey = Driver.BUFFERMAXMEMORY;
375: String expectedValue = DefaultProperties.BUFFER_MAX_MEMORY;
376: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
377: fieldName, expectedValue);
378: if (!isOnlySqlServerTests()) {
379: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
380: fieldName, expectedValue);
381: }
382: }
383:
384: /**
385: * Test the <code>bufferMinPackets</code> property.
386: */
387: public void test_bufferMinPackets() {
388: String fieldName = "bufferMinPackets";
389: String messageKey = Driver.BUFFERMINPACKETS;
390: String expectedValue = DefaultProperties.BUFFER_MIN_PACKETS;
391: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
392: fieldName, expectedValue);
393: if (!isOnlySqlServerTests()) {
394: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
395: fieldName, expectedValue);
396: }
397: }
398:
399: /**
400: * Test the <code>cacheMetaData</code> property.
401: */
402: public void test_cacheMetaData() {
403: String fieldName = "useMetadataCache";
404: String messageKey = Driver.CACHEMETA;
405: String expectedValue = DefaultProperties.CACHEMETA;
406: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
407: fieldName, expectedValue);
408: if (!isOnlySqlServerTests()) {
409: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
410: fieldName, expectedValue);
411: }
412: }
413:
414: /**
415: * Test the <code>tcpNoDelay</code> property.
416: */
417: public void test_tcpNoDelay() {
418: String fieldName = "tcpNoDelay";
419: String messageKey = Driver.TCPNODELAY;
420: String expectedValue = DefaultProperties.TCP_NODELAY;
421: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
422: fieldName, expectedValue);
423: if (!isOnlySqlServerTests()) {
424: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
425: fieldName, expectedValue);
426: }
427: }
428:
429: /**
430: * Test the <code>useCursors</code> property.
431: */
432: public void test_useCursors() {
433: String fieldName = "useCursors";
434: String messageKey = Driver.USECURSORS;
435: String expectedValue = DefaultProperties.USECURSORS;
436: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
437: fieldName, expectedValue);
438: if (!isOnlySqlServerTests()) {
439: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
440: fieldName, expectedValue);
441: }
442: }
443:
444: /**
445: * Test the <code>useJCIFS</code> property.
446: */
447: public void test_useJCIFS() {
448: String fieldName = "useJCIFS";
449: String messageKey = Driver.USEJCIFS;
450: String expectedValue = DefaultProperties.USEJCIFS;
451: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
452: fieldName, expectedValue);
453: if (!isOnlySqlServerTests()) {
454: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
455: fieldName, expectedValue);
456: }
457: }
458:
459: /**
460: * Test the <code>useLOBs</code> property.
461: */
462: public void test_useLOBs() {
463: String fieldName = "useLOBs";
464: String messageKey = Driver.USELOBS;
465: String expectedValue = DefaultProperties.USELOBS;
466: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
467: fieldName, expectedValue);
468: if (!isOnlySqlServerTests()) {
469: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
470: fieldName, expectedValue);
471: }
472: }
473:
474: /**
475: * Test the <code>wsid</code> property.
476: */
477: public void test_wsid() {
478: String fieldName = "wsid";
479: String messageKey = Driver.WSID;
480: String expectedValue = DefaultProperties.WSID;
481: assertDefaultPropertyByServerType(URL_SQLSERVER, messageKey,
482: fieldName, expectedValue);
483: if (!isOnlySqlServerTests()) {
484: assertDefaultPropertyByServerType(URL_SYBASE, messageKey,
485: fieldName, expectedValue);
486: }
487: }
488:
489: /**
490: * Assert that the <code>expected</code> property value is set using
491: * a given <code>url</code> and <code>tdsVersion</code> property.
492: *
493: * @param url The JDBC URL.
494: * @param tdsVersion The TDS version.
495: * @param key The message key.
496: * @param fieldName The field name used in the class.
497: * @param expected The expected value of the property.
498: */
499: private void assertDefaultPropertyByTdsVersion(String url,
500: String tdsVersion, String key, String fieldName,
501: String expected) {
502:
503: Properties properties = new Properties();
504: properties.setProperty(Messages.get(Driver.TDS), tdsVersion);
505: getTester().assertDefaultProperty("Default property incorrect",
506: url, properties, fieldName, key, expected);
507: }
508:
509: /**
510: * Assert that the <code>expected</code> property value is set using
511: * a given <code>url</code>.
512: *
513: * @param url The JDBC URL.
514: * @param key The message key.
515: * @param fieldName The field name used in the class.
516: * @param expected The expected value of the property.
517: */
518: private void assertDefaultPropertyByServerType(String url,
519: String key, String fieldName, String expected) {
520: getTester().assertDefaultProperty("Default property incorrect",
521: url, new Properties(), fieldName, key, expected);
522: }
523:
524: /**
525: * Getter for {@link #tester}.
526: *
527: * @return Value of {@link #tester}.
528: */
529: protected DefaultPropertiesTester getTester() {
530: return tester;
531: }
532:
533: /**
534: * Setter for <code>#tester</code>.
535: *
536: * @param tester The value to set <code>#tester</code> to.
537: */
538: public void setTester(DefaultPropertiesTester tester) {
539: this .tester = tester;
540: }
541:
542: /**
543: * Getter for <code>#onlySqlServerTests</code>.
544: *
545: * @return Value of <code>#onlySqlServerTests</code>.
546: */
547: public boolean isOnlySqlServerTests() {
548: return onlySqlServerTests;
549: }
550:
551: /**
552: * Setter for {@link #onlySqlServerTests}.
553: *
554: * @param onlySqlServerTests The value to set {@link #onlySqlServerTests} to.
555: */
556: protected void setOnlySqlServerTests(boolean onlySqlServerTests) {
557: this .onlySqlServerTests = onlySqlServerTests;
558: }
559:
560: /**
561: * Getter for <code>#onlyTds70Tests</code>.
562: *
563: * @return Value of <code>#onlyTds70Tests</code>.
564: */
565: public boolean isOnlyTds70Tests() {
566: return onlyTds70Tests;
567: }
568:
569: /**
570: * Setter for {@link #onlyTds70Tests}.
571: *
572: * @param onlyTds70Tests The value to set {@link #onlyTds70Tests} to.
573: */
574: protected void setOnlyTds70Tests(boolean onlyTds70Tests) {
575: this.onlyTds70Tests = onlyTds70Tests;
576: }
577:
578: }
|