Java tutorial
/** * Copyright (c) 2005-2010 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: DbUnitUtils.java 1186 2010-08-29 16:34:10Z calvinxiu $ */ package org.springside.modules.test.utils; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import javax.sql.DataSource; import org.dbunit.DatabaseUnitException; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.ext.h2.H2Connection; import org.dbunit.operation.DatabaseOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springside.modules.utils.PropertiesUtils; /** * DBUnit?H2???. */ public class DbUnitUtils { private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class); private static ResourceLoader resourceLoader = new DefaultResourceLoader(); /** * ?XML?H2?. * * XML?????. * * @param xmlFilePaths ?Spring Resource?. */ public static void loadData(DataSource h2DataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.CLEAN_INSERT, h2DataSource, xmlFilePaths); } /** * ?XML?H2?. */ public static void appendData(DataSource h2DataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.INSERT, h2DataSource, xmlFilePaths); } /** * H2?XML???. */ public static void removeData(DataSource h2DataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.DELETE_ALL, h2DataSource, xmlFilePaths); } /** * DBUnit OperationXML??. * * @param xmlFilePaths ?Spring Resource?. */ private static void execute(DatabaseOperation operation, DataSource h2DataSource, String... xmlFilePaths) throws DatabaseUnitException, SQLException { IDatabaseConnection connection = new H2Connection(h2DataSource.getConnection(), ""); for (String xmlPath : xmlFilePaths) { try { InputStream input = resourceLoader.getResource(xmlPath).getInputStream(); IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(input); operation.execute(connection, dataSet); } catch (IOException e) { logger.warn(xmlPath + " file not found", e); } } } }