KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > transform > SQLToAppDataRuntimeTest


1 package org.apache.torque.engine.database.transform;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.*;
20 import java.util.Vector JavaDoc;
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25
26 import org.apache.torque.BaseRuntimeTestCase;
27 import org.apache.torque.engine.database.model.Database;
28 import org.apache.torque.engine.sql.ParseException;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * @author <a HREF="mailto:andyhot@di.uoa.gr">Andreas Andreou</a>
37  * @version $Id: SQLToAppDataRuntimeTest.java,v 1.1 2005/03/19 06:25:27 tfischer Exp $
38  */

39 public class SQLToAppDataRuntimeTest extends BaseRuntimeTestCase
40 {
41     /** The path to the configuration file. */
42     private static final String JavaDoc SQL_FOLDER = "target/test/rttest/sql";
43
44     private Vector JavaDoc files;
45
46     /**
47      * Creates a new instance.
48      */

49     public SQLToAppDataRuntimeTest(String JavaDoc name)
50     {
51         super(name);
52     }
53
54     public void setUp()
55     {
56         // this may not be needed
57
//super.setUp();
58
// init the vector
59
files = new Vector JavaDoc();
60         // find all sql files to test
61
File sqlFolder = new File(SQL_FOLDER);
62         if (sqlFolder != null && sqlFolder.isDirectory())
63         {
64             File allFiles[] = sqlFolder.listFiles();
65             for (int i = 0; i < allFiles.length; i++)
66             {
67                 File thisFile = allFiles[i];
68                 if (!thisFile.isDirectory() &&
69                         thisFile.getName().toUpperCase().endsWith("SQL"))
70                 {
71                     System.out.println("Adding file:" + thisFile.getName());
72                     files.add(thisFile);
73                 }
74             }
75         }
76         if (files.size() == 0)
77         {
78             System.out.println("No files where found to test the sql2xml task");
79         }
80     }
81
82     public void testConvertToXml()
83     {
84         try
85         {
86             for (int i = 0; i < files.size(); i++)
87             {
88                 File file = (File) files.elementAt(i);
89                 String JavaDoc filename = file.getAbsolutePath();
90                 // load the sql file
91
SQLToAppData s2a = new SQLToAppData(filename);
92                 Database ad = s2a.execute();
93                 // write the output to a new xml file
94
String JavaDoc xmlFilename = filename + ".xml";
95                 PrintWriter out = new PrintWriter(
96                         new FileOutputStream(xmlFilename, false),true);
97                 out.println(ad);
98                 out.close();
99                 // compare result
100
compareXmlFiles(filename + ".ref.xml", xmlFilename);
101             }
102         }
103         catch (IOException expIo)
104         {
105             expIo.printStackTrace(System.out);
106         }
107         catch (ParseException expParse)
108         {
109             expParse.printStackTrace(System.out);
110         }
111     }
112
113     private void renameDTD(String JavaDoc sFile)
114     {
115         try
116         {
117             BufferedReader reader = new BufferedReader(new FileReader(sFile));
118             String JavaDoc line;
119             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(5000);
120             while ((line=reader.readLine())!=null)
121             {
122                 sb.append(line).append("\n");
123             }
124             reader.close();
125             String JavaDoc data=sb.toString();
126             if (data == null || data.length() == 0)
127                 return;
128             int index=data.indexOf("<!DOCTYPE");
129             if (index != -1)
130             {
131                 int index2 = data.indexOf(">", index);
132                 if (index2 != -1)
133                 {
134                     data = data.substring(0, index - 1)
135                             + data.substring(index2 + 1);
136                 }
137             }
138             //data.replaceFirst("/database.dtd","/database.xxx");
139

140             BufferedWriter writer = new BufferedWriter(
141                     new FileWriter(sFile, false));
142             writer.write(data);
143             writer.close();
144         }
145         catch (FileNotFoundException e)
146         {
147             e.printStackTrace();
148         }
149         catch (IOException e)
150         {
151             e.printStackTrace();
152         }
153     }
154
155     private void compareXmlFiles(String JavaDoc refFile, String JavaDoc newFile)
156     {
157         System.out.println("Comparing " + newFile + " against " + refFile);
158         //System.out.println("Rename DTD to disable checking for default values...");
159
// The dom parser uses the DTD to add default values to the xml nodes.
160
// This makes difficult comparing the xml files.
161
// Since I couldn't find any way to disable this behavior,
162
// I chose to delete the DTD declaration from the xml files.
163
renameDTD(refFile);
164         renameDTD(newFile);
165
166         try
167         {
168             DocumentBuilderFactory JavaDoc docFactory = DocumentBuilderFactory.newInstance();
169             docFactory.setExpandEntityReferences(false);
170             docFactory.setValidating(false);
171
172             DocumentBuilder JavaDoc doc = docFactory.newDocumentBuilder();
173
174             Document JavaDoc refDoc = doc.parse(new File(refFile));
175             Document JavaDoc newDoc = doc.parse(new File(newFile));
176
177             NodeList JavaDoc refList = refDoc.getElementsByTagName("database");
178             NodeList JavaDoc newList = newDoc.getElementsByTagName("database");
179
180             assertNotNull(refList);
181             assertNotNull(newList);
182
183             for (int i = 0; i < refList.getLength(); i++)
184             {
185                 Node JavaDoc refNode = refList.item(i);
186                 Node JavaDoc newNode = newList.item(i);
187
188                 checkNodes(refNode, newNode);
189
190                 refNode = refNode.getFirstChild();
191                 newNode = newNode.getFirstChild();
192
193             }
194         }
195         catch (ParserConfigurationException JavaDoc e)
196         {
197             e.printStackTrace(System.out);
198         }
199         catch (FactoryConfigurationError JavaDoc factoryConfigurationError)
200         {
201             factoryConfigurationError.printStackTrace(System.out);
202         }
203         catch (SAXException JavaDoc e)
204         {
205             e.printStackTrace(System.out);
206         }
207         catch (IOException e)
208         {
209             e.printStackTrace(System.out);
210         }
211     }
212
213     private void checkNodes(Node JavaDoc refNodeStart, Node JavaDoc newNodeStart)
214     {
215         Node JavaDoc refNode = refNodeStart;
216         Node JavaDoc newNode = newNodeStart;
217         while (refNode != null)
218         {
219             assertNotNull(newNode);
220             if (refNode.getNodeType() != Node.TEXT_NODE)
221             {
222                 // check matching names
223
System.out.println(refNode.getNodeName() + " : "
224                         + newNode.getNodeName());
225                 assertEquals(refNode.getNodeName(), newNode.getNodeName());
226                 // check matching attributes
227
NamedNodeMap JavaDoc refNnm = refNode.getAttributes();
228                 NamedNodeMap JavaDoc newNnm = newNode.getAttributes();
229                 for (int j = 0; j < refNnm.getLength(); j++)
230                 {
231                     Node JavaDoc refItem = refNnm.item(j);
232                     String JavaDoc refName = refItem.getNodeName();
233
234                     Node JavaDoc newItem = newNnm.getNamedItem(refName);
235                     // check existance
236
assertNotNull(newItem);
237
238                     // check matching value
239
System.out.println(" " + refName + " : "
240                             + refItem.getNodeValue()+" -> "
241                             + newItem.getNodeValue());
242                     assertEquals(refItem.getNodeValue(), newItem.getNodeValue());
243                 }
244             }
245
246             Node JavaDoc refChild = refNode.getFirstChild();
247             Node JavaDoc newChild = newNode.getFirstChild();
248             if (refChild != null)
249             {
250                 assertNotNull(newChild);
251                 checkNodes(refChild, newChild);
252             }
253
254             // check matching siblings
255
refNode = refNode.getNextSibling();
256             newNode = newNode.getNextSibling();
257         }
258     }
259
260     // just for internal test
261
public static void main(String JavaDoc args[])
262     {
263         SQLToAppDataRuntimeTest test = new SQLToAppDataRuntimeTest("inner test");
264         //test.compareXmlFiles("c:/schema.sql.xml", "c:/schema.sql.xml.new");
265
test.compareXmlFiles(
266                 "C:/java/projects/jakarta-turbine-torque/jakarta-turbine-torque/target/test/rttest/sql/schema.sql.ref.xml",
267                 "C:/java/projects/jakarta-turbine-torque/jakarta-turbine-torque/target/test/rttest/sql/schema.sql.xml");
268     }
269 }
270
Popular Tags