package foo.bar.entities;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import database.Manager;
import database.HibernateUtil;
import junit.framework.TestCase;

public class TestEntities extends TestCase {

	private String database = "test.db12";
	private Session session = null;
	private SessionFactory sessionFactory = null;

	@SuppressWarnings("unused")
	private void createSessionFactory() {
		// A SessionFactory is set up once for an application!
		final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
		sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
	}

	public void createSessionFactory(String filename) {
		Configuration configuration = HibernateUtil.createConfiguration("hibernate.cfg.xml");
		configuration.setProperty("hibernate.connection.url", "jdbc:sqlite:" + filename);
		sessionFactory = configuration.buildSessionFactory();
	}

	protected void setUp() {
		createSessionFactory(database);
		session = sessionFactory.openSession();
		session.beginTransaction();
	}

	public Event createEvent(String title, Date date) {
		return new Event(title, date);
	}

	public void testMain() {

		assertTrue(new File(database).isFile());

		Manager<Event> manager = Manager.create(Event.class, session);

		// Creating entities that will be saved to the sqlite database
		Event hello = createEvent("Hello", new Date());
		Event world = createEvent("World", new Date());

		// Saving to the database
		manager.save(hello);
		manager.save(world);

		// Committing the change in the database.
		session.getTransaction().commit();

		List<Event> result = manager.getAll();

		// Fetching saved data
		// List<Event> result = session.createQuery("from Event").list();

		assertTrue(result != null);
		assertTrue(result.size() == 2);
		assertTrue(result.get(0).getTitle() == "Hello");
		assertTrue(result.get(1).getTitle() == "World");

		// session.getTransaction().rollback();

	}

	protected void tearDown() {
		session.close();
		sessionFactory.close();
		new File(database).delete();
	}

}
@@ -87,17 +87,16 @@
 l.List;%0A
-%0A
 import o
@@ -158,178 +158,8 @@
 ry;%0A
-import org.hibernate.boot.MetadataSources;%0Aimport org.hibernate.boot.registry.StandardServiceRegistry;%0Aimport org.hibernate.boot.registry.StandardServiceRegistryBuilder;%0A
 impo
@@ -194,17 +194,16 @@
 ration;%0A
-%0A
 import d
@@ -455,326 +455,8 @@
 l;%0A%0A
-%09@SuppressWarnings(%22unused%22)%0A%09private void createSessionFactory() %7B%0A%09%09// A SessionFactory is set up once for an application!%0A%09%09final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();%0A%09%09sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();%0A%09%7D%0A%0A
 %09pub
@@ -571,25 +571,20 @@
 ration(%22
-hiberna
 te
+st
 .cfg.xml

Resolve hibernate.cfg.xml conflict.