Author: tchemit Date: 2010-09-17 21:21:52 +0200 (Fri, 17 Sep 2010) New Revision: 2117 Url: http://nuiton.org/repositories/revision/topia/2117 Log: improve test Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityHelperTest.java Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityHelperTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityHelperTest.java 2010-09-13 12:17:04 UTC (rev 2116) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityHelperTest.java 2010-09-17 19:21:52 UTC (rev 2117) @@ -25,19 +25,15 @@ package org.nuiton.topia.persistence.util; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestDAOHelper; -import static org.junit.Assert.*; +import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaEntityEnum; -import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.TopiaId; import org.nuiton.topiatest.Company; import org.nuiton.topiatest.CompanyImpl; import org.nuiton.topiatest.Department; @@ -49,13 +45,20 @@ import org.nuiton.topiatest.PersonneAbstract; import org.nuiton.topiatest.PersonneImpl; -/** - * - * @author tchemit <chemit@codelutin.com> - */ +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** @author tchemit <chemit@codelutin.com> */ public class TopiaEntityHelperTest { static TopiaEntityEnum[] contracts; + final Set<Class<? extends TopiaEntity>> contractsClass; public TopiaEntityHelperTest() { @@ -75,9 +78,7 @@ contracts = null; } - /** - * Test of getContractClass method, of class TopiaEntityHelper. - */ + /** Test of getContractClass method, of class TopiaEntityHelper. */ @Test public void testgetContract() { @@ -88,7 +89,6 @@ expResult = Employe.class; - getContractClass(expResult, EmployeImpl.class); getContractClass(expResult, EmployeAbstract.class); getContractClass(expResult, Employe.class); @@ -100,9 +100,7 @@ getContractClass(expResult, Personne.class); } - /** - * Test of retainContracts method, of class TopiaEntityHelper. - */ + /** Test of retainContracts method, of class TopiaEntityHelper. */ @Test public void testRetainContracts() { Set<Class<? extends TopiaEntity>> classes = new HashSet<Class<? extends TopiaEntity>>(); @@ -141,6 +139,7 @@ /** * Test of detectTypes method, of class TopiaEntityHelper. + * * @throws TopiaException */ @Test @@ -167,31 +166,46 @@ public void testDetector() throws Exception { Company company = new CompanyImpl(); - EmployeImpl employe = new EmployeImpl(); + company.setTopiaId(TopiaId.create(Company.class)); + Employe employe = new EmployeImpl(); + employe.setTopiaId(TopiaId.create(Employe.class)); Department department = new DepartmentImpl(); + department.setTopiaId(TopiaId.create(Department.class)); detectEntities(new Class<?>[]{Company.class, Employe.class, Department.class}, new int[]{1, 1, 1}, company, employe, department); + detectEntityIds(new Class<?>[]{Company.class, Employe.class, Department.class}, new int[]{1, 1, 1}, company, employe, department); company.addEmploye(employe); detectEntities(new Class<?>[]{Company.class, Employe.class}, new int[]{1, 1, 1}, company); + detectEntityIds(new Class<?>[]{Company.class, Employe.class}, new int[]{1, 1, 1}, company); company.addDepartment(department); department.setCompany(company); detectEntities(new Class<?>[]{Company.class, Employe.class, Department.class}, new int[]{1, 1, 1}, company); + detectEntityIds(new Class<?>[]{Company.class, Employe.class, Department.class}, new int[]{1, 1, 1}, company); company.removeEmploye(employe); company.removeDepartment(department); detectEntities(new Class<?>[]{Company.class}, new int[]{1}, company); + detectEntityIds(new Class<?>[]{Company.class}, new int[]{1}, company); detectEntities(new Class<?>[]{Company.class}, new int[]{2}, company, new CompanyImpl()); + detectEntityIds(new Class<?>[]{Company.class}, new int[]{2}, company, new CompanyImpl()); + department.setCompany(company); + + detectEntities(new Class<?>[]{Company.class, Department.class}, new int[]{1, 1}, department); + detectEntityIds(new Class<?>[]{Company.class, Department.class}, new int[]{1, 1}, department); + //TODO faire des tests avec des entites avec cycles } - protected void detectEntities(Class<?>[] expected, int[] sizes, TopiaEntity... data) throws TopiaException { + protected void detectEntities(Class<?>[] expected, + int[] sizes, + TopiaEntity... data) throws TopiaException { Map<Class<? extends TopiaEntity>, List<TopiaEntity>> actual = null; @@ -212,6 +226,32 @@ } } + protected void detectEntityIds(Class<?>[] expected, + int[] sizes, + TopiaEntity... data) throws TopiaException { + + TopiaEntityIdsMap actual = null; + + try { + actual = TopiaEntityHelper.detectEntityIds(contracts, + contractsClass, + data + ); + + Assert.assertEquals(expected.length, actual.size()); + int index = 0; + for (Class<?> c : expected) { + List<String> value = actual.get(c); + int expectedSize = sizes[index++]; + Assert.assertEquals(expectedSize, value.size()); + } + } finally { + if (actual != null) { + actual.clear(); + } + } + } + protected void detectTypes(Class<?>[] expected, TopiaEntity... data) throws TopiaException { Set<String> fqns = new HashSet<String>(expected.length); Set<Class<? extends TopiaEntity>> actual = null;