package com.gmscheduler.unittests;

import junit.framework.TestCase;
import junit.framework.Assert;
import com.gmscheduler.model.Person;
import com.gmscheduler.model.HibernateUtil;
import org.hibernate.Session;

public class PersonTest extends TestCase {
	private Person p1;
	private Person p2;
	private Person p3;
	private Person hp1;
	private Person hp2;
	private Long hp1id;
	private String s1,s2;

	protected void setUp() throws Exception {
		p1 = new Person("George");
		p2 = new Person(p1);
		p3 = new Person("Sally");
		hp1 = new Person("Petey");
		hp1.setEmail("JoMomma@Hotmomma.com");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(hp1);
		session.getTransaction().commit();

		hp1id = hp1.getId();
		//System.out.println(hp1id);
		
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Person hp2 = new Person((Person)session.load(Person.class, hp1id));
		session.getTransaction().commit();
		s2 = hp2.toString();
		
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testPersonEquals() {
		Assert.assertEquals(p1.toString(),p2.toString());
		Assert.assertTrue(!p1.equals(p3));
	}
	
	public void testPersonSet() {
		p3.setName("Mary Jo");
		Assert.assertEquals(p3.getName(),"Mary Jo");
	}

	public void testPersonSaveAndLoad() {
		Assert.assertEquals(hp1.toString(),s2);
		// "Petey | JoMomma@Hotmomma.com | 3"
	}
}
