Author: tchemit Date: 2010-03-15 11:55:07 +0100 (Mon, 15 Mar 2010) New Revision: 1842 Log: improve TopiaEntityRefTester + add javadoc Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java 2010-03-15 10:54:46 UTC (rev 1841) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java 2010-03-15 10:55:07 UTC (rev 1842) @@ -6,15 +6,15 @@ import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaEntityEnum; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.SortedMap; +import java.util.*; import static java.util.Map.Entry; /** - * A abstract class to help testing {@link TopiaEntityRef} + * A abstract class to help testing {@link TopiaEntityRef} as detectes types, or + * detects or references. + * <p/> + * An example of use if given in the test {@link TopiaEntityRefTesterTest}. * * @author tchemit <chemit@codelutin.com> * @since 2.3.1 @@ -31,6 +31,8 @@ protected T[] contracts; + protected int index; + protected T[] getContracts() { if (contracts == null) { contracts = getContracts0(); @@ -38,6 +40,7 @@ return contracts; } + /** @return all the {@link TopiaEntityEnum} to be used */ protected abstract T[] getContracts0(); @After @@ -46,9 +49,92 @@ entry = null; refs = null; detected = null; + index = 0; } + /** + * Creates a new entity with the given {@code topiaId} (will use the {@link + * TopiaEntityEnum#getImplementation()} class). + * + * @param constant the constant defining the entity + * @param topiaId the topia to assign + * @param <T> the type of entity + * @return the new entity + * @throws IllegalAccessException if can no access entity constructor + * @throws InstantiationException if can no instanciate the entity + */ + protected <T extends TopiaEntity> T newEntity( + TopiaEntityEnum constant, String topiaId) throws + IllegalAccessException, + InstantiationException { + Class<? extends TopiaEntity> impl = constant.getImplementation(); + TopiaEntity topiaEntity = impl.newInstance(); + topiaEntity.setTopiaId(topiaId); + return (T) topiaEntity; + } + /** + * Obtain the reference of an association for a given entity. + * <p/> + * Example, to obtain the Pet 'pudding' on a Person : + * <pre>pet[@topiaId='pudding']</pre> + * invoke + * <pre>getAssociationRef('pet',pet);</pre> + * + * @param associationName the name of the association + * @param e the required entity + * @return the reference of the association + */ + protected String getAssociationRef(String associationName, TopiaEntity e) { + return getAssociationRef(associationName, e.getTopiaId()); + } + + /** + * Obtain the reference of an association for a given id. + * <p/> + * Example to obtain the Pet 'pudding' on a Person : + * <pre>pet[@topiaId='pudding']</pre> + * invoke + * <pre>getAssociationRef('pet','pudding');</pre> + * + * @param associationName the name of the association + * @param e the id + * @return the reference of the association + */ + protected String getAssociationRef(String associationName, String e) { + String s = String.format( + TopiaEntityHelper.ASSOCIATION_PATTERN, + associationName, + e + ); + return s; + } + + /** + * Obtain the next entry from the iterator. + * <p/> + * As a side-effect, it will increment the state {@link #index}. + */ + protected void nextEntry() { + if (!itr.hasNext()) { + throw new IllegalStateException("no more entry to get..."); + } + entry = itr.next(); + index = 0; + } + + /** + * Detects the references from the given {@code entity} which have their + * topiaId in the given list of {@code ids}. + * <p/> + * As a side-effect, it will update the states {@code detected} and set the + * iterator {@code itr} to the first position on detected entries. + * + * @param entity the entity to seek + * @param nb the required number of entries + * @param ids the ids to seek + * @throws TopiaException if any pb while visiting entities + */ protected void detectReferences( TopiaEntity entity, int nb, String... ids) throws TopiaException { detected = TopiaEntityHelper.detectReferences( @@ -57,6 +143,18 @@ } + /** + * Detects the references from the given {@code entity} which have their + * topiaId in the given list of {@code ids}. + * <p/> + * As a side-effect, it will update the states {@code detected} and set the + * iterator {@code itr} to the first position on detected entries. + * + * @param entity the entity to seek + * @param nb the required number of entries + * @param ids the ids to seek + * @throws TopiaException if any pb while visiting entities + */ protected void detectReferences( Collection<? extends TopiaEntity> entity, int nb, String... ids) throws TopiaException { @@ -65,10 +163,87 @@ assertDetected(nb); } + /** + * Detects the type of entities fro the given {@code data}. + * + * @param expected the array of expected types + * @param data the data to seek + * @throws TopiaException if any pb while visiting data + */ + protected void detectTypes(Class<?>[] expected, + TopiaEntity... data) throws TopiaException { + Set<String> fqns = new HashSet<String>(expected.length); + Set<Class<? extends TopiaEntity>> actual = null; + for (Class<?> c : expected) { + fqns.add(c.getName()); + } + try { + actual = TopiaEntityHelper.detectTypes(getContracts(), data); + Assert.assertEquals(expected.length, actual.size()); + for (Class<? extends TopiaEntity> c : actual) { + Assert.assertTrue(fqns.contains(c.getName())); + } + } finally { + fqns.clear(); + if (actual != null) { + actual.clear(); + } + } + } + + /** + * Asserts if the given entry definition is equals to the next entry + * references on internal state state {@code entry}. + * + * @param invoker the invoker of the reference + * @param invokerProperty the access path of the reference + * @param expected the expected topia entities path to access + * reference target + */ + protected void assertNextEntityRef(TopiaEntity invoker, + String invokerProperty, + TopiaEntity... expected) { + assertEntityRef(index, invoker, invokerProperty, expected); + index++; + } + + /** + * Asserts if the given entry definition (of an association) is equals to + * the next entry reference on internal state {@code entry}. + * + * @param invoker the invoker of the reference + * @param association the association name + * @param id the id of the association ( see {@link + * #getAssociationRef(String, TopiaEntity)} + * @param expected the expected topia entities path to access reference + * target + */ + protected void assertNextAssociationEntityRef(TopiaEntity invoker, + String association, + String id, + TopiaEntity... expected) { + String invokerProperty = getAssociationRef(association, id); + assertNextEntityRef(invoker, invokerProperty, expected); + } + + /** + * Asserts if the given entry definition is equals to the reference entry at + * position {@code index} on internal state {@code entry}. + * + * @param index the index of the reference to test in {@code + * entry} + * @param invoker the invoker of the reference + * @param invokerProperty the path of the reference + * @param expected th expected topia entities path to access + * reference target + */ protected void assertEntityRef(int index, TopiaEntity invoker, String invokerProperty, TopiaEntity... expected) { + Assert.assertNotNull(refs); + Assert.assertTrue("no index [" + index + "] in refs of size " + + refs.size(), index < refs.size()); TopiaEntityRef actual = refs.get(index); Assert.assertEquals(actual.getInvoker(), invoker); Assert.assertEquals(actual.getInvokerProperty(), invokerProperty); @@ -80,14 +255,44 @@ } } - protected void assertEntry(TopiaEntity expected, - int nbPath, - Entry<TopiaEntity, List<TopiaEntityRef>> entry) { + /** + * Asserts if the given {@code expected} entity is equals to the source of + * the internal state {@code entry}. + * + * @param expected the required source of the entry + * @param nbPath the expected number of references of the given entry + */ + protected void assertCurrentEntry(TopiaEntity expected, + int nbPath) { + assertEntry(expected, nbPath, entry); + } + + /** + * Asserts if the given {@code entry} has a good source and a good number of + * references. + * + * @param expected the expected entry source + * @param nbPath the expected number of reference of the entry + * @param entry the entry to test + */ + private void assertEntry(TopiaEntity expected, + int nbPath, + Entry<TopiaEntity, List<TopiaEntityRef>> entry) { + Assert.assertNotNull(entry); Assert.assertEquals(expected, entry.getKey()); refs = entry.getValue(); Assert.assertEquals(nbPath, refs.size()); } + /** + * Asserts that the number of detected entries (store in internal state + * {@code detected}) is ok. + * <p/> + * As a side-effect, it will reset the internal state {@code itr} on the + * first entry of the {@code detected} list. + * + * @param size the expected number of detected entries + */ protected void assertDetected(int size) { Assert.assertNotNull(detected); Assert.assertEquals(size, detected.size()); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java 2010-03-15 10:54:46 UTC (rev 1841) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java 2010-03-15 10:55:07 UTC (rev 1842) @@ -1,10 +1,12 @@ package org.nuiton.topia.persistence.util; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Test; -import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestDAOHelper.TopiaTestEntityEnum; -import org.nuiton.topia.persistence.TopiaEntity; -import org.nuiton.topia.test.entities.*; +import org.nuiton.topia.test.entities.Person; +import org.nuiton.topia.test.entities.Pet; +import org.nuiton.topia.test.entities.Race; /** * Test the {@link TopiaEntityRefTester} on @@ -17,16 +19,18 @@ */ public class TopiaEntityRefTesterTest extends TopiaEntityRefTester<TopiaTestEntityEnum> { + /** Logger */ + private static final Log log = + LogFactory.getLog(TopiaEntityRefTesterTest.class); + private static final String PET1 = "pet1"; + private static final String PET2 = "pet2"; + private static final String RACE1 = "race1"; private static final String PERSON1 = "person1"; - private static final String PET2 = "pet2"; - - protected String PET_REF = "pet[@" + TopiaEntity.TOPIA_ID + "=\"%1$s\"]"; - @Override protected TopiaTestEntityEnum[] getContracts0() { return new TopiaTestEntityEnum[]{ @@ -37,88 +41,90 @@ } @Test - public void testPet() throws TopiaException { + public void testNoReferences() throws Exception { - Pet pet = new PetImpl(); - pet.setTopiaId(PET1); + Pet pet = newEntity(TopiaTestEntityEnum.Pet, PET1); detectReferences(pet, 0); - Race race = new RaceImpl(); - race.setTopiaId(RACE1); + Race race = newEntity(TopiaTestEntityEnum.Race, RACE1); detectReferences(race, 0); - Person person = new PersonImpl(); - person.setTopiaId(PERSON1); + Person person = newEntity(TopiaTestEntityEnum.Person, PERSON1); - detectReferences(race, 0); + detectReferences(person, 0); + } + + @Test + public void testReferences() throws Exception { + + Pet pet = newEntity(TopiaTestEntityEnum.Pet, PET1); + Race race = newEntity(TopiaTestEntityEnum.Race, RACE1); pet.setRace(race); + Person person = newEntity(TopiaTestEntityEnum.Person, PERSON1); detectReferences(pet, 1, RACE1); - entry = itr.next(); - assertEntry(race, 1, entry); - assertEntityRef(0, pet, Pet.RACE, pet, race); + nextEntry(); + assertCurrentEntry(race, 1); + assertNextEntityRef(pet, Pet.RACE, pet, race); pet.setPerson(person); detectReferences(pet, 2, RACE1, PERSON1); - entry = itr.next(); - assertEntry(person, 1, entry); - assertEntityRef(0, pet, Pet.PERSON, pet, person); + nextEntry(); + assertCurrentEntry(person, 1); + assertNextEntityRef(pet, Pet.PERSON, pet, person); - entry = itr.next(); - assertEntry(race, 1, entry); - assertEntityRef(0, pet, Pet.RACE, pet, race); + nextEntry(); + assertCurrentEntry(race, 1); + assertNextEntityRef(pet, Pet.RACE, pet, race); person.addPet(pet); detectReferences(person, 1, PET1); - entry = itr.next(); - assertEntry(pet, 1, entry); - assertEntityRef(0, person, String.format(PET_REF, PET1), person, pet); + nextEntry(); + assertCurrentEntry(pet, 1); + assertNextAssociationEntityRef(person, Person.PET, PET1, person, pet); - Pet pet2 = new PetImpl(); - pet2.setTopiaId(PET2); + Pet pet2 = newEntity(TopiaTestEntityEnum.Pet, PET2); person.addPet(pet2); detectReferences(person, 3, PET1, PET2, RACE1); - entry = itr.next(); - assertEntry(pet, 1, entry); - assertEntityRef(0, person, String.format(PET_REF, PET1), person, pet); + nextEntry(); + assertCurrentEntry(pet, 1); + assertNextAssociationEntityRef(person, Person.PET, PET1, person, pet); + nextEntry(); + assertCurrentEntry(pet2, 1); + assertNextAssociationEntityRef(person, Person.PET, PET2, person, pet2); - entry = itr.next(); - assertEntry(pet2, 1, entry); - assertEntityRef(0, person, String.format(PET_REF, PET2), person, pet2); + nextEntry(); + assertCurrentEntry(race, 1); + assertNextEntityRef(pet, Pet.RACE, person, pet, race); - entry = itr.next(); - assertEntry(race, 1, entry); - assertEntityRef(0, pet, Pet.RACE, person, pet, race); - pet2.setRace(race); detectReferences(person, 3, PET1, PET2, RACE1); - entry = itr.next(); - assertEntry(pet, 1, entry); - assertEntityRef(0, person, String.format(PET_REF, PET1), person, pet); + nextEntry(); + assertCurrentEntry(pet, 1); + assertNextAssociationEntityRef(person, Person.PET, PET1, person, pet); + nextEntry(); + assertCurrentEntry(pet2, 1); + assertNextAssociationEntityRef(person, Person.PET, PET2, person, pet2); - entry = itr.next(); - assertEntry(pet2, 1, entry); - assertEntityRef(0, person, String.format(PET_REF, PET2), person, pet2); + nextEntry(); + assertCurrentEntry(race, 2); + assertNextEntityRef(pet, Pet.RACE, person, pet, race); + assertNextEntityRef(pet2, Pet.RACE, person, pet2, race); - entry = itr.next(); - assertEntry(race, 2, entry); - assertEntityRef(0, pet, Pet.RACE, person, pet, race); - assertEntityRef(1, pet2, Pet.RACE, person, pet2, race); - } }
participants (1)
-
tchemit@users.nuiton.org