From: <sc...@hy...> - 2008-03-09 21:37:38
|
Author: scottmf Date: 2008-03-09 14:37:34 -0700 (Sun, 09 Mar 2008) New Revision: 8148 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=8148 Added: trunk/src/org/hyperic/util/unittest/server/UnitTestDBException.java trunk/thirdparty/lib/dbunit-2.2.jar Modified: trunk/src/org/hyperic/util/unittest/server/BaseServerTestCase.java Log: adding db dump / restore functionality to unit testing framework Modified: trunk/src/org/hyperic/util/unittest/server/BaseServerTestCase.java =================================================================== --- trunk/src/org/hyperic/util/unittest/server/BaseServerTestCase.java 2008-03-09 21:03:34 UTC (rev 8147) +++ trunk/src/org/hyperic/util/unittest/server/BaseServerTestCase.java 2008-03-09 21:37:34 UTC (rev 8148) @@ -26,7 +26,31 @@ package org.hyperic.util.unittest.server; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; +import org.dbunit.DatabaseUnitException; +import org.dbunit.database.DatabaseConnection; +import org.dbunit.database.IDatabaseConnection; +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSet; +import org.dbunit.operation.DatabaseOperation; +import org.hyperic.util.jdbc.DBUtil; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; + import junit.framework.TestCase; /** @@ -38,7 +62,16 @@ */ public abstract class BaseServerTestCase extends TestCase { + private static final String logCtx = BaseServerTestCase.class.getName(); + private static final String DUMP_FILE = "dbdump.xml.gz"; + /** + * Path designated for file in the current run + * contents may be deleted while not running + */ + private static final String WORKING_DIR = "/tmp"; + + /** * The environment variable specifying the path to the jboss deployment * that will be used for unit testing. */ @@ -52,7 +85,101 @@ private static ServerLifecycle server; + protected Connection getConnection(boolean forRestore) + throws UnitTestDBException { + try { + File file = new File("server/default/deploy/hq-ds.xml"); + Document doc = new SAXBuilder().build(file); + Element element = + doc.getRootElement().getChild("local-tx-datasource"); + String url = element.getChild("connection-url").getText(); + String user = element.getChild("user-name").getText(); + String passwd = element.getChild("password").getText(); + String driverClass = element.getChild("driver-class").getText(); + if (forRestore && driverClass.toLowerCase().contains("mysql")) { + String buf = "?"; + if (driverClass.toLowerCase().contains("?")) { + buf = "&"; + } + driverClass = driverClass + + buf + "sessionVariables=FOREIGN_KEY_CHECKS=0"; + } + Driver driver = (Driver)Class.forName(driverClass).newInstance(); + Properties props = new Properties(); + props.setProperty("user", user); + props.setProperty("password", passwd); + return driver.connect(url, props); + } catch (JDOMException e) { + throw new UnitTestDBException(e); + } catch (IOException e) { + throw new UnitTestDBException(e); + } catch (InstantiationException e) { + throw new UnitTestDBException(e); + } catch (SQLException e) { + throw new UnitTestDBException(e); + } catch (IllegalAccessException e) { + throw new UnitTestDBException(e); + } catch (ClassNotFoundException e) { + throw new UnitTestDBException(e); + } + } + protected void restoreDatabase() + throws UnitTestDBException { + Connection conn = null; + Statement stmt = null; + try { + conn = getConnection(true); + IDatabaseConnection idbConn = new DatabaseConnection(conn); + conn.setAutoCommit(false); + stmt = conn.createStatement(); + // this is done for MySQL via another method + if (DBUtil.isPostgreSQL(conn)) { + stmt.execute("set constraints all deferred"); + } else if (DBUtil.isOracle(conn)) { + stmt.execute("alter session set constraints = deferred"); + } + IDataSet dataset = new FlatXmlDataSet(new GZIPInputStream( + new FileInputStream(WORKING_DIR+"/"+DUMP_FILE))); + DatabaseOperation.CLEAN_INSERT.execute(idbConn, dataset); + conn.commit(); + } catch (SQLException e) { + throw new UnitTestDBException(e); + } catch (DataSetException e) { + throw new UnitTestDBException(e); + } catch (FileNotFoundException e) { + throw new UnitTestDBException(e); + } catch (IOException e) { + throw new UnitTestDBException(e); + } catch (DatabaseUnitException e) { + throw new UnitTestDBException(e); + } finally { + DBUtil.closeJDBCObjects(logCtx, conn, stmt, null); + } + } + + protected void dumpDatabase(Connection conn) + throws UnitTestDBException { + try { + IDatabaseConnection idbConn = new DatabaseConnection(conn); + IDataSet fullDataSet; + fullDataSet = idbConn.createDataSet(); + GZIPOutputStream gstream = + new GZIPOutputStream( + new FileOutputStream(WORKING_DIR+"/"+DUMP_FILE)); + FlatXmlDataSet.write(fullDataSet, gstream); + gstream.finish(); + } catch (SQLException e) { + throw new UnitTestDBException(e); + } catch (FileNotFoundException e) { + throw new UnitTestDBException(e); + } catch (IOException e) { + throw new UnitTestDBException(e); + } catch (DataSetException e) { + throw new UnitTestDBException(e); + } + } + public BaseServerTestCase(String name) { super(name); } Added: trunk/src/org/hyperic/util/unittest/server/UnitTestDBException.java =================================================================== --- trunk/src/org/hyperic/util/unittest/server/UnitTestDBException.java (rev 0) +++ trunk/src/org/hyperic/util/unittest/server/UnitTestDBException.java 2008-03-09 21:37:34 UTC (rev 8148) @@ -0,0 +1,46 @@ +/* + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2004-2008], Hyperic, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.util.unittest.server; + +public class UnitTestDBException extends Exception { + + public UnitTestDBException() { + super(); + } + + public UnitTestDBException(String msg) { + super(msg); + } + + public UnitTestDBException(Throwable ex) { + super(ex); + } + + public UnitTestDBException(String msg, Throwable ex) { + super(msg, ex); + } + +} Added: trunk/thirdparty/lib/dbunit-2.2.jar =================================================================== (Binary files differ) Property changes on: trunk/thirdparty/lib/dbunit-2.2.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream |