org.springside.modules.test.data.H2Fixtures.java Source code

Java tutorial

Introduction

Here is the source code for org.springside.modules.test.data.H2Fixtures.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.springside.modules.test.data;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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.springframework.jdbc.core.JdbcTemplate;
import org.springside.modules.utils.Exceptions;

/**
 * DBUnit??H2?.
 */
public class H2Fixtures {

    private static Logger logger = LoggerFactory.getLogger(H2Fixtures.class);
    private static ResourceLoader resourceLoader = new DefaultResourceLoader();

    private H2Fixtures() {
    }

    /**
     * ?XML??.
     *  
     * @param xmlFilePaths ?Spring Resource?.
     */
    public static void loadData(DataSource dataSource, String... xmlFilePaths) throws Exception {
        execute(DatabaseOperation.INSERT, dataSource, xmlFilePaths);
    }

    /**
     * XML???, ??XML??.
     * 
     * reloadAllTable?, ????.
     * 
     * @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 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);

        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);
            }
        }
    }

    protected static H2Connection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException {
        return new H2Connection(dataSource.getConnection(), null);
    }

    /**
     * ??, ??XML??.
     * 
     * @param xmlFilePaths ?Spring Resource?.
     */
    public static void reloadAllTable(DataSource dataSource, String... xmlFilePaths) throws Exception {
        deleteAllTable(dataSource);
        loadData(dataSource, xmlFilePaths);
    }

    /**
     * ,excludeTables.disable.
     */
    public static void deleteAllTable(DataSource dataSource, String... excludeTables) {

        List<String> tableNames = new ArrayList<String>();
        ResultSet rs = null;
        try {
            rs = dataSource.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" });
            while (rs.next()) {
                String tableName = rs.getString("TABLE_NAME");
                if (Arrays.binarySearch(excludeTables, tableName) < 0) {
                    tableNames.add(tableName);
                }
            }

            deleteTable(dataSource, tableNames.toArray(new String[tableNames.size()]));
        } catch (SQLException e) {
            Exceptions.unchecked(e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * , disable.
     */
    public static void deleteTable(DataSource dataSource, String... tableNames) {
        JdbcTemplate template = new JdbcTemplate(dataSource);

        template.update("SET REFERENTIAL_INTEGRITY FALSE");

        for (String tableName : tableNames) {
            template.update("DELETE FROM " + tableName);
        }

        template.update("SET REFERENTIAL_INTEGRITY TRUE");
    }
}