1 package org.apache.torque.engine.database.transform; 2 3 18 19 import java.io.*; 20 import java.util.Vector ; 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.FactoryConfigurationError ; 24 import javax.xml.parsers.ParserConfigurationException ; 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 ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.xml.sax.SAXException ; 34 35 39 public class SQLToAppDataRuntimeTest extends BaseRuntimeTestCase 40 { 41 42 private static final String SQL_FOLDER = "target/test/rttest/sql"; 43 44 private Vector files; 45 46 49 public SQLToAppDataRuntimeTest(String name) 50 { 51 super(name); 52 } 53 54 public void setUp() 55 { 56 files = new Vector (); 60 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 filename = file.getAbsolutePath(); 90 SQLToAppData s2a = new SQLToAppData(filename); 92 Database ad = s2a.execute(); 93 String xmlFilename = filename + ".xml"; 95 PrintWriter out = new PrintWriter( 96 new FileOutputStream(xmlFilename, false),true); 97 out.println(ad); 98 out.close(); 99 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 sFile) 114 { 115 try 116 { 117 BufferedReader reader = new BufferedReader(new FileReader(sFile)); 118 String line; 119 StringBuffer sb = new StringBuffer (5000); 120 while ((line=reader.readLine())!=null) 121 { 122 sb.append(line).append("\n"); 123 } 124 reader.close(); 125 String 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 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 refFile, String newFile) 156 { 157 System.out.println("Comparing " + newFile + " against " + refFile); 158 renameDTD(refFile); 164 renameDTD(newFile); 165 166 try 167 { 168 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 169 docFactory.setExpandEntityReferences(false); 170 docFactory.setValidating(false); 171 172 DocumentBuilder doc = docFactory.newDocumentBuilder(); 173 174 Document refDoc = doc.parse(new File(refFile)); 175 Document newDoc = doc.parse(new File(newFile)); 176 177 NodeList refList = refDoc.getElementsByTagName("database"); 178 NodeList newList = newDoc.getElementsByTagName("database"); 179 180 assertNotNull(refList); 181 assertNotNull(newList); 182 183 for (int i = 0; i < refList.getLength(); i++) 184 { 185 Node refNode = refList.item(i); 186 Node newNode = newList.item(i); 187 188 checkNodes(refNode, newNode); 189 190 refNode = refNode.getFirstChild(); 191 newNode = newNode.getFirstChild(); 192 193 } 194 } 195 catch (ParserConfigurationException e) 196 { 197 e.printStackTrace(System.out); 198 } 199 catch (FactoryConfigurationError factoryConfigurationError) 200 { 201 factoryConfigurationError.printStackTrace(System.out); 202 } 203 catch (SAXException 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 refNodeStart, Node newNodeStart) 214 { 215 Node refNode = refNodeStart; 216 Node newNode = newNodeStart; 217 while (refNode != null) 218 { 219 assertNotNull(newNode); 220 if (refNode.getNodeType() != Node.TEXT_NODE) 221 { 222 System.out.println(refNode.getNodeName() + " : " 224 + newNode.getNodeName()); 225 assertEquals(refNode.getNodeName(), newNode.getNodeName()); 226 NamedNodeMap refNnm = refNode.getAttributes(); 228 NamedNodeMap newNnm = newNode.getAttributes(); 229 for (int j = 0; j < refNnm.getLength(); j++) 230 { 231 Node refItem = refNnm.item(j); 232 String refName = refItem.getNodeName(); 233 234 Node newItem = newNnm.getNamedItem(refName); 235 assertNotNull(newItem); 237 238 System.out.println(" " + refName + " : " 240 + refItem.getNodeValue()+" -> " 241 + newItem.getNodeValue()); 242 assertEquals(refItem.getNodeValue(), newItem.getNodeValue()); 243 } 244 } 245 246 Node refChild = refNode.getFirstChild(); 247 Node newChild = newNode.getFirstChild(); 248 if (refChild != null) 249 { 250 assertNotNull(newChild); 251 checkNodes(refChild, newChild); 252 } 253 254 refNode = refNode.getNextSibling(); 256 newNode = newNode.getNextSibling(); 257 } 258 } 259 260 public static void main(String args[]) 262 { 263 SQLToAppDataRuntimeTest test = new SQLToAppDataRuntimeTest("inner test"); 264 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 |