KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > test > TestSqlParser


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.test;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.FileReader JavaDoc;
36 import java.io.FileWriter JavaDoc;
37 import java.io.FilenameFilter JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
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 JavaDoc SCHEMA_FILE = "sqlparser/TestSchemaDefinition.xml";
52   final static String JavaDoc TEST_DIR = "sqlparser";
53
54   public TestSqlParser(String JavaDoc arg0)
55   {
56     super(arg0);
57   }
58
59   public String JavaDoc readFile(File JavaDoc inFile) throws Exception JavaDoc
60   {
61     System.out.println("Reading " + inFile.getAbsolutePath());
62     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
63     StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
64     String JavaDoc 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 JavaDoc findDiffLine(String JavaDoc a, String JavaDoc 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     // back to start of line:
110
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 JavaDoc inFile)
139   {
140     try
141     {
142       boolean createResult = false;
143
144       String JavaDoc src = readFile(inFile);
145       File JavaDoc resultFile = new File JavaDoc(inFile.getAbsolutePath() + ".result");
146       String JavaDoc res = "";
147       try
148       {
149         res = readFile(resultFile);
150       }
151       catch (FileNotFoundException JavaDoc 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 JavaDoc stSrc = new StringTokenizer JavaDoc(src, ";");
159       StringTokenizer JavaDoc stRes = new StringTokenizer JavaDoc(res, ";");
160
161       StringBuffer JavaDoc daResult = new StringBuffer JavaDoc(20000);
162
163       while (stSrc.hasMoreElements())
164       {
165         String JavaDoc execSrc = stSrc.nextToken().trim();
166         String JavaDoc execRes = "";
167         if (!createResult) execRes = stRes.nextToken().trim() + ";";
168
169         if (execSrc.trim().length() > 0)
170         {
171           ArrayList JavaDoc metadata = new ArrayList JavaDoc();
172           String JavaDoc toTest = mgr.external2internalStatement(execSrc, metadata).trim() + ";";
173           // for (int i = 0; i < metadata.size(); i++)
174
// {
175
// if (i != 0)
176
// System.out.print(", ");
177
// System.out.print(metadata.get(i).toString());
178
// }
179
// System.out.println();
180

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 JavaDoc fw = new FileWriter JavaDoc(resultFile);
205         fw.write(daResult.toString());
206         fw.close();
207       }
208     }
209     catch (Exception JavaDoc 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 JavaDoc testdir = new File JavaDoc(Util.resource2FileName(TestSqlParser.class.getResource("sqlparser")));
229       File JavaDoc[] 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 JavaDoc x)
240     {
241       DjLogger.log(x);
242       Assert.fail(x.getMessage());
243     }
244   }
245 }
246
247 class TestFileFilter implements FilenameFilter JavaDoc
248 {
249
250   public boolean accept(File JavaDoc dir, String JavaDoc name)
251   {
252     name = name.toLowerCase();
253     return name.endsWith(".sql") && name.startsWith("test");
254   }
255 }
Popular Tags