KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > sql > request > ReplaceRequestPatternTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.sql.request;
26
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
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 /**
41  * <code>ReplaceRequestPatternTest</code> test class.
42  *
43  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
44  */

45 public class ReplaceRequestPatternTest extends NoTemplate
46 {
47   /** File name containing the requests to test. */
48   private static final String JavaDoc REPLACE_PATTERN_REQUESTS_FILE = getTextPath("replace-pattern-requests.txt");
49
50   /** List of <code>ParsingResult</code> objects. */
51   private ArrayList JavaDoc results;
52
53   private static boolean inited = false;
54   private int count = 0;
55
56   /**
57    * @see junit.framework.TestCase#setUp()
58    */

59   protected void setUp()
60   {
61     synchronized (this)
62     {
63       if (inited)
64         return;
65       results = new ArrayList JavaDoc();
66       String JavaDoc ruleType = null, matchPattern = null, replacePattern = null, originalQuery = null, replaceQuery = null;
67       try
68       {
69         File JavaDoc file = new File JavaDoc(REPLACE_PATTERN_REQUESTS_FILE);
70         MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file),
71             "requests");
72
73         String JavaDoc 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           // Get the score
87
ruleType = line;
88           matchPattern = in.readNextLine();
89           replacePattern = in.readNextLine();
90           originalQuery = in.readNextLine();
91
92           // Get expected results for this request
93
if (in.readNextLine().equals("true"))
94           {
95             // Query was a match on the rule
96
replaceQuery = in.readNextLine();
97             results.add(new ParsingResult(ruleType, matchPattern,
98                 replacePattern, originalQuery, replaceQuery));
99           }
100           else
101           {
102             // No match
103
results.add(new ParsingResult(ruleType, matchPattern,
104                 replacePattern, originalQuery));
105           }
106         }
107       }
108       catch (IOException JavaDoc e)
109       {
110         String JavaDoc 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   /**
122    * org.objectweb.cjdbc.common.sql.CreateRequest#parse(DatabaseSchema, int,
123    * boolean)
124    */

125   public void testParseRewriteRequest()
126   {
127     Iterator JavaDoc it = results.iterator();
128     while (it.hasNext())
129     {
130       System.out.println("Test: " + count++);
131       parse((ParsingResult) it.next(), false);
132     }
133   }
134
135   /**
136    * Perfoms the parsing test.
137    *
138    * @param result expected result
139    * @param isCaseSensitive <code>true</code> if the parsing must be case
140    * sensitive.
141    */

142   private void parse(ParsingResult result, boolean isCaseSensitive)
143   {
144     String JavaDoc ruleType = result.ruleType.trim();
145     String JavaDoc matchPattern = result.matchPattern.trim();
146     String JavaDoc originalQuery = result.originalQuery.trim();
147     String JavaDoc 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 JavaDoc("Unexecpected Rule Type:" + ruleType);
160
161     String JavaDoc 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   /**
183    * Stores the expected result of the parsing call
184    */

185   protected class ParsingResult
186   {
187     String JavaDoc ruleType;
188     String JavaDoc matchPattern;
189     String JavaDoc replacePattern;
190     String JavaDoc originalQuery;
191     String JavaDoc replacedQuery;
192     boolean isMatched;
193
194     /**
195      * Create a result where the rule was matching
196      *
197      * @param matchPattern string
198      * @param replacePattern string
199      * @param originalQuery string
200      * @param replacedQuery string
201      */

202     ParsingResult(String JavaDoc ruleType, String JavaDoc matchPattern, String JavaDoc replacePattern,
203         String JavaDoc originalQuery, String JavaDoc 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     /**
214      * Create a result where the rule was not matching and the original query
215      * was left unchanged
216      *
217      * @param matchPattern string
218      * @param replacePattern string
219      * @param originalQuery string
220      */

221     ParsingResult(String JavaDoc ruleType, String JavaDoc matchPattern, String JavaDoc replacePattern,
222         String JavaDoc 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