Index: topiatest/src/test/org/codelutin/test2/AppTest.java diff -u topiatest/src/test/org/codelutin/test2/AppTest.java:1.7 topiatest/src/test/org/codelutin/test2/AppTest.java:1.8 --- topiatest/src/test/org/codelutin/test2/AppTest.java:1.7 Thu Aug 4 17:03:26 2005 +++ topiatest/src/test/org/codelutin/test2/AppTest.java Thu Aug 11 16:44:28 2005 @@ -1,16 +1,23 @@ package org.codelutin.test2; -import java.util.HashSet; +import java.util.Collection; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; import org.codelutin.test2Context; +import org.codelutin.test2EntitiesHelper; import org.codelutin.test2.entities.Company; import org.codelutin.test2.entities.CompanyPersistenceService; +import org.codelutin.test2.entities.Employee; +import org.codelutin.test2.entities.EmployeePersistenceService; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaId; +import org.codelutin.topia.TopiaUser; +import org.codelutin.topia.persistence.PersistenceStorageJDBC; +import org.codelutin.topia.persistence.TopiaPersistenceObject; import org.codelutin.topia.persistence.TopiaTransaction; -import org.codelutin.test2EntitiesHelper; public class AppTest extends TestCase { @@ -25,6 +32,116 @@ companyPS = child.getCompanyPersistenceService(); } + public void testEventListener() throws Exception { + // TODO + } + + public void testSousTransaction() throws Exception { + test2Context child1; + test2Context child2; + CompanyPersistenceService companyPS1; + CompanyPersistenceService companyPS2; + + // 1. On créé une transaction et une sous-transaction, + child1 = topiaContext.beginTransaction(); + companyPS1 = child1.getCompanyPersistenceService(); + child2 = child1.beginTransaction(); + companyPS2 = child2.getCompanyPersistenceService(); + // - on ajoute un elem dans la 1ere, il existe dans la 2eme, + Company comp = companyPS1.create(); + comp.setName("thim"); + String id = comp.get_topiaId_(); + companyPS1.update(comp); + comp = companyPS1.findBy_TopiaId_(id); + assertNotSame(null, comp); + assertEquals("thim", comp.getName()); + comp = companyPS2.findBy_TopiaId_(id); + assertNotSame(null, comp); + assertEquals("thim", comp.getName()); + // - puis on rollback la 1ere, il n'existe plus dans la 2eme + child1.rollbackTransaction(); + assertEquals(child1.getTransaction().getId(), child2.getTransaction().getParent().getId()); + boolean catched = false; + try { + companyPS1.findBy_TopiaId_(id); + }catch(TopiaException eee){ //Doit renvoyer une exception pour dire le l'objet n'a pas été trouvé + catched = true; + } + assertTrue(catched); + catched = false; + try { + companyPS2.findBy_TopiaId_(id); + }catch(TopiaException eee){ //idem + catched = true; + } + assertTrue(catched); + + + + // 2. Meme schéma + child1 = topiaContext.beginTransaction(); + companyPS1 = child1.getCompanyPersistenceService(); + child2 = child1.beginTransaction(); + companyPS2 = child2.getCompanyPersistenceService(); + + // - on ajoute un elem dans la 1ere, on le modifie dans la 2eme, on recup la valeur pour les deux, la valeur est modifiée slt pr la deuxieme + comp = companyPS1.create(); + id = comp.get_topiaId_(); + comp.setName("thim"); + companyPS1.update(comp); + comp = companyPS2.findBy_TopiaId_(id); + comp.setName("thimel"); + companyPS2.update(comp); + + comp = companyPS1.findBy_TopiaId_(id); + assertEquals("thim", comp.getName()); + comp = companyPS2.findBy_TopiaId_(id); + assertEquals("thimel", comp.getName()); + + // - on rollback la 2eme, la premiere doit reprendre la valeur avant le debut de la 2eme + child2.rollbackTransaction(); + comp = companyPS1.findBy_TopiaId_(id); + assertEquals("thim", comp.getName()); + comp = companyPS2.findBy_TopiaId_(id); + assertEquals("thim", comp.getName()); + + comp = companyPS2.findBy_TopiaId_(id); + comp.setName("thimel"); + companyPS2.update(comp); + + child2.commitTransaction(); + + comp = companyPS1.findBy_TopiaId_(id); + assertEquals("thimel", comp.getName()); + comp = companyPS2.findBy_TopiaId_(id); + assertEquals("thimel", comp.getName()); + + + // 3. Même schéma +// child1 = topiaContext.beginTransaction(); +// child2 = child1.beginTransaction(); +// storage = (PersistenceStorageJDBC)child2.getPersistenceHelper().getStorage(); +// +// id = TopiaId.create(TopiaUser.class); +// o = new TopiaPersistenceObject(id); +// o.getData().setField("name", "pousssin"); +// storage.store(child1, o); +// +// o.getData().setField("name", "poussin"); +// storage.store(child2, o); +// +// child2.commitTransaction(); +// +// boolean catched = false; +// try { +// child1.rollbackTransaction(); +// } catch (TopiaPersistenceException tpe) { +// catched = true; +// } +// assertTrue(catched); + } + + /** * Rigourous Test :-) */ @@ -56,7 +173,7 @@ System.out.println("size: " + count); assertEquals(10, count); - + System.out.println("-------- makePersistent --------"); company = companyPS.create(); System.out.println("company: " + company); @@ -68,7 +185,7 @@ company.setBool(count % 2 == 0); System.out.println("company: " + company); company = companyPS.update(company); - +System.out.println(company.getName()); System.out.println("company: " + company); assertEquals(count + 1, companyPS.size()); @@ -107,9 +224,9 @@ companies = companyPS.find(companyPS.newQuery().where("name not like \"%"+count+"\"")); assertEquals(count, companies.size()); -// System.out.println("-------- find entier = "+count+" --------"); -// companies = companyPS.find(companyPS.newQuery().where("entier = " + count)); -// assertEquals(1, companies.size()); + System.out.println("-------- find entier = "+count+" --------"); + companies = companyPS.find(companyPS.newQuery().where("entier = " + count)); + assertEquals(1, companies.size()); System.out.println("-------- find entier = ? ("+count+") --------"); companies = companyPS.find(companyPS.newQuery().where("entier = ?").addArg(count)); @@ -135,22 +252,22 @@ companies = companyPS.find(companyPS.newQuery().where("entier != ?").addArg(count)); assertEquals(count, companies.size()); -// System.out.println("-------- find entier < 3 --------"); -// companies = companyPS.find(companyPS.newQuery().where("entier < 3")); -// System.out.println("size:" + companies.size() + " count:" + count); -// assertEquals(Math.min(3,count+1), companies.size()); -// -// System.out.println("-------- find and1 --------"); -// companies = companyPS.find(companyPS.newQuery().where("bool = true and entier <= 0 ")); -// assertEquals(1, companies.size()); -// -// System.out.println("-------- find and2 --------"); -// companies = companyPS.find(companyPS.newQuery().where("bool = true and entier <= ? ").addArg(count)); -// assertEquals(count/2+1, companies.size()); -// -// System.out.println("-------- find not and3 --------"); -// companies = companyPS.find(companyPS.newQuery().where("not bool = true and entier <= ? ").addArg(count)); -// assertEquals((count+1)/2, companies.size()); + System.out.println("-------- find entier < 3 --------"); + companies = companyPS.find(companyPS.newQuery().where("entier < 3")); + System.out.println("size:" + companies.size() + " count:" + count); + assertEquals(Math.min(3,count+1), companies.size()); + + System.out.println("-------- find and1 --------"); + companies = companyPS.find(companyPS.newQuery().where("bool = true and entier <= 0 ")); + assertEquals(1, companies.size()); + + System.out.println("-------- find and2 --------"); + companies = companyPS.find(companyPS.newQuery().where("bool = true and entier <= ? ").addArg(count)); + assertEquals(count/2+1, companies.size()); + + System.out.println("-------- find not and3 --------"); + companies = companyPS.find(companyPS.newQuery().where("not bool = true and entier <= ? ").addArg(count)); + assertEquals((count+1)/2, companies.size()); System.out.println("-------- find and4 --------"); companies = companyPS.find(companyPS.newQuery().where("name like ? and entier = ? ").addArg("Compa%").addArg(0)); @@ -192,6 +309,7 @@ // companies = companyPS.find(companyPS.newQuery().where("name in ?").addArg(list)); // assertEquals(3, companies.size()); + System.out.println("-------- suppression --------"); companies = companyPS.findAll(); for(Company c : companies){ @@ -238,6 +356,29 @@ child.commitTransaction(); + } + + public void testEmloyee() throws Exception { + test2Context ctx = test2Context.getContext().beginTransaction(); + EmployeePersistenceService empPS = ctx.getEmployeePersistenceService(); + CompanyPersistenceService companyPS = ctx.getCompanyPersistenceService(); + for(Employee emp : empPS.findAll()){ + empPS.delete(emp); + } + assertEquals(0, empPS.findAll().size()); + + Employee emp = empPS.create(); + emp.setName("Plouhinec"); + Company comp = companyPS.create(); + comp.setName("CodeLutin"); + emp.addCompany(comp); + empPS.update(emp); + Collection companies; + companies = emp.getCompany(); + assertEquals(1, companies.size()); + for(Company c: companies){ + assertEquals(emp, c.getEmployee()); + } } static public void main(String [] args) throws Exception {