Skip to content

Instantly share code, notes, and snippets.

@omerucel
Last active December 14, 2015 17:48
Show Gist options
  • Save omerucel/5124422 to your computer and use it in GitHub Desktop.
Save omerucel/5124422 to your computer and use it in GitHub Desktop.
Veritabanı ile ilgili işlemler içeren testlerde extend edilecek sınıf.
package com.risenlabs;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.BeforeClass;
import org.yaml.snakeyaml.Yaml;
public class DatabaseTest {
public static SessionFactory sessionFactory;
@BeforeClass
public static void setUp() throws IOException
{
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Yaml yaml = new Yaml();
@SuppressWarnings("unchecked")
Map<String, List<Object>> all = (Map<String, List<Object>>) yaml.load(FileUtils.readFileToString(new File("src/test/resources/datasource.yaml")));
sessionFactory.getCurrentSession().beginTransaction();
for (Object user : all.get("users")) {
sessionFactory.getCurrentSession().save(user);
}
for (Object session : all.get("sessions")) {
sessionFactory.getCurrentSession().save(session);
}
sessionFactory.getCurrentSession().getTransaction().commit();
}
}
users:
- !!com.risenlabs.auth.model.User
id: 1
email: "[email protected]"
password: "0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e" # sha256 password1
- !!com.risenlabs.auth.model.User
id: 2
email: "[email protected]"
password: "0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e" # sha256 password1
sessions:
- !!com.risenlabs.auth.model.Session
id: 1
sessionKey: "108129fad82080669d9f2df2fa9b5ae5951ff073e5bd8fcdff6b7bf3877f27af" # sha256 sessionkey1
user: !!com.risenlabs.auth.model.User
id: 1
- !!com.risenlabs.auth.model.Session
id: 2
sessionKey: "69abb33504df9501dde9808d2b28aa07bb7fae96addee5577b684a8a81ed56ef" # sha256 sessionkey2
user: !!com.risenlabs.auth.model.User
id: 2
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:app;MODE=MYSQL</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="com.tutorial.auth.model.User" />
<mapping class="com.tutorial.client.model.UserInfo" />
</session-factory>
</hibernate-configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment