Path: blob/master/test/jdk/java/sql/driverModuleTests/DriverManagerModuleTests.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.sql.Connection;24import java.sql.Driver;25import java.sql.DriverManager;26import java.sql.SQLException;27import static org.testng.Assert.*;28import org.testng.annotations.AfterClass;29import org.testng.annotations.AfterMethod;30import org.testng.annotations.BeforeClass;31import org.testng.annotations.BeforeMethod;32import org.testng.annotations.Test;3334/*35* @test36* @library /java/sql/modules37* @build luckydogdriver/* mystubdriver/*38* @run testng/othervm DriverManagerModuleTests39* @summary Tests that a JDBC Driver that is a module can be loaded40* via the service-provider loading mechanism.41*/42public class DriverManagerModuleTests {4344private final String LUCKYDOGDRIVER_URL = "jdbc:tennis:myDB";45private static final String STUBDRIVERURL = "jdbc:stub:myDB";46private static final String CONNECTION_CLASS_NAME = "com.luckydogtennis.StubConnection";4748@BeforeClass49public static void setUpClass() throws Exception {50}5152@AfterClass53public static void tearDownClass() throws Exception {54}5556@BeforeMethod57public void setUpMethod() throws Exception {58}5960@AfterMethod61public void tearDownMethod() throws Exception {62}6364/**65* Validate JDBC drivers as modules will be accessible. One driver will be66* loaded and registered via the service-provider loading mechanism. The67* other driver will need to be explictly loaded68*69* @throws java.lang.Exception70*/71@Test72public void test() throws Exception {73System.out.println("\n$$$ runing Test()\n");74dumpRegisteredDrivers();75Driver d = DriverManager.getDriver(STUBDRIVERURL);76assertNotNull(d, "StubDriver should not be null");77assertTrue(isDriverRegistered(d));78Driver d2 = null;7980// This driver should not be found until it is explictly loaded81try {82d2 = DriverManager.getDriver(LUCKYDOGDRIVER_URL);83} catch (SQLException e) {84// ignore expected Exception85}86assertNull(d2, "LuckyDogDriver should be null");87loadDriver();88d2 = DriverManager.getDriver(LUCKYDOGDRIVER_URL);89assertNotNull(d2, "LuckyDogDriver should not be null");90assertTrue(isDriverRegistered(d2), "Driver was NOT registered");9192dumpRegisteredDrivers();93DriverManager.deregisterDriver(d2);94assertFalse(isDriverRegistered(d2), "Driver IS STILL registered");95dumpRegisteredDrivers();9697}9899/**100* Validate that a Connection can be obtained from a JDBC driver which is a101* module and loaded via the service-provider loading mechanism.102*103* @throws java.lang.Exception104*/105@Test106public void test00() throws Exception {107System.out.println("\n$$$ runing Test00()\n");108Connection con = DriverManager.getConnection(STUBDRIVERURL);109assertNotNull(con, "Returned Connection should not be NULL");110System.out.println("con=" + con.getClass().getName());111assertTrue(con.getClass().getName().equals(CONNECTION_CLASS_NAME));112113}114115/**116* Utility method to see if a driver is registered117*/118private static void dumpRegisteredDrivers() {119System.out.println("\n+++ Loaded Drivers +++");120121DriverManager.drivers().forEach(d -> System.out.println("\t\t### Driver:" + d));122123System.out.println("++++++++++++++++++++++++");124}125126/**127* Utility method to load the LuckyDogDriver128*/129private static void loadDriver() {130try {131Class.forName("luckydogtennis.LuckyDogDriver");132} catch (ClassNotFoundException ex) {133System.out.println("**** Error: luckydogtennis.LuckyDogDriver not found");134}135System.out.println("Driver Loaded");136}137138/**139* Utility method to see if a driver is registered140*/141private static boolean isDriverRegistered(Driver d) {142return DriverManager.drivers().filter(driver-> driver == d).findFirst().isPresent();143144}145}146147148