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.rowset; 24 25 import com.sun.rowset.RowSetFactoryImpl; 26 import java.io.File; 27 import java.net.URL; 28 import java.net.URLClassLoader; 29 import java.sql.SQLException; 30 import javax.sql.rowset.RowSetFactory; 31 import javax.sql.rowset.RowSetProvider; 32 import static org.testng.Assert.*; 33 import org.testng.annotations.AfterClass; 34 import org.testng.annotations.AfterMethod; 35 import org.testng.annotations.BeforeClass; 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 import util.BaseTest; 39 import util.StubRowSetFactory; 40 41 public class RowSetProviderTests extends BaseTest { 42 43 // Default RowSetFactory Implementation 44 private final String DEFFAULT_FACTORY_CLASSNAME = "com.sun.rowset.RowSetFactoryImpl"; 45 // Stub RowSetFactory Implementation 46 private final String STUB_FACTORY_CLASSNAME = "util.StubRowSetFactory"; 47 // Indicator that the factory implementation does not need to be checked 48 private final String NO_VALADATE_IMPL = ""; 49 // Original System property value for javax.sql.rowset.RowSetFactory 50 private static String origFactoryProperty; 51 // Original ClassLoader 52 private static ClassLoader cl; 53 // Path to the location of the jar files used by the ServiceLoader API 54 private static String jarPath; 55 56 /* 57 * Save off the original property value for javax.sql.rowset.RowSetFactory, 58 * original classloader and define the path to the jars directory 59 */ 60 @BeforeClass 61 public static void setUpClass() throws Exception { 62 origFactoryProperty = System.getProperty("javax.sql.rowset.RowSetFactory"); 63 cl = Thread.currentThread().getContextClassLoader(); 64 jarPath = System.getProperty("test.src", ".") + File.separatorChar 65 + "jars" + File.separatorChar; 66 } 67 68 /* 69 * Install the original javax.sql.rowset.RowSetFactory property value 70 */ 71 @AfterClass 72 public static void tearDownClass() throws Exception { 73 if (origFactoryProperty != null) { 74 System.setProperty("javax.sql.rowset.RowSetFactory", 75 origFactoryProperty); 76 } 77 } 78 79 /* 80 * Clear the javax.sql.rowset.RowSetFactory property value and 81 * reset the classloader to its original value 82 */ 83 @AfterMethod 84 public void tearDownMethod() throws Exception { 85 System.clearProperty("javax.sql.rowset.RowSetFactory"); 86 Thread.currentThread().setContextClassLoader(cl); 87 } 88 89 /* 90 * Validate that the correct RowSetFactory is returned by newFactory(). 91 */ 92 @Test(dataProvider = "RowSetFactoryValues") 93 public void test(RowSetFactory rsf, String impl) throws SQLException { 94 validateProvider(rsf, impl); 95 } 96 97 /* 98 * Validate that the default RowSetFactory is returned by newFactory() 99 * when specified by the javax.sql.rowset.RowSetFactory property. 100 */ 101 @Test 102 public void test01() throws SQLException { 103 System.setProperty("javax.sql.rowset.RowSetFactory", 104 DEFFAULT_FACTORY_CLASSNAME); 105 validateProvider(RowSetProvider.newFactory(), DEFFAULT_FACTORY_CLASSNAME); 106 } 107 108 /* 109 * Validate that the correct RowSetFactory is returned by newFactory() 110 * when specified by the javax.sql.rowset.RowSetFactory property. 111 */ 112 @Test(enabled = true) 113 public void test02() throws SQLException { 114 System.setProperty("javax.sql.rowset.RowSetFactory", STUB_FACTORY_CLASSNAME); 115 validateProvider(RowSetProvider.newFactory(), STUB_FACTORY_CLASSNAME); 116 } 117 118 /* 119 * Validate that a SQLException is thrown by newFactory() 120 * when specified RowSetFactory specified by the 121 * javax.sql.rowset.RowSetFactory property is not valid. 122 */ 123 @Test(expectedExceptions = SQLException.class) 124 public void test03() throws SQLException { 125 System.setProperty("javax.sql.rowset.RowSetFactory", 126 "invalid.RowSetFactoryImpl"); 127 RowSetFactory rsf = RowSetProvider.newFactory(); 128 } 129 130 /* 131 * Validate that the correct RowSetFactory is returned by newFactory() 132 * when specified by the ServiceLoader API. 133 */ 134 @Test 135 public void test04() throws Exception { 136 File f = new File(jarPath + "goodFactory"); 137 URLClassLoader loader = new URLClassLoader(new URL[]{ 138 new URL(f.toURI().toString())}, getClass().getClassLoader()); 139 Thread.currentThread().setContextClassLoader(loader); 140 validateProvider(RowSetProvider.newFactory(), STUB_FACTORY_CLASSNAME); 141 } 142 143 /* 144 * Validate that a SQLException is thrown by newFactory() if the default 145 * RowSetFactory specified by the ServiceLoader API is not valid 146 */ 147 @Test(expectedExceptions = SQLException.class) 148 public void test05() throws Exception { 149 File f = new File(jarPath + "badFactory"); 150 URLClassLoader loader = new URLClassLoader(new URL[]{ 151 new URL(f.toURI().toString())}, getClass().getClassLoader()); 152 Thread.currentThread().setContextClassLoader(loader); 153 RowSetProvider.newFactory(); 154 } 155 156 /* 157 * Utility Method to validate that the RowsetFactory returned from 158 * RowSetProvider.newFactory() is correct 159 */ 160 private void validateProvider(RowSetFactory rsf, String implName) { 161 assertNotNull(rsf, "RowSetFactory should not be null"); 162 switch (implName) { 163 case DEFFAULT_FACTORY_CLASSNAME: 164 assertTrue(rsf instanceof RowSetFactoryImpl); 165 break; 166 case STUB_FACTORY_CLASSNAME: 167 assertTrue(rsf instanceof StubRowSetFactory); 168 break; 169 default: 170 } 171 } 172 173 /* 174 * DataProvider used to provide a RowSetFactory and the expected 175 * RowSetFactory implementation that should be returned 176 */ 177 @DataProvider(name = "RowSetFactoryValues") 178 private Object[][] RowSetFactoryValues() throws SQLException { 179 RowSetFactory rsf = RowSetProvider.newFactory(); 180 RowSetFactory rsf1 = RowSetProvider.newFactory(STUB_FACTORY_CLASSNAME, null); 181 RowSetFactory rsf2 = RowSetProvider.newFactory(DEFFAULT_FACTORY_CLASSNAME, null); 182 return new Object[][]{ 183 {rsf, NO_VALADATE_IMPL}, 184 {rsf, DEFFAULT_FACTORY_CLASSNAME}, 185 {rsf1, STUB_FACTORY_CLASSNAME}, 186 {rsf2, DEFFAULT_FACTORY_CLASSNAME} 187 }; 188 } 189 }