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      */
  59     @BeforeClass
  60     public static void setUpClass() throws Exception {
  61         origFactoryProperty = System.getProperty("javax.sql.rowset.RowSetFactory");
  62         cl = Thread.currentThread().getContextClassLoader();
  63         jarPath = System.getProperty("test.src", ".") + File.separatorChar
  64                 + "jars" +  File.separatorChar;
  65     }
  66 
  67     /*
  68      * Install the original javax.sql.rowset.RowSetFactory property value
  69      */
  70     @AfterClass
  71     public static void tearDownClass() throws Exception {
  72         if (origFactoryProperty != null) {
  73             System.setProperty("javax.sql.rowset.RowSetFactory",
  74                     origFactoryProperty);
  75         }
  76     }
  77 
  78     /*
  79      * Clear the javax.sql.rowset.RowSetFactory property value
  80      */
  81     @AfterMethod
  82     public void tearDownMethod() throws Exception {
  83         System.clearProperty("javax.sql.rowset.RowSetFactory");
  84         Thread.currentThread().setContextClassLoader(cl);
  85     }
  86 
  87     /*
  88      * Validate that the correct RowSetFactory is returned by newFactory().
  89      */
  90     @Test(dataProvider = "RowSetFactoryValues")
  91     public void test(RowSetFactory rsf, String impl) throws SQLException {
  92         validateProvider(rsf, impl);
  93     }
  94 
  95     /*
  96      * Validate that the default RowSetFactory is returned by newFactory()
  97      * when specified by the javax.sql.rowset.RowSetFactory property.
  98      */
  99     @Test
 100     public void test01() throws SQLException {
 101         System.setProperty("javax.sql.rowset.RowSetFactory",
 102                 DEFFAULT_FACTORY_CLASSNAME);
 103         validateProvider(RowSetProvider.newFactory(), DEFFAULT_FACTORY_CLASSNAME);
 104     }
 105 
 106     /*
 107      * Validate that the correct RowSetFactory is returned by newFactory()
 108      * when specified by the javax.sql.rowset.RowSetFactory property.
 109      */
 110     @Test
 111     public void test2() throws SQLException {
 112         System.setProperty("javax.sql.rowset.RowSetFactory", STUB_FACTORY_CLASSNAME);
 113         validateProvider(RowSetProvider.newFactory(), STUB_FACTORY_CLASSNAME);
 114     }
 115 
 116     /*
 117      * Validate that a SQLException is thrown by newFactory()
 118      * when specified  RowSetFactory specified by the
 119      * javax.sql.rowset.RowSetFactory property is not valid.
 120      */
 121     @Test(expectedExceptions = SQLException.class)
 122     public void test3() throws SQLException {
 123         System.setProperty("javax.sql.rowset.RowSetFactory",
 124                 "invalid.RowSetFactoryImpl");
 125         RowSetFactory rsf = RowSetProvider.newFactory();
 126     }
 127 
 128     /*
 129      * Validate that the correct RowSetFactory is returned by newFactory()
 130      * when specified by the ServiceLoader API.
 131      */
 132     @Test
 133     public void test4() throws Exception {
 134         File f = new File(jarPath + "goodFactory.jar");
 135         URLClassLoader loader = new URLClassLoader(new URL[]{
 136             new URL(f.toURI().toString())}, getClass().getClassLoader());
 137         Thread.currentThread().setContextClassLoader(loader);
 138         validateProvider(RowSetProvider.newFactory(), STUB_FACTORY_CLASSNAME);
 139     }
 140 
 141     /*
 142      * Validate that a SQLException is thrown by newFactory() if the default
 143      *RowSetFactory specified by the ServlceLoader API is not valid
 144      */
 145     @Test(expectedExceptions = SQLException.class)
 146     public void test5() throws Exception {
 147         File f = new File(jarPath + "badFactory.jar");
 148         URLClassLoader loader = new URLClassLoader(new URL[]{
 149             new URL(f.toURI().toString())}, getClass().getClassLoader());
 150         Thread.currentThread().setContextClassLoader(loader);
 151         RowSetProvider.newFactory();
 152 
 153     }
 154 
 155     /*
 156      * Utility Method to validate that the RowsetFactory returned from
 157      * RowSetProvider.newFactory() is correct
 158      */
 159     private void validateProvider(RowSetFactory rsf, String implName) {
 160         assertNotNull(rsf, "RowSetFactory should not be null");
 161         switch (implName) {
 162             case DEFFAULT_FACTORY_CLASSNAME:
 163                 assertTrue(rsf instanceof RowSetFactoryImpl);
 164                 break;
 165             case STUB_FACTORY_CLASSNAME:
 166                 assertTrue(rsf instanceof StubRowSetFactory);
 167                 break;
 168             default:
 169         }
 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 }