Java tutorial
/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.micmiu.modules.test.data; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; import org.dbunit.DatabaseUnitException; import org.dbunit.database.DatabaseConnection; 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.ext.mysql.MySqlConnection; import org.dbunit.ext.oracle.OracleConnection; 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; /** * DBUnit???. */ public class DataFixtures { private static Logger logger = LoggerFactory.getLogger(DataFixtures.class); private static ResourceLoader resourceLoader = new DefaultResourceLoader(); /** * ??, ??XML??. * * @param xmlFilePaths ?Spring Resource?. */ public static void reloadData(DataSource dataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.CLEAN_INSERT, dataSource, xmlFilePaths); } /** * ?XML??. * * @param xmlFilePaths ?Spring Resource?. */ public static void loadData(DataSource dataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.INSERT, dataSource, xmlFilePaths); } /** * ?XML??. * * @param xmlFilePaths ?Spring Resource?. */ public static void deleteData(DataSource dataSource, String... xmlFilePaths) throws Exception { execute(DatabaseOperation.DELETE_ALL, dataSource, xmlFilePaths); } /** * XML??Operation. * * @param xmlFilePaths ?Spring Resource?. */ private static void execute(DatabaseOperation operation, DataSource dataSource, String... xmlFilePaths) throws DatabaseUnitException, SQLException { IDatabaseConnection connection = getConnection(dataSource); try { 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); } } } finally { if (connection != null) { connection.close(); } } } /** * DataSource?Connection(??)?url??Connection. */ protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException { Connection connection = dataSource.getConnection(); String dbName = connection.getMetaData().getURL(); if (StringUtils.contains(dbName, ":h2:")) { return new H2Connection(connection, null); } else if (StringUtils.contains(dbName, ":mysql:")) { return new MySqlConnection(connection, null); } else if (StringUtils.contains(dbName, ":oracle:")) { return new OracleConnection(connection, null); } else { return new DatabaseConnection(connection); } } }