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.Hashtable;
021: import java.util.Properties;
022: import javax.naming.Context;
023: import javax.naming.Reference;
024: import javax.naming.Name;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: import net.sourceforge.jtds.jdbc.Support;
030: import net.sourceforge.jtds.jdbcx.JtdsDataSource;
031: import net.sourceforge.jtds.jdbcx.JtdsObjectFactory;
032:
033: /**
034: * Unit tests for the {@link JtdsObjectFactory} class.
035: *
036: * @author David D. Kilzer
037: * @author Alin Sinpalean
038: * @version $Id: JtdsObjectFactoryUnitTest.java,v 1.12 2007/07/12 21:20:03 bheineman Exp $
039: */
040: public class JtdsObjectFactoryUnitTest extends UnitTestBase {
041:
042: /**
043: * Construct a test suite for this class.
044: * <p/>
045: * The test suite includes the tests in this class, and adds tests from
046: * {@link DefaultPropertiesTestLibrary} after creating an anonymous
047: * {@link DefaultPropertiesTester} object.
048: *
049: * @return The test suite to run.
050: */
051: public static Test suite() {
052:
053: TestSuite testSuite = new TestSuite(
054: JtdsObjectFactoryUnitTest.class);
055:
056: testSuite
057: .addTest(new TestSuite(
058: JtdsObjectFactoryUnitTest.Test_JtdsObjectFactory_getObjectInstance_DefaultValues.class,
059: "test_getObjectInstance_DefaultValues"));
060:
061: return testSuite;
062: }
063:
064: /**
065: * Constructor.
066: *
067: * @param name The name of the test.
068: */
069: public JtdsObjectFactoryUnitTest(String name) {
070: super (name);
071: }
072:
073: /**
074: * Tests that the factory can correctly rebuild a DataSource with no
075: * properties set (i.e. all values should be null and no NPE should be
076: * thrown).
077: */
078: public void testNoProperties() throws Exception {
079: JtdsDataSource ds = new JtdsDataSource();
080:
081: Reference dsRef = ds.getReference();
082: assertEquals("net.sourceforge.jtds.jdbcx.JtdsObjectFactory",
083: dsRef.getFactoryClassName());
084: assertEquals("net.sourceforge.jtds.jdbcx.JtdsDataSource", dsRef
085: .getClassName());
086:
087: ds = (JtdsDataSource) new JtdsObjectFactory()
088: .getObjectInstance(dsRef, null, null, null);
089:
090: assertNull(ds.getServerName());
091: assertEquals(0, ds.getServerType());
092: assertEquals(0, ds.getPortNumber());
093: assertNull(ds.getDatabaseName());
094: assertNull(ds.getDatabaseName());
095: assertEquals(0, ds.getPortNumber());
096: assertNull(ds.getTds());
097: assertNull(ds.getCharset());
098: assertNull(ds.getLanguage());
099: assertNull(ds.getDomain());
100: assertNull(ds.getInstance());
101: assertEquals(false, ds.getLastUpdateCount());
102: assertEquals(false, ds.getSendStringParametersAsUnicode());
103: assertEquals(false, ds.getNamedPipe());
104: assertNull(ds.getMacAddress());
105: assertEquals(0, ds.getPrepareSql());
106: assertEquals(0, ds.getPacketSize());
107: assertEquals(false, ds.getTcpNoDelay());
108: assertNull(ds.getUser());
109: assertNull(ds.getPassword());
110: assertEquals(0, ds.getLoginTimeout());
111: assertEquals(0, ds.getLobBuffer());
112: assertEquals(0, ds.getMaxStatements());
113: assertNull(ds.getAppName());
114: assertNull(ds.getProgName());
115: assertEquals(false, ds.getXaEmulation());
116: assertNull(ds.getLogFile());
117: assertNull(ds.getSsl());
118: assertEquals(0, ds.getBatchSize());
119: assertNull(ds.getDescription());
120: assertNull(ds.getBindAddress());
121: assertEquals(false, ds.getUseJCIFS());
122: }
123:
124: /** Class used to test {@link JtdsObjectFactory#getObjectInstance(Object, Name, Context, Hashtable)}. */
125: public static class Test_JtdsObjectFactory_getObjectInstance_DefaultValues
126: extends DefaultPropertiesTestLibrary {
127:
128: /**
129: * Default constructor.
130: */
131: public Test_JtdsObjectFactory_getObjectInstance_DefaultValues() {
132: setTester(new DefaultPropertiesTester() {
133:
134: public void assertDefaultProperty(String message,
135: String url, Properties properties,
136: String fieldName, String key, String expected) {
137:
138: try {
139: // Hack for JtdsDataSource.cacheMetaData
140: {
141: if ("useMetadataCache".equals(fieldName)) {
142: fieldName = "cacheMetaData";
143: }
144: }
145:
146: JtdsDataSource referenceDataSource = new JtdsDataSource();
147: invokeSetInstanceField(referenceDataSource,
148: fieldName, expected);
149: Reference reference = referenceDataSource
150: .getReference();
151: JtdsObjectFactory jtdsObjectFactory = new JtdsObjectFactory();
152: JtdsDataSource dataSource = (JtdsDataSource) jtdsObjectFactory
153: .getObjectInstance(reference, null,
154: null, null);
155:
156: // Hack for JtdsDataSource.getTds()
157: {
158: if ("tdsVersion".equals(fieldName)) {
159: fieldName = "tds";
160: }
161: }
162: String actual = String
163: .valueOf(invokeInstanceMethod(
164: dataSource, "get"
165: + ucFirst(fieldName),
166: new Class[] {}, new Object[] {}));
167:
168: assertEquals(message, expected, actual);
169: } catch (Exception e) {
170: RuntimeException runtimeException = new RuntimeException(
171: e.getMessage());
172: Support.linkException(runtimeException, e);
173: throw runtimeException;
174: }
175: }
176: });
177: }
178: }
179:
180: }
|