org.asqatasun.websnapshot.utils.AbstractDaoTestCase.java Source code

Java tutorial

Introduction

Here is the source code for org.asqatasun.websnapshot.utils.AbstractDaoTestCase.java

Source

/*
 * Web-Snapshot
 * Copyright (C) 2008-2016  Asqatasun.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact us by mail: asqatasun AT asqatasun DOT org
 */
package org.asqatasun.websnapshot.utils;

import java.io.FileInputStream;
import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 *
 * @author alingua
 */
public abstract class AbstractDaoTestCase extends DBTestCase {

    /**
     * driver JDBC
     */
    private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
    /**
     * base de donnes HSQLDB nomme "database" qui fonctionne en mode mmoire
     */
    private static final String DATABASE = "jdbc:hsqldb:websnapshot";
    /**
     * utilisateur qui se connecte  la base de donnes
     */
    private static final String USER = "sa";
    /**
     * getDataSet mot de passe pour se connecter  la base de donnes
     */
    private static final String PASSWORD = "";
    private static final String SPRING_FILE_PATH = "src/test/resources/conf/spring/application-context.xml";
    private String inputDataFileName = "";
    protected BeanFactory springBeanFactory;

    public AbstractDaoTestCase(String testName, String inputDataFileName) {
        super(testName);

        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, JDBC_DRIVER);
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, DATABASE);
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, USER);
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, PASSWORD);
        ApplicationContext springApplicationContext = new FileSystemXmlApplicationContext(SPRING_FILE_PATH);
        springBeanFactory = springApplicationContext;
        this.inputDataFileName = inputDataFileName;
    }

    //    public Collection<String> asIdList(Collection<? extends Entity> entities) {
    //        Collection<String> ids = new ArrayList<String>(entities.size());
    //
    //        for (Iterator<? extends Entity> it = entities.iterator(); it.hasNext();) {
    //            ids.add(it.next().name());
    //        }
    //        return ids;
    //    }
    /**
     * Override method to set custom properties/features {@inheritDoc}
     */
    @Override
    protected void setUpDatabaseConfig(DatabaseConfig config) {
        super.setUpDatabaseConfig(config);
        config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, new Integer(97));
    }

    /**
     * Charge le jeu de donnes  partir d'un fichier XML d'import
     */
    @Override
    protected IDataSet getDataSet() throws Exception {
        FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
        FlatXmlDataSet loadedDataSet = flatXmlDataSetBuilder.build(new FileInputStream(getInputDataFileName()));
        return loadedDataSet;

    }

    @Override
    protected DatabaseOperation getSetUpOperation() throws Exception {
        return DatabaseOperation.CLEAN_INSERT;
    }

    @Override
    protected DatabaseOperation getTearDownOperation() throws Exception {
        return DatabaseOperation.DELETE_ALL;
    }

    /**
     *
     * @return
     */
    public String getInputDataFileName() {
        return inputDataFileName;
    }

    /**
     *
     * @param inputDataFileName
     */
    public void setInputDataFileName(String inputDataFileName) {
        this.inputDataFileName = inputDataFileName;
    }
}