package de.superx.bin; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Vector; import org.apache.commons.io.FileUtils; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; import com.meterware.httpunit.WebTable; import de.memtext.db.ConnectionCreator; import de.memtext.util.EqualsUtil; import de.memtext.util.XMLUtils; public class SxXmlTester { Document document; List webserverlist = new LinkedList(); List dbserverConnections = new LinkedList(); StringBuffer errors = new StringBuffer(); private void readdbServers() throws IOException, SQLException, ClassNotFoundException { NodeList nl = document.getElementsByTagName("dbserver"); for (int i = 0; i < nl.getLength(); i++) { Node serverNode = (Node) nl.item(i); Connection con = ConnectionCreator.getConnectionCryptPassword(XMLUtils.getAttribValue(serverNode, "db_properties"), "driverName", "connectionURL", "connectionName", "connectionPassword"); System.out.println("Found 1 db.properties node"); dbserverConnections.add(con); } } private void readWebServers() { NodeList nl = document.getElementsByTagName("webserver"); for (int i = 0; i < nl.getLength(); i++) { Node serverNode = (Node) nl.item(i); webserverlist.add(XMLUtils.getAttribValue(serverNode, "url")); System.out.println("Found 1 webserver node"); } } private void runDbTests() throws IOException, SQLException, ClassNotFoundException { readdbServers(); NodeList nl = document.getElementsByTagName("dbtest"); for (int i = 0; i < nl.getLength(); i++) { Node testNode = (Node) nl.item(i); String name = XMLUtils.getAttribValue(testNode, "name"); System.out.println("Db-Testnr " + (i + 1) + " :" + name); String sql = XMLUtils.getAttribValue(testNode, "sql"); for (Iterator it = dbserverConnections.iterator(); it.hasNext();) { Connection con = (Connection) it.next(); Statement stm = con.createStatement(); ResultSet rs = stm.executeQuery(sql); Vector result = new Vector(); int colcount = 0; while (rs.next()) { colcount = rs.getMetaData().getColumnCount(); Vector row = new Vector(); for (int col = 1; col <= colcount; col++) { row.add(rs.getObject(col)); } result.add(row); } for (Iterator it2 = XMLUtils.getChildNodeIterator(testNode); it2.hasNext();) { Node childNode = (Node) it2.next(); if (childNode.getNodeName().equals("valuetest")) { String rowStr = XMLUtils.getAttribValue(childNode, "row"); int row = Integer.parseInt(rowStr); String colStr = XMLUtils.getAttribValue(childNode, "col"); int col = Integer.parseInt(colStr); String val = XMLUtils.getAttribValue(childNode, "val"); if (col > colcount) { errors.append("Test " + name + " Versuch Spalte " + col + " zu lesen, Tabelle hat nur " + colcount + " Spalten\n"); continue; } if (row > result.size()) { errors.append("Test " + name + " Versuch Zeile " + row + " zu lesen, ResultSet hat nur " + result.size() + " Zeilen\n"); continue; } Vector arow = (Vector) result.get(row - 1); String gefunden = arow.get(col - 1).toString(); if (!EqualsUtil.areEqual(val, gefunden)) { errors.append("Test " + name + " Falscher Wert Zeile " + row + " Spalte " + col + " erwartet:" + val + " gefunden:" + gefunden + "\n(sql:" + sql + "\n\n"); } } } } } } public void go(String args[]) throws SAXException, IOException, SQLException, ClassNotFoundException { document = XMLUtils.buildDocument(new File(args[0])); runDbTests(); runWebtests(); if (errors.length() > 0) { System.out.println("Es sind Fehler aufgetaucht siehe output-errors.txt"); FileUtils.writeStringToFile(new File("output-errors.txt"), new Date() + "\n" + errors.toString()); } else { System.out.println("Keine Fehler aufgefallen"); } } private void runWebtests() throws SAXException, IOException { readWebServers(); NodeList nl = document.getElementsByTagName("webtest"); WebConversation wc = new WebConversation(); for (int i = 0; i < nl.getLength(); i++) { Node testNode = (Node) nl.item(i); String name = XMLUtils.getAttribValue(testNode, "name"); System.out.println("Webserver-Testnr " + (i + 1) + " :" + name); String params = XMLUtils.getChildNodeValue(testNode, "params"); for (Iterator it = webserverlist.iterator(); it.hasNext();) { String serverUrl = (String) it.next(); serverUrl += params; WebRequest req = new GetMethodWebRequest(serverUrl); WebResponse resp; try { resp = wc.getResponse(req); } catch (IOException e) { System.err.println("Aufruf fehlgeschlagen von " + serverUrl); throw e; } FileUtils.writeStringToFile(new File("output_" + name + ".htm"), resp.getText()); if (resp.getTables().length < 2) { errors.append("Test " + name + " Keine Ergebnistabelle geliefert\n "); continue; } for (Iterator it2 = XMLUtils.getChildNodeIterator(testNode); it2.hasNext();) { Node childNode = (Node) it2.next(); if (childNode.getNodeName().equals("valuetest")) { String rowStr = XMLUtils.getAttribValue(childNode, "row"); int row = Integer.parseInt(rowStr); String colStr = XMLUtils.getAttribValue(childNode, "col"); int col = Integer.parseInt(colStr); String val = XMLUtils.getAttribValue(childNode, "val"); WebTable table = resp.getTables()[1]; if (col > table.getColumnCount()) { FileUtils.writeStringToFile(new File("output_" + name + ".htm"), resp.getText()); errors.append("Test " + name + " Versuch Spalte " + col + " zu lesen, Tabelle hat nur " + table.getColumnCount() + " Spalten\n"); continue; } if (row > table.getRowCount()) { FileUtils.writeStringToFile(new File("output_" + name + ".htm"), resp.getText()); errors.append("Test " + name + " Versuch Zeile " + row + " zu lesen, Tabelle hat nur " + table.getRowCount() + " Zeilen\n"); continue; } String gefunden = table.getTableCell(row - 1, col - 1).getText(); if (!EqualsUtil.areEqual(val, gefunden)) { errors.append("Test " + name + " Falscher Wert Zeile " + row + " Spalte " + col + " erwartet:" + val + " gefunden:" + gefunden + "\n(url:" + serverUrl + "\n\n"); } } } } } } public static void main(String[] args) { System.out.println(""); if (args.length != 1) { System.err.println("usage SxXmlTester test.xml"); System.exit(-1); } try { new SxXmlTester().go(args); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } // junit.textui.TestRunner.run(AutoTest.class); } //Created on 27.04.2006 at 10:25:34