1 24 25 package org.objectweb.cjdbc.scenario.tools; 26 27 import java.io.DataInputStream ; 28 import java.io.DataOutputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.sql.Connection ; 35 import java.sql.ResultSet ; 36 import java.util.ArrayList ; 37 38 import org.objectweb.cjdbc.driver.Blob; 39 40 46 public class ScenarioUtility 47 { 48 49 58 public static ArrayList getSingleQueryResult(String query, Connection con, 59 boolean prepare) throws Exception 60 { 61 if (prepare) 62 return convertResultSet(con.prepareStatement(query).executeQuery()); 63 else 64 return convertResultSet(con.createStatement().executeQuery(query)); 65 } 66 67 75 public static ArrayList getSingleQueryResult(String query, Connection con) 76 throws Exception 77 { 78 return getSingleQueryResult(query, con, false); 79 } 80 81 84 public static void displaySingleQueryResult(String query, Connection con) 85 throws Exception 86 { 87 displayResultOnScreen(getSingleQueryResult(query, con)); 88 } 89 90 93 public static void displaySingleQueryResult(String query, Connection con, 94 boolean prepare) throws Exception 95 { 96 displayResultOnScreen(getSingleQueryResult(query, con, prepare)); 97 } 98 99 104 public static void displayResultOnScreen(ArrayList result) 105 { 106 int size = result.size(); 107 ArrayList list; 108 for (int i = 0; i < size; i++) 109 { 110 list = (ArrayList ) result.get(i); 111 System.out.println("row[" + i + "]:" + list); 112 } 113 } 114 115 121 public static void displayResultOnScreen(ResultSet set) throws Exception 122 { 123 displayResultOnScreen(convertResultSet(set)); 124 } 125 126 133 public static final ArrayList convertResultSet(ResultSet set) 134 throws Exception 135 { 136 ArrayList list = new ArrayList (); 137 int colCount = set.getMetaData().getColumnCount(); 138 while (set.next()) 139 { 140 ArrayList row = new ArrayList (); 141 for (int i = 1; i <= colCount; i++) 142 { 143 row.add(set.getString(i)); 144 } 145 list.add(row); 146 } 147 return list; 149 } 150 151 159 public static byte[] readBinary(File file) throws IOException 160 { 161 long len = file.length(); 162 FileInputStream fis = new FileInputStream (file); 163 byte[] data = new byte[(int) len]; 164 DataInputStream bais = new DataInputStream (fis); 165 bais.read(data); 166 return data; 167 } 168 169 176 public static Blob createBlob(String storeFile) throws Exception 177 { 178 File fis = new File (storeFile); 179 if (!fis.exists()) 180 fis = new File (new ScenarioUtility().getClass().getResource(storeFile) 181 .getFile()); 182 if (!fis.exists()) 183 throw new FileNotFoundException (); 184 Blob bob = new org.objectweb.cjdbc.driver.Blob(ScenarioUtility 185 .readBinary(fis)); 186 return bob; 187 } 188 189 196 public static void writeBinary(byte[] data, File file) throws IOException 197 { 198 FileOutputStream fos = new FileOutputStream (file); 199 DataOutputStream baos = new DataOutputStream (fos); 200 baos.write(data); 201 } 202 203 211 public static boolean checkEquals(ResultSet rs1, ResultSet rs2) 212 { 213 try 214 { 215 ArrayList list1 = ScenarioUtility.convertResultSet(rs1); 216 ArrayList list2 = ScenarioUtility.convertResultSet(rs2); 218 if (list1.size() != list2.size()) 220 return false; 221 222 Object o1; 223 Object o2; 224 for (int i = 0; i < list1.size(); i++) 225 { 226 ArrayList list11 = (ArrayList ) list1.get(i); 227 ArrayList list22 = (ArrayList ) list2.get(i); 228 if (list11.size() != list22.size()) 229 return false; 230 for (int j = 0; j < list11.size(); j++) 231 { 232 o1 = list11.get(j); 233 o2 = list22.get(j); 234 if (o1 == null && o2 == null) 235 continue; 236 else if (o1 == null || o2 == null) 237 return false; 238 if (o1.equals(o2)) 239 continue; 240 else 241 return false; 242 } 243 } 244 return true; 245 } 246 catch (Exception e) 247 { 248 e.printStackTrace(); 249 return false; 250 } 251 } 252 253 259 public static boolean deleteDir(File dir) 260 { 261 File candir; 265 try 266 { 267 candir = dir.getCanonicalFile(); 268 } 269 catch (IOException e) 270 { 271 return false; 272 } 273 274 if (!candir.equals(dir.getAbsoluteFile())) 277 { 278 return false; 282 } 283 284 File [] files = candir.listFiles(); 287 if (files != null) 288 { 289 for (int i = 0; i < files.length; i++) 290 { 291 File file = files[i]; 292 293 boolean deleted = file.delete(); 297 if (!deleted) 298 { 299 if (file.isDirectory()) 302 deleteDir(file); 303 304 } 306 } 307 } 308 309 return dir.delete(); 312 } 313 } | Popular Tags |