1 /*
   2  * Copyright (c) 2017, 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 
  24 import java.sql.Connection;
  25 import java.sql.Driver;
  26 import java.sql.DriverManager;
  27 import java.sql.SQLException;
  28 import static org.testng.Assert.*;
  29 import org.testng.annotations.AfterClass;
  30 import org.testng.annotations.AfterMethod;
  31 import org.testng.annotations.BeforeClass;
  32 import org.testng.annotations.BeforeMethod;
  33 import org.testng.annotations.Test;
  34 
  35 /*
  36  * @test
  37  * @library /java/sql/modules
  38  * @build luckydogdriver/* mystubdriver/*
  39  * @run testng/othervm DriverManagerModuleTests
  40  * @summary Tests that a JDBC Driver that is a module can be loaded
  41  * via the service-provider loading mechanism.
  42  */
  43 public class DriverManagerModuleTests {
  44 
  45     private final String LUCKYDOGDRIVER_URL = "jdbc:tennis:myDB";
  46     private static final String STUBDRIVERURL = "jdbc:stub:myDB";
  47     private static final String CONNECTION_CLASS_NAME = "com.luckydogtennis.StubConnection";
  48 
  49     @BeforeClass
  50     public static void setUpClass() throws Exception {
  51     }
  52 
  53     @AfterClass
  54     public static void tearDownClass() throws Exception {
  55     }
  56 
  57     @BeforeMethod
  58     public void setUpMethod() throws Exception {
  59     }
  60 
  61     @AfterMethod
  62     public void tearDownMethod() throws Exception {
  63     }
  64 
  65     /**
  66      * Validate JDBC drivers as modules will be accessible. One driver will be
  67      * loaded and registered via the service-provider loading mechanism. The
  68      * other driver will need to be explictly loaded
  69      *
  70      * @throws java.lang.Exception
  71      */
  72     @Test
  73     public void test() throws Exception {
  74         System.out.println("\n$$$ runing Test()\n");
  75         dumpRegisteredDrivers();
  76         Driver d = DriverManager.getDriver(STUBDRIVERURL);
  77         assertNotNull(d, "StubDriver should not be null");
  78         assertTrue(isDriverRegistered(d));
  79         Driver d2 = null;
  80 
  81         // This driver should not be found until it is explictly loaded
  82         try {
  83             DriverManager.getDriver(LUCKYDOGDRIVER_URL);
  84         } catch (SQLException e) {
  85             // ignore expected Exception
  86         }
  87         assertNull(d2, "LuckyDogDriver should  be null");
  88         loadDriver();
  89         d2 = DriverManager.getDriver(LUCKYDOGDRIVER_URL);
  90         assertNotNull(d2, "LuckyDogDriver should not be null");
  91         assertTrue(isDriverRegistered(d2), "Driver was NOT registered");
  92 
  93         dumpRegisteredDrivers();
  94         DriverManager.deregisterDriver(d2);
  95         assertFalse(isDriverRegistered(d2), "Driver IS STILL registered");
  96         dumpRegisteredDrivers();
  97 
  98     }
  99 
 100     /**
 101      * Validate that a Connection can be obtained from a JDBC driver which is a
 102      * module and loaded via the service-provider loading mechanism.
 103      *
 104      * @throws java.lang.Exception
 105      */
 106     @Test
 107     public void test00() throws Exception {
 108         System.out.println("\n$$$ runing Test00()\n");
 109         Connection con = DriverManager.getConnection(STUBDRIVERURL);
 110         assertNotNull(con, "Returned Connection should not be NULL");
 111         System.out.println("con=" + con.getClass().getName());
 112         assertTrue(con.getClass().getName().equals(CONNECTION_CLASS_NAME));
 113 
 114     }
 115 
 116     /**
 117      * Utility method to see if a driver is registered
 118      */
 119     private static void dumpRegisteredDrivers() {
 120         System.out.println("\n+++ Loaded Drivers +++");
 121 
 122          DriverManager.drivers().forEach(d -> System.out.println("\t\t### Driver:" + d));
 123 
 124         System.out.println("++++++++++++++++++++++++");
 125     }
 126 
 127     /**
 128      * Utility method to load the LuckyDogDriver
 129      */
 130     private static void loadDriver() {
 131         try {
 132             Class.forName("luckydogtennis.LuckyDogDriver");
 133         } catch (ClassNotFoundException ex) {
 134             System.out.println("**** Error: luckydogtennis.LuckyDogDriver not found");
 135         }
 136         System.out.println("Driver Loaded");
 137     }
 138 
 139     /**
 140      * Utility method to see if a driver is registered
 141      */
 142     private static boolean isDriverRegistered(Driver d) {
 143         return DriverManager.drivers().filter(driver-> driver == d).findFirst().isPresent();
 144 
 145     }
 146 }