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 junit.framework.Test;
021: import junit.framework.TestSuite;
022: import net.sourceforge.jtds.jdbc.DefaultProperties;
023: import net.sourceforge.jtds.jdbc.Driver;
024: import net.sourceforge.jtds.jdbc.Messages;
025: import net.sourceforge.jtds.jdbc.TdsCore;
026: import java.sql.DriverPropertyInfo;
027: import java.sql.SQLException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Properties;
032:
033: /**
034: * Unit tests for the {@link Driver} class.
035: *
036: * @author David D. Kilzer
037: * @version $Id: DriverUnitTest.java,v 1.20 2007/07/08 18:08:54 bheineman Exp $
038: */
039: public class DriverUnitTest extends UnitTestBase {
040:
041: /**
042: * Construct a test suite for this class.
043: * <p/>
044: * The test suite includes the tests in this class, and adds tests
045: * from {@link DefaultPropertiesTestLibrary} after creating
046: * anonymous {@link DefaultPropertiesTester} objects.
047: *
048: * @return The test suite to run.
049: */
050: public static Test suite() {
051:
052: TestSuite testSuite = new TestSuite(DriverUnitTest.class);
053:
054: testSuite
055: .addTest(Test_Driver_setupConnectProperties
056: .suite("test_setupConnectProperties_DefaultProperties"));
057:
058: testSuite.addTest(Test_Driver_getPropertyInfo
059: .suite("test_getPropertyInfo_DefaultProperties"));
060:
061: return testSuite;
062: }
063:
064: /**
065: * Constructor.
066: *
067: * @param name The name of the test.
068: */
069: public DriverUnitTest(final String name) {
070: super (name);
071: }
072:
073: /**
074: * Tests that passing in a null properties argument to
075: * {@link Driver#getPropertyInfo(String, Properties)}
076: * causes the url to be parsed, which then throws a {@link SQLException}.
077: */
078: public void test_getPropertyInfo_ThrowsSQLExceptionWithNullProperties() {
079: try {
080: new Driver().getPropertyInfo("wxyz:", null);
081: fail("Expected SQLException to be throw");
082: } catch (SQLException e) {
083: // Expected
084: }
085: }
086:
087: /**
088: * Tests that passing in a non-null properties argument to
089: * {@link Driver#getPropertyInfo(String, Properties)}
090: * causes the url to be parsed, which then throws a {@link SQLException}.
091: */
092: public void test_getPropertyInfo_ThrowsSQLExceptionWithNonNullProperties() {
093: try {
094: new Driver().getPropertyInfo("wxyz:", new Properties());
095: fail("Expected SQLException to be throw");
096: } catch (SQLException e) {
097: // Expected
098: }
099: }
100:
101: /**
102: * Tests that the {@link DriverPropertyInfo} array returned from
103: * {@link Driver#getPropertyInfo(String, Properties)}
104: * matches the list of properties defined in <code>Messages.properties</code>.
105: */
106: public void test_getPropertyInfo_MatchesMessagesProperties() {
107:
108: final Map infoMap = new HashMap();
109: loadDriverPropertyInfoMap(infoMap);
110:
111: final Map propertyMap = new HashMap();
112: final Map descriptionMap = new HashMap();
113: invokeStaticMethod(Messages.class, "loadDriverProperties",
114: new Class[] { Map.class, Map.class }, new Object[] {
115: propertyMap, descriptionMap });
116:
117: assertEquals(
118: "Properties list size (expected) does not equal DriverPropertyInfo array length (actual)",
119: propertyMap.size(), infoMap.keySet().size());
120: assertEquals(
121: "Description list size (expected) does not equal DriverPropertyInfo array length (actual)",
122: descriptionMap.size(), infoMap.keySet().size());
123:
124: for (Iterator iterator = propertyMap.keySet().iterator(); iterator
125: .hasNext();) {
126: final String key = (String) iterator.next();
127: final DriverPropertyInfo driverPropertyInfo = (DriverPropertyInfo) infoMap
128: .get(propertyMap.get(key));
129: assertNotNull(
130: "No DriverPropertyInfo object exists for property '"
131: + key + "'", driverPropertyInfo);
132: assertEquals(
133: "Property description (expected) does not match DriverPropertyInfo description (actual)",
134: descriptionMap.get(key),
135: driverPropertyInfo.description);
136: }
137: }
138:
139: /**
140: * Tests that the {@link DriverPropertyInfo} array returned from
141: * {@link Driver#getPropertyInfo(String, Properties)} contains
142: * the correct <code>choices</code> value on each of the objects.
143: */
144: public void test_getPropertyInfo_Choices() {
145:
146: String[] expectedBooleanChoices = new String[] {
147: String.valueOf(true), String.valueOf(false), };
148: String[] expectedPrepareSqlChoices = new String[] {
149: String.valueOf(TdsCore.UNPREPARED),
150: String.valueOf(TdsCore.TEMPORARY_STORED_PROCEDURES),
151: String.valueOf(TdsCore.EXECUTE_SQL),
152: String.valueOf(TdsCore.PREPARE) };
153: String[] expectedServerTypeChoices = new String[] {
154: String.valueOf(Driver.SQLSERVER),
155: String.valueOf(Driver.SYBASE), };
156: String[] expectedTdsChoices = new String[] {
157: DefaultProperties.TDS_VERSION_42,
158: DefaultProperties.TDS_VERSION_50,
159: DefaultProperties.TDS_VERSION_70,
160: DefaultProperties.TDS_VERSION_80, };
161:
162: Map expectedChoicesMap = new HashMap();
163: expectedChoicesMap.put(Messages.get(Driver.LASTUPDATECOUNT),
164: expectedBooleanChoices);
165: expectedChoicesMap.put(Messages.get(Driver.NAMEDPIPE),
166: expectedBooleanChoices);
167: expectedChoicesMap.put(Messages.get(Driver.PREPARESQL),
168: expectedPrepareSqlChoices);
169: expectedChoicesMap.put(Messages.get(Driver.SERVERTYPE),
170: expectedServerTypeChoices);
171: expectedChoicesMap.put(Messages.get(Driver.TDS),
172: expectedTdsChoices);
173: expectedChoicesMap.put(Messages
174: .get(Driver.SENDSTRINGPARAMETERSASUNICODE),
175: expectedBooleanChoices);
176: expectedChoicesMap.put(Messages.get(Driver.CACHEMETA),
177: expectedBooleanChoices);
178: expectedChoicesMap.put(Messages.get(Driver.USECURSORS),
179: expectedBooleanChoices);
180: expectedChoicesMap.put(Messages.get(Driver.USELOBS),
181: expectedBooleanChoices);
182:
183: final Map infoMap = new HashMap();
184: loadDriverPropertyInfoMap(infoMap);
185:
186: final Iterator iterator = infoMap.keySet().iterator();
187: while (iterator.hasNext()) {
188: String key = (String) iterator.next();
189: DriverPropertyInfo info = (DriverPropertyInfo) infoMap
190: .get(key);
191: if (expectedChoicesMap.containsKey(key)) {
192: assertEquals("Choices did not match for key " + key,
193: ((String[]) expectedChoicesMap.get(key)),
194: info.choices);
195: } else {
196: assertNull(
197: "Expected choices to be null for key " + key,
198: expectedChoicesMap.get(key));
199: }
200: }
201: }
202:
203: /**
204: * Tests that the {@link DriverPropertyInfo} array returned from
205: * {@link Driver#getPropertyInfo(String, Properties)} contains
206: * the correct <code>required</code> value on each of the objects.
207: */
208: public void test_getPropertyInfo_Required() {
209:
210: Map requiredTrueMap = new HashMap();
211: requiredTrueMap.put(Messages.get(Driver.SERVERNAME),
212: Boolean.TRUE);
213: requiredTrueMap.put(Messages.get(Driver.SERVERTYPE),
214: Boolean.TRUE);
215:
216: final Map infoMap = new HashMap();
217: loadDriverPropertyInfoMap(infoMap);
218:
219: final Iterator iterator = infoMap.keySet().iterator();
220: while (iterator.hasNext()) {
221: String key = (String) iterator.next();
222: DriverPropertyInfo info = (DriverPropertyInfo) infoMap
223: .get(key);
224: if (requiredTrueMap.containsKey(key)) {
225: assertTrue("The 'required' field is not true for key "
226: + key, info.required);
227: } else {
228: assertFalse(
229: "The 'required' field is not false for key "
230: + key, info.required);
231: }
232: }
233: }
234:
235: /**
236: * Retrieve the {@link DriverPropertyInfo} array from
237: * {@link Driver#getPropertyInfo(String, Properties)} and convert it
238: * into a {@link Map} using the <code>name</code> property for the keys.
239: *
240: * @param driverPropertyInfoMap The map of {@link DriverPropertyInfo} objects to be populated.
241: */
242: private void loadDriverPropertyInfoMap(
243: final Map driverPropertyInfoMap) {
244: try {
245: final DriverPropertyInfo[] driverPropertyInfoArray = new Driver()
246: .getPropertyInfo(
247: "jdbc:jtds:sqlserver://servername/databasename",
248: new Properties());
249: for (int i = 0; i < driverPropertyInfoArray.length; i++) {
250: DriverPropertyInfo driverPropertyInfo = driverPropertyInfoArray[i];
251: driverPropertyInfoMap.put(driverPropertyInfo.name,
252: driverPropertyInfo);
253: }
254: } catch (SQLException e) {
255: throw new RuntimeException(e.getMessage());
256: }
257: }
258:
259: /**
260: * Class used to test <code>Driver.setupConnectProperties(String, java.util.Properties)</code>.
261: */
262: public static class Test_Driver_setupConnectProperties extends
263: DefaultPropertiesTestLibrary {
264:
265: /**
266: * Construct a test suite for this library.
267: *
268: * @param name The name of the tests.
269: * @return The test suite.
270: */
271: public static Test suite(String name) {
272: return new TestSuite(
273: Test_Driver_setupConnectProperties.class, name);
274: }
275:
276: /**
277: * Default constructor.
278: */
279: public Test_Driver_setupConnectProperties() {
280: setTester(new DefaultPropertiesTester() {
281:
282: public void assertDefaultProperty(String message,
283: String url, Properties properties,
284: String fieldName, String key, String expected) {
285:
286: Properties results = (Properties) invokeInstanceMethod(
287: new Driver(), "setupConnectProperties",
288: new Class[] { String.class,
289: Properties.class }, new Object[] {
290: url, properties });
291:
292: assertEquals(message, expected, results
293: .getProperty(Messages.get(key)));
294: }
295: });
296: }
297: }
298:
299: /**
300: * Class used to test {@link Driver#getPropertyInfo(String, Properties)}.
301: */
302: public static class Test_Driver_getPropertyInfo extends
303: DefaultPropertiesTestLibrary {
304:
305: /**
306: * Construct a test suite for this library.
307: *
308: * @param name The name of the tests.
309: * @return The test suite.
310: */
311: public static Test suite(String name) {
312: return new TestSuite(Test_Driver_getPropertyInfo.class,
313: name);
314: }
315:
316: /**
317: * Default constructor.
318: */
319: public Test_Driver_getPropertyInfo() {
320: setTester(new DefaultPropertiesTester() {
321:
322: public void assertDefaultProperty(String message,
323: String url, Properties properties,
324: String fieldName, String key, String expected) {
325:
326: try {
327: boolean found = false;
328: String messageKey = Messages.get(key);
329:
330: DriverPropertyInfo[] infoArray = new Driver()
331: .getPropertyInfo(url, properties);
332: for (int i = 0; i < infoArray.length; i++) {
333: DriverPropertyInfo info = infoArray[i];
334: if (info.name.equals(messageKey)) {
335: assertEquals(message, expected,
336: info.value);
337: found = true;
338: }
339: }
340:
341: if (!found) {
342: fail("DriverPropertyInfo for '"
343: + messageKey + "' not found!");
344: }
345: } catch (SQLException e) {
346: throw new RuntimeException(e.getMessage());
347: }
348: }
349: });
350: }
351: }
352:
353: }
|