Author: tchemit Date: 2010-03-15 21:20:02 +0100 (Mon, 15 Mar 2010) New Revision: 1789 Log: clean code + little improvments Added: trunk/jaxx-runtime/src/test/java/jaxx/runtime/context/DataContextEntryIteratorTest.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DataContext.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DefaultJAXXContext.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/JAXXContextEntryDef.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-en_GB.properties trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-fr_FR.properties Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/JAXXUtil.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -132,7 +132,20 @@ return contextEntryDef; } + public static <K, V> JAXXContextEntryDef<Map> newMapContextEntryDef( + String name) { + Class<Map> mapClass = Map.class; + JAXXContextEntryDef<Map> contextEntryDef = + new JAXXContextEntryDef<Map>(name, mapClass); + return contextEntryDef; + } + @SuppressWarnings({"unchecked"}) + protected static Class<Map> castMap() { + return Map.class; + } + + @SuppressWarnings({"unchecked"}) protected static <O> Class<List<O>> castList() { return (Class<List<O>>) Collections.emptyList().getClass(); } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/SwingUtil.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -34,6 +34,7 @@ import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.util.List; import javax.swing.*; import javax.swing.event.*; @@ -70,6 +71,8 @@ import org.jdesktop.jxlayer.JXLayer; import org.jdesktop.swingx.JXTreeTable; +import static org.nuiton.i18n.I18n._; + /** * The runtime swing util class with some nice stuff. * <p/> @@ -869,6 +872,34 @@ } } + public static void openLink(String url) { + + try { + if (!Desktop.isDesktopSupported()) { + throw new IllegalStateException( + _("swing.error.desktop.not.supported")); + } + + + Desktop desktop = Desktop.getDesktop(); + + if (!desktop.isSupported(Desktop.Action.BROWSE)) { + + throw new IllegalStateException( + _("swing.error.desktop.browse.not.supported")); + } + + URI uri = new URI(url); + desktop.browse(uri); + } + catch (Exception e) { + + if (log.isErrorEnabled()) { + log.error("Error while opening link", e); + } + } + } + /** * Expand all childs of a tree. * Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DataContext.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DataContext.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DataContext.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -35,54 +35,42 @@ * Un contexte de données qui permet l'utilisation des bindings sur les * entrées du contexte. * - * TODO javadoc - * - * @author tony + * @author tchemit <chemit@codelutin.com> * @since 1.3 */ public abstract class DataContext { /** to use log facility, just put in your code: log.info(\"...\"); */ static private Log log = LogFactory.getLog(DataContext.class); + + public static final DataContextEntry<?>[] EMPTY_DATA_CONTEXT_ENTRY_ARRAY = + new DataContextEntry<?>[0]; + /** le context qui contient les données */ protected final DefaultJAXXContext delegate; + /** la definition de l'entree actuallement selectionnee */ protected DataContextEntry<?> currentEntry; + /** to manage properties modifications */ protected final PropertyChangeSupport pcs; + protected DataContextEntry<?>[] entries; + protected final String[] DEFAULT_JAXX_PCS; public abstract String getContextPath(Object... e); - public DataContext(String[] DEFAULT_JAXX_PCS, DataContextEntry<?>[] entries) { + public DataContext(String[] DEFAULT_JAXX_PCS, + DataContextEntry<?>[] entries) { this.DEFAULT_JAXX_PCS = DEFAULT_JAXX_PCS; this.entries = entries; delegate = new DefaultJAXXContext() { -// @Override -// protected void setUi(JAXXObject ui) { -// throw new IllegalStateException("can not use this method for this type of context"); -// } -// -// @Override -// public <O extends Container> O getParentContainer(Class<O> clazz) { -// throw new IllegalStateException("can not use this method for this type of context"); -// } -// -// @Override -// public <O extends Container> O getParentContainer(Object top, Class<O> clazz) { -// throw new IllegalStateException("can not use this method for this type of context"); -// } -// -// @Override -// protected JAXXObject getUi() { -// throw new IllegalStateException("can not use this method for this type of context"); -// } - @Override protected void setParentContext(JAXXContext parentContext) { - throw new IllegalStateException("can not use this method for this type of context"); + throw new IllegalStateException( + "can not use this method for this type of context"); } @Override @@ -123,7 +111,8 @@ }; } - public Iterable<? extends DataContextEntry<?>> iterateToLevel(final int level) { + public Iterable<? extends DataContextEntry<?>> iterateToLevel( + final int level) { return new Iterable<DataContextEntry<?>>() { @Override @@ -172,21 +161,59 @@ return result; } - public void setContextValue(DataContextEntry<?> entry, Object value, String key) { + public void setContextValue(DataContextEntry<?> entry, + Object value, + String key) { String contextKey = getKey(entry, key); delegate.setContextValue(value, contextKey); } - public void removeContextValue(DataContextEntry<?> entry, Object value, String key) { + /** + * + * @param entry + * @param value + * @param key + * @deprecated since 2.0.1, use instead {@link #removeContextValue(DataContextEntry, String)} + */ + public void removeContextValue(DataContextEntry<?> entry, + Object value, + String key) { + removeContextValue(entry,key); +// String contextKey = getKey(entry, key); +// delegate.removeContextValue(entry.getKlass(), contextKey); +// delegate.removeContextValue(value.getClass(), contextKey); + } + + /** + * + * @param entry + * @param klass + * @param key + */ + public void removeContextValue(DataContextEntry<?> entry, + Class<?> klass, + String key) { String contextKey = getKey(entry, key); - delegate.removeContextValue(value.getClass(), contextKey); + delegate.removeContextValue(klass,key); +// String contextKey = getKey(entry, key); +// delegate.removeContextValue(entry.getKlass(), contextKey); +// delegate.removeContextValue(value.getClass(), contextKey); } + public void removeContextValue(DataContextEntry<?> entry, + String key) { + String contextKey = getKey(entry, key); + delegate.removeContextValue(entry.getKlass(), contextKey); + } + @SuppressWarnings("unchecked") - public void updateSelectedData(String path, Object data, UpdateDataContext updator) { + public void updateSelectedData(String path, + Object data, + UpdateDataContext updator) { if (log.isDebugEnabled()) { - log.debug("----------------------------------------------------------------"); + log.debug("-----------------------------------------------------" + + "-----------"); } if (currentEntry != null) { @@ -209,7 +236,8 @@ if (currentEntry != null) { - for (DataContextEntry<?> s : iterateToLevel(currentEntry.getLevel())) { + for (DataContextEntry<?> s : + iterateToLevel(currentEntry.getLevel())) { if (log.isDebugEnabled()) { log.debug("add entry " + s); @@ -223,7 +251,8 @@ pcs.addPropertyChangeListener(listener); } - public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + public void addPropertyChangeListener(String propertyName, + PropertyChangeListener listener) { pcs.addPropertyChangeListener(propertyName, listener); } @@ -231,7 +260,8 @@ pcs.removePropertyChangeListener(listener); } - public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + public void removePropertyChangeListener(String propertyName, + PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } @@ -239,7 +269,8 @@ return pcs.hasListeners(propertyName); } - public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) { + public synchronized PropertyChangeListener[] getPropertyChangeListeners( + String propertyName) { return pcs.getPropertyChangeListeners(propertyName); } @@ -248,7 +279,9 @@ } public void removeJaxxPropertyChangeListener() { - PropertyChangeListener[] toRemove = JAXXUtil.findJaxxPropertyChangeListener(DEFAULT_JAXX_PCS, getPropertyChangeListeners()); + PropertyChangeListener[] toRemove = + JAXXUtil.findJaxxPropertyChangeListener( + DEFAULT_JAXX_PCS, getPropertyChangeListeners()); if (toRemove == null || toRemove.length == 0) { return; } @@ -264,7 +297,9 @@ } } - protected void firePropertyChange(String name, Object oldValue, Object newValue) { + protected void firePropertyChange(String name, + Object oldValue, + Object newValue) { pcs.firePropertyChange(name, oldValue, newValue); } @@ -299,9 +334,9 @@ public DataContextEntry(Class<E> klass, DataContextEntry<?> previous) { this.previous = previous; - this.level = previous.level + 1; + level = previous.level + 1; this.klass = klass; - this.parents = new DataContextEntry<?>[level]; + parents = new DataContextEntry<?>[level]; int i = level; while (i > 0) { parents[--i] = previous; @@ -310,10 +345,10 @@ } public DataContextEntry(Class<E> klass) { - this.level = 0; + level = 0; this.klass = klass; - this.previous = null; - this.parents = new DataContextEntry<?>[0]; + previous = null; + parents = EMPTY_DATA_CONTEXT_ENTRY_ARRAY; } public Class<E> getKlass() { @@ -355,7 +390,7 @@ } } - public static interface UpdateDataContext<D extends DataContext> { + public interface UpdateDataContext<D extends DataContext> { void onRemovingData(D context, DataContextEntry<D> entry); @@ -373,15 +408,19 @@ this(datas, false, -1); } - public DataContextEntryIterator(DataContextEntry<?>[] datas, int level) { + public DataContextEntryIterator(DataContextEntry<?>[] datas, + int level) { this(datas, false, level); } - public DataContextEntryIterator(DataContextEntry<?>[] datas, boolean reverse) { + public DataContextEntryIterator(DataContextEntry<?>[] datas, + boolean reverse) { this(datas, reverse, -1); } - DataContextEntryIterator(DataContextEntry<?>[] datas, boolean reverse, int level) { + DataContextEntryIterator(DataContextEntry<?>[] datas, + boolean reverse, + int level) { this.datas = datas; this.reverse = reverse; if (reverse) { @@ -397,7 +436,8 @@ if (reverse) { return index > 0; } else { - return index + 1 < datas.length && (level == -1 || datas[index + 1].getLevel() <= level); + return index + 1 < datas.length && + (level == -1 || datas[index + 1].getLevel() <= level); } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DefaultJAXXContext.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DefaultJAXXContext.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/DefaultJAXXContext.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -26,54 +26,46 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** - * The default {@link JAXXContext} to be used in a {@link JAXXObject} by delegation. + * The default {@link JAXXContext} to be used in a {@link JAXXObject} by + * delegation. * <p/> - * The values are store in a {@link Map} but we can not use directly the values as key. + * The values are store in a {@link Map} but we can not use directly the values + * as key. * <p/> - * Because, it does not work if we add for the same object multi entries (named and unamed)... + * Because, it does not work if we add for the same object multi entries (named + * and unamed)... * <p/> - * We prefer use as entry the {@link JAXXContextEntryDef} associated with the value. + * We prefer use as entry the {@link JAXXContextEntryDef} associated with the + * value. * * @author chemit */ public class DefaultJAXXContext implements JAXXContext { - /** - * entry of the parent context - */ - protected static final JAXXContextEntryDef<JAXXContext> PARENT_CONTEXT_ENTRY = JAXXUtil.newContextEntryDef(JAXXContext.class); - /** - * Logger - */ + /** entry of the parent context */ + protected static final JAXXContextEntryDef<JAXXContext> + PARENT_CONTEXT_ENTRY = JAXXUtil.newContextEntryDef(JAXXContext.class); + + /** Logger */ static private final Log log = LogFactory.getLog(DefaultJAXXContext.class); -// /** -// * l'ui auquel est rattache le context - // */ -// protected JAXXObject ui; - /** - * le context parent - */ + + /** le context parent */ protected JAXXContext parentContext; - /** - * les données contenues dans le context - */ + + /** les données contenues dans le context */ protected final Map<JAXXContextEntryDef<?>, Object> data; public DefaultJAXXContext() { data = new HashMap<JAXXContextEntryDef<?>, Object>(); } -// public DefaultJAXXContext(JAXXObject ui) { -// this(); -// this.ui = ui; -// } - @Override public <T> void setContextValue(T o) { setContextValue(o, null); @@ -90,7 +82,8 @@ Object oldValue = remove0(o.getClass(), name); if (oldValue != null) { if (log.isDebugEnabled()) { - log.debug("remove value " + oldValue.getClass() + " for " + entry); + log.debug("remove value " + oldValue.getClass() + " for " + + entry); } } // then can put safely @@ -105,7 +98,9 @@ @SuppressWarnings({"unchecked"}) @Override public <T> T getContextValue(Class<T> clazz, String name) { - if (parentContext != null && parentContext.getClass() == clazz || PARENT_CONTEXT_ENTRY.accept(clazz, name)) { + if (parentContext != null && + parentContext.getClass().equals(clazz) || + PARENT_CONTEXT_ENTRY.accept(clazz, name)) { return (T) getParentContext(); } for (Entry<JAXXContextEntryDef<?>, Object> entry : data.entrySet()) { @@ -115,8 +110,9 @@ } // no value found in this context, will try in the parent context - if (JAXXContext.class == clazz) { - // no seek in the parent context, since we are already looking for it + if (JAXXContext.class.equals(clazz)) { + // no seek in the parent context, since we are already looking + // for it return null; } @@ -139,30 +135,6 @@ remove0(klazz, name); } -// @Override -// public <O extends Container> O getParentContainer(Class<O> clazz) { -// return this.getParentContainer(ui, clazz); -// } -// -// @SuppressWarnings({"unchecked"}) -// @Override -// public <O extends Container> O getParentContainer(Object top, Class<O> clazz) { -// if (ui == null) { -// throw new IllegalStateException("no ui attached to this context"); -// } -// if (top == null) { -// throw new IllegalArgumentException("top parameter can not be null"); -// } -// if (!Container.class.isAssignableFrom(top.getClass())) { -// throw new IllegalArgumentException("top parameter " + top + " is not a " + Container.class); -// } -// Container parent = ((Container) top).getParent(); -// if (parent != null && !clazz.isAssignableFrom(parent.getClass())) { -// parent = getParentContainer(parent, clazz); -// } -// return (O) parent; -// } - /** * Obtain all the keys of data for a given type. * @@ -171,9 +143,9 @@ * @since 1.3 */ public String[] getKeys(Class<?> klass) { - List<String> keys = new java.util.ArrayList<String>(); + List<String> keys = new ArrayList<String>(); for (JAXXContextEntryDef<?> key : data.keySet()) { - if (key.getKlass() == klass) { + if (key.getKlass().equals(klass)) { keys.add(key.getName()); } } @@ -185,14 +157,6 @@ data.clear(); } -// protected JAXXObject getUi() { -// return ui; -// } -// -// protected void setUi(JAXXObject ui) { -// this.ui = ui; -// } - protected JAXXContextEntryDef<?> getKey(String name, Class<?> klass) { return JAXXUtil.newContextEntryDef(name, klass); } @@ -215,7 +179,7 @@ return (T) data.remove(entry); } - if (JAXXContext.class == klazz) { + if (JAXXContext.class.equals(klazz)) { return null; } // try in parentContext Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/JAXXContextEntryDef.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/JAXXContextEntryDef.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/context/JAXXContextEntryDef.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -20,20 +20,21 @@ */ package jaxx.runtime.context; +import java.io.Serializable; import java.util.List; -import java.util.Map; import jaxx.runtime.JAXXContext; /** * To qualify an entry in a {@link JAXXContext}. * <p/> - * Use the factory methods <code>newContextEntryDef</code> and <code>newListContextEntryDef</code< to obtain new instances. + * Use the factory methods <code>newContextEntryDef</code> and + * <code>newListContextEntryDef</code< to obtain new instances. * * @param <O> type of the entry associated to the definition * @author chemit */ -public class JAXXContextEntryDef<O> implements java.io.Serializable { +public class JAXXContextEntryDef<O> implements Serializable { /** name of the entry, can be nuill for a unamed entry. */ protected String name; @@ -43,27 +44,6 @@ private static final long serialVersionUID = 1L; -// public static <O> JAXXContextEntryDef<O> newContextEntryDef(Class<O> klass) { -// return newContextEntryDef(null, klass); -// } -// -// public static <O> JAXXContextEntryDef<O> newContextEntryDef(String name, Class<O> klass) { -// return new JAXXContextEntryDef<O>(name, klass); -// } -// -// public static <O> JAXXContextEntryDef<List<O>> newListContextEntryDef() { -// return newListContextEntryDef(null); -// } -// -// @SuppressWarnings({"unchecked"}) -// public static <O> JAXXContextEntryDef<List<O>> newListContextEntryDef(String name) { -// Class<List<O>> castList = JAXXContextEntryDef.<O>castList(); -// JAXXContextEntryDef<List<O>> contextEntryDef = new JAXXContextEntryDef<List<O>>(name,castList); -// contextEntryDef.klass = castList; -// return contextEntryDef; -// } - - public String getName() { return name; } @@ -104,11 +84,6 @@ } this.klass = klass; } -// -// @SuppressWarnings({"unchecked"}) -// protected static <O> Class<List<O>> castList() { -// return (Class<List<O>>) Collections.emptyList().getClass(); -// } @Override public boolean equals(Object o) { @@ -119,28 +94,32 @@ return false; } JAXXContextEntryDef<?> that = (JAXXContextEntryDef<?>) o; - return klass.equals(that.klass) && !(name != null ? !name.equals(that.name) : that.name != null); + return klass.equals(that.klass) && + !(name != null ? !name.equals(that.name) : that.name != null); } @Override public int hashCode() { - int result = (name != null ? name.hashCode() : 0); + int result = name != null ? name.hashCode() : 0; return 31 * result + klass.hashCode(); } public boolean accept(Class<?> klass, String name) { if (klass == Object.class && this.klass != Object.class) { // try on name only - return (this.name != null && name != null && this.name.equals(name)); + return this.name != null && name != null && this.name.equals(name); } return klass.isAssignableFrom(this.klass) && (this.name == null && name == null || (this.name != null && name != null && this.name.equals(name))); } public boolean accept2(Class<?> klass, String name) { - return !(klass == Object.class && this.klass != Object.class) && - this.klass.isAssignableFrom(klass) && (this.name == null && - name == null || (this.name != null && name != null && this.name.equals(name))); + return !(Object.class.equals(klass) && + !Object.class.equals(this.klass)) && + this.klass.isAssignableFrom(klass) && + (this.name == null && name == null || + (this.name != null && name != null && this.name.equals(name)) + ); } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -98,7 +98,7 @@ protected NavigationTreeContextHelper contextHelper; protected NavigationTreeHandler(String contextPrefix, JAXXObject context, Strategy strategy) { - this.contextHelper = new NavigationTreeContextHelper(contextPrefix); + contextHelper = new NavigationTreeContextHelper(contextPrefix); this.context = context; this.strategy = strategy; addTreeSelectionListener(this); @@ -210,6 +210,8 @@ @Override public void setSelectionPaths(TreePath[] paths) { + //FIXME-TC20100315 this can not work... + // can't test like this paths as Array... if (paths.equals(getSelectionPaths())) { // stay on same node, can skip if (log.isDebugEnabled()) { Modified: trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-en_GB.properties =================================================================== --- trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-en_GB.properties 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-en_GB.properties 2010-03-15 20:20:02 UTC (rev 1789) @@ -1,3 +1,5 @@ +swing.error.desktop.browse.not.supported=Action 'Go to' not available +swing.error.desktop.not.supported=Desktop not accessible validator.field.header=Field validator.field.header.tip=The field involved validator.field.tip=Property '%1$s' Modified: trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-fr_FR.properties =================================================================== --- trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-fr_FR.properties 2010-03-15 14:19:31 UTC (rev 1788) +++ trunk/jaxx-runtime/src/main/resources/i18n/jaxx-runtime-fr_FR.properties 2010-03-15 20:20:02 UTC (rev 1789) @@ -1,3 +1,5 @@ +swing.error.desktop.browse.not.supported=Action 'aller \u00E0' non disponible +swing.error.desktop.not.supported=Desktop non accessible validator.field.header=Champ validator.field.header.tip=Le champ surquel intervient le message validator.field.tip=Propri\u00E9t\u00E9 '%1$s' Added: trunk/jaxx-runtime/src/test/java/jaxx/runtime/context/DataContextEntryIteratorTest.java =================================================================== --- trunk/jaxx-runtime/src/test/java/jaxx/runtime/context/DataContextEntryIteratorTest.java (rev 0) +++ trunk/jaxx-runtime/src/test/java/jaxx/runtime/context/DataContextEntryIteratorTest.java 2010-03-15 20:20:02 UTC (rev 1789) @@ -0,0 +1,284 @@ +/** + * *##% + * ObServe :: Services + * Copyright (C) 2008 - 2010 IRD, Codelutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * ##%* + */ +package jaxx.runtime.context; + +import jaxx.runtime.context.DataContext.DataContextEntry; +import jaxx.runtime.context.DataContext.DataContextEntryIterator; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.regex.Pattern; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests {@link DataContext}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.1 + */ +public class DataContextEntryIteratorTest { + + static DataContextEntry<?>[] entries; + + static class DataContextEntry2 extends DataContextEntry<Object> { + + public DataContextEntry2() { + super(Object.class); + } + + public DataContextEntry2(DataContextEntry<?> previous) { + super(Object.class, previous); + } + + @Override + public Pattern getPattern() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public String getContextPath(Object... args) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean acceptType(Class<?> type) { + throw new UnsupportedOperationException("Not supported yet."); + } + } + + @BeforeClass + public static void setUpClass() throws Exception { + entries = new DataContextEntry<?>[3]; + entries[0] = new DataContextEntry2(); + entries[1] = new DataContextEntry2(entries[0]); + entries[2] = new DataContextEntry2(entries[1]); + } + + DataContextEntryIterator instance; + + @Test + public void testEmptyIterator() { + + instance = new DataContextEntryIterator( + DataContext.EMPTY_DATA_CONTEXT_ENTRY_ARRAY); + + boolean result = instance.hasNext(); + assertEquals(false, result); + } + + @Test + public void testEmptyReverseIterator() { + + instance = new DataContextEntryIterator( + DataContext.EMPTY_DATA_CONTEXT_ENTRY_ARRAY, true); + + boolean result = instance.hasNext(); + assertEquals(false, result); + } + + @Test + public void testEmptyLevelIterator() { + + instance = new DataContextEntryIterator( + DataContext.EMPTY_DATA_CONTEXT_ENTRY_ARRAY, 0); + + boolean result = instance.hasNext(); + assertEquals(false, result); + + instance = new DataContextEntryIterator( + new DataContextEntry<?>[]{entries[1]}, 0); + + result = instance.hasNext(); + assertEquals(false, result); + } + + @Test + public void testSingletonIterator() { + + instance = new DataContextEntryIterator( + new DataContextEntry<?>[]{entries[0]}); + + boolean result = instance.hasNext(); + assertEquals(true, result); + DataContextEntry<?> data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + } + + @Test + public void testSingletonReverseIterator() { + + instance = new DataContextEntryIterator( + new DataContextEntry<?>[]{entries[0]}, true); + + boolean result = instance.hasNext(); + assertEquals(true, result); + DataContextEntry<?> data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + } + + @Test + public void testSingletonLevelIterator() { + + instance = new DataContextEntryIterator( + new DataContextEntry<?>[]{entries[0]}, 0); + + boolean result = instance.hasNext(); + assertEquals(true, result); + DataContextEntry<?> data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + + instance = new DataContextEntryIterator( + new DataContextEntry<?>[]{entries[0]}, 1); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + + instance = new DataContextEntryIterator(entries, 0); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + + } + + @Test + public void testIterator() { + + instance = new DataContextEntryIterator(entries); + + boolean result = instance.hasNext(); + assertEquals(true, result); + DataContextEntry<?> data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[1], data); + assertEquals(1, data.getLevel()); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[2], data); + assertEquals(2, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + + + } + + @Test + public void testReverseIterator() { + + instance = new DataContextEntryIterator(entries, true); + + boolean result = instance.hasNext(); + assertEquals(true, result); + Object data = instance.next(); + assertNotNull(data); + assertEquals(entries[2], data); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[1], data); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + + result = instance.hasNext(); + assertEquals(false, result); + + + } + + @Test + public void testLevelIterator() { + + instance = new DataContextEntryIterator(entries, 2); + + boolean result = instance.hasNext(); + assertEquals(true, result); + DataContextEntry<?> data = instance.next(); + assertNotNull(data); + assertEquals(entries[0], data); + assertEquals(0, data.getLevel()); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[1], data); + assertEquals(1, data.getLevel()); + + result = instance.hasNext(); + assertEquals(true, result); + data = instance.next(); + assertNotNull(data); + assertEquals(entries[2], data); + assertEquals(2, data.getLevel()); + + result = instance.hasNext(); + assertEquals(false, result); + + + } +} Property changes on: trunk/jaxx-runtime/src/test/java/jaxx/runtime/context/DataContextEntryIteratorTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL