/* * This file is part of rasdaman community. * * Rasdaman community is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Rasdaman community is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with rasdaman community. If not, see . * * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann / rasdaman GmbH. * * For more information please see * or contact Peter Baumann via . */ /************************************************************* * * * PURPOSE: used in RMANHOME/systemtest/testjava.sh * * ************************************************************/ package examples; import rasj.*; import rasj.odmg.*; import org.odmg.*; import java.util.*; import java.io.*; /** * used in RMANHOME/systemtest/testjava.sh * * set the server name with -server, the database name with -database, the collection name with -collection, * set port number with -port, the user login with -user, the password with -passwd, * and path to query file with -queryfile, path to log file with -logfile, path to query test directory wtih -testdirectory */ class Record { static StringBuffer locQuery = new StringBuffer(20000); static StringBuffer locExpected = new StringBuffer(20000); boolean commitBefore = false; String queryFileShortName = null; String queryFileFullName = null; String query = null; String expectedFile = null; String expected = null; volatile String answer = null; volatile DBag result = null; boolean loadQuery() { FileReader fr = null; BufferedReader in = null; boolean answer = true; String ret = null; try { if(queryFileFullName == null) return false; //System.out.println(file); fr = new FileReader(queryFileFullName); in = new BufferedReader(fr); String s; locQuery.delete(0, locQuery.length()); locQuery.append( "-- " + queryFileFullName + "\n"); while((s = in.readLine()) != null) { locQuery.append("\n" + s); } query = locQuery.toString(); } catch(FileNotFoundException e) { ret = "Query-file not found!\n"+e.getMessage(); System.err.println(ret); answer = false; } catch(IOException e) { ret = "Could not read Query-file!\n"+e.getMessage(); System.err.println(ret); answer = false; } finally { try{ if(in != null) in.close(); if(fr != null) fr.close(); } catch(Exception e){} } return answer; } boolean loadExpected() { FileReader fr = null; BufferedReader in = null; boolean answer = true; String ret = null; String s = null; try { if(expectedFile == null) return false; //System.out.println(file); fr = new FileReader(expectedFile); in = new BufferedReader(fr); locExpected.delete(0, locExpected.length()); if((s=in.readLine()) != null) locExpected.append(s); while((s = in.readLine()) != null) { locExpected.append("\n" + s); } expected = locExpected.toString(); } catch(FileNotFoundException e) { ret = "Expected-file not found!\n"+e.getMessage(); System.err.println(ret); answer = false; } catch(IOException e) { ret = "Could not read expected-file!\n"+e.getMessage(); System.err.println(ret); answer = false; } finally { try{ if(in != null) in.close(); if(fr != null) fr.close(); } catch(Exception e){} } return answer; } } class FilesToProcess { Vector records; FilesToProcess(String path) throws Exception { records = new Vector(); File filePath = new File(path); processPath(filePath); loadAllQueries(); } Record getRecord(int i) { return (Record)records.get(i); } int countRecords() { return records.size(); } void print() { System.out.println("List of all files to process"); for(int i = 0; i 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } private void loadAllQueries() { for(int i = 0; i ""; good result -> null; // bad result -> error text built from the following catch's return ret; } catch(RasRuntimeException e) { ret = "\n RasRuntimeException: " + e.getMessage(); } catch(RasException e) { ret = "\n RasException: "+e.getMessage(); } catch (org.odmg.ODMGException e) { ret = "\n ODMGException: "+ e.getMessage(); } return ret; } void connectToRasDaMan() throws ODMGException, RasRuntimeException, ODMGRuntimeException { if(myTa != null) return; myApp = new RasImplementation("http://"+server+":"+port); ((RasImplementation)myApp).setUserIdentification(user, passwd); myDb = myApp.newDatabase(); myTa = myApp.newTransaction(); tryOpenDatabase(); } void clearConnection(boolean error) { try{ if(myTa != null) if(error) myTa.abort(); else myTa.commit(); if(myDb != null) myDb.close(); myTa = null; myDb = null; myApp = null; } catch ( org.odmg.ODMGException exp ) { // can't heapen, our DB.close doesn't throw anything } } void createQueryObject(String query) throws org.odmg.ODMGException, RasRuntimeException, RasException { myQu = myApp.newOQLQuery(); myQu.create(query); StringTokenizer strTok = new StringTokenizer(query, "$"); RasMInterval domain; RasGMArray mddConst; if(strTok.hasMoreTokens()) strTok.nextToken(); while(strTok.hasMoreTokens()) { strTok.nextToken(); domain = new RasMInterval("[0:10,0:10]"); RasStorageLayout stl = new RasStorageLayout(); stl.setTileSize(8); mddConst = new RasGMArray(domain, 4, stl); byte[] data = new byte[mddConst.getArray().length]; for(int j = 0; j < data.length; j++) { data[j] = (byte)j; } mddConst.setArray(data); mddConst.setObjectTypeName("ULongImage"); //System.out.println(mddConst); myQu.bind(mddConst); } accessMode=Database.OPEN_READ_WRITE; // we cache transactions, so let it like this /* if(isUpdateQuery(query)) accessMode=Database.OPEN_READ_WRITE; else accessMode=Database.OPEN_READ_ONLY; */ } void tryOpenDatabase() throws ODMGException, RasRuntimeException, ODMGRuntimeException { countRetry=0; while(true) { try { //System.out.println( "Opening database ..." ); myDb.open( database, accessMode); //System.out.println( "Starting transaction ..." ); myTa.begin(); break; } catch(RasRuntimeException e) { if(eatException() == false) { clearConnection(true); throw e; } } catch (ODMGException e) { if(eatException() == false) { clearConnection(true); throw e; } } catch (ODMGRuntimeException e) { if(eatException() == false) { clearConnection(true); throw e; } } } } boolean eatException() { boolean retval=false; countRetry++; if((countRetry % 1000) == 0) System.out.println( "Server busy, retry ... " + countRetry + " / " + noOfRetries ); if(countRetryRecord.answer AnswerConvertor answerConvertor = new AnswerConvertor(fp); answerConvertor.start(); // this brings the expected results from disk - starts the least because it's very fast ExpectedLoader exp = new ExpectedLoader(fp); exp.start(); //used to write the output if is requested FileWriter outputFW = null; for(int i=0;i < fp.countRecords(); i++) { Record record = fp.getRecord(i); while(record.answer == null) { try { Thread.sleep(1000); } catch(Exception e){} } System.out.print("" + (i+1) + ": " + record.queryFileShortName); if(record.answer.equals(record.expected)) { System.out.println(" OK"); logWriter.write(i + ": OK (" + record.queryFileFullName + ")\n"); } else { System.out.println(" FAILED!"); logWriter.write(i + ": FAILED (" + record.queryFileFullName + ")\n"); if(dontDeleteResultFile == true) { try { outputFW=new FileWriter(record.queryFileFullName + ".java.tmp"); outputFW.write(record.answer); outputFW.close(); outputFW=null; } catch(Exception a) { if(outputFW != null) { try { outputFW.close(); } catch(Exception b) { } } } } logWriter.write(" expected=" + record.expected + "\n but got=" + record.answer + "\n"); } } //put time stamp end = new Date(); System.out.println("Test ended at: " + end.toString()); logWriter.println("TEST JAVA ENDED AT: " + end.toString()); logWriter.close(); logWriter = null; start=null; end=null; } static PrintWriter openLogWriter(String logFileName) { if(logFileName == null || logFileName.equals("")) { System.out.println("No logfile name provided!"); return null; } try { FileOutputStream fos = new FileOutputStream(logFileName, true); return new PrintWriter(fos); } catch(FileNotFoundException e) { System.out.println("Log-file not found!\n"+e.getMessage()); } catch(IOException e) { System.out.println("Could not write Log-file!\n"+e.getMessage()); } return null; } }