1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 32 import javax.naming.*; 33 import javax.naming.spi.ObjectFactory ; 34 35 47 public class dataSourceReference 48 { 49 public static void main(String [] args) throws Exception { 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 82 private static void testDSReference(String dsName) throws Exception 83 { 84 Object ds = Class.forName(dsName).newInstance(); 85 86 System.out.println("DataSource class " + dsName); 87 String [] 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 factoryClassName = dsAsReference.getFactoryClassName(); 99 100 ObjectFactory factory = (ObjectFactory ) Class.forName(factoryClassName).newInstance(); 101 102 Object 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 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 112 ObjectOutputStream oos = new ObjectOutputStream (baos); 113 oos.writeObject(ds); 114 oos.flush(); 115 oos.close(); 116 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 117 ObjectInputStream ois = new ObjectInputStream (bais); 118 recreatedDS = ois.readObject(); 119 System.out.println(" empty DataSource recreated using serialization"); 120 compareDS(properties, ds, recreatedDS); 121 122 for (int i = 0; i < properties.length; i++) 124 { 125 String property = properties[i]; 126 Method getMethod = getGet(property, ds); 127 128 Method setMethod = getSet(getMethod, ds); 129 130 Class pt = getMethod.getReturnType(); 131 132 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 [] {new Integer (val)}); 140 continue; 141 } 142 if (pt.equals(String .class)) 143 { 144 String 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 [] {value}); 153 continue; 154 } 155 if (pt.equals(Boolean.TYPE)) 156 { 157 Object gbv = getMethod.invoke(ds, null); 159 Boolean sbv = Boolean.FALSE.equals(gbv) ? Boolean.TRUE : Boolean.FALSE; 160 setMethod.invoke(ds, new Object [] {sbv}); 161 continue; 162 } 163 if (pt.equals(Short.TYPE)) 164 { 165 setMethod.invoke(ds, new Object [] {new Short ((short)val)}); 166 continue; 167 } 168 if (pt.equals(Long.TYPE)) 169 { 170 setMethod.invoke(ds, new Object [] {new Long (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 baos = new ByteArrayOutputStream (); 186 oos = new ObjectOutputStream (baos); 187 oos.writeObject(ds); 188 oos.flush(); 189 oos.close(); 190 bais = new ByteArrayInputStream (baos.toByteArray()); 191 ois = new ObjectInputStream (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 [] getPropertyBeanList(Object ds) throws Exception 198 { 199 Method [] allMethods = ds.getClass().getMethods(); 200 201 ArrayList properties = new ArrayList (); 202 for (int i = 0; i < allMethods.length; i++) 203 { 204 Method m = allMethods[i]; 205 String methodName = m.getName(); 206 if (methodName.length() < 5) 208 continue; 209 if (!methodName.startsWith("get")) 210 continue; 211 if (m.getParameterTypes().length != 0) 212 continue; 213 214 Class rt = m.getReturnType(); 215 216 if (rt.equals(Integer.TYPE) || rt.equals(String .class) || rt.equals(Boolean.TYPE) 217 || rt.equals(Short.TYPE) || rt.equals(Long.TYPE)) 218 { 219 String 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 [] propertyList = (String []) properties.toArray(new String [0]); 232 233 Arrays.sort(propertyList); 234 235 return propertyList; 236 } 237 238 private static Method getGet(String property, Object ds) throws Exception 239 { 240 String methodName = 241 "get" + property.substring(0,1).toUpperCase() 242 + property.substring(1); 243 Method m = ds.getClass().getMethod(methodName, null); 244 return m; 245 } 246 private static Method getSet(Method getMethod, Object ds) throws Exception 247 { 248 String methodName = "s" + getMethod.getName().substring(1); 249 Method m = ds.getClass().getMethod(methodName, new Class [] {getMethod.getReturnType()}); 250 return m; 251 } 252 private static void compareDS(String [] properties, Object ds, Object rds) throws Exception 253 { 254 System.out.println(" Start compare recreated"); 255 for (int i = 0; i < properties.length; i++) 256 { 257 Method getMethod = getGet(properties[i], ds); 258 259 Object dsValue = getMethod.invoke(ds, null); 260 Object 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 |