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.spi;
24 
25 import com.sun.rowset.providers.RIOptimisticProvider;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import javax.naming.Context;
33 import javax.sql.rowset.spi.SyncFactory;
34 import javax.sql.rowset.spi.SyncFactoryException;
35 import javax.sql.rowset.spi.SyncProvider;
36 import static org.testng.Assert.*;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
39 import util.PropertyStubProvider;
40 import util.StubSyncProvider;
41 import util.StubContext;
42 
43 //com.sun.jndi.ldap.LdapCtxFactory
44 public class SyncFactoryTests {
45     private static String origFactory;
46 
47     private final String stubProvider = "util.StubSyncProvider";
48     private final String propertyStubProvider = "util.PropertyStubProvider";
49     private final Logger alogger = Logger.getLogger(this.getClass().getName());
50     // Initial providers including those set via a property
51     List<String> initialProviders;
52     // All providers including those specifically registered
53     List<String> allProviders;
54     private  Context ctx= null;
55 
SyncFactoryTests()56     public SyncFactoryTests() {
57 
58         // Add a provider via a property
59         System.setProperty("rowset.provider.classname", propertyStubProvider);
60         initialProviders = Arrays.asList(
61                 "com.sun.rowset.providers.RIOptimisticProvider",
62                 "com.sun.rowset.providers.RIXMLProvider",
63                 propertyStubProvider);
64         allProviders = new ArrayList<>();
65         allProviders.addAll(initialProviders);
66         allProviders.add(stubProvider);
67         ctx = new StubContext();
68     }
69 
70     @BeforeMethod
setUpMethod()71     public void setUpMethod() throws Exception {
72         // Make sure the provider provider that is registered is removed
73         // before each run
74         SyncFactory.unregisterProvider(stubProvider);
75     }
76 
77     /*
78      * Validate a non-null factory is returned
79      */
80     @Test
test()81     public void test() throws SyncFactoryException {
82         SyncFactory syncFactory = SyncFactory.getSyncFactory();
83         assertTrue(syncFactory != null);
84     }
85 
86     /*
87      * Check that the correct SyncProvider is returned for the specified
88      * providerID for the provider registered via a property
89      */
90     @Test
test00()91     public void test00() throws SyncFactoryException {
92         SyncProvider p = SyncFactory.getInstance(propertyStubProvider);
93         assertTrue(p instanceof PropertyStubProvider);
94     }
95 
96     /*
97      * Check that the correct SyncProvider is returned for the specified
98      * providerID
99      */
100     @Test
test01()101     public void test01() throws SyncFactoryException {
102         SyncFactory.registerProvider(stubProvider);
103         SyncProvider p = SyncFactory.getInstance(stubProvider);
104         assertTrue(p instanceof StubSyncProvider);
105     }
106 
107     /*
108      * Check that the Default SyncProvider is returned if an empty String is
109      * passed or if an invalid providerID is specified
110      */
111     @Test
test02()112     public void test02() throws SyncFactoryException {
113         SyncProvider p = SyncFactory.getInstance("");
114         assertTrue(p instanceof RIOptimisticProvider);
115         // Attempt to get an invalid provider and get the default provider
116         p = SyncFactory.getInstance("util.InvalidSyncProvider");
117         assertTrue(p instanceof RIOptimisticProvider);
118     }
119 
120     /*
121      * Validate that a SyncFactoryException is thrown if the ProviderID is null
122      */
123     @Test(expectedExceptions = SyncFactoryException.class)
test03()124     public void test03() throws SyncFactoryException {
125         SyncProvider p = SyncFactory.getInstance(null);
126     }
127 
128     /*
129      * Validate that a SyncFactoryException is thrown if the Logger is null
130      */
131     @Test(expectedExceptions = SyncFactoryException.class,enabled=true)
test04()132     public void test04() throws SyncFactoryException {
133         Logger l = SyncFactory.getLogger();
134     }
135 
136     /*
137      * Validate that the correct logger is returned by getLogger
138      */
139     @Test
test05()140     public void test05() throws SyncFactoryException {
141         SyncFactory.setLogger(alogger);
142         Logger l = SyncFactory.getLogger();
143         assertTrue(l.equals(alogger));
144     }
145 
146     /*
147      * Validate that the correct logger is returned by getLogger
148      */
149     @Test
test06()150     public void test06() throws SyncFactoryException {
151         SyncFactory.setLogger(alogger, Level.INFO);
152         Logger l = SyncFactory.getLogger();
153         assertTrue(l.equals(alogger));
154     }
155 
156     /*
157      *  Validate that a driver that is registered is returned by
158      * getRegisteredProviders and  if it is unregistered, that it is
159      * not returned by getRegisteredProviders
160      */
161     @Test
test07()162     public void test07() throws SyncFactoryException {
163 
164         // Validate that only the default providers and any specified via
165         // a System property are available
166         validateProviders(initialProviders);
167 
168         // Register a provider and make sure it is avaiable
169         SyncFactory.registerProvider(stubProvider);
170         validateProviders(allProviders);
171 
172         // Check that if a provider is unregistered, it does not show as
173         // registered
174         SyncFactory.unregisterProvider(stubProvider);
175         validateProviders(initialProviders);
176     }
177 
178     /*
179      * Validate that setJNDIContext throws a SyncFactoryException if the
180      * context is null
181      */
182     @Test(expectedExceptions = SyncFactoryException.class, enabled=true)
test08()183     public void test08() throws Exception {
184         SyncFactory.setJNDIContext(null);
185     }
186 
187     /*
188      * Validate that setJNDIContext succeeds
189      */
190     @Test(enabled=true)
test09()191     public void test09() throws Exception {
192         SyncFactory.setJNDIContext(ctx);
193     }
194 
195     /*
196      * Utility method to validate the expected providers are regsitered
197      */
validateProviders(List<String> expectedProviders)198     private void validateProviders(List<String> expectedProviders)
199             throws SyncFactoryException {
200         List<String> results = new ArrayList<>();
201         Enumeration<SyncProvider> providers = SyncFactory.getRegisteredProviders();
202 
203         while (providers.hasMoreElements()) {
204             SyncProvider p = providers.nextElement();
205             results.add(p.getProviderID());
206         }
207         assertTrue(expectedProviders.containsAll(results)
208                 && results.size() == expectedProviders.size());
209     }
210 
211     /*
212      * Utility method to dump out SyncProvider info for a registered provider
213      */
showImpl(SyncProvider impl)214     private void showImpl(SyncProvider impl) {
215         System.out.println("Provider implementation:"
216                 + "\nVendor: " + impl.getVendor()
217                 + "\nVersion: " + impl.getVersion()
218                 + "\nProviderID: " + impl.getProviderID());
219     }
220 }
221