1 30 package com.genimen.djeneric.test; 31 32 import java.io.BufferedReader ; 33 import java.io.File ; 34 import java.io.FileNotFoundException ; 35 import java.io.FileReader ; 36 import java.io.FileWriter ; 37 import java.io.FilenameFilter ; 38 import java.util.ArrayList ; 39 import java.util.StringTokenizer ; 40 41 import junit.framework.Assert; 42 import junit.framework.TestCase; 43 44 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 45 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager; 46 import com.genimen.djeneric.util.DjLogger; 47 import com.genimen.djeneric.util.DjString; 48 49 public class TestSqlParser extends TestCase 50 { 51 final static String SCHEMA_FILE = "sqlparser/TestSchemaDefinition.xml"; 52 final static String TEST_DIR = "sqlparser"; 53 54 public TestSqlParser(String arg0) 55 { 56 super(arg0); 57 } 58 59 public String readFile(File inFile) throws Exception 60 { 61 System.out.println("Reading " + inFile.getAbsolutePath()); 62 BufferedReader br = new BufferedReader (new FileReader (inFile)); 63 StringBuffer src = new StringBuffer (100); 64 String ln; 65 while ((ln = br.readLine()) != null) 66 { 67 src.append(ln); 68 src.append("\n"); 69 } 70 br.close(); 71 return src.toString(); 72 } 73 74 public String findDiffLine(String a, String b) 75 { 76 int len = a.length(); 77 if (len > b.length()) len = b.length(); 78 79 int idx = -1; 80 int endIdxA = -1; 81 int endIdxB = -1; 82 int line = 1; 83 84 for (int i = 0; i < len; i++) 85 { 86 if (idx == -1) 87 { 88 if (a.charAt(i) == '\n') line++; 89 if (a.charAt(i) != b.charAt(i)) 90 { 91 idx = i; 92 } 93 } 94 if (idx != -1 && endIdxA == -1) 95 { 96 if (a.charAt(i) == '\n') 97 { 98 endIdxA = i; 99 } 100 } 101 if (idx != -1 && endIdxB == -1) 102 { 103 if (b.charAt(i) == '\n') 104 { 105 endIdxB = i; 106 } 107 } 108 } 109 int col = 0; 111 while (idx > 0) 112 { 113 if (a.charAt(idx) == '\n') 114 { 115 idx++; 116 col--; 117 break; 118 } 119 idx--; 120 col++; 121 } 122 123 if ((endIdxA == -1) || (endIdxA <= idx)) 124 { 125 endIdxA = a.length() - 1; 126 } 127 if ((endIdxB == -1) || (endIdxB <= idx)) 128 { 129 endIdxB = b.length() - 1; 130 } 131 if (idx == -1) idx = 0; 132 if (col == -1) col = 0; 133 134 return "Line " + line + " column " + (col + 1) + "\n" + DjString.appendToLength("", " ", col) + "v\n" 135 + a.substring(idx, endIdxA) + "\n" + b.substring(idx, endIdxB); 136 } 137 138 public void runTestFile(RdbmsPersistenceManager mgr, File inFile) 139 { 140 try 141 { 142 boolean createResult = false; 143 144 String src = readFile(inFile); 145 File resultFile = new File (inFile.getAbsolutePath() + ".result"); 146 String res = ""; 147 try 148 { 149 res = readFile(resultFile); 150 } 151 catch (FileNotFoundException fnf) 152 { 153 System.out.println("Warning: file not found.\n" 154 + "Resultfile will be created based on current logic and input\n"); 155 createResult = true; 156 } 157 158 StringTokenizer stSrc = new StringTokenizer (src, ";"); 159 StringTokenizer stRes = new StringTokenizer (res, ";"); 160 161 StringBuffer daResult = new StringBuffer (20000); 162 163 while (stSrc.hasMoreElements()) 164 { 165 String execSrc = stSrc.nextToken().trim(); 166 String execRes = ""; 167 if (!createResult) execRes = stRes.nextToken().trim() + ";"; 168 169 if (execSrc.trim().length() > 0) 170 { 171 ArrayList metadata = new ArrayList (); 172 String toTest = mgr.external2internalStatement(execSrc, metadata).trim() + ";"; 173 181 daResult.append(toTest.trim()); 182 daResult.append("\n"); 183 184 if (!createResult) 185 { 186 Assert.assertEquals(toTest.trim(), execRes); 187 if (toTest.trim().equals(execRes)) 188 { 189 System.out.println("Test succeeded"); 190 } 191 else 192 { 193 System.out.println("Test FAILED ***************"); 194 System.out.println(findDiffLine(toTest, execRes) + "\n"); 195 System.out.println("*** Source stmt:\n" + execSrc + "\n"); 196 System.out.println("*** Expected stmt:\n" + execRes + "\n"); 197 System.out.println("*** Actual stmt:\n" + toTest + "\n"); 198 } 199 } 200 } 201 } 202 if (createResult) 203 { 204 FileWriter fw = new FileWriter (resultFile); 205 fw.write(daResult.toString()); 206 fw.close(); 207 } 208 } 209 catch (Exception x) 210 { 211 System.out.println("Test FAILED with exception ***************"); 212 DjLogger.log(x); 213 Assert.fail(x.getMessage()); 214 } 215 } 216 217 public void testSqlParser() 218 { 219 try 220 { 221 222 DjPersistenceManagerFactory factory = new DjPersistenceManagerFactory(TestSqlParser.class 223 .getResourceAsStream("repositories.xml")); 224 RdbmsPersistenceManager mgr = (RdbmsPersistenceManager) factory.createInstance("test"); 225 226 mgr.setModel(TestSqlParser.class.getResourceAsStream(SCHEMA_FILE)); 227 228 File testdir = new File (Util.resource2FileName(TestSqlParser.class.getResource("sqlparser"))); 229 File [] testFiles = testdir.listFiles(new TestFileFilter()); 230 231 TestSqlParser test = new TestSqlParser("dummy"); 232 233 for (int i = 0; i < testFiles.length; i++) 234 { 235 test.runTestFile(mgr, testFiles[i]); 236 } 237 238 } 239 catch (Exception x) 240 { 241 DjLogger.log(x); 242 Assert.fail(x.getMessage()); 243 } 244 } 245 } 246 247 class TestFileFilter implements FilenameFilter 248 { 249 250 public boolean accept(File dir, String name) 251 { 252 name = name.toLowerCase(); 253 return name.endsWith(".sql") && name.startsWith("test"); 254 } 255 } | Popular Tags |