Author: tchemit
Date: 2010-03-13 22:49:57 +0100 (Sat, 13 Mar 2010)
New Revision: 1839
Log:
introduce TopiaEntityRefTester to help test of refs + reformat code
Added:
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/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-03-12 20:15:11 UTC (rev 1838)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-03-13 21:49:57 UTC (rev 1839)
@@ -27,7 +27,6 @@
import org.nuiton.topia.TopiaException;
import org.nuiton.topia.persistence.TopiaDAO;
import org.nuiton.topia.persistence.TopiaEntity;
-import org.nuiton.topia.persistence.util.Collector.CollectorVisitor;
import org.nuiton.util.FileUtil;
import java.io.ByteArrayOutputStream;
@@ -52,6 +51,7 @@
import java.util.TreeMap;
import org.apache.commons.beanutils.MethodUtils;
import org.nuiton.topia.persistence.TopiaEntityEnum;
+import org.nuiton.util.ObjectUtil;
/**
* Une classe avec des méthodes utiles sur les entités.
@@ -62,10 +62,15 @@
/** to use log facility, just put in your code: log.info(\"...\"); */
private static final Log log = LogFactory.getLog(TopiaEntityHelper.class);
+
protected static final Integer ZERO = 0;
+
protected static final Float ZEROF = 0f;
+
protected static final Long ZEROL = 0l;
+
protected static final Double ZEROD = 0.;
+
protected static final Byte ZEROB = 0;
/**
@@ -73,7 +78,7 @@
* @param type the type to test
* @return null object (default value for primitive types)
* @deprecated since 2.3.0, prefer use the
- * {@link org.nuiton.util.ObjectUtil#getNullValue(Class)} method
+ * {@link ObjectUtil#getNullValue(Class)} method
*/
@Deprecated
protected static Object getNullValue(Class<?> type) {
@@ -106,7 +111,7 @@
* @return {@code true} if value is null or default value for primitive
* types)
* @deprecated since 2.3.0, prefer use the
- * {@link org.nuiton.util.ObjectUtil#isNullValue(Object)} method
+ * {@link ObjectUtil#isNullValue(Object)} method
*/
@Deprecated
public static boolean isNullValue(Object value) {
@@ -156,7 +161,8 @@
/**
* Récupère une entité qui doit exister à partir de son id.
* <p/>
- * Si l'entité n'existe pas, on déclanche une exception {@link IllegalArgumentException}.
+ * Si l'entité n'existe pas, on déclanche une exception
+ * {@link IllegalArgumentException}.
*
* @param dao la dao pour récupérer la valeur
* @param topiaId l'id de l'entité recherchée
@@ -165,10 +171,13 @@
* @throws TopiaException pour tout pb lors de la récupération de l'entité
* @throws IllegalArgumentException si l'entité n'existe pas.
*/
- public static <E extends TopiaEntity> E getExistingEntity(TopiaDAO<E> dao, String topiaId) throws TopiaException, IllegalArgumentException {
+ public static <E extends TopiaEntity> E getExistingEntity(
+ TopiaDAO<E> dao, String topiaId) throws TopiaException,
+ IllegalArgumentException {
E entity = dao.findByTopiaId(topiaId);
if (entity == null) {
- throw new IllegalArgumentException("could not find entity with topiaId " + topiaId);
+ throw new IllegalArgumentException(
+ "could not find entity with topiaId " + topiaId);
}
return entity;
}
@@ -182,7 +191,8 @@
* @param <E> le type de l'entité
* @return l'entité trouvée, ou <code<null</code< si elle n'est pas trouvée.
*/
- public static <E extends TopiaEntity> E getEntityByTopiaId(Collection<E> entities, String topiaId) {
+ public static <E extends TopiaEntity> E getEntityByTopiaId(
+ Collection<E> entities, String topiaId) {
if (entities != null) {
for (E e : entities) {
if (topiaId.equals(e.getTopiaId())) {
@@ -202,13 +212,16 @@
* @throws IllegalStateException si l'entité n'a pas de topiaId
* @throws NullPointerException si l'entité est null
*/
- public static <E extends TopiaEntity> void checkNotNullAndExistingEntity(String paramName, E bean) throws IllegalStateException, NullPointerException {
+ public static <E extends TopiaEntity> void checkNotNullAndExistingEntity(
+ String paramName, E bean) throws IllegalStateException,
+ NullPointerException {
if (bean == null) {
throw new NullPointerException(paramName + " can not be null");
}
if (bean.getTopiaId() == null) {
// can not create bean here
- throw new IllegalStateException("can not create " + bean.getClass() + " here...");
+ throw new IllegalStateException(
+ "can not create " + bean.getClass() + " here...");
}
}
@@ -221,13 +234,16 @@
* @throws NullPointerException si l'entité est nulle
* @throws IllegalStateException si l'entité possède un topiaId.
*/
- public static <E extends TopiaEntity> void checkNotNullAndNoneExistingEntity(String paramName, E bean) throws NullPointerException, IllegalStateException {
+ public static <E extends TopiaEntity> void checkNotNullAndNoneExistingEntity(
+ String paramName, E bean) throws NullPointerException,
+ IllegalStateException {
if (bean == null) {
throw new NullPointerException(paramName + " can not be null");
}
if (bean.getTopiaId() != null) {
// can not create bean here
- throw new IllegalStateException("can not update " + bean.getClass() + " here...");
+ throw new IllegalStateException(
+ "can not update " + bean.getClass() + " here...");
}
}
@@ -241,7 +257,11 @@
* @throws IOException if any io exception
* @throws NullPointerException if parameters are null
*/
- public static void createDBFromSQL(File dbDirectory, TopiaContext topiaContext, URI resource) throws IOException, TopiaException, NullPointerException {
+ public static void createDBFromSQL(
+ File dbDirectory,
+ TopiaContext topiaContext,
+ URI resource) throws IOException,
+ TopiaException, NullPointerException {
if (dbDirectory == null) {
throw new NullPointerException("dbDirectory can not be null");
}
@@ -255,10 +275,16 @@
File databaseDump;
if (resource.isOpaque()) {
- ByteArrayOutputStream output = FileUtil.readBytesFrom(resource.toURL().openStream(), 8048);
+ ByteArrayOutputStream output =
+ FileUtil.readBytesFrom(resource.toURL().openStream(), 8048);
File tempFile = File.createTempFile("topiaDumpDatabase", ".sql.gz");
- output.writeTo(new FileOutputStream(tempFile));
+ FileOutputStream stream = new FileOutputStream(tempFile);
+ try {
+ output.writeTo(stream);
+ } finally {
+ stream.close();
+ }
tempFile.deleteOnExit();
databaseDump = tempFile;
@@ -298,7 +324,9 @@
* @throws NullPointerException if parameters are null
* @throws IOException if could not create gzipFile container directory
*/
- public static void saveDB(File gzipFile, TopiaContext topiaContext) throws TopiaException, IOException, NullPointerException {
+ public static void saveDB(
+ File gzipFile, TopiaContext topiaContext)
+ throws TopiaException, IOException, NullPointerException {
if (gzipFile == null) {
throw new NullPointerException("gzipFile can not be null");
}
@@ -307,7 +335,8 @@
}
if (!gzipFile.getParentFile().exists()) {
if (!gzipFile.getParentFile().mkdirs()) {
- throw new IOException("could not create directory " + gzipFile.getParentFile());
+ throw new IOException("could not create directory " +
+ gzipFile.getParentFile());
}
}
log.info("store database to [" + gzipFile + "]");
@@ -353,7 +382,9 @@
* @return the list of filtered entities for the given entity type in the list
* @since 2.1.4
*/
- public static List<TopiaEntity> filter(Collection<TopiaEntity> entities, Class<? extends TopiaEntity> filterClass) {
+ public static List<TopiaEntity> filter(
+ Collection<TopiaEntity> entities,
+ Class<? extends TopiaEntity> filterClass) {
List<TopiaEntity> result = new ArrayList<TopiaEntity>();
for (TopiaEntity e : entities) {
if (filterClass.isAssignableFrom(e.getClass())) {
@@ -363,14 +394,19 @@
return result;
}
- public static Class<? extends TopiaEntity> getContractClass(TopiaEntityEnum[] contracts, Class<? extends TopiaEntity> klass) {
+ public static Class<? extends TopiaEntity> getContractClass(
+ TopiaEntityEnum[] contracts,
+ Class<? extends TopiaEntity> klass) {
if (contracts == null || contracts.length == 0) {
// pas de contract connus...
return null;
}
// on recupere l'ensemble des interfaces connues pour la klass donnee
- Set<Class<? extends TopiaEntity>> klassInterfaces = getInterfaces(klass, new HashSet<Class<? extends TopiaEntity>>());
+ Set<Class<? extends TopiaEntity>> klassInterfaces = getInterfaces(
+ klass,
+ new HashSet<Class<? extends TopiaEntity>>()
+ );
for (TopiaEntityEnum contract : contracts) {
Class<? extends TopiaEntity> contractClass = contract.getContract();
@@ -381,12 +417,14 @@
return null;
}
- public static TopiaEntityEnum getEntityEnum(Class<? extends TopiaEntity> klass, TopiaEntityEnum... contracts) {
+ public static TopiaEntityEnum getEntityEnum(
+ Class<? extends TopiaEntity> klass, TopiaEntityEnum... contracts) {
if (contracts == null || contracts.length == 0) {
// pas de contract connus...
return null;
}
- Class<? extends TopiaEntity> contractClass = getContractClass(contracts, klass);
+ Class<? extends TopiaEntity> contractClass =
+ getContractClass(contracts, klass);
for (TopiaEntityEnum t : contracts) {
if (t.accept(contractClass)) {
return t;
@@ -404,10 +442,14 @@
* @return l'ensemble des contrats filtres
* @since 2.2.0
*/
- public static Set<Class<? extends TopiaEntity>> retainContracts(TopiaEntityEnum[] contracts, Set<Class<? extends TopiaEntity>> classes) {
- Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> dico = new HashMap<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>>();
+ public static Set<Class<? extends TopiaEntity>> retainContracts(
+ TopiaEntityEnum[] contracts,
+ Set<Class<? extends TopiaEntity>> classes) {
+ Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> dico;
+ dico = new HashMap<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>>();
try {
- Set<Class<? extends TopiaEntity>> result = new HashSet<Class<? extends TopiaEntity>>();
+ Set<Class<? extends TopiaEntity>> result =
+ new HashSet<Class<? extends TopiaEntity>>();
for (Class<? extends TopiaEntity> c : classes) {
Class<? extends TopiaEntity> contractClass = dico.get(c);
@@ -434,13 +476,15 @@
}
/**
- * Ontenir l'ensemble des contrats d'entites a partir des descriptions d'entites.
+ * Ontenir l'ensemble des contrats d'entites a partir des descriptions
+ * d'entites.
*
* @param contracts les contracts connus
* @return l'ensemble des contrats d'entites
* @since 2.2.0
*/
- public static Class<? extends TopiaEntity>[] getContracts(TopiaEntityEnum[] contracts) {
+ public static Class<? extends TopiaEntity>[] getContracts(
+ TopiaEntityEnum[] contracts) {
Class<?>[] result = new Class<?>[contracts.length];
int index = 0;
for (TopiaEntityEnum e : contracts) {
@@ -461,14 +505,20 @@
*
* @throws TopiaException
*/
- public static Set<Class<? extends TopiaEntity>> detectTypes(final TopiaEntityEnum[] contracts, TopiaEntity... entities) throws TopiaException {
+ public static Set<Class<? extends TopiaEntity>> detectTypes(
+ final TopiaEntityEnum[] contracts,
+ TopiaEntity... entities) throws TopiaException {
- Collector<Set<Class<? extends TopiaEntity>>> collector = new Collector<Set<Class<? extends TopiaEntity>>>(contracts) {
+ Collector<Set<Class<? extends TopiaEntity>>> collector;
+ collector =
+ new Collector<Set<Class<? extends TopiaEntity>>>(contracts) {
+
/**
* La liste des types d'entités détectées
*/
- protected Set<Class<? extends TopiaEntity>> detectedTypes = new HashSet<Class<? extends TopiaEntity>>();
+ protected Set<Class<? extends TopiaEntity>> detectedTypes =
+ new HashSet<Class<? extends TopiaEntity>>();
@Override
protected boolean onStarting(TopiaEntity e) {
@@ -482,8 +532,10 @@
}
@Override
- protected Set<Class<? extends TopiaEntity>> afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
- Set<Class<? extends TopiaEntity>> result = TopiaEntityHelper.retainContracts(contracts, detectedTypes);
+ protected Set<Class<? extends TopiaEntity>> afterAll(
+ CollectorVisitor visitor, TopiaEntity... entities) {
+ Set<Class<? extends TopiaEntity>> result =
+ retainContracts(contracts, detectedTypes);
return result;
}
};
@@ -506,14 +558,19 @@
*
* @throws TopiaException
*/
- public static Map<Class<? extends TopiaEntity>, List<TopiaEntity>> detectEntities(final TopiaEntityEnum[] contracts, final Set<Class<? extends TopiaEntity>> types, TopiaEntity... entities) throws TopiaException {
+ public static Map<Class<? extends TopiaEntity>, List<TopiaEntity>>
+ detectEntities(final TopiaEntityEnum[] contracts,
+ final Set<Class<? extends TopiaEntity>> types,
+ TopiaEntity... entities) throws TopiaException {
- Collector<Map<Class<? extends TopiaEntity>, List<TopiaEntity>>> collector = new Collector<Map<Class<? extends TopiaEntity>, List<TopiaEntity>>>(contracts) {
+ Collector<Map<Class<? extends TopiaEntity>, List<TopiaEntity>>> collector;
+ collector = new Collector<Map<Class<? extends TopiaEntity>, List<TopiaEntity>>>(contracts) {
/**
* le dictionnaire des références détectées indexées par leur type
*/
- protected Map<Class<? extends TopiaEntity>, List<TopiaEntity>> detectedRefs = new HashMap<Class<? extends TopiaEntity>, List<TopiaEntity>>();
+ protected Map<Class<? extends TopiaEntity>, List<TopiaEntity>> detectedRefs =
+ new HashMap<Class<? extends TopiaEntity>, List<TopiaEntity>>();
@Override
protected void onStarted(TopiaEntity e, boolean enter) {
@@ -531,19 +588,21 @@
List<TopiaEntity> refs = detectedRefs.get(entityClass);
if (refs == null) {
- refs = new java.util.ArrayList<TopiaEntity>();
+ refs = new ArrayList<TopiaEntity>();
detectedRefs.put(entityClass, refs);
}
refs.add(e);
}
@Override
- protected Map<Class<? extends TopiaEntity>, List<TopiaEntity>> afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
+ protected Map<Class<? extends TopiaEntity>, List<TopiaEntity>>
+ afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
return detectedRefs;
}
};
- Map<Class<? extends TopiaEntity>, List<TopiaEntity>> result = collector.detect(entities);
+ Map<Class<? extends TopiaEntity>, List<TopiaEntity>> result =
+ collector.detect(entities);
return result;
}
@@ -561,14 +620,19 @@
*
* @throws TopiaException
*/
- public static Map<Class<? extends TopiaEntity>, List<String>> detectEntityIds(final TopiaEntityEnum[] contracts, final Set<Class<? extends TopiaEntity>> types, TopiaEntity... entities) throws TopiaException {
+ public static Map<Class<? extends TopiaEntity>, List<String>>
+ detectEntityIds(final TopiaEntityEnum[] contracts,
+ final Set<Class<? extends TopiaEntity>> types,
+ TopiaEntity... entities) throws TopiaException {
- Collector<Map<Class<? extends TopiaEntity>, List<String>>> collector = new Collector<Map<Class<? extends TopiaEntity>, List<String>>>(contracts) {
+ Collector<Map<Class<? extends TopiaEntity>, List<String>>> collector;
+ collector = new Collector<Map<Class<? extends TopiaEntity>, List<String>>>(contracts) {
/**
* le dictionnaire des références détectées indexées par leur type
*/
- protected Map<Class<? extends TopiaEntity>, List<String>> detectedRefs = new HashMap<Class<? extends TopiaEntity>, List<String>>();
+ protected Map<Class<? extends TopiaEntity>, List<String>> detectedRefs =
+ new HashMap<Class<? extends TopiaEntity>, List<String>>();
@Override
protected void onStarted(TopiaEntity e, boolean enter) {
@@ -593,12 +657,14 @@
}
@Override
- protected Map<Class<? extends TopiaEntity>, List<String>> afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
+ protected Map<Class<? extends TopiaEntity>, List<String>>
+ afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
return detectedRefs;
}
};
- Map<Class<? extends TopiaEntity>, List<String>> result = collector.detect(entities);
+ Map<Class<? extends TopiaEntity>, List<String>> result =
+ collector.detect(entities);
return result;
}
@@ -615,8 +681,12 @@
*
* @see TopiaEntityRef
*/
- public static SortedMap<TopiaEntity, List<TopiaEntityRef>> detectReferences(final TopiaEntityEnum[] contracts, final String[] expressions, TopiaEntity entities) throws TopiaException {
- return detectReferences(contracts, expressions, Collections.singleton(entities));
+ public static SortedMap<TopiaEntity, List<TopiaEntityRef>> detectReferences(
+ TopiaEntityEnum[] contracts,
+ String[] expressions,
+ TopiaEntity entities) throws TopiaException {
+ return detectReferences(contracts, expressions,
+ Collections.singleton(entities));
}
/**
@@ -632,14 +702,25 @@
*
* @see TopiaEntityRef
*/
- public static SortedMap<TopiaEntity, List<TopiaEntityRef>> detectReferences(final TopiaEntityEnum[] contracts, final String[] expressions, Collection<? extends TopiaEntity> entities) throws TopiaException {
+ public static SortedMap<TopiaEntity, List<TopiaEntityRef>>
+ detectReferences(
+ final TopiaEntityEnum[] contracts,
+ final String[] expressions,
+ Collection<? extends TopiaEntity> entities) throws TopiaException {
- Collector<SortedMap<TopiaEntity, List<TopiaEntityRef>>> collector = new Collector<SortedMap<TopiaEntity, List<TopiaEntityRef>>>(contracts) {
+ Collector<SortedMap<TopiaEntity, List<TopiaEntityRef>>> collector;
+ collector = new Collector<SortedMap<TopiaEntity, List<TopiaEntityRef>>>(
+ contracts) {
/** la liste des ids a accepter ou rejecter selon acceptMode */
List<String> ids = Arrays.asList(expressions);
+
/** le dictionnaire des références détectées indexées par leur type */
- SortedMap<TopiaEntity, List<TopiaEntityRef>> refs = new TreeMap<TopiaEntity, List<TopiaEntityRef>>(TopiaEntityHelper.getTopiaIdComparator());
+ SortedMap<TopiaEntity, List<TopiaEntityRef>> refs;{
+ refs = new TreeMap<TopiaEntity, List<TopiaEntityRef>>(
+ getTopiaIdComparator());
+ }
+
/** le path courant depuis le depart */
Deque<TopiaEntity> path = new LinkedList<TopiaEntity>();
StringBuilder accessorExpression = new StringBuilder();
@@ -681,7 +762,9 @@
}
@Override
- protected boolean onVisiting(TopiaEntity e, String name, Class<?> type, Object value) {
+ protected boolean onVisiting(TopiaEntity e,
+ String name,
+ Class<?> type, Object value) {
TopiaEntity e1 = getTopiaValue(value);
if (e1 != null) {
// on est sur une entite
@@ -696,7 +779,8 @@
if (visitor.getAlreadyExplored().contains(e1)) {
boolean contains = visitor.getStack().contains(e1);
if (log.isDebugEnabled()) {
- log.debug("already enter " + e1.getTopiaId() + ", can reenter ? " + !contains);
+ log.debug("already enter " + e1.getTopiaId() +
+ ", can reenter ? " + !contains);
}
if (!contains) {
try {
@@ -718,7 +802,10 @@
}
@Override
- protected void onVisited(TopiaEntity e, String name, Class<?> type, Object value, boolean enter) {
+ protected void onVisited(TopiaEntity e,
+ String name,
+ Class<?> type,
+ Object value, boolean enter) {
super.onVisited(e, name, type, value, enter);
if (enter) {
removePath();
@@ -726,7 +813,11 @@
}
@Override
- protected boolean onVisiting(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value) {
+ protected boolean onVisiting(TopiaEntity e,
+ String name,
+ Class<?> collectionType,
+ Class<?> type,
+ int index, Object value) {
TopiaEntity e1 = getTopiaValue(value);
if (e1 != null) {
addPath(e1, name, index);
@@ -740,7 +831,8 @@
if (visitor.getAlreadyExplored().contains(e1)) {
boolean contains = visitor.getStack().contains(e1);
if (log.isDebugEnabled()) {
- log.debug("already enter " + e1.getTopiaId() + ", can reenter ? " + !contains);
+ log.debug("already enter " + e1.getTopiaId() +
+ ", can reenter ? " + !contains);
}
if (!contains) {
try {
@@ -762,15 +854,21 @@
}
@Override
- protected void onVisited(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value, boolean enter) {
- super.onVisited(e, name, collectionType, type, index, value, enter);
+ protected void onVisited(TopiaEntity e,
+ String name,
+ Class<?> collectionType,
+ Class<?> type,
+ int index, Object value, boolean enter) {
+ super.onVisited(e, name, collectionType, type, index, value,
+ enter);
if (enter) {
removePath();
}
}
@Override
- protected SortedMap<TopiaEntity, List<TopiaEntityRef>> afterAll(CollectorVisitor visitor, TopiaEntity... entities) {
+ protected SortedMap<TopiaEntity, List<TopiaEntityRef>> afterAll(
+ CollectorVisitor visitor, TopiaEntity... entities) {
return refs;
}
@@ -796,10 +894,14 @@
}
accessorExpression.append(name);
if (index > -1) {
- accessorExpression.append("[@topiaId=\"" + e.getTopiaId() + "\"]");
+ accessorExpression.append("[@topiaId=\"");
+ accessorExpression.append(e.getTopiaId());
+ accessorExpression.append("\"]");
}
if (log.isTraceEnabled()) {
- log.trace("add to stack : " + e.getTopiaId() + ", new size : " + path.size() + ", path : " + accessorExpression.toString());
+ log.trace("add to stack : " + e.getTopiaId() +
+ ", new size : " + path.size() + ", path : " +
+ accessorExpression.toString());
}
}
@@ -808,13 +910,17 @@
if (path.isEmpty()) {
accessorExpression.setLength(0);
} else {
- int index = accessorExpression.lastIndexOf(TopiaEntityRef.SEPARATOR);
+ int index = accessorExpression.lastIndexOf(
+ TopiaEntityRef.SEPARATOR);
if (index > -1) {
- accessorExpression.delete(index, accessorExpression.length());
+ accessorExpression.delete(index,
+ accessorExpression.length());
}
}
if (log.isTraceEnabled()) {
- log.trace("remove from stack : " + e.getTopiaId() + ", new size : " + path.size() + ", path : " + accessorExpression.toString());
+ log.trace("remove from stack : " + e.getTopiaId() +
+ ", new size : " + path.size() + ", path : " +
+ accessorExpression.toString());
}
}
@@ -830,18 +936,28 @@
String expression = accessorExpression.toString();
// add the path for the detected
- list.add(new TopiaEntityRef(root, e, expression, path.toArray(new TopiaEntity[path.size()])));
+ list.add(new TopiaEntityRef(
+ root,
+ e,
+ expression,
+ path.toArray(new TopiaEntity[path.size()]))
+ );
if (log.isDebugEnabled()) {
- log.debug(expression + " (" + e.getTopiaId() + ") - " + list.size() + " , root:" + root.getTopiaId());
+ log.debug(expression + " (" + e.getTopiaId() + ") - " +
+ list.size() + " , root:" + root.getTopiaId());
}
if (log.isTraceEnabled()) {
- log.trace(e.getTopiaId() + " : new size " + list.size() + ", path : " + expression);
+ log.trace(e.getTopiaId() + " : new size " + list.size() +
+ ", path : " + expression);
}
}
};
- SortedMap<TopiaEntity, List<TopiaEntityRef>> result = collector.detect(entities.toArray(new TopiaEntity[entities.size()]));
+ SortedMap<TopiaEntity, List<TopiaEntityRef>> result =
+ collector.detect(
+ entities.toArray(new TopiaEntity[entities.size()])
+ );
return result;
}
@@ -851,7 +967,8 @@
* @param entities la liste des entités
* @return la liste des topiaId
*/
- public static List<String> getTopiaIdList(List<? extends TopiaEntity> entities) {
+ public static List<String> getTopiaIdList(
+ List<? extends TopiaEntity> entities) {
List<String> ids = new ArrayList<String>(entities.size());
for (TopiaEntity entity : entities) {
ids.add(entity.getTopiaId());
@@ -860,16 +977,19 @@
}
/**
- * Construit une list d'entite dont les ids sont tous dans la liste d'ids donnee.
+ * Construit une list d'entite dont les ids sont tous dans la liste d'ids
+ * donnee.
+ *
* @param <E> le type des entites de la liste
* @param list la liste a filter
* @param topiaIds la liste des ids a retenir
* @return la nouvelle liste filtree
*/
- public static <E extends TopiaEntity> List<E> retainEntities(List<E> list, List<String> topiaIds) {
+ public static <E extends TopiaEntity> List<E> retainEntities(
+ List<E> list, List<String> topiaIds) {
- List<E> r = new ArrayList<E>(list.size());
- if (list!=null) {
+ List<E> r = new ArrayList<E>(list == null ? 0 : list.size());
+ if (list != null) {
for (E e : list) {
if (topiaIds.contains(e.getTopiaId())) {
r.add(e);
@@ -885,20 +1005,22 @@
* @param <E> le type des entites
* @param referentiel la liste considere comme reference
* @param locale la liste a mettre a jour
- * @return le dictionnaire des etats des entites ajoutees, modifiees ou obsoletes.
+ * @return le dictionnaire des etats des entites ajoutees, modifiees ou
+ * obsoletes.
*
* @see DiffState
* @since 2.2.0
*/
- public static <E extends TopiaEntity> EnumMap<DiffState, List<String>> buildDifferentiel(List<E> referentiel, List<E> locale) {
+ public static <E extends TopiaEntity> EnumMap<DiffState, List<String>>
+ buildDifferentiel(List<E> referentiel, List<E> locale) {
EnumMap<DiffState, List<String>> result = DiffState.newMap();
// construction des deux listes d'id
List<String> referentielIdList = getTopiaIdList(referentiel);
List<String> localeIdList = getTopiaIdList(locale);
- for (int i = 0; i < referentiel.size(); i++) {
- TopiaEntity referentielEntity = referentiel.get(i);
+ for (E aReferentiel : referentiel) {
+ TopiaEntity referentielEntity = aReferentiel;
String refId = referentielEntity.getTopiaId();
if (localeIdList.contains(refId)) {
// id existant sur le storage locale
@@ -913,7 +1035,8 @@
break;
}
}
- boolean wasModified = referentielEntity.getTopiaVersion() > localeEntity.getTopiaVersion();
+ boolean wasModified = referentielEntity.getTopiaVersion() >
+ localeEntity.getTopiaVersion();
if (wasModified) {
result.get(DiffState.MODIFIED).add(refId);
@@ -938,7 +1061,9 @@
return result;
}
- public static Set<Class<? extends TopiaEntity>> getInterfaces(Class<? extends TopiaEntity> klass, Set<Class<? extends TopiaEntity>> klassInterfaces) {
+ public static Set<Class<? extends TopiaEntity>> getInterfaces(
+ Class<? extends TopiaEntity> klass,
+ Set<Class<? extends TopiaEntity>> klassInterfaces) {
if (klassInterfaces.contains(klass)) {
// deja traite
return klassInterfaces;
@@ -955,17 +1080,26 @@
if (interfaces.length > 0) {
// des interfaces detectees, on les ajoutes
for (Class<?> interfac : interfaces) {
- getInterfaces((Class<? extends TopiaEntity>) interfac, klassInterfaces);
+ getInterfaces(
+ (Class<? extends TopiaEntity>) interfac,
+ klassInterfaces
+ );
}
}
- if (klass.getSuperclass() != null && TopiaEntity.class.isAssignableFrom(klass.getSuperclass())) {
- getInterfaces((Class<? extends TopiaEntity>) klass.getSuperclass(), klassInterfaces);
+ if (klass.getSuperclass() != null &&
+ TopiaEntity.class.isAssignableFrom(klass.getSuperclass())) {
+ getInterfaces(
+ (Class<? extends TopiaEntity>) klass.getSuperclass(),
+ klassInterfaces
+ );
}
return klassInterfaces;
}
- protected static void addInterface(Set<Class<? extends TopiaEntity>> interfaces, Class<? extends TopiaEntity> klass) {
+ protected static void addInterface(
+ Set<Class<? extends TopiaEntity>> interfaces,
+ Class<? extends TopiaEntity> klass) {
Iterator<Class<? extends TopiaEntity>> iterator = interfaces.iterator();
for (; iterator.hasNext();) {
Class<? extends TopiaEntity> next = iterator.next();
Added: 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 (rev 0)
+++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java 2010-03-13 21:49:57 UTC (rev 1839)
@@ -0,0 +1,96 @@
+package org.nuiton.topia.persistence.util;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.nuiton.topia.TopiaException;
+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 static java.util.Map.Entry;
+
+/**
+ * A abstract class to help testing {@link TopiaEntityRef}
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.3.1
+ */
+public abstract class TopiaEntityRefTester<T extends TopiaEntityEnum> {
+
+ protected SortedMap<TopiaEntity, List<TopiaEntityRef>> detected;
+
+ protected Iterator<Entry<TopiaEntity, List<TopiaEntityRef>>> itr;
+
+ protected Entry<TopiaEntity, List<TopiaEntityRef>> entry;
+
+ protected List<TopiaEntityRef> refs;
+
+ protected T[] contracts;
+
+ protected T[] getContracts() {
+ if (contracts == null) {
+ contracts = getContracts0();
+ }
+ return contracts;
+ }
+
+ protected abstract T[] getContracts0();
+
+ @After
+ public void after() {
+ itr = null;
+ entry = null;
+ refs = null;
+ detected = null;
+ }
+
+
+ protected void detectReferences(
+ TopiaEntity entity, int nb, String... ids) throws TopiaException {
+ detected = TopiaEntityHelper.detectReferences(
+ getContracts(), ids, entity);
+ assertDetected(nb);
+
+ }
+
+ protected void detectReferences(
+ Collection<? extends TopiaEntity> entity,
+ int nb, String... ids) throws TopiaException {
+ detected = TopiaEntityHelper.detectReferences(
+ getContracts(), ids, entity);
+ assertDetected(nb);
+ }
+
+ protected void assertEntityRef(int index,
+ TopiaEntity invoker,
+ String invokerProperty,
+ TopiaEntity... expected) {
+ TopiaEntityRef actual = refs.get(index);
+ Assert.assertEquals(actual.getInvoker(), invoker);
+ Assert.assertEquals(actual.getInvokerProperty(), invokerProperty);
+
+ TopiaEntity[] path = actual.getPath();
+ Assert.assertEquals(expected.length, path.length);
+ for (int i = 0; i < expected.length; i++) {
+ Assert.assertEquals(expected[i].getTopiaId(), path[i].getTopiaId());
+ }
+ }
+
+ protected void assertEntry(TopiaEntity expected,
+ int nbPath,
+ Entry<TopiaEntity, List<TopiaEntityRef>> entry) {
+ Assert.assertEquals(expected, entry.getKey());
+ refs = entry.getValue();
+ Assert.assertEquals(nbPath, refs.size());
+ }
+
+ protected void assertDetected(int size) {
+ Assert.assertNotNull(detected);
+ Assert.assertEquals(size, detected.size());
+ itr = detected.entrySet().iterator();
+ }
+}
Property changes on: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTester.java
___________________________________________________________________
Added: svn:keywords
+ "Author Date Id Revision HeadURL
Added: 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 (rev 0)
+++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java 2010-03-13 21:49:57 UTC (rev 1839)
@@ -0,0 +1,124 @@
+package org.nuiton.topia.persistence.util;
+
+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.*;
+
+/**
+ * Test the {@link TopiaEntityRefTester} on
+ * <p/>
+ * <ul> <li>{@link Pet}</li> <li>{@link Race}</li> <li>{@link Person}</li>
+ * </ul>
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.3.1
+ */
+public class TopiaEntityRefTesterTest extends TopiaEntityRefTester<TopiaTestEntityEnum> {
+
+ private static final String PET1 = "pet1";
+
+ 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[]{
+ TopiaTestEntityEnum.Pet,
+ TopiaTestEntityEnum.Person,
+ TopiaTestEntityEnum.Race,
+ };
+ }
+
+ @Test
+ public void testPet() throws TopiaException {
+
+ Pet pet = new PetImpl();
+ pet.setTopiaId(PET1);
+
+ detectReferences(pet, 0);
+
+ Race race = new RaceImpl();
+ race.setTopiaId(RACE1);
+
+ detectReferences(race, 0);
+
+ Person person = new PersonImpl();
+ person.setTopiaId(PERSON1);
+
+ detectReferences(race, 0);
+
+ pet.setRace(race);
+
+ detectReferences(pet, 1, RACE1);
+
+ entry = itr.next();
+ assertEntry(race, 1, entry);
+ assertEntityRef(0, 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);
+
+ entry = itr.next();
+ assertEntry(race, 1, entry);
+ assertEntityRef(0, 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);
+
+ Pet pet2 = new PetImpl();
+ pet2.setTopiaId(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);
+
+
+ entry = itr.next();
+ assertEntry(pet2, 1, entry);
+ assertEntityRef(0, person, String.format(PET_REF, PET2), person, pet2);
+
+ 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);
+
+
+ entry = itr.next();
+ assertEntry(pet2, 1, entry);
+ assertEntityRef(0, person, String.format(PET_REF, PET2), person, pet2);
+
+ entry = itr.next();
+ assertEntry(race, 2, entry);
+ assertEntityRef(0, pet, Pet.RACE, person, pet, race);
+ assertEntityRef(1, pet2, Pet.RACE, person, pet2, race);
+
+ }
+}
Property changes on: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/util/TopiaEntityRefTesterTest.java
___________________________________________________________________
Added: svn:keywords
+ "Author Date Id Revision HeadURL