Skip to content

Instantly share code, notes, and snippets.

@skrymets
Created July 11, 2018 12:17
Show Gist options
  • Save skrymets/81bc2b98b0f6de7c59bac094b23f6bdd to your computer and use it in GitHub Desktop.
Save skrymets/81bc2b98b0f6de7c59bac094b23f6bdd to your computer and use it in GitHub Desktop.
Dump H2 DB in JUnit tests
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractTest {
private static final String TEST_PU = "test_PU";
private static final Logger LOG = LoggerFactory.getLogger(AbstractTest.class);
private static EntityManagerFactory entityManagerFactory;
protected EntityManager entityManager;
@BeforeClass
public static void setUpClass() {
entityManagerFactory = Persistence.createEntityManagerFactory(TEST_PU);
}
@AfterClass
public static void tearDownClass() {
entityManagerFactory.close();
}
@Before
public void setUp() {
entityManager = entityManagerFactory.createEntityManager();
}
@After
public void tearDown() {
// <version.hibernate>5.3.2.Final</version.hibernate>
try {
org.hibernate.Session session = getEntityManager().unwrap(org.hibernate.Session.class);
org.hibernate.engine.spi.SessionFactoryImplementor sfImpl = (org.hibernate.engine.spi.SessionFactoryImplementor) session.getSessionFactory();
java.sql.Connection jdbcConnection = sfImpl
.getJdbcServices()
.getBootstrapJdbcConnectionAccess()
.obtainConnection();
try (java.sql.Statement jdbcStatement = jdbcConnection.createStatement()) {
java.sql.ResultSet rs = jdbcStatement.executeQuery("SCRIPT SIMPLE NODATA TO './DUMP.sql';");
}
} catch (java.sql.SQLException e) {
LOG.error(e.getMessage());
}
entityManager.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>udm</groupId>
<artifactId>udm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.hibernate>5.3.2.Final</version.hibernate>
</properties>
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${version.hibernate}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${version.hibernate}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment