com.xyxy.platform.modules.core.test.data.DataFixtures.java Source code

Java tutorial

Introduction

Here is the source code for com.xyxy.platform.modules.core.test.data.DataFixtures.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.xyxy.platform.modules.core.test.data;

import javax.sql.DataSource;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.jdbc.JdbcTestUtils;

/**
 * SQL?
 * 
 *
 */
public class DataFixtures {

    public static final String DEFAULT_ENCODING = "UTF-8";

    private static ResourceLoader resourceLoader = new DefaultResourceLoader();

    public static void executeScript(DataSource dataSource, String... sqlResourcePaths) throws DataAccessException {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        for (String sqlResourcePath : sqlResourcePaths) {
            Resource resource = resourceLoader.getResource(sqlResourcePath);
            JdbcTestUtils.executeSqlScript(jdbcTemplate, new EncodedResource(resource, DEFAULT_ENCODING), true);
        }
    }
}