1 24 25 package org.objectweb.cjdbc.scenario.standalone.sql.request; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 33 import org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule; 34 import org.objectweb.cjdbc.controller.backend.rewriting.PatternRewritingRule; 35 import org.objectweb.cjdbc.controller.backend.rewriting.ReplaceAllRewritingRule; 36 import org.objectweb.cjdbc.controller.backend.rewriting.SimpleRewritingRule; 37 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 38 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 39 40 45 public class ReplaceRequestPatternTest extends NoTemplate 46 { 47 48 private static final String REPLACE_PATTERN_REQUESTS_FILE = getTextPath("replace-pattern-requests.txt"); 49 50 51 private ArrayList results; 52 53 private static boolean inited = false; 54 private int count = 0; 55 56 59 protected void setUp() 60 { 61 synchronized (this) 62 { 63 if (inited) 64 return; 65 results = new ArrayList (); 66 String ruleType = null, matchPattern = null, replacePattern = null, originalQuery = null, replaceQuery = null; 67 try 68 { 69 File file = new File (REPLACE_PATTERN_REQUESTS_FILE); 70 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 71 "requests"); 72 73 String line; 74 while ((line = in.readLine()) != null) 75 { 76 if (line.trim().equals("") || line.startsWith("//") 77 || line.startsWith("#")) 78 continue; 79 80 ruleType = null; 81 matchPattern = null; 82 replacePattern = null; 83 originalQuery = null; 84 replaceQuery = null; 85 86 ruleType = line; 88 matchPattern = in.readNextLine(); 89 replacePattern = in.readNextLine(); 90 originalQuery = in.readNextLine(); 91 92 if (in.readNextLine().equals("true")) 94 { 95 replaceQuery = in.readNextLine(); 97 results.add(new ParsingResult(ruleType, matchPattern, 98 replacePattern, originalQuery, replaceQuery)); 99 } 100 else 101 { 102 results.add(new ParsingResult(ruleType, matchPattern, 104 replacePattern, originalQuery)); 105 } 106 } 107 } 108 catch (IOException e) 109 { 110 String error = "An error occurs while parsing requests file: " + e; 111 if (matchPattern != null && originalQuery != null) 112 error += " (pattern: '" + matchPattern + "', query:'" + originalQuery 113 + "')"; 114 fail(error); 115 } 116 inited = true; 117 } 118 119 } 120 121 125 public void testParseRewriteRequest() 126 { 127 Iterator it = results.iterator(); 128 while (it.hasNext()) 129 { 130 System.out.println("Test: " + count++); 131 parse((ParsingResult) it.next(), false); 132 } 133 } 134 135 142 private void parse(ParsingResult result, boolean isCaseSensitive) 143 { 144 String ruleType = result.ruleType.trim(); 145 String matchPattern = result.matchPattern.trim(); 146 String originalQuery = result.originalQuery.trim(); 147 String replacePattern = result.replacePattern.trim(); 148 149 AbstractRewritingRule rule = null; 150 if (ruleType.equalsIgnoreCase("PatternRewritingRule")) 151 rule = new PatternRewritingRule(matchPattern, replacePattern, false, 152 false); 153 else if (ruleType.equalsIgnoreCase("ReplaceAllRewritingRule")) 154 rule = new ReplaceAllRewritingRule(matchPattern, replacePattern, false, 155 false); 156 else if (ruleType.equalsIgnoreCase("SimpleRewritingRule")) 157 rule = new SimpleRewritingRule(matchPattern, replacePattern, false, false); 158 else 159 throw new RuntimeException ("Unexecpected Rule Type:" + ruleType); 160 161 String rewritten = rule.rewrite(originalQuery); 162 163 assertEquals("Matching result differs from expected", rule.hasMatched(), 164 result.isMatched); 165 166 if (rule.hasMatched()) 167 { 168 System.out.println("RuleType:\t" + ruleType); 169 System.out.println("Original:\t" + originalQuery); 170 System.out.println("Rewritten:\t" + rewritten); 171 System.out.println("Expected:\t" + result.replacedQuery); 172 assertEquals("Replaced Query was different than expected", rewritten, 173 result.replacedQuery); 174 } 175 else 176 { 177 assertEquals("Rule should NOT have matched but reported a success", rule 178 .hasMatched(), result.isMatched); 179 } 180 } 181 182 185 protected class ParsingResult 186 { 187 String ruleType; 188 String matchPattern; 189 String replacePattern; 190 String originalQuery; 191 String replacedQuery; 192 boolean isMatched; 193 194 202 ParsingResult(String ruleType, String matchPattern, String replacePattern, 203 String originalQuery, String replacedQuery) 204 { 205 this.ruleType = ruleType; 206 this.matchPattern = matchPattern; 207 this.replacePattern = replacePattern; 208 this.originalQuery = originalQuery; 209 this.replacedQuery = replacedQuery; 210 this.isMatched = true; 211 } 212 213 221 ParsingResult(String ruleType, String matchPattern, String replacePattern, 222 String originalQuery) 223 { 224 this.ruleType = ruleType; 225 this.matchPattern = matchPattern; 226 this.replacePattern = replacePattern; 227 this.originalQuery = originalQuery; 228 this.replacedQuery = originalQuery; 229 this.isMatched = false; 230 } 231 } 232 } | Popular Tags |