cz.muni.fi.editor.database.test.helpers.DatabaseConfigurationTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.database.test.helpers.DatabaseConfigurationTest.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * 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 cz.muni.fi.editor.database.test.helpers;

import com.github.springtestdbunit.bean.DatabaseConfigBean;
import com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean;
import cz.muni.fi.editor.database.helpers.DataSourceConfiguration;
import cz.muni.fi.editor.database.helpers.HibernateConfiguration;
import lombok.extern.log4j.Log4j2;
import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 8.8.2016.
 */
@Configuration
@ComponentScan("cz.muni.fi.editor.database")
@PropertySources({ @PropertySource("classpath:config/hibernate-test.properties"),
        @PropertySource("classpath:config/jdbc-test.properties"),
        @PropertySource("classpath:config/database-test.properties") })
@EnableTransactionManagement(proxyTargetClass = true)
@Import({ DataSourceConfiguration.class, HibernateConfiguration.class })
@Log4j2
public class DatabaseConfigurationTest {
    private LocalContainerEntityManagerFactoryBean emf;
    private DataSource dataSource;

    @Autowired
    public DatabaseConfigurationTest(LocalContainerEntityManagerFactoryBean emf, DataSource dataSource) {
        this.emf = emf;
        this.dataSource = dataSource;
    }

    @Bean
    public EnhancedRandomFactory enhancedRandomFactory() {
        return new EnhancedRandomFactory();
    }

    @Bean
    public JpaTransactionManager jpaTransactionManager() {
        return new JpaTransactionManager(emf.getObject());
    }

    @Bean(name = "dbUnitDatabaseConfig")
    public DatabaseConfigBean databaseConfigBean() {
        DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();
        databaseConfigBean.setDatatypeFactory(new HsqldbDataTypeFactory());

        return databaseConfigBean;
    }

    @Bean(name = "dbUnitDatabaseConnection")
    public DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactoryBean() {
        DatabaseDataSourceConnectionFactoryBean factoryBean = new DatabaseDataSourceConnectionFactoryBean(
                dataSource);
        factoryBean.setDatabaseConfig(databaseConfigBean());

        return factoryBean;
    }
}