eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest.java Source code

Java tutorial

Introduction

Here is the source code for eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest.java

Source

/*
 * Copyright (C) 2013 DASISH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package eu.dasish.annotation.backend.dao.impl;

import eu.dasish.annotation.backend.Helpers;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;

/**
 *
 * @author olhsha
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/spring-test-config/dataSource.xml" })
public class JdbcResourceDaoTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static String getNormalisedSql() throws FileNotFoundException, URISyntaxException {
        // remove the unsupported sql for the test
        final URL sqlUrl = JdbcResourceDaoTest.class.getResource("/sql/DashishAnnotatorCreate.sql");
        String sqlString = new Scanner(new File(sqlUrl.toURI()), "UTF8").useDelimiter("\\Z").next();
        for (String unknownToken : new String[] { "SET client_encoding", "CREATE DATABASE", "\\\\connect",
                "SET default_with_oids",
                //"ALTER SEQUENCE",
                //ALTER TABLE ONLY",
                //"ADD CONSTRAINT",
                //"CREATE INDEX", 
                // "ALTER TABLE ONLY [a-z]* ALTER COLUMN",
                // "ALTER TABLE ONLY [^A]* ADD CONSTRAINT"
        }) {
            sqlString = sqlString.replaceAll(unknownToken, "-- " + unknownToken);
        }

        sqlString = sqlString.replaceAll("bytea", "blob");
        sqlString = sqlString.replaceAll("SERIAL NOT NULL", "INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
        sqlString = sqlString.replaceAll("\\(current_timestamp AT TIME ZONE 'UTC'\\)",
                "current_timestamp AT TIME ZONE INTERVAL '00:00' HOUR TO MINUTE");
        return sqlString;
    }

    public static String getTestDataInsertSql() throws FileNotFoundException, URISyntaxException {
        final URL sqlUrl = JdbcResourceDaoTest.class.getResource("/test-data/InsertTestData.sql");
        String sqlString = new Scanner(new File(sqlUrl.toURI()), "UTF8").useDelimiter("\\Z").next();
        return sqlString;
    }

    @Before
    public void setUp() throws DataAccessException, FileNotFoundException, URISyntaxException {
        jdbcTemplate.execute("DROP SCHEMA PUBLIC CASCADE");
        // consume the DashishAnnotatorCreate sql script to create the database
        jdbcTemplate.execute(getNormalisedSql());
        jdbcTemplate.execute(getTestDataInsertSql());
    }

    @After
    public void tearDown() {
    }

    @Test
    public void dummy() {

    }

}