com.github.dactiv.fear.commons.test.ServiceTestCaseSupport.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.commons.test.ServiceTestCaseSupport.java

Source

/*
 * Copyright 2015 dactiv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.dactiv.fear.commons.test;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * ?
 * @author maurice
 */
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
public class ServiceTestCaseSupport {

    private DataSource dataSource;

    protected JdbcTemplate jdbcTemplate;
    private ResourceLoader resourceLoader = new DefaultResourceLoader();

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /**
     * ??
     *
     * @param tableName ??
     *
     * @return int
     */
    protected int countRowsInTable(String tableName) {
        return jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class);
    }

    /**
     *
     * ??? dataSource 
     *
     * @param sqlResourcePaths sql 
     *
     * @throws SQLException
     */
    public void install(String... sqlResourcePaths) throws SQLException {
        executeScript(dataSource.getConnection(), sqlResourcePaths);
    }

    /**
     * ?sql
     *
     * @param connection jdbc 
     * @param sqlResourcePaths sql 
     *
     * @throws SQLException
     */
    private void executeScript(Connection connection, String... sqlResourcePaths) throws SQLException {

        for (String sqlResourcePath : sqlResourcePaths) {
            Resource resource = resourceLoader.getResource(sqlResourcePath);
            ScriptUtils.executeSqlScript(connection, resource);
        }
        connection.close();
    }

    @Test
    public void empty() {
        Assert.assertTrue(true);
    }
}