KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > dataSourceReference


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.dataSourcePermissions
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31
32 import javax.naming.*;
33 import javax.naming.spi.ObjectFactory JavaDoc;
34
35 /**
36  * Test obtaining a javax.naming.Reference from a Derby data source
37  * and recreating a Derby data source from it. Tests that the recreated
38  * value has the same value for all the properties the data source supports.
39  * The list of properties is obtained dynamically from the getXXX methods
40  * that return int, String, boolean, short, long. Should Derby data sources
41  * support any other bean property types then this test should be modified
42  * to pick them up and handle them. Hopefully the test should fail when such
43  * a property is added.
44  *
45  * At no point does this test attempt to connect using these data sources.
46  */

47 public class dataSourceReference
48 {
49     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
50
51         System.out.println("Starting dataSourceReference");
52         
53         testDSReference("org.apache.derby.jdbc.EmbeddedDataSource");
54         testDSReference("org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource");
55         testDSReference("org.apache.derby.jdbc.EmbeddedXADataSource");
56         
57         
58         testDSReference("org.apache.derby.jdbc.ClientDataSource");
59         testDSReference("org.apache.derby.jdbc.ClientConnectionPoolDataSource");
60         testDSReference("org.apache.derby.jdbc.ClientXADataSource");
61         
62         System.out.println("Completed dataSourceReference");
63
64     }
65     
66     /**
67      * Test a data source
68      * <OL>
69      * <LI> Create an empty one from the class name
70      * <LI> Discover the property list
71      * <LI> Create a reference and recreate a data source
72      * <LI> Compare the two
73      * <LI> Serialize athe data source and recreate
74      * <LI> Compare the two
75      * <LI> Set every property for the data source
76      * <LI> Create a reference and recreate a data source
77      * <LI> Compare the two
78      * </OL>
79      * @param dsName
80      * @throws Exception
81      */

82     private static void testDSReference(String JavaDoc dsName) throws Exception JavaDoc
83     {
84         Object JavaDoc ds = Class.forName(dsName).newInstance();
85         
86         System.out.println("DataSource class " + dsName);
87         String JavaDoc[] properties = getPropertyBeanList(ds);
88         System.out.println(" property list");
89         for (int i = 0; i < properties.length; i++)
90         {
91             System.out.println(" " + properties[i]);
92         }
93         
94         Referenceable refDS = (Referenceable) ds;
95         
96         Reference dsAsReference = refDS.getReference();
97         
98         String JavaDoc factoryClassName = dsAsReference.getFactoryClassName();
99         
100         ObjectFactory JavaDoc factory = (ObjectFactory JavaDoc) Class.forName(factoryClassName).newInstance();
101         
102         Object JavaDoc recreatedDS = factory.getObjectInstance(dsAsReference, null, null, null);
103         
104         System.out.println(" empty DataSource recreated using Reference as " + recreatedDS.getClass().getName());
105         if (recreatedDS == ds)
106             System.out.println("FAIL recreated as same instance!");
107         
108         compareDS(properties, ds, recreatedDS);
109         
110         // now serialize and recreate
111
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
112         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
113         oos.writeObject(ds);
114         oos.flush();
115         oos.close();
116         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
117         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
118         recreatedDS = ois.readObject();
119         System.out.println(" empty DataSource recreated using serialization");
120         compareDS(properties, ds, recreatedDS);
121         
122         // now populate the data source
123
for (int i = 0; i < properties.length; i++)
124         {
125             String JavaDoc property = properties[i];
126             Method JavaDoc getMethod = getGet(property, ds);
127             
128             Method JavaDoc setMethod = getSet(getMethod, ds);
129             
130             Class JavaDoc pt = getMethod.getReturnType();
131             
132             // generate a somewhat unique value for a property
133
int val = 0;
134             for (int j = 0; j < property.length(); j++)
135                 val += property.charAt(j);
136             
137             if (pt.equals(Integer.TYPE))
138             {
139                 setMethod.invoke(ds, new Object JavaDoc[] {new Integer JavaDoc(val)});
140                 continue;
141             }
142             if (pt.equals(String JavaDoc.class))
143             {
144                 String JavaDoc value;
145                 if (property.equals("createDatabase"))
146                     value = "create";
147                 else if (property.equals("shutdownDatabase"))
148                     value = "shutdown";
149                 else
150                     value = "XX_" + property + "_" + val;
151                     
152                 setMethod.invoke(ds, new Object JavaDoc[] {value});
153                 continue;
154             }
155             if (pt.equals(Boolean.TYPE))
156             {
157                 // set the opposite value
158
Object JavaDoc gbv = getMethod.invoke(ds, null);
159                 Boolean JavaDoc sbv = Boolean.FALSE.equals(gbv) ? Boolean.TRUE : Boolean.FALSE;
160                 setMethod.invoke(ds, new Object JavaDoc[] {sbv});
161                 continue;
162             }
163             if (pt.equals(Short.TYPE))
164             {
165                 setMethod.invoke(ds, new Object JavaDoc[] {new Short JavaDoc((short)val)});
166                 continue;
167             }
168             if (pt.equals(Long.TYPE))
169             {
170                 setMethod.invoke(ds, new Object JavaDoc[] {new Long JavaDoc(val)});
171                 continue;
172             }
173             System.out.println("FAIL " + property + " not settable - uhpdate test!!");
174         }
175         
176         dsAsReference = refDS.getReference();
177         recreatedDS = factory.getObjectInstance(dsAsReference, null, null, null);
178         System.out.println(" populated DataSource recreated using Reference as " + recreatedDS.getClass().getName());
179         if (recreatedDS == ds)
180             System.out.println("FAIL recreated as same instance!");
181         
182         compareDS(properties, ds, recreatedDS);
183
184         // now serialize and recreate
185
baos = new ByteArrayOutputStream JavaDoc();
186         oos = new ObjectOutputStream JavaDoc(baos);
187         oos.writeObject(ds);
188         oos.flush();
189         oos.close();
190         bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
191         ois = new ObjectInputStream JavaDoc(bais);
192         recreatedDS = ois.readObject();
193         System.out.println(" populated DataSource recreated using serialization");
194         compareDS(properties, ds, recreatedDS);
195     }
196     
197     private static String JavaDoc[] getPropertyBeanList(Object JavaDoc ds) throws Exception JavaDoc
198     {
199         Method JavaDoc[] allMethods = ds.getClass().getMethods();
200         
201         ArrayList JavaDoc properties = new ArrayList JavaDoc();
202         for (int i = 0; i < allMethods.length; i++)
203         {
204             Method JavaDoc m = allMethods[i];
205             String JavaDoc methodName = m.getName();
206             // Need at least getXX
207
if (methodName.length() < 5)
208                 continue;
209             if (!methodName.startsWith("get"))
210                 continue;
211             if (m.getParameterTypes().length != 0)
212                 continue;
213
214             Class JavaDoc rt = m.getReturnType();
215             
216             if (rt.equals(Integer.TYPE) || rt.equals(String JavaDoc.class) || rt.equals(Boolean.TYPE)
217                     || rt.equals(Short.TYPE) || rt.equals(Long.TYPE))
218             {
219                 // valid Java Bean property
220
String JavaDoc beanName = methodName.substring(3,4).toLowerCase() + methodName.substring(4);
221
222                 properties.add(beanName);
223                 continue;
224             }
225             
226             if (rt.isPrimitive())
227                 System.out.println("FAIL " + methodName + " not supported - update test!!");
228
229         }
230         
231         String JavaDoc[] propertyList = (String JavaDoc[]) properties.toArray(new String JavaDoc[0]);
232         
233         Arrays.sort(propertyList);
234         
235         return propertyList;
236     }
237     
238     private static Method JavaDoc getGet(String JavaDoc property, Object JavaDoc ds) throws Exception JavaDoc
239     {
240         String JavaDoc methodName =
241             "get" + property.substring(0,1).toUpperCase()
242             + property.substring(1);
243         Method JavaDoc m = ds.getClass().getMethod(methodName, null);
244         return m;
245     }
246     private static Method JavaDoc getSet(Method JavaDoc getMethod, Object JavaDoc ds) throws Exception JavaDoc
247     {
248         String JavaDoc methodName = "s" + getMethod.getName().substring(1);
249         Method JavaDoc m = ds.getClass().getMethod(methodName, new Class JavaDoc[] {getMethod.getReturnType()});
250         return m;
251     }
252     private static void compareDS(String JavaDoc[] properties, Object JavaDoc ds, Object JavaDoc rds) throws Exception JavaDoc
253     {
254         System.out.println(" Start compare recreated");
255         for (int i = 0; i < properties.length; i++)
256         {
257             Method JavaDoc getMethod = getGet(properties[i], ds);
258             
259             Object JavaDoc dsValue = getMethod.invoke(ds, null);
260             Object JavaDoc rdsValue = getMethod.invoke(rds, null);
261             
262             if (dsValue == null)
263             {
264                 if (rdsValue != null)
265                 {
266                     System.out.println(" FAIL: " + properties[i] + " originally null, recreated as " + rdsValue);
267                     continue;
268                 }
269             }
270             else
271             {
272                 if (!dsValue.equals(rdsValue)) {
273                     System.out.println(" FAIL: " + properties[i] + " originally " + dsValue + ", recreated as " + rdsValue);
274                     continue;
275                 }
276                 
277                 
278             }
279             if (dsValue != null)
280                 System.out.println(" " + properties[i] + "=" + dsValue);
281         
282         }
283         System.out.println(" Completed compare recreated");
284
285     }
286
287 }
288
Popular Tags