com.evasion.plugin.junit.persistence.AbstractPersistentTest Maven / Gradle / Ivy
package com.evasion.plugin.junit.persistence;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang.StringUtils;
import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author sebastien.glon
*/
public abstract class AbstractPersistentTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(AbstractPersistentTest.class);
/** Suffixe pour les fichiers DBUnit */
private static final String DATASET_FILENAME_SUFFIX = "-dataset.xml";
// ======================================
// = Attributes =
// ======================================
protected static EntityManagerFactory emf;
protected static EntityManager em;
protected static EntityTransaction tx;
private static DataSource dataSource;
// ======================================
// = Lifecycle Methods =
// ======================================
public static void initDataSource() throws Exception{
try {
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName("${}");
basicDataSource.setUrl("${}");
basicDataSource.setUsername("${}");
basicDataSource.setPassword("${}");
dataSource = basicDataSource;
} catch (IOException ex) {
LOGGER.debug("Pas de fichier de conf de la base");
}
}
@BeforeClass
public static void initEntityManager() throws Exception {
emf = Persistence.createEntityManagerFactory("autreTestPU");
em = emf.createEntityManager();
initDataSource();
}
@AfterClass
public static void closeEntityManager() throws SQLException {
em.close();
emf.close();
}
@Before
public void initTransaction() {
tx = em.getTransaction();
}
@Before
public void initTestDataWithinTransaction() throws Exception {
if (dataSource == null) {
LOGGER.debug("pas d'Initializing de la dataSource");
return;
}
final IDatabaseTester databaseTester = new DataSourceDatabaseTester(
dataSource);
final IDataSet dataSet = loadDataSet();
if (dataSet != null) {
// DBUnit setup
LOGGER.debug("Initializing DBUnit DataSet");
databaseTester.setDataSet(dataSet);
databaseTester.setSetUpOperation(getDatabaseSetupOperation());
databaseTester.setTearDownOperation(getDatabaseTearDownOperation());
databaseTester.onSetup();
} else {
LOGGER.debug("pas d'Initializing DBUnit DataSet");
}
}
/**
* Returns the DBUnit DataSet for this test. By default it searches in the
* classpath for a file named : <ClassName>-dataset.xml
.
*
* @return the DBUnit dataSet for this test, or null if no dataSet is used.
* @throws Exception
* in case of an error.
*/
protected IDataSet loadDataSet() throws Exception {
IDataSet dataSet = null;
final URL url = Thread.currentThread().getContextClassLoader().getSystemResource(getDataSetFilename());
if (url==null) {
return dataSet;
}
final String dataSetFileName = url.getFile();
try {
final File file = new File(dataSetFileName);
dataSet = getXmlDataSet(file);
} catch (FileNotFoundException e) {
// Dataset are not mandatory, so a FileNotFoundException is
// acceptable, just inform the user
LOGGER.info("No dataset named '" + dataSetFileName
+ "' was found in the classpath.");
}
return dataSet;
}
/**
* Methode retournant le XMLDataSet sous forme de flat A surcharger pour
* changer de type de fichier
*
* @param file
* le fichier XML
* @return le XML Data Set
* @throws DataSetException
* si il y a une erreur sur le dataSet
* @throws IOException
* si il y a une erreur au niveau de la lecture du fichier
*/
protected IDataSet getXmlDataSet(File file) throws DataSetException,
IOException {
return new FlatXmlDataSetBuilder().build(file);
}
/**
* Retourne le nom du fichier DataSet.
*
* @return le nom du fichier
*/
protected String getDataSetFilename() {
return StringUtils.replaceChars(this.getClass().getCanonicalName(),
'.', File.separatorChar)
+ DATASET_FILENAME_SUFFIX;
}
/**
* Returns the DBUnit Database operation used during setUp. Default is
* DatabaseOperation.INSERT
*
* @return the DBUnit Database operation
*/
protected DatabaseOperation getDatabaseSetupOperation() {
return DatabaseOperation.CLEAN_INSERT;
}
/**
* Returns the DBUnit Database operation used during tearDown. Default is
* DatabaseOperation.NONE
*
* @return the DBUnit Database operation
*/
protected DatabaseOperation getDatabaseTearDownOperation() {
return DatabaseOperation.NONE;
}
}