Author: tchemit Date: 2010-03-15 13:12:51 +0100 (Mon, 15 Mar 2010) New Revision: 1786 Log: * Evolution #403: Introduce a extented jaxx.runtime.decorator.MapPropertyHandler * Introduce a simple JXPathContext tester jaxx.runtime.decorator.JXPathContextTester * Reformat decorator package Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MapPropertyHandler.java trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/JXPathContextTester.java trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/MapPropertyHandlerTest.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/Decorator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorProvider.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorUtils.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/JXPathDecorator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MultiJXPathDecorator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/PropertyDecorator.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/Decorator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/Decorator.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/Decorator.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -20,6 +20,8 @@ */ package jaxx.runtime.decorator; +import java.io.Serializable; + /** * A simple contract to define a String decorator on any java objet. * @@ -27,7 +29,7 @@ * @author chemit * @since 1.7.2 (was previously {@code jaxx.runtime.Decorator}) */ -public abstract class Decorator<O> implements java.io.Serializable { +public abstract class Decorator<O> implements Serializable { private static final long serialVersionUID = -1L; /** Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorProvider.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorProvider.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorProvider.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -50,7 +50,8 @@ } /** - * Obtain a decorator given a object and an extra name to qualify the context. + * Obtain a decorator given a object and an extra name to qualify the + * context. * * @param object object of decorated object * @param name extra name to qualify the decorator to use @@ -98,7 +99,8 @@ } } - protected void registerPropertyDecorator(Class<?> klass, String expression) { + protected void registerPropertyDecorator(Class<?> klass, + String expression) { registerPropertyDecorator(klass, null, expression); } @@ -106,22 +108,38 @@ registerJXPathDecorator(klass, null, expression); } - protected void registerMultiJXPathDecorator(Class<?> klass, String expression, String separator, String separatorReplacement) { - registerMultiJXPathDecorator(klass, null, expression, separator, separatorReplacement); + protected void registerMultiJXPathDecorator(Class<?> klass, + String expression, + String separator, + String separatorReplacement) { + registerMultiJXPathDecorator(klass, null, expression, separator, + separatorReplacement); } - protected void registerPropertyDecorator(Class<?> klass, String name, String expression) { - Decorator<?> decorator = DecoratorUtils.newPropertyDecorator(klass, expression); + protected void registerPropertyDecorator(Class<?> klass, + String name, + String expression) { + Decorator<?> decorator = + DecoratorUtils.newPropertyDecorator(klass, expression); registerDecorator(name, decorator); } - protected void registerJXPathDecorator(Class<?> klass, String name, String expression) { - Decorator<?> decorator = DecoratorUtils.newJXPathDecorator(klass, expression); + protected void registerJXPathDecorator(Class<?> klass, + String name, + String expression) { + Decorator<?> decorator = + DecoratorUtils.newJXPathDecorator(klass, expression); registerDecorator(name, decorator); } - protected void registerMultiJXPathDecorator(Class<?> klass, String name, String expression, String separator, String separatorReplacement) { - Decorator<?> decorator = DecoratorUtils.newMultiJXPathDecorator(klass, expression, separator, separatorReplacement); + protected void registerMultiJXPathDecorator(Class<?> klass, + String name, + String expression, + String separator, + String separatorReplacement) { + Decorator<?> decorator = DecoratorUtils.newMultiJXPathDecorator( + klass, expression, separator, separatorReplacement + ); registerDecorator(name, decorator); } @@ -136,16 +154,21 @@ * @param context the name decorator * @param decorator the decorator to register */ - protected <T> void registerDecorator(String context, Decorator<T> decorator) { + protected <T> void registerDecorator(String context, + Decorator<T> decorator) { // obtain the decorator context - DecoratorContext<?> result = getDecoratorContext(decorator.getInternalClass(), context); + DecoratorContext<?> result = + getDecoratorContext(decorator.getInternalClass(), context); if (result != null) { - throw new IllegalArgumentException("there is an already register decorator with context " + result); + throw new IllegalArgumentException( + "there is an already register decorator with context " + + result); } - DecoratorContext<T> decoratorContext = new DecoratorContext<T>(context, decorator); + DecoratorContext<T> decoratorContext = + new DecoratorContext<T>(context, decorator); if (log.isDebugEnabled()) { log.debug(decoratorContext); } @@ -160,7 +183,8 @@ } @SuppressWarnings({"unchecked"}) - protected <T> DecoratorContext<T> getDecoratorContext(Class<T> type, String context) { + protected <T> DecoratorContext<T> getDecoratorContext(Class<T> type, + String context) { DecoratorContext<T> result = null; if (decorators != null) { for (DecoratorContext<?> d : decorators) { @@ -214,12 +238,14 @@ } public boolean accept(String context) { - return ((this.context == null && context == null) || (this.context != null && this.context.equals(context))); + return ((this.context == null && context == null) || + (this.context != null && this.context.equals(context))); } @Override public String toString() { - return super.toString() + "<type: " + getType().getName() + ", context :" + context + ">"; + return super.toString() + "<type: " + getType().getName() + + ", context :" + context + ">"; } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorUtils.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorUtils.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/DecoratorUtils.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -26,14 +26,14 @@ import java.util.*; /** - * Some usefull methods on {@link Decorator} to create, and sort data with decorators. + * Some usefull methods on {@link Decorator} to create, and sort data with + * decorators. * <p/> - * To create a new decorator, use one of the methods : - * <ul> - * <li>{@link #newPropertyDecorator(Class, String)}</li> - * <li>{@link #newJXPathDecorator(Class, String)}</li> - * <li>{@link #newMultiJXPathDecorator(Class, String, String)})</li> - * <li>{@link #newMultiJXPathDecorator(Class, String, String, String)})</li> + * To create a new decorator, use one of the methods : <ul> <li>{@link + * #newPropertyDecorator(Class, String)}</li> <li>{@link + * #newJXPathDecorator(Class, String)}</li> <li> + * {@link #newMultiJXPathDecorator(Class, String, String)})</li> + * <li>{@link #newMultiJXPathDecorator(Class, String,String, String)})</li> * </ul> * <p/> * To sort a list of data, using a {@link JXPathDecorator}, use the method @@ -49,9 +49,11 @@ * Factory method to instanciate a new {@link PropertyDecorator} for the * given class {@code internlaClass} and a readable property name. * - * @param internalClass the class of the objects decorated by the new decorator + * @param internalClass the class of the objects decorated by the new + * decorator * @param property the property - * @param <O> the generic type of class to be decorated by the new decorator + * @param <O> the generic type of class to be decorated by the new + * decorator * @return the new instanciated decorator * @throws IllegalArgumentException if the expression is not valid, says: * <p/> @@ -68,14 +70,14 @@ } /** - * Factory method to instanciate a new {@link JXPathDecorator} for the - * given class {@code internalClass} and expression. + * Factory method to instanciate a new {@link JXPathDecorator} for the given + * class {@code internalClass} and expression. * * @param internalClass the class of the objects decorated by the new * decorator * @param expression the expression to use to decorated objects - * @param <O> the generic type of class to be decorated by the - * new decorator + * @param <O> the generic type of class to be decorated by the new + * decorator * @return the new instanciated decorator * @throws IllegalArgumentException if the expression is not valid, says: * <p/> @@ -171,16 +173,16 @@ // seek end of jxpath end = expression.indexOf("}", start + 1); if (end == -1) { - throw new IllegalArgumentException("could not find the rigth " + - "brace starting at car " + start + " : " + - expression.substring(start + 2)); + throw new IllegalArgumentException( + "could not find the rigth brace starting at car " + + start + " : " + expression.substring(start + 2)); } String jxpath = expression.substring(start + 2, end); // not allowed ${ inside a jxpath token if (jxpath.indexOf("${") > -1) { - throw new IllegalArgumentException("could not find a ${ " + - "inside a jxpath expression at car " + - (start + 2) + " : " + jxpath); + throw new IllegalArgumentException( + "could not find a ${ inside a jxpath expression at " + + "car " + (start + 2) + " : " + jxpath); } // save the jxpath token lTokens.add(jxpath); Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/JXPathDecorator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/JXPathDecorator.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/JXPathDecorator.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -24,6 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.io.Serializable; import java.util.*; /** @@ -75,7 +76,7 @@ Context<O> context) throws IllegalArgumentException, NullPointerException { super(internalClass); - this.initialExpression = expression; + initialExpression = expression; if (context != null) { setContext(context); } @@ -130,7 +131,7 @@ public void setContext(Context<O> context) { this.context = context; - this.nbToken = context.tokens.length; + nbToken = context.tokens.length; // always reset comparator //this.context.comparator = null; if (log.isDebugEnabled()) { @@ -158,7 +159,7 @@ public JXPathComparator(String expression) { this.expression = expression; - this.valueCache = new HashMap<O, Comparable<Comparable<?>>>(); + valueCache = new HashMap<O, Comparable<Comparable<?>>>(); } @Override @@ -192,7 +193,7 @@ } } - public static class Context<O> implements java.io.Serializable { + public static class Context<O> implements Serializable { /** * expression to format using {@link String#format(String, Object...)}, Added: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MapPropertyHandler.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MapPropertyHandler.java (rev 0) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MapPropertyHandler.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -0,0 +1,176 @@ +/* + * *##% + * JAXX Runtime + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package jaxx.runtime.decorator; + +import org.apache.commons.jxpath.DynamicPropertyHandler; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * A extended handler to deal with map in JXPath contexts. + * <p/> + * The basic one in JXPath api only deals with map keys as string. + * <p/> + * We offers a hook in method {@link #getKey(Object)} to obtain a string + * representation of a real object in map (key or value). + * <p/> + * More over, you can also access directly to a key or a value, using this + * syntax : + * <p/> + * <pre>context.getValue(".[@name='key:programme2']")</pre> + * <pre>context.getValue(".[@name='value:programme2']")</pre> + * <p/> + * If the values are iterable, then will scan inot it when looking for a direct + * value. + * + * @author tchemit <chemit@codelutin.com> + * @see DynamicPropertyHandler + * @since 2.0.1 + */ +public class MapPropertyHandler implements DynamicPropertyHandler { + + /** Logger */ + private static final Log log = + LogFactory.getLog(MapPropertyHandler.class); + + @Override + public String[] getPropertyNames(Object object) { + Map<?, ?> map = (Map<?, ?>) object; + Set<?> set = map.keySet(); + String[] names = new String[set.size()]; + Iterator<?> it = set.iterator(); + for (int i = 0; i < names.length; i++) { + Object o = it.next(); + names[i] = getKey(o); + } + return names; + } + + @Override + public Object getProperty(Object object, String propertyName) { + Map<?, ?> map = (Map<?, ?>) object; + boolean getKey = false; + Object property0; + if (propertyName.startsWith("value:")) { + propertyName = propertyName.substring(6); + if (log.isDebugEnabled()) { + log.debug("property value name " + propertyName); + } + property0 = getPropertyValue(map, propertyName); + if (log.isDebugEnabled()) { + log.debug("property value = " + property0); + } + return property0; + } + if (propertyName.startsWith("key:")) { + propertyName = propertyName.substring(4); + getKey = true; + } + property0 = getPropertyKey(map, propertyName); + Object result = null; + if (property0 != null) { + if (getKey) { + result = property0; + } else { + result = map.get(property0); + } + } + return result; + } + + @Override + public void setProperty(Object object, String propertyName, Object value) { + Map map = (Map) object; + Object property0 = getPropertyKey(map, propertyName); + if (property0 != null) { + map.put(property0, value); + } + } + + /** + * Obtain the key from the map keys which matches the given {@code key}. + * <p/> + * To compare object ot string, please refers to the method {@link + * #getKey(Object)}. + * + * @param map the map to scan + * @param key the string representation of the required key as object + * @return the found key, or {@code null} if not found. + */ + public Object getPropertyKey(Map<?, ?> map, String key) { + Set<?> set = map.keySet(); + for (Object o : set) { + String k = getKey(o); + if (key.equals(k)) { + return o; + } + } + return null; + } + + /** + * Obtain the value from the map values which matches the given {@code + * value}. + * <p/> + * To compare object to string, please refer to the method {@link + * #getKey(Object)}. + * + * @param map the map to scan + * @param value the string representation of the value + * @return the found value, or {@code null} if not found.} + */ + public Object getPropertyValue(Map<?, ?> map, String value) { + Collection<?> set = map.values(); + for (Object o : set) { + if (o instanceof Iterable<?>) { + Iterable<?> c = (Iterable<?>) o; + for (Object oo : c) { + String k = getKey(oo); + if (value.equals(k)) { + return oo; + } + } + continue; + } + String k = getKey(o); + if (value.equals(k)) { + return o; + } + } + return null; + } + + /** + * Obtain a string representation of an object. + * + * @param o the object to decorate + * @return the string representation of the object + */ + protected String getKey(Object o) { + String k = String.valueOf(o); + return k; + } +} Property changes on: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MapPropertyHandler.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MultiJXPathDecorator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MultiJXPathDecorator.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/MultiJXPathDecorator.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -28,34 +28,35 @@ /** * TODO * + * @author chemit * @param <O> type of data to decorate - * @author chemit * @see Decorator * @since 1.7.2 (was previously {@code jaxx.runtime.MultiJXPathDecorator}) */ public class MultiJXPathDecorator<O> extends JXPathDecorator<O> { private static final long serialVersionUID = 1L; - /** - * Logger - */ - private static final Log log = LogFactory.getLog(MultiJXPathDecorator.class); - /** - * Contexts of the decorator - */ + + /** Logger */ + private static final Log log = + LogFactory.getLog(MultiJXPathDecorator.class); + + /** Contexts of the decorator */ protected Context<O>[] contexts; - /** - * context separator - */ + + /** context separator */ protected String separator; - /** - * context separator replacement - */ + + /** context separator replacement */ protected String separatorReplacement; - protected MultiJXPathDecorator(Class<O> internalClass, String expression, - String separator, String separatorReplacement, - Context<O>[] contexts) throws IllegalArgumentException, NullPointerException { + protected MultiJXPathDecorator( + Class<O> internalClass, + String expression, + String separator, + String separatorReplacement, + Context<O>[] contexts) throws IllegalArgumentException, + NullPointerException { super(internalClass, expression, null); this.separator = separator; this.separatorReplacement = separatorReplacement; @@ -64,7 +65,7 @@ setContextIndex(0); if (log.isDebugEnabled()) { - log.debug(expression + " --> " + this.context); + log.debug(expression + " --> " + context); } } @@ -92,9 +93,13 @@ return context1.getComparator(0); } - protected void ensureContextIndex(MultiJXPathDecorator<?> decorator, int pos) { + protected void ensureContextIndex(MultiJXPathDecorator<?> decorator, + int pos) { if (pos < -1 || pos > decorator.contexts.length) { - throw new ArrayIndexOutOfBoundsException("context index " + pos + " is out of bound, can be inside [" + 0 + "," + decorator.contexts.length + "]"); + throw new ArrayIndexOutOfBoundsException( + "context index " + pos + + " is out of bound, can be inside [" + 0 + "," + + decorator.contexts.length + "]"); } } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/PropertyDecorator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/PropertyDecorator.java 2010-03-15 11:20:55 UTC (rev 1785) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/decorator/PropertyDecorator.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -28,7 +28,8 @@ import java.lang.reflect.Method; /** - * Simple property decorator based on {@link String#format(String, Object[])} method. + * Simple property decorator based on {@link String#format(String, Object...)} + * method. * <p/> * To use it, give him a class and the property name to render. * <p/> @@ -37,22 +38,21 @@ * Decorator<Object> d = DecoratorUtils.newPropertyDecorator(PropertyDecorator.class,"expressions"); * </pre> * + * @author chemit * @param <O> type of data to decorate - * @author chemit * @see Decorator * @since 1.7.2 (was previously {@code jaxx.runtime.PropertyDecorator}) */ public class PropertyDecorator<O> extends Decorator<O> { private static final long serialVersionUID = 1L; - /** - * Logger - */ + + /** Logger */ static private final Log log = LogFactory.getLog(PropertyDecorator.class); - /** - * name of property - */ + + /** name of property */ protected String property; + protected transient Method m; @Override @@ -69,7 +69,8 @@ return property; } - protected PropertyDecorator(Class<O> internalClass, String property) throws NullPointerException { + protected PropertyDecorator(Class<O> internalClass, + String property) throws NullPointerException { super(internalClass); if (property == null) { throw new NullPointerException("property can not be null."); @@ -83,7 +84,7 @@ if (m == null) { for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(internalClass)) { if (propertyDescriptor.getName().equals(property)) { - this.m = propertyDescriptor.getReadMethod(); + m = propertyDescriptor.getReadMethod(); break; } } Added: trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/JXPathContextTester.java =================================================================== --- trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/JXPathContextTester.java (rev 0) +++ trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/JXPathContextTester.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -0,0 +1,53 @@ +/* + * *##% + * JAXX Runtime + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package jaxx.runtime.decorator; + +import org.apache.commons.jxpath.JXPathContext; +import org.junit.After; +import org.junit.Assert; + +/** + * A simple class to test JXPath context. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.1 + */ +public abstract class JXPathContextTester { + + JXPathContext context; + + @After + public void after() { + context = null; + + } + + protected void newContext(Object o) { + context = JXPathContext.newContext(o); + } + + protected <T> T getValue(String path) { + Assert.assertNotNull("pas de context initialisé", context); + Object value = context.getValue(path); + return (T) value; + } + +} Property changes on: trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/JXPathContextTester.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Added: trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/MapPropertyHandlerTest.java =================================================================== --- trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/MapPropertyHandlerTest.java (rev 0) +++ trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/MapPropertyHandlerTest.java 2010-03-15 12:12:51 UTC (rev 1786) @@ -0,0 +1,124 @@ +/* + * *##% + * JAXX Runtime + * Copyright (C) 2008 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package jaxx.runtime.decorator; + +import org.apache.commons.jxpath.JXPathIntrospector; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Map; +import java.util.TreeMap; + +/** + * Tests the class {@link MapPropertyHandler}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.1 + */ +public class MapPropertyHandlerTest extends JXPathContextTester { + + + /** Logger */ + private static final Log log = + LogFactory.getLog(MapPropertyHandlerTest.class); + + private static final String TWO_ID = "two"; + + private static final String ONE_ID = "one"; + + @BeforeClass + public static void beforeClass() { + + // pour initialiser le JXPathContext + JXPathIntrospector.registerDynamicClass( + Map.class, + MapPropertyHandler.class + ); + } + + @Before + public void setUp() { + + Map<String, Boolean> map = new TreeMap<String, Boolean>(); + map.put(ONE_ID, true); + map.put(TWO_ID, false); + + if (log.isInfoEnabled()) { + log.info("init map : " + map); + } + newContext(map); + } + + @Test + public void testValues() throws Exception { + + Boolean value; + + value = (Boolean) getValue(".[@name='one']"); + Assert.assertNotNull(value); + Assert.assertTrue(value); + + value = (Boolean) getValue(".[@name='two']"); + Assert.assertNotNull(value); + Assert.assertFalse(value); + + value = (Boolean) getValue(".[@name='three']"); + Assert.assertNull(value); + } + + @Test + public void testKey() throws Exception { + + String key; + + key = (String) getValue(".[@name='key:one']"); + Assert.assertNotNull(key); + Assert.assertEquals(ONE_ID, key); + + key = (String) getValue(".[@name='key:two']"); + Assert.assertNotNull(key); + Assert.assertEquals(TWO_ID, key); + + key = (String) getValue(".[@name='key:fake']"); + Assert.assertNull(key); + } + + @Test + public void testValue() throws Exception { + + Boolean value; + + value = (Boolean) getValue(".[@name='value:true']"); + Assert.assertNotNull(value); + Assert.assertTrue(value); + + value = (Boolean) getValue(".[@name='value:false']"); + Assert.assertNotNull(value); + Assert.assertFalse(value); + + value = (Boolean) getValue(".[@name='value:fake']"); + Assert.assertNull(value); + } +} Property changes on: trunk/jaxx-runtime/src/test/java/jaxx/runtime/decorator/MapPropertyHandlerTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL
participants (1)
-
tchemit@users.nuiton.org