// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.db.oom.dao;

import jodd.db.DbHsqldbTestCase;
import jodd.db.DbSession;
import jodd.db.ThreadDbSessionHolder;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class GenericDaoTest  extends DbHsqldbTestCase {

	@Test
	public void testAppDao1() {
		DbSession session = new DbSession(cp);
		ThreadDbSessionHolder.set(session);

		GenericDao dao = new GenericDao();

		// save

		Girl girl = new Girl();
		girl.setName("Emma");
		girl.setSpeciality("piano");
		girl.setId(Long.valueOf(1));

		dao.save(girl);
		assertEquals(1, girl.getId().longValue());

		Boy boy1 = new Boy();
		boy1.setId(1);
		boy1.setName("Oleg");
		boy1.setGirlId(1);

		Boy boy2 = new Boy();
		boy2.setId(2);
		boy2.setName("Marco");
		boy2.setGirlId(1);

		ArrayList<Boy> boys = new ArrayList<Boy>();
		boys.add(boy1);
		boys.add(boy2);

		dao.saveAll(boys);

		// find

		Boy dbBoy = dao.findById(Boy.class, 1);
		assertEquals(boy1.getId(), dbBoy.getId());
		assertEquals(boy1.getName(), dbBoy.getName());
		assertEquals(boy1.getGirlId(), dbBoy.getGirlId());

		// update

		girl.setSpeciality("Guitar");
		dao.update(girl);

		long count = dao.count(Girl.class);
		assertEquals(1, count);

		Girl dbGirl = dao.findById(Girl.class, 1);
		assertEquals("Guitar", dbGirl.getSpeciality());
		assertEquals("Emma", dbGirl.getName());

		// update property

		dao.updateProperty(girl, "speciality", "math");

		dbGirl = dao.findById(Girl.class, 1);
		assertEquals("math", dbGirl.getSpeciality());
		assertEquals("math", girl.getSpeciality());
		assertEquals("Emma", dbGirl.getName());

		// add one more girl
		Girl girl2 = new Girl();
		girl2.setName("Lina");
		girl2.setSpeciality("crazy");
		girl2.setId(Long.valueOf(2));

		dao.save(girl2);
		count = dao.count(Girl.class);
		assertEquals(2, count);

		Girl emma = dao.findOneByProperty(Girl.class, "name", "Emma");
		assertNotNull(emma);
		assertEquals(1, emma.getId().longValue());

		Girl none = dao.findOneByProperty(Girl.class, "name", "Www");
		assertNull(none);

		// find by matching

		Girl match = new Girl();
		match.setName("Emma");
		dbGirl = dao.findOne(match);

		assertEquals(emma.getId(), dbGirl.getId());
		assertEquals(emma.getName(), dbGirl.getName());
		assertEquals(emma.getSpeciality(), dbGirl.getSpeciality());

		Boy boyMatch = new Boy();
		boyMatch.setGirlId(1);

		List<Boy> dbBoys = dao.find(boyMatch);
		assertEquals(2, dbBoys.size());

		// find by separate matching class

		boyMatch = new Boy();
		boyMatch.setName("Oleg");
		dbBoys = dao.find(Boy.class, boyMatch);
		assertEquals(0, dbBoys.size());		// this doesn't work since boy has girldId set to 0

		// correct way

		BoyCriteria boyCriteria = new BoyCriteria();
		boyCriteria.setName("Oleg");

		dbBoys = dao.find(Boy.class, boyCriteria);
		assertEquals(1, dbBoys.size());


		// list all

		dbBoys = dao.listAll(Boy.class);
		assertEquals(2, dbBoys.size());

		// related

		dbBoys = dao.findRelated(Boy.class, emma);
		assertEquals(2, dbBoys.size());


		// delete

		dao.deleteById(Boy.class, 1);
		dao.deleteById(boy2);

		count = dao.count(Boy.class);
		assertEquals(0, count);

	    // delete all

		List<Girl> girls = dao.listAll(Girl.class);
		dao.deleteAllById(girls);

		count = dao.count(Girl.class);
		assertEquals(0, count);

		session.closeSession();
		ThreadDbSessionHolder.remove();
	}
}
@@ -188,16 +188,74 @@
 Holder;%0A
+import jodd.db.oom.DbOomManager;%0Aimport org.junit.Before;%0A
 import o
@@ -265,24 +265,24 @@
 junit.Test;%0A
-
 %0Aimport java
@@ -485,17 +485,16 @@
 DaoTest 
- 
 extends 
@@ -513,16 +513,236 @@
 Case %7B%0A%0A
+%09@Before%0A%09public void setUp() throws Exception %7B%0A%09%09super.setUp();%0A%0A%09%09DbOomManager.resetAll();%0A%09%09DbOomManager dbOom = DbOomManager.getInstance();%0A%09%09dbOom.registerEntity(Girl.class);%0A%09%09dbOom.registerEntity(Boy.class);%0A%09%7D%0A%0A
 %09@Test%0A%09

fix tests