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.Properties;
  38 import static org.testng.Assert.*;
  39 import org.testng.annotations.AfterClass;
  40 import org.testng.annotations.AfterMethod;
  41 import org.testng.annotations.BeforeClass;
  42 import org.testng.annotations.BeforeMethod;
  43 import org.testng.annotations.Test;
  44 import util.StubDriver;
  45 
  46 public class DriverManagerTests {
  47 
  48     private final String StubDriverURL = "jdbc:tennis:boy";
  49     private final String StubDriverDAURL = "jdbc:luckydog:tennis";
  50     private final String InvalidURL = "jdbc:cardio:tennis";
  51     private String[] results = {"output", "more output", "and more", "the end"};
  52     private String noOutput = "should not find this";
  53 
  54     public DriverManagerTests() {
  55     }
  56 
  57     @BeforeClass
  58     public static void setUpClass() throws Exception {
  59     }
  60 
  61     @AfterClass
  62     public static void tearDownClass() throws Exception {
  63     }
  64 
  65     @BeforeMethod
  66     public void setUpMethod() throws Exception {
  67         removeAllDrivers();
  68     }
  69 
  70     @AfterMethod
  71     public void tearDownMethod() throws Exception {
  72     }
  73 
  74     /**
  75      * Utility method to remove all registered drivers
  76      */
  77     private static void removeAllDrivers() {
  78         java.util.Enumeration e = DriverManager.getDrivers();
  79         while (e.hasMoreElements()) {
  80             try {
  81                 DriverManager.deregisterDriver((Driver) (e.nextElement()));
  82             } catch (SQLException ex) {
  83                 System.out.print(ex.getMessage());
  84             }
  85         }
  86     }
  87 
  88     /**
  89      * Utility method to see if a driver is registered
  90      */
  91     private boolean isDriverRegistered(Driver d) {
  92         boolean foundDriver = false;
  93         java.util.Enumeration e = DriverManager.getDrivers();
  94         while (e.hasMoreElements()) {
  95             if (d == (Driver) e.nextElement()) {
  96                 foundDriver = true;
  97                 break;
  98             }
  99         }
 100         return foundDriver;
 101     }
 102 
 103     /**
 104      * Validate that values set using setLoginTimeout will be returned by
 105      * getLoginTimeout
 106      */
 107     @Test
 108     public void test() {
 109         int[] vals = {-1, 0, 5};
 110         for (int val : vals) {
 111             DriverManager.setLoginTimeout(val);
 112             assertEquals(val, DriverManager.getLoginTimeout());
 113         }
 114     }
 115 
 116     /**
 117      * Validate that NullPointerException is thrown when null is passed to
 118      * registerDriver
 119      */
 120     @Test(expectedExceptions = NullPointerException.class)
 121     public void test1() throws Exception {
 122         Driver d = null;
 123         DriverManager.registerDriver(d);
 124     }
 125 
 126     /**
 127      * Validate that NullPointerException is thrown when null is passed to
 128      * registerDriver
 129      */
 130     @Test(expectedExceptions = NullPointerException.class)
 131     public void test2() throws Exception {
 132         Driver d = null;
 133         DriverManager.registerDriver(d, null);
 134     }
 135 
 136     /**
 137      * Validate that a null value allows for deRegisterDriver to return
 138      */
 139     @Test
 140     public void test3() throws Exception {
 141         DriverManager.deregisterDriver(null);
 142 
 143     }
 144 
 145     /**
 146      * Validate that SQLException is thrown when there is no Driver to service
 147      * the URL
 148      */
 149     @Test(expectedExceptions = SQLException.class)
 150     public void test4() throws Exception {
 151         DriverManager.getConnection(InvalidURL);
 152     }
 153 
 154     /**
 155      * Validate that SQLException is thrown when there is no Driver to service
 156      * the URL
 157      */
 158     @Test(expectedExceptions = SQLException.class)
 159     public void test5() throws Exception {
 160         DriverManager.getConnection(InvalidURL, new Properties());
 161     }
 162 
 163     /**
 164      * Validate that SQLException is thrown when there is no Driver to service
 165      * the URL
 166      */
 167     @Test(expectedExceptions = SQLException.class)
 168     public void test6() throws Exception {
 169         DriverManager.getConnection(InvalidURL, "LuckyDog", "tennisanyone");
 170     }
 171 
 172     /**
 173      * Validate that SQLException is thrown when null is passed for the URL
 174      */
 175     @Test(expectedExceptions = SQLException.class)
 176     public void test7() throws Exception {
 177         DriverManager.getConnection(null);
 178     }
 179 
 180     /**
 181      * Validate that SQLException is thrown when null is passed for the URL
 182      */
 183     @Test(expectedExceptions = SQLException.class)
 184     public void test8() throws Exception {
 185         DriverManager.getConnection(null, new Properties());
 186     }
 187 
 188     /**
 189      * Validate that SQLException is thrown when null is passed for the URL
 190      */
 191     @Test(expectedExceptions = SQLException.class)
 192     public void test9() throws Exception {
 193         DriverManager.getConnection(null, "LuckyDog", "tennisanyone");
 194     }
 195 
 196     /**
 197      * Validate that SQLException is thrown when there is no Driver to service
 198      * the URL
 199      */
 200     @Test(expectedExceptions = SQLException.class)
 201     public void test10() throws Exception {
 202         DriverManager.getDriver(InvalidURL);
 203     }
 204 
 205     /**
 206      * Validate that SQLException is thrown when null is passed for the URL
 207      */
 208     @Test(expectedExceptions = SQLException.class)
 209     public void test11() throws Exception {
 210         DriverManager.getDriver(null);
 211     }
 212 
 213     /**
 214      * Validate that a non-null Driver is returned by getDriver when a valid URL
 215      * is specified
 216      */
 217     @Test
 218     public void test12() throws Exception {
 219 
 220         DriverManager.registerDriver(new StubDriver());
 221         assertTrue(DriverManager.getDriver(StubDriverURL) != null);
 222     }
 223 
 224     /**
 225      * Validate that SQLException is thrown when the URL is not valid for any of
 226      * the registered drivers
 227      */
 228     @Test(expectedExceptions = SQLException.class)
 229     public void test13() throws Exception {
 230         DriverManager.registerDriver(new StubDriver());
 231         DriverManager.getDriver(InvalidURL);
 232     }
 233 
 234     /**
 235      * Validate that a Connection object is returned when a valid URL is
 236      * specified to getConnection
 237      *
 238      */
 239     @Test
 240     public void test14() throws Exception {
 241 
 242         DriverManager.registerDriver(new StubDriver());
 243         assertTrue(
 244                 DriverManager.getConnection(StubDriverURL) != null);
 245         assertTrue(DriverManager.getConnection(StubDriverURL,
 246                 "LuckyDog", "tennisanyone") != null);
 247         Properties props = new Properties();
 248         props.put("user", "LuckyDog");
 249         props.put("password", "tennisanyone");
 250         assertTrue(
 251                 DriverManager.getConnection(StubDriverURL,
 252                         props) != null);
 253     }
 254 
 255     /**
 256      * Register a driver and make sure you find it via its URL. Deregister the
 257      * driver and validate it is not longer registered
 258      *
 259      * @throws Exception
 260      */
 261     @Test()
 262     public void test15() throws Exception {
 263         DriverManager.registerDriver(new StubDriver());
 264         Driver d = DriverManager.getDriver(StubDriverURL);
 265         assertTrue(d != null);
 266         assertTrue(isDriverRegistered(d));
 267         DriverManager.deregisterDriver(d);
 268         assertFalse(isDriverRegistered(d));
 269     }
 270 
 271     /**
 272      * Validate that DriverAction.release is called when a driver is registered
 273      * via registerDriver(Driver, DriverAction)
 274      *
 275      * @throws Exception
 276      */
 277     @Test
 278     public void test16() throws Exception {
 279         File file = new File(util.StubDriverDA.DriverActionCalled);
 280         file.delete();
 281         assertFalse(file.exists());
 282         Driver d = null;
 283         Class.forName("util.StubDriverDA");
 284         d = DriverManager.getDriver(StubDriverDAURL);
 285         DriverManager.deregisterDriver(d);
 286         assertFalse(isDriverRegistered(d), "Driver is registered");
 287         assertTrue(file.exists());
 288     }
 289 
 290     /**
 291      * Create a PrintStream and use to send output via DriverManager.println
 292      * Validate that if you disable the stream, the output sent is not present
 293      */
 294     @Test
 295     public void tests17() throws Exception {
 296         ByteArrayOutputStream os = new ByteArrayOutputStream();
 297         PrintStream ps = new PrintStream(os);
 298         DriverManager.setLogStream(ps);
 299         assertTrue(DriverManager.getLogStream() == ps);
 300 
 301         DriverManager.println(results[0]);
 302         DriverManager.setLogStream((PrintStream) null);
 303         assertTrue(DriverManager.getLogStream() == null);
 304         DriverManager.println(noOutput);
 305         DriverManager.setLogStream(ps);
 306         DriverManager.println(results[1]);
 307         DriverManager.println(results[2]);
 308         DriverManager.println(results[3]);
 309         DriverManager.setLogStream((PrintStream) null);
 310         DriverManager.println(noOutput);
 311 
 312         /*
 313          * Check we do not get the output when the stream is disabled
 314          */
 315         InputStreamReader is
 316                 = new InputStreamReader(new ByteArrayInputStream(os.toByteArray()));
 317         BufferedReader reader = new BufferedReader(is);
 318         for (String result : results) {
 319             assertTrue(result.equals(reader.readLine()));
 320         }
 321     }
 322 
 323     /**
 324      * Create a PrintWriter and use to to send output via DriverManager.println
 325      * Validate that if you disable the writer, the output sent is not present
 326      */
 327     @Test
 328     public void tests18() throws Exception {
 329         CharArrayWriter cw = new CharArrayWriter();
 330         PrintWriter pw = new PrintWriter(cw);
 331         DriverManager.setLogWriter(pw);
 332         assertTrue(DriverManager.getLogWriter() == pw);
 333 
 334         DriverManager.println(results[0]);
 335         DriverManager.setLogWriter(null);
 336         assertTrue(DriverManager.getLogWriter() == null);
 337         DriverManager.println(noOutput);
 338         DriverManager.setLogWriter(pw);
 339         DriverManager.println(results[1]);
 340         DriverManager.println(results[2]);
 341         DriverManager.println(results[3]);
 342         DriverManager.setLogWriter(null);
 343         DriverManager.println(noOutput);
 344 
 345         /*
 346          * Check we do not get the output when the stream is disabled
 347          */
 348         BufferedReader reader
 349                 = new BufferedReader(new CharArrayReader(cw.toCharArray()));
 350         for (String result : results) {
 351             assertTrue(result.equals(reader.readLine()));
 352         }
 353     }
 354 }