1 /*
2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.sql;
24 
25 import java.io.BufferedReader;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.CharArrayReader;
29 import java.io.CharArrayWriter;
30 import java.io.File;
31 import java.io.InputStreamReader;
32 import java.io.PrintStream;
33 import java.io.PrintWriter;
34 import java.sql.Driver;
35 import java.sql.DriverManager;
36 import java.sql.SQLException;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.Properties;
40 import java.util.stream.Collectors;
41 
42 import static org.testng.Assert.*;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.AfterMethod;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
48 import util.StubDriver;
49 
50 public class DriverManagerTests {
51 
52     private final String StubDriverURL = "jdbc:tennis:boy";
53     private final String StubDriverDAURL = "jdbc:luckydog:tennis";
54     private final String InvalidURL = "jdbc:cardio:tennis";
55     private String[] results = {"output", "more output", "and more", "the end"};
56     private String noOutput = "should not find this";
57 
58     public DriverManagerTests() {
59     }
60 
61     @BeforeClass
62     public static void setUpClass() throws Exception {
63     }
64 
65     @AfterClass
66     public static void tearDownClass() throws Exception {
67     }
68 
69     @BeforeMethod
70     public void setUpMethod() throws Exception {
71         removeAllDrivers();
72     }
73 
74     @AfterMethod
75     public void tearDownMethod() throws Exception {
76     }
77 
78     /**
79      * Utility method to remove all registered drivers
80      */
81     private static void removeAllDrivers() {
82         java.util.Enumeration e = DriverManager.getDrivers();
83         while (e.hasMoreElements()) {
84             try {
85                 DriverManager.deregisterDriver((Driver) (e.nextElement()));
86             } catch (SQLException ex) {
87                 System.out.print(ex.getMessage());
88             }
89         }
90     }
91 
92     /**
93      * Utility method to see if a driver is registered
94      */
95     private boolean isDriverRegistered(Driver d) {
96         boolean foundDriver = false;
97         java.util.Enumeration e = DriverManager.getDrivers();
98         while (e.hasMoreElements()) {
99             if (d == (Driver) e.nextElement()) {
100                 foundDriver = true;
101                 break;
102             }
103         }
104         return foundDriver;
105     }
106 
107     /**
108      * Validate that values set using setLoginTimeout will be returned by
109      * getLoginTimeout
110      */
111     @Test
112     public void test() {
113         int[] vals = {-1, 0, 5};
114         for (int val : vals) {
115             DriverManager.setLoginTimeout(val);
116             assertEquals(val, DriverManager.getLoginTimeout());
117         }
118     }
119 
120     /**
121      * Validate that NullPointerException is thrown when null is passed to
122      * registerDriver
123      */
124     @Test(expectedExceptions = NullPointerException.class)
125     public void test1() throws Exception {
126         Driver d = null;
127         DriverManager.registerDriver(d);
128     }
129 
130     /**
131      * Validate that NullPointerException is thrown when null is passed to
132      * registerDriver
133      */
134     @Test(expectedExceptions = NullPointerException.class)
135     public void test2() throws Exception {
136         Driver d = null;
137         DriverManager.registerDriver(d, null);
138     }
139 
140     /**
141      * Validate that a null value allows for deRegisterDriver to return
142      */
143     @Test
144     public void test3() throws Exception {
145         DriverManager.deregisterDriver(null);
146 
147     }
148 
149     /**
150      * Validate that SQLException is thrown when there is no Driver to service
151      * the URL
152      */
153     @Test(expectedExceptions = SQLException.class)
154     public void test4() throws Exception {
155         DriverManager.getConnection(InvalidURL);
156     }
157 
158     /**
159      * Validate that SQLException is thrown when there is no Driver to service
160      * the URL
161      */
162     @Test(expectedExceptions = SQLException.class)
163     public void test5() throws Exception {
164         DriverManager.getConnection(InvalidURL, new Properties());
165     }
166 
167     /**
168      * Validate that SQLException is thrown when there is no Driver to service
169      * the URL
170      */
171     @Test(expectedExceptions = SQLException.class)
172     public void test6() throws Exception {
173         DriverManager.getConnection(InvalidURL, "LuckyDog", "tennisanyone");
174     }
175 
176     /**
177      * Validate that SQLException is thrown when null is passed for the URL
178      */
179     @Test(expectedExceptions = SQLException.class)
180     public void test7() throws Exception {
181         DriverManager.getConnection(null);
182     }
183 
184     /**
185      * Validate that SQLException is thrown when null is passed for the URL
186      */
187     @Test(expectedExceptions = SQLException.class)
188     public void test8() throws Exception {
189         DriverManager.getConnection(null, new Properties());
190     }
191 
192     /**
193      * Validate that SQLException is thrown when null is passed for the URL
194      */
195     @Test(expectedExceptions = SQLException.class)
196     public void test9() throws Exception {
197         DriverManager.getConnection(null, "LuckyDog", "tennisanyone");
198     }
199 
200     /**
201      * Validate that SQLException is thrown when there is no Driver to service
202      * the URL
203      */
204     @Test(expectedExceptions = SQLException.class)
205     public void test10() throws Exception {
206         DriverManager.getDriver(InvalidURL);
207     }
208 
209     /**
210      * Validate that SQLException is thrown when null is passed for the URL
211      */
212     @Test(expectedExceptions = SQLException.class)
213     public void test11() throws Exception {
214         DriverManager.getDriver(null);
215     }
216 
217     /**
218      * Validate that a non-null Driver is returned by getDriver when a valid URL
219      * is specified
220      */
221     @Test
222     public void test12() throws Exception {
223 
224         DriverManager.registerDriver(new StubDriver());
225         assertTrue(DriverManager.getDriver(StubDriverURL) != null);
226     }
227 
228     /**
229      * Validate that SQLException is thrown when the URL is not valid for any of
230      * the registered drivers
231      */
232     @Test(expectedExceptions = SQLException.class)
233     public void test13() throws Exception {
234         DriverManager.registerDriver(new StubDriver());
235         DriverManager.getDriver(InvalidURL);
236     }
237 
238     /**
239      * Validate that a Connection object is returned when a valid URL is
240      * specified to getConnection
241      *
242      */
243     @Test
244     public void test14() throws Exception {
245 
246         DriverManager.registerDriver(new StubDriver());
247         assertTrue(
248                 DriverManager.getConnection(StubDriverURL) != null);
249         assertTrue(DriverManager.getConnection(StubDriverURL,
250                 "LuckyDog", "tennisanyone") != null);
251         Properties props = new Properties();
252         props.put("user", "LuckyDog");
253         props.put("password", "tennisanyone");
254         assertTrue(
255                 DriverManager.getConnection(StubDriverURL,
256                         props) != null);
257     }
258 
259     /**
260      * Register a driver and make sure you find it via its URL. Deregister the
261      * driver and validate it is not longer registered
262      *
263      * @throws Exception
264      */
265     @Test()
266     public void test15() throws Exception {
267         DriverManager.registerDriver(new StubDriver());
268         Driver d = DriverManager.getDriver(StubDriverURL);
269         assertTrue(d != null);
270         assertTrue(isDriverRegistered(d));
271         DriverManager.deregisterDriver(d);
272         assertFalse(isDriverRegistered(d));
273     }
274 
275     /**
276      * Validate that DriverAction.release is called when a driver is registered
277      * via registerDriver(Driver, DriverAction)
278      *
279      * @throws Exception
280      */
281     @Test
282     public void test16() throws Exception {
283         File file = new File(util.StubDriverDA.DriverActionCalled);
284         file.delete();
285         assertFalse(file.exists());
286         Driver d = null;
287         Class.forName("util.StubDriverDA");
288         d = DriverManager.getDriver(StubDriverDAURL);
289         DriverManager.deregisterDriver(d);
290         assertFalse(isDriverRegistered(d), "Driver is registered");
291         assertTrue(file.exists());
292     }
293 
294     /**
295      * Create a PrintStream and use to send output via DriverManager.println
296      * Validate that if you disable the stream, the output sent is not present
297      */
298     @Test
299     public void tests17() throws Exception {
300         ByteArrayOutputStream os = new ByteArrayOutputStream();
301         PrintStream ps = new PrintStream(os);
302         DriverManager.setLogStream(ps);
303         assertTrue(DriverManager.getLogStream() == ps);
304 
305         DriverManager.println(results[0]);
306         DriverManager.setLogStream((PrintStream) null);
307         assertTrue(DriverManager.getLogStream() == null);
308         DriverManager.println(noOutput);
309         DriverManager.setLogStream(ps);
310         DriverManager.println(results[1]);
311         DriverManager.println(results[2]);
312         DriverManager.println(results[3]);
313         DriverManager.setLogStream((PrintStream) null);
314         DriverManager.println(noOutput);
315 
316         /*
317          * Check we do not get the output when the stream is disabled
318          */
319         InputStreamReader is
320                 = new InputStreamReader(new ByteArrayInputStream(os.toByteArray()));
321         BufferedReader reader = new BufferedReader(is);
322         for (String result : results) {
323             assertTrue(result.equals(reader.readLine()));
324         }
325     }
326 
327     /**
328      * Create a PrintWriter and use to to send output via DriverManager.println
329      * Validate that if you disable the writer, the output sent is not present
330      */
331     @Test
332     public void tests18() throws Exception {
333         CharArrayWriter cw = new CharArrayWriter();
334         PrintWriter pw = new PrintWriter(cw);
335         DriverManager.setLogWriter(pw);
336         assertTrue(DriverManager.getLogWriter() == pw);
337 
338         DriverManager.println(results[0]);
339         DriverManager.setLogWriter(null);
340         assertTrue(DriverManager.getLogWriter() == null);
341         DriverManager.println(noOutput);
342         DriverManager.setLogWriter(pw);
343         DriverManager.println(results[1]);
344         DriverManager.println(results[2]);
345         DriverManager.println(results[3]);
346         DriverManager.setLogWriter(null);
347         DriverManager.println(noOutput);
348 
349         /*
350          * Check we do not get the output when the stream is disabled
351          */
352         BufferedReader reader
353                 = new BufferedReader(new CharArrayReader(cw.toCharArray()));
354         for (String result : results) {
355             assertTrue(result.equals(reader.readLine()));
356         }
357     }
358 
359     /**
360      * Register some driver implementations and validate that the driver
361      * elements covered by the Enumeration obtained from
362      * {@link DriverManager#getDrivers()} are the same as driver elements
363      * covered by the stream obtained from {@link DriverManager#drivers()}}
364      */
365     @Test
366     public void tests19() throws Exception {
367         int n = 8;
368         for (int i = 0; i < n; i++) {
369             DriverManager.registerDriver(new StubDriver());
370         }
371 
372         Collection<Driver> expectedDrivers = Collections.list(DriverManager.getDrivers());
373         assertEquals(expectedDrivers.size(), n);
374         Collection<Driver> drivers = DriverManager.drivers().collect(Collectors.toList());
375 
376         assertEquals(drivers, expectedDrivers);
377     }
378 }
379