Author: julien(a)jboss.com
Date: 2008-01-20 17:13:06 -0500 (Sun, 20 Jan 2008)
New Revision: 9541
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/CustomRuntimeException.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/KeyConverter.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/StringToIntegerMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapEntrySetTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapValuesTestCase.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/ValueConverter.java
Removed:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java
Modified:
modules/common/trunk/common/pom.xml
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/LazyMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java
Log:
- rewrote a part of the TypedMap / ParameterMap stuff for improving its robustness
- added many test cases for TypedMap / ParameterMap
Modified: modules/common/trunk/common/pom.xml
===================================================================
--- modules/common/trunk/common/pom.xml 2008-01-20 18:09:36 UTC (rev 9540)
+++ modules/common/trunk/common/pom.xml 2008-01-20 22:13:06 UTC (rev 9541)
@@ -125,6 +125,8 @@
<forkMode>never</forkMode>
<argLine>-enableassertions</argLine>
-->
+ <forkMode>never</forkMode>
+ <argLine>-enableassertions</argLine>
<excludes>
<exclude>org/jboss/portal/test/common/i18n/BundleNameParserTestCase*.java</exclude>
<exclude>org/jboss/portal/test/common/net/URLNavigatorTestCase.java</exclude>
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -344,11 +344,15 @@
* @throws IllegalArgumentException if the serializable object is null
* @throws IOException
*/
- public static Object clone(Serializable serializable) throws IllegalArgumentException,
IOException
+ public static <S extends Serializable> S clone(S serializable) throws
IllegalArgumentException, IOException
{
+ if (serializable == null)
+ {
+ throw new IllegalArgumentException("Cannot clone null");
+ }
try
{
- return unserialize(serialize(serializable));
+ return (S)unserialize(serialize(serializable));
}
catch (ClassNotFoundException e)
{
Copied:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
(from rev 9529,
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java)
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,700 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.util;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.AbstractCollection;
+import java.util.ArrayList;
+
+/**
+ * A map implementations that use a conversion mechanism to provide a type mapping
+ * between the external types declared by the map and the internal representations
+ * backed by the internal map.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractTypedMap<EK, EV, IK, IV> implements Map<EK,
EV>
+{
+
+ /**
+ * Transform an input to an output.
+ */
+ private abstract static class Transformer<Out, In>
+ {
+ /**
+ * Convert an object to another one.
+ *
+ * @param in the input
+ * @return the output
+ */
+ protected abstract Out transform(In in);
+ }
+
+ /**
+ * A transformer that specialize the behavior upon failures.
+ */
+ private abstract static class Unwrapper<Out, In> extends Transformer<Out,
In>
+ {
+ /**
+ * @throws NullPointerException if the in argument is null
+ * @throws IllegalArgumentException if the converted in argument is null
+ */
+ protected final Out transform(In in) throws NullPointerException,
IllegalArgumentException
+ {
+ if (in == null)
+ {
+ throw new NullPointerException("No null input accepted");
+ }
+
+ //
+ Out out = convert(in);
+
+ //
+ if (out == null)
+ {
+ throw new IllegalArgumentException("The provided input " + out +
" was converted to a null output");
+ }
+
+ //
+ return out;
+ }
+
+ protected abstract Out convert(In in);
+
+ }
+
+ /**
+ * An transformer that specialize the behavior upon failures.
+ */
+ private abstract static class Wrapper<Out, In> extends Transformer<Out,
In>
+ {
+ /**
+ * @throws IllegalStateException if the in argument is null or its conversion is
null
+ * @throws IllegalArgumentException if the converted in argument is null
+ */
+ protected final Out transform(In in)
+ {
+ if (in == null)
+ {
+ throw new IllegalStateException("No null input accepted");
+ }
+
+ //
+ Out out = convert(in);
+
+ //
+ if (out == null)
+ {
+ throw new IllegalStateException("The provided input " + out +
" was converted to a null output");
+ }
+
+ //
+ return out;
+ }
+
+ protected abstract Out convert(In in);
+ }
+
+ /**
+ * Defines a converter that converts an internal value to an external value and vice
versa.
+ * Null values will not passed as arguments to the methods and if a conversion method
returns
+ * a null value it will be considered as a conversion failure although the
implementations should
+ * not rely and this behavior and rather rely on a adapted exception.
+ */
+ public abstract static class Converter<E, I>
+ {
+
+ private final Unwrapper<I, E> unwrapper = new Unwrapper<I, E>()
+ {
+ protected I convert(E e)
+ {
+ return getInternal(e);
+ }
+ };
+
+ private final Wrapper<E, I> wrapper = new Wrapper<E, I>()
+ {
+ protected E convert(I i)
+ {
+ return getExternal(i);
+ }
+ };
+
+ /**
+ * Converts the external value to the its internal representation that will be
stored in the map.
+ *
+ * @param external the external value
+ * @return the the internal value
+ * @throws ClassCastException if the class of the specified argument prevents it
from being converter
+ * @throws IllegalArgumentException if some aspect of this argument prevents it
from being converted
+ */
+ protected abstract I getInternal(E external) throws IllegalArgumentException,
ClassCastException;
+
+ /**
+ * Converts the internal value into its external representation.
+ *
+ * @param internal the internal value
+ * @return the external value
+ * @throws ClassCastException if the class of the specified argument prevents it
from being converter
+ * @throws IllegalArgumentException if some aspect of this argument prevents it
from being converted
+ */
+ protected abstract E getExternal(I internal) throws IllegalArgumentException,
ClassCastException;
+
+ /**
+ * Compare internal values, the passed argument are never null so the method does
not need to check
+ * nullity of the arguments.
+ *
+ * @param left the left value
+ * @param right the right value
+ * @return true if the values are considered equals
+ */
+ protected abstract boolean equals(I left, I right);
+ }
+
+ private static <I> boolean safeEquals(I left, I right, Converter<?, I>
converter)
+ {
+ // Check the internal value, it should not be null
+ return !(left == null || right == null) && converter.equals(left, right);
+ }
+
+ public abstract Converter<EK, IK> getKeyConverter();
+
+ public abstract Converter<EV, IV> getValueConverter();
+
+ protected abstract Map<IK, IV> getDelegate();
+
+ public final int size()
+ {
+ return getDelegate().size();
+ }
+
+ public final void clear()
+ {
+ getDelegate().clear();
+ }
+
+ public final boolean isEmpty()
+ {
+ return getDelegate().isEmpty();
+ }
+
+ public final boolean containsKey(Object key)
+ {
+ EK ek = (EK)key;
+ IK ik = getKeyConverter().unwrapper.transform(ek);
+ return getDelegate().containsKey(ik);
+ }
+
+ public final Set<EK> keySet()
+ {
+ return new KeySet();
+ }
+
+ public final EV put(EK ek, EV ev)
+ {
+ IK ik = getKeyConverter().unwrapper.transform(ek);
+ IV iv = getValueConverter().unwrapper.transform(ev);
+ Map<IK, IV> map = getDelegate();
+
+ //
+ iv = map.put(ik, iv);
+
+ // Do we have a result ?
+ if (iv == null)
+ {
+ ev = null;
+ }
+ else
+ {
+ boolean rollback = true;
+ try
+ {
+ ev = getValueConverter().wrapper.transform(iv);
+ rollback = false;
+ }
+ finally
+ {
+ if (rollback)
+ {
+ map.put(ik, iv);
+ }
+ }
+ }
+
+ //
+ return ev;
+ }
+
+ public final EV get(Object key)
+ {
+ EK ek = (EK)key;
+ IK ik = getKeyConverter().unwrapper.transform(ek);
+ IV iv = getDelegate().get(ik);
+ EV ev = null;
+ if (iv != null)
+ {
+ ev = getValueConverter().wrapper.transform(iv);
+ }
+ return ev;
+ }
+
+ public final EV remove(Object key)
+ {
+ EK ek = (EK)key;
+ IK ik = getKeyConverter().unwrapper.transform(ek);
+ Map<IK, IV> map = getDelegate();
+ IV iv = map.remove(ik);
+ EV ev = null;
+ if (iv != null)
+ {
+ boolean rollback = true;
+ try
+ {
+ ev = getValueConverter().wrapper.transform(iv);
+ rollback = false;
+ }
+ finally
+ {
+ if (rollback)
+ {
+ map.put(ik, iv);
+ }
+ }
+ }
+ return ev;
+ }
+
+ public final boolean containsValue(Object value)
+ {
+ EV ev = (EV)value;
+ IV iv = getValueConverter().unwrapper.transform(ev);
+ return getDelegate().containsValue(iv);
+ }
+
+ public final Set<Entry<EK, EV>> entrySet()
+ {
+ return new TypedEntrySet();
+ }
+
+ public final void putAll(Map<? extends EK, ? extends EV> em)
+ {
+ Map<IK, IV> im = convert(em);
+ getDelegate().putAll(im);
+ }
+
+ public final Collection<EV> values()
+ {
+ return new ValueCollection();
+ }
+
+ /** Compare to parameters objects. */
+ public final boolean equals(Object o)
+ {
+ if (o == this)
+ {
+ return true;
+ }
+ if (o instanceof Map)
+ {
+ Map<EK, EV> that = (Map<EK,EV>)o;
+ Map<IK, IV> delegate = getDelegate();
+
+ // Must have same sizes
+ if (that.size() != delegate.size())
+ {
+ return false;
+ }
+
+ //
+ for (Entry<EK, EV> thatEntry : that.entrySet())
+ {
+
+ EK thatKey = thatEntry.getKey();
+ EV thatValue = thatEntry.getValue();
+
+ //
+ try
+ {
+ // Unwrap key, mostly for checking its type is correct
+ IK ik = getKeyConverter().unwrapper.transform(thatKey);
+
+ // Unwrap value
+ IV iv = getValueConverter().unwrapper.transform(thatValue);
+
+ // Get the internal value
+ IV internalValue = delegate.get(ik);
+
+ // Perform value comparison
+ if (!safeEquals(internalValue, iv, getValueConverter()))
+ {
+ return false;
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ return false;
+ }
+ catch (ClassCastException e)
+ {
+ return false;
+ }
+ catch (NullPointerException e)
+ {
+ return false;
+ }
+ }
+
+ //
+ return true;
+ }
+
+ //
+ return false;
+ }
+
+ public String toString()
+ {
+ return getDelegate().toString();
+ }
+
+ /**
+ * Validates and unwraps the map.
+ */
+ protected final Map<IK, IV> convert(Map<? extends EK, ? extends EV> t)
throws IllegalArgumentException, NullPointerException, ClassCastException
+ {
+ if (t == null)
+ {
+ throw new NullPointerException("No null map can be accepted");
+ }
+ Map<IK, IV> u = new HashMap<IK, IV>(t.size());
+ for (Entry<? extends EK, ? extends EV> entry : t.entrySet())
+ {
+ IK ik = getKeyConverter().unwrapper.transform(entry.getKey());
+ IV iv = getValueConverter().unwrapper.transform(entry.getValue());
+ u.put(ik, iv);
+ }
+ return u;
+ }
+
+ /**
+ * Replace the content with the new map which is validated before replacement.
+ */
+ public final void replace(Map<EK, EV> map) throws ClassCastException,
NullPointerException, IllegalArgumentException
+ {
+ Map<IK, IV> tmp = convert(map);
+
+ //
+ Map<IK, IV> delegate = getDelegate();
+ delegate.clear();
+ delegate.putAll(tmp);
+ }
+
+ /**
+ * Validate the content.
+ */
+ public final void validate() throws ClassCastException, NullPointerException,
IllegalArgumentException
+ {
+ for (Entry<IK, IV> entry : getDelegate().entrySet())
+ {
+ getKeyConverter().wrapper.transform(entry.getKey());
+ getValueConverter().wrapper.transform(entry.getValue());
+ }
+ }
+
+ private static class TypedCollection<E,I> extends AbstractCollection<E>
+ {
+
+ /** . */
+ private final Collection<I> delegate;
+
+ /** . */
+ private final Unwrapper<I, E> unwrapper;
+
+ /** . */
+ private final Wrapper<E, I> wrapper;
+
+ private TypedCollection(Collection<I> delegate, Unwrapper<I, E>
unwrapper, Wrapper<E, I> wrapper)
+ {
+ this.delegate = delegate;
+ this.unwrapper = unwrapper;
+ this.wrapper = wrapper;
+ }
+
+ public int size()
+ {
+ return delegate.size();
+ }
+
+ public void clear()
+ {
+ delegate.clear();
+ }
+
+ public boolean isEmpty()
+ {
+ return delegate.isEmpty();
+ }
+
+ public boolean contains(Object o)
+ {
+ E e = (E)o;
+ I i = unwrapper.transform(e);
+ return delegate.contains(i);
+ }
+
+ public boolean addAll(Collection<? extends E> ek)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean remove(Object o)
+ {
+ E e = (E)o;
+ I i = unwrapper.transform(e);
+ return delegate.remove(i);
+ }
+
+ public boolean removeAll(Collection<?> c)
+ {
+ ArrayList<I> tmp = new ArrayList<I>(c.size());
+
+ //
+ for (Object o : c)
+ {
+ E e = (E)o;
+ I i = unwrapper.transform(e);
+ tmp.add(i);
+ }
+
+ //
+ return delegate.removeAll(tmp);
+ }
+
+ public boolean retainAll(Collection<?> c)
+ {
+ ArrayList<I> tmp = new ArrayList<I>(c.size());
+
+ //
+ for (Object o : c)
+ {
+ E e = (E)o;
+ I i = unwrapper.transform(e);
+ tmp.add(i);
+ }
+
+ //
+ return delegate.retainAll(tmp);
+ }
+
+ public Iterator<E> iterator()
+ {
+ return new TypedIterator();
+ }
+
+ private final class TypedIterator implements Iterator<E>
+ {
+
+ /** . */
+ private final Iterator<I> delegate;
+
+ public TypedIterator()
+ {
+ this.delegate = TypedCollection.this.delegate.iterator();
+ }
+
+ public void remove()
+ {
+ delegate.remove();
+ }
+
+ public boolean hasNext()
+ {
+ return delegate.hasNext();
+ }
+
+ public E next()
+ {
+ I i = delegate.next();
+ return wrapper.transform(i);
+ }
+ }
+ }
+
+ private static abstract class TypedEntry<EK, EV, IK, IV> implements Entry<EK,
EV>
+ {
+
+ private final Entry<IK, IV> delegate;
+
+ protected TypedEntry(Entry<IK, IV> delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ protected abstract Transformer<EK, IK> getKeyWrapper();
+
+ protected abstract Transformer<EV, IV> getValueWrapper();
+
+ protected abstract Transformer<IV, EV> getValueUnwrapper();
+
+ public EK getKey()
+ {
+ return getKeyWrapper().transform(delegate.getKey());
+ }
+
+ public EV getValue()
+ {
+ return getValueWrapper().transform(delegate.getValue());
+ }
+
+ public EV setValue(EV value)
+ {
+ IV iv = getValueUnwrapper().transform(value);
+ IV oldIV = delegate.setValue(iv);
+ boolean rollback = true;
+ try
+ {
+ EV ev = getValueWrapper().transform(oldIV);
+ rollback = false;
+ return ev;
+ }
+ finally
+ {
+ if (rollback)
+ {
+ delegate.setValue(oldIV);
+ }
+ }
+ }
+
+ public int hashCode()
+ {
+ return delegate.getKey().hashCode();
+ }
+ }
+
+ private class ExternalEntry extends TypedEntry<EK, EV, IK, IV>
+ {
+ private ExternalEntry(Entry<IK, IV> delegate)
+ {
+ super(delegate);
+ }
+
+ protected Transformer<EK, IK> getKeyWrapper()
+ {
+ return getKeyConverter().wrapper;
+ }
+
+ protected Transformer<EV, IV> getValueWrapper()
+ {
+ return getValueConverter().wrapper;
+ }
+
+ protected Transformer<IV, EV> getValueUnwrapper()
+ {
+ return getValueConverter().unwrapper;
+ }
+ }
+
+ private class InternalEntry extends TypedEntry<IK, IV, EK, EV>
+ {
+ private InternalEntry(Entry<EK, EV> delegate)
+ {
+ super(delegate);
+ }
+
+ protected Transformer<IK, EK> getKeyWrapper()
+ {
+ return getKeyConverter().unwrapper;
+ }
+
+ protected Transformer<IV, EV> getValueWrapper()
+ {
+ return getValueConverter().unwrapper;
+ }
+
+ protected Transformer<EV, IV> getValueUnwrapper()
+ {
+ return getValueConverter().wrapper;
+ }
+ }
+
+ private class EntryWrapper extends Wrapper<Map.Entry<EK, EV>,
Map.Entry<IK, IV>>
+ {
+ protected Entry<EK, EV> convert(Entry<IK, IV> externalEntry)
+ {
+ return new ExternalEntry(externalEntry);
+ }
+ }
+
+ private class EntryUnwrapper extends Unwrapper<Map.Entry<IK, IV>,
Map.Entry<EK, EV>>
+ {
+ protected Entry<IK, IV> convert(Entry<EK, EV> internalEntry)
+ {
+ return new InternalEntry(internalEntry);
+ }
+ }
+
+ private final class KeySet extends TypedCollection<EK,IK> implements
Set<EK>
+ {
+ public KeySet()
+ {
+ super(getDelegate().keySet(), getKeyConverter().unwrapper,
getKeyConverter().wrapper);
+ }
+ }
+
+ private final class ValueCollection extends TypedCollection<EV,IV>
+ {
+ public ValueCollection()
+ {
+ super(getDelegate().values(), getValueConverter().unwrapper,
getValueConverter().wrapper);
+ }
+ }
+
+ private class TypedEntrySet extends TypedCollection<Map.Entry<EK, EV>,
Map.Entry<IK, IV>> implements Set<Map.Entry<EK, EV>>
+ {
+ private TypedEntrySet()
+ {
+ super(getDelegate().entrySet(), new EntryUnwrapper(), new EntryWrapper());
+ }
+
+ public boolean remove(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean removeAll(Collection<?> c)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean retainAll(Collection<?> c)
+ {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
Property changes on:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
___________________________________________________________________
Name: svn:executable
+
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/LazyMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/LazyMap.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/LazyMap.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -31,92 +31,92 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 5451 $
*/
-public abstract class LazyMap implements Map
+public class LazyMap<K, V> implements Map<K, V>
{
/** . */
- private Map delegate;
+ private MapAccessor<K, V> delegate;
/** . */
private boolean modified;
/**
- * Create the delegate. There are no guarantees that this method will be called only
once.
+ * @param delegate the delegate
+ * @throws IllegalArgumentException if the argument is null
*/
- protected abstract Map createDelegate();
+ public LazyMap(MapAccessor<K, V> delegate) throws IllegalArgumentException
+ {
+ if (delegate == null)
+ {
+ throw new IllegalArgumentException("No null delegate can be
accepted");
+ }
+ this.delegate = delegate;
+ }
public boolean isModified()
{
return modified;
}
- private Map getDelegate()
+ private Map<K, V> getDelegate(boolean wantWrite)
{
- if (delegate == null)
- {
- delegate = createDelegate();
- }
- if (delegate == null)
- {
- throw new IllegalStateException("No delegate obtained");
- }
- return delegate;
+ return delegate.getMap(wantWrite);
}
public int size()
{
- return getDelegate().size();
+ return getDelegate(false).size();
}
public boolean isEmpty()
{
- return getDelegate().isEmpty();
+ return getDelegate(false).isEmpty();
}
public boolean containsKey(Object key)
{
- return getDelegate().containsKey(key);
+ return getDelegate(false).containsKey(key);
}
public boolean containsValue(Object value)
{
- return getDelegate().containsValue(value);
+ return getDelegate(false).containsValue(value);
}
- public Object get(Object key)
+ public V get(Object key)
{
- return getDelegate().get(key);
+ return getDelegate(false).get(key);
}
- public Object put(Object key, Object value)
+ public V put(K key, V value)
{
modified = true;
- return getDelegate().put(key, value);
+ return getDelegate(true).put(key, value);
}
- public Object remove(Object key)
+ public V remove(Object key)
{
modified = true;
- return getDelegate().remove(key);
+ return getDelegate(true).remove(key);
}
- public void putAll(Map t)
+ public void putAll(Map<? extends K, ? extends V> t)
{
modified = true;
- getDelegate().putAll(t);
+ getDelegate(true).putAll(t);
}
public void clear()
{
modified = true;
- getDelegate().clear();
+ getDelegate(true).clear();
}
- public Set keySet()
+ public Set<K> keySet()
{
- return new Set()
+ return new Set<K>()
{
- Set keySet = getDelegate().keySet();
+ Set<K> keySet = getDelegate(true).keySet();
public int size()
{
@@ -133,7 +133,7 @@
return keySet.contains(o);
}
- public Iterator iterator()
+ public Iterator<K> iterator()
{
return keySet.iterator();
}
@@ -143,12 +143,12 @@
return keySet.toArray();
}
- public Object[] toArray(Object[] a)
+ public <T> T[] toArray(T[] a)
{
return keySet.toArray(a);
}
- public boolean add(Object o)
+ public boolean add(K o)
{
modified = true;
return keySet.add(o);
@@ -160,23 +160,23 @@
return keySet.remove(o);
}
- public boolean containsAll(Collection c)
+ public boolean containsAll(Collection<?> c)
{
return keySet.containsAll(c);
}
- public boolean addAll(Collection c)
+ public boolean addAll(Collection<? extends K> c)
{
modified = true;
return keySet.addAll(c);
}
- public boolean retainAll(Collection c)
+ public boolean retainAll(Collection<?> c)
{
return keySet.retainAll(c);
}
- public boolean removeAll(Collection c)
+ public boolean removeAll(Collection<?> c)
{
modified = true;
return keySet.removeAll(c);
@@ -200,12 +200,12 @@
};
}
- public Collection values()
+ public Collection<V> values()
{
- return new Collection()
+ return new Collection<V>()
{
/** . */
- Collection values = getDelegate().values();
+ Collection<V> values = getDelegate(true).values();
public int size()
{
@@ -222,7 +222,7 @@
return values.contains(o);
}
- public Iterator iterator()
+ public Iterator<V> iterator()
{
return values.iterator();
}
@@ -232,12 +232,12 @@
return values.toArray();
}
- public Object[] toArray(Object[] a)
+ public <T> T[] toArray(T[] a)
{
return values.toArray(a);
}
- public boolean add(Object o)
+ public boolean add(V o)
{
modified = true;
return values.add(o);
@@ -249,24 +249,24 @@
return values.remove(o);
}
- public boolean containsAll(Collection c)
+ public boolean containsAll(Collection<?> c)
{
return values.containsAll(c);
}
- public boolean addAll(Collection c)
+ public boolean addAll(Collection<? extends V> c)
{
modified = true;
return values.addAll(c);
}
- public boolean removeAll(Collection c)
+ public boolean removeAll(Collection<?> c)
{
modified = true;
return values.removeAll(c);
}
- public boolean retainAll(Collection c)
+ public boolean retainAll(Collection<?> c)
{
return values.retainAll(c);
}
@@ -289,12 +289,12 @@
};
}
- public Set entrySet()
+ public Set<Map.Entry<K, V>> entrySet()
{
- return new Set()
+ return new Set<Map.Entry<K, V>>()
{
/** . */
- Set entrySet = getDelegate().entrySet();
+ Set<Map.Entry<K, V>> entrySet = getDelegate(true).entrySet();
public int size()
{
@@ -311,7 +311,7 @@
return entrySet.contains(o);
}
- public Iterator iterator()
+ public Iterator<Map.Entry<K, V>> iterator()
{
return entrySet.iterator();
}
@@ -321,12 +321,12 @@
return entrySet.toArray();
}
- public Object[] toArray(Object[] a)
+ public <T> T[] toArray(T[] a)
{
return entrySet.toArray(a);
}
- public boolean add(Object o)
+ public boolean add(Entry<K, V> o)
{
modified = true;
return entrySet.add(o);
@@ -338,24 +338,24 @@
return entrySet.remove(o);
}
- public boolean containsAll(Collection c)
+ public boolean containsAll(Collection<?> c)
{
return entrySet.containsAll(c);
}
- public boolean addAll(Collection c)
+ public boolean addAll(Collection<? extends Entry<K, V>> c)
{
modified = true;
return entrySet.addAll(c);
}
- public boolean retainAll(Collection c)
+ public boolean retainAll(Collection<?> c)
{
modified = true;
return entrySet.retainAll(c);
}
- public boolean removeAll(Collection c)
+ public boolean removeAll(Collection<?> c)
{
modified = true;
return entrySet.removeAll(c);
@@ -381,11 +381,11 @@
public boolean equals(Object o)
{
- return getDelegate().equals(o);
+ return getDelegate(false).equals(o);
}
public int hashCode()
{
- return getDelegate().hashCode();
+ return getDelegate(false).hashCode();
}
}
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.io.Serializable;
+import java.io.IOException;
/**
* A decorator that enforce the map content to be <String,String[]>. It also
provides capabilities for
@@ -35,10 +36,10 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 6671 $
*/
-public class ParameterMap extends TypedMap<String, String[], String, String[]>
implements Serializable
+public final class ParameterMap extends AbstractTypedMap<String, String[], String,
String[]> implements Serializable
{
- /** . */
+ /** Just a string to string converter. */
private static final KeyConverter keyConv = new KeyConverter();
/**
@@ -54,19 +55,31 @@
*/
public static ParameterMap clone(Map<String, String[]> map) throws
NullPointerException, ClassCastException, IllegalArgumentException
{
+ return clone(map, AccessMode.A);
+ }
+
+ /**
+ * Copy the parameter map.
+ *
+ * @param map the parameter map to copy
+ * @param accessMode the access mode
+ * @return a parameter map initialized from the argument map
+ * @throws NullPointerException if the map contains a null key or a null value
+ * @throws IllegalArgumentException if the map is null or it contains a value with a
zero length array or a null
+ * element in the array
+ * @throws ClassCastException if the map contains a key that is not a string or
a value that is not a string
+ * array
+ */
+ public static ParameterMap clone(Map<String, String[]> map, AccessMode
accessMode) throws NullPointerException, ClassCastException, IllegalArgumentException
+ {
if (map == null)
{
throw new IllegalArgumentException("No null map accepted");
}
//
- ParameterMap pm = new ParameterMap();
-
- //
- pm.replace(map);
-
- //
- return pm;
+ HashMap<String, String[]> delegate = new HashMap<String,
String[]>(map);
+ return new ParameterMap(delegate, accessMode);
}
/**
@@ -78,48 +91,38 @@
*/
public static ParameterMap wrap(Map<String, String[]> map)
{
- if (map instanceof ParameterMap)
- {
- return (ParameterMap)map;
- }
- else
- {
- return new ParameterMap(map);
- }
+ return wrap(map, AccessMode.A);
}
/**
* Safely wrap the map as a portlet parameters object. If the map is already a
parameter map object, just return
- * that object otherwise return a wrapper around the map.
+ * that object with the new access mode otherwise return a wrapper around the map.
*
* @param map the map
+ * @param accessMode the access mode
* @return the portlet parameters
*/
- public static ParameterMap wrap(Map<String, String[]> map, AccessMode
accessPolicy)
+ public static ParameterMap wrap(Map<String, String[]> map, AccessMode
accessMode)
{
if (map instanceof ParameterMap)
{
- return new ParameterMap((ParameterMap)map, accessPolicy);
+ return new ParameterMap((ParameterMap)map, accessMode);
}
else
{
- return new ParameterMap(map, accessPolicy);
+ return new ParameterMap(map, accessMode);
}
}
/** . */
- private final AccessMode accessMode;
+ private AccessMode accessMode;
- /**
- * Reuse the same underlying map than the provided parameter map but it allows to have
an alternative configuration
- * of the access. This is not a copy constructor.
- */
- public ParameterMap(ParameterMap map, AccessMode accessMode)
+ /** . */
+ private Map<String, String[]> delegate;
+
+ public ParameterMap()
{
- super(map);
-
- //
- this.accessMode = accessMode;
+ this(AccessMode.A);
}
public ParameterMap(AccessMode accessMode)
@@ -127,34 +130,46 @@
this(new HashMap<String, String[]>(), accessMode);
}
- public ParameterMap(MapAccessor<String, String[]> accessor)
- {
- this(accessor, AccessMode.A);
- }
-
public ParameterMap(Map<String, String[]> delegate)
{
this(delegate, AccessMode.A);
}
- public ParameterMap()
+ public ParameterMap(Map<String, String[]> delegate, AccessMode accessMode)
{
- this(AccessMode.A);
- }
+ if (delegate == null)
+ {
+ throw new IllegalArgumentException("No null delegate accepted");
+ }
+ if (delegate == null)
+ {
+ throw new IllegalArgumentException("No null access mode accepted");
+ }
- public ParameterMap(MapAccessor<String, String[]> accessor, AccessMode
accessMode)
- {
- super(accessor, keyConv, accessMode.converter);
-
//
+ this.delegate = delegate;
this.accessMode = accessMode;
}
- public ParameterMap(Map<String, String[]> delegate, AccessMode accessMode)
+ /**
+ * Exposed through the <code>as(AccessMode accessMode)</code> method.
+ *
+ * @param that the map to rewrap
+ * @param accessMode the new access mode
+ */
+ private ParameterMap(ParameterMap that, AccessMode accessMode)
{
- super(delegate, keyConv, accessMode.converter);
+ if (that == null)
+ {
+ throw new IllegalArgumentException("No null parameter map accepted");
+ }
+ if (that == null)
+ {
+ throw new IllegalArgumentException("No null access mode accepted");
+ }
//
+ this.delegate = that.delegate;
this.accessMode = accessMode;
}
@@ -163,16 +178,44 @@
return accessMode;
}
+ public ParameterMap as(AccessMode accessMode)
+ {
+ return new ParameterMap(this, accessMode);
+ }
+
+ public Converter<String, String> getKeyConverter()
+ {
+ return keyConv;
+ }
+
+ public Converter<String[], String[]> getValueConverter()
+ {
+ return accessMode.converter;
+ }
+
+ protected Map<String, String[]> getDelegate()
+ {
+ return delegate;
+ }
+
/**
* Return the parameter value or null if it does not exist.
*
* @param name the parameter name
* @return the parameter value or null if it does not exist
- * @throws NullPointerException if the name is null
+ * @throws IllegalArgumentException if the name is null
*/
public String getValue(String name) throws IllegalArgumentException
{
- String[] value = get(name);
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ // Access delegate directly to avoid copy on read when it is enabled
+ String[] value = delegate.get(name);
+
+ //
return value == null ? null : value[0];
}
@@ -181,7 +224,7 @@
*
* @param name the value to get
* @return the parameter values
- * @throws NullPointerException if the name is null
+ * @throws IllegalArgumentException if the name is null
*/
public String[] getValues(String name) throws IllegalArgumentException
{
@@ -189,6 +232,8 @@
{
throw new IllegalArgumentException("No null name");
}
+
+ //
return get(name);
}
@@ -197,11 +242,21 @@
*
* @param name the parameter name
* @param value the parameter value
- * @throws NullPointerException if the name or the value is null
+ * @throws IllegalArgumentException if the name or the value is null
*/
- public void setValue(String name, String value)
+ public void setValue(String name, String value) throws IllegalArgumentException
{
- put(name, new String[]{value});
+ if (name == null)
+ {
+ throw new IllegalArgumentException("No null name");
+ }
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null name");
+ }
+
+ // Access delegate directly to avoid copy on read write it is enabled
+ delegate.put(name, new String[]{value});
}
/**
@@ -210,10 +265,20 @@
* @param name the parameter name
* @param values the parameter values
* @throws NullPointerException if the name or the value is null
- * @throws IllegalArgumentException if the values length is zero or contains a null
element
+ * @throws IllegalArgumentException if the name is null or the values is null or the
values length is zero or contains a null element
*/
- public void setValues(String name, String[] values) throws NullPointerException,
IllegalArgumentException
+ public void setValues(String name, String[] values) throws IllegalArgumentException
{
+ if (name == null)
+ {
+ throw new IllegalArgumentException("No null name");
+ }
+ if (values == null)
+ {
+ throw new IllegalArgumentException("No null name");
+ }
+
+ //
put(name, values);
}
@@ -231,23 +296,32 @@
*/
public void append(Map<String, String[]> params) throws ClassCastException,
NullPointerException, IllegalArgumentException
{
- // Clone
+ // Clone to have an atomic operation
params = new HashMap<String, String[]>(params);
//
for (Map.Entry<String, String[]> entry : params.entrySet())
{
- String[] existingValue = get(entry.getKey());
+ String[] appendedValue = entry.getValue();
- // Perform the appending operation if the entry exist
+ //
+ String[] existingValue = delegate.get(entry.getKey());
if (existingValue != null)
{
- String[] appendedValue = entry.getValue();
+ // Perform the concatenation operation if the entry exist
String[] newValue = new String[existingValue.length + appendedValue.length];
System.arraycopy(existingValue, 0, newValue, 0, existingValue.length);
System.arraycopy(appendedValue, 0, newValue, existingValue.length,
appendedValue.length);
- entry.setValue(newValue);
+ appendedValue = newValue;
}
+ else
+ {
+ // Clone the new value otherwise we would modify an the passed map
+ appendedValue = appendedValue.clone();
+ }
+
+ //
+ entry.setValue(appendedValue);
}
//
@@ -276,6 +350,23 @@
return buffer.toString();
}
+ private void writeObject(java.io.ObjectOutputStream out) throws IOException
+ {
+ out.writeBoolean(accessMode.copyValueOnRead);
+ out.writeBoolean(accessMode.copyValueOnWrite);
+ out.writeObject(delegate);
+ }
+
+ private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException
+ {
+ boolean copyValueOnRead = in.readBoolean();
+ boolean copyValueOnWrite = in.readBoolean();
+
+ //
+ accessMode = AccessMode.get(copyValueOnRead, copyValueOnWrite);
+ delegate = (Map<String, String[]>)in.readObject();
+ }
+
private static class KeyConverter extends Converter<String, String>
{
protected String getInternal(String external) throws IllegalArgumentException,
ClassCastException
@@ -293,11 +384,18 @@
}
/**
- * Defines how the state of the values are managed.
+ * Defines how the state of the values of a parameter map are managed.
*/
- public static class AccessMode implements Serializable
+ public static class AccessMode
{
+ /**
+ * Factory method for an access mode.
+ *
+ * @param copyValueOnRead if true the value will be copied on a read
+ * @param copyValueOnWrite if true the value will be copied on a write
+ * @return the convenient access mode
+ */
public static AccessMode get(boolean copyValueOnRead, boolean copyValueOnWrite)
{
return copyValueOnRead ? copyValueOnWrite ? D : C : copyValueOnWrite ? B : A;
@@ -342,7 +440,7 @@
}
}
- private static class ValueConverter extends Converter<String[], String[]>
implements Serializable
+ private static class ValueConverter extends Converter<String[], String[]>
{
/** . */
@@ -405,3 +503,5 @@
}
}
}
+
+
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -926,4 +926,33 @@
}
return false;
}
+
+ /**
+ * Attempt to cast the value argument to the provided type argument. If the value
argument type is assignable
+ * to the provided type, the value is returned, otherwise if it is not or the value is
null, null is returned.
+ *
+ * todo: Move that to common package.
+ *
+ * @param value the value to cast
+ * @param type the type to downcast
+ * @return the casted value or null
+ */
+ public static <T> T safeCast(Object value, Class<T> type)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+ else
+ {
+ if (type.isAssignableFrom(value.getClass()))
+ {
+ return type.cast(value);
+ }
+ else
+ {
+ return null;
+ }
+ }
+ }
}
\ No newline at end of file
Deleted:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -1,809 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This 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 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software 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 *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.common.util;
-
-import org.jboss.portal.common.NotYetImplemented;
-
-import java.util.Map;
-import java.util.Set;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.HashMap;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class TypedMap<EK, EV, IK, IV> implements Map<EK, EV>
-{
-
- public abstract static class Converter<E, I>
- {
-
- /**
- * Unwraps the key to the the internal key that will be stored in the map. This
method calls the
- * <code>assertKeyValidity(Object key)</code> and returns the same key.
It can be overriden to provide a customized
- * key that will be used instead of the external key.
- *
- * @param external the key to unwrap
- * @return the unwrapped key
- * @throws ClassCastException if the class of the specified key prevents it
from being stored in this map
- * @throws IllegalArgumentException if some aspect of this key prevents it from
being stored in this map
- */
- protected abstract I getInternal(E external) throws IllegalArgumentException,
ClassCastException;
-
- /**
- * Wrap the internal key into its external representation, by default returns the
same key. It can be overriden to
- * provide a customized key that will be used instead of the internal key.
- */
- protected abstract E getExternal(I internal);
-
- public I unwrap(E external) throws IllegalArgumentException, ClassCastException,
NullPointerException
- {
- if (external == null)
- {
- throw new NullPointerException("No null key accepted");
- }
-
- //
- I internal = getInternal(external);
-
- //
- if (internal == null)
- {
- throw new IllegalArgumentException("The provided key " + external +
" was converted to a null key");
- }
-
- //
- return internal;
- }
-
- public E wrap(I internal) throws IllegalStateException
- {
- if (internal == null)
- {
- throw new IllegalStateException("Got an internal null key");
- }
-
- //
- E external = getExternal(internal);
-
- //
- if (external == null)
- {
- throw new IllegalStateException("Converted an internal key to a null key
" + internal);
- }
-
- //
- return external;
- }
-
- public boolean safeEquals(I left, I right)
- {
- // Check the internal value, it should not be null
- return !(left == null || right == null) && equals(left, right);
- }
-
- /**
- * Compare internal values, the passed argument are never null.
- *
- * @param left the left value
- * @param right the right value
- * @return true if the values are equals
- */
- protected abstract boolean equals(I left, I right);
- }
-
- /** The map accessor. */
- private final MapAccessor<IK, IV> accessor;
-
- /** The key converter. */
- private final Converter<EK, IK> keyConverter;
-
- /** The value converter. */
- private final Converter<EV, IV> valueConverter;
-
- /**
- * Constructor that will reuse the same attributes than the provided map. This is not
a copy constructor.
- *
- * @param that the other map
- */
- public TypedMap(TypedMap<EK, EV, IK, IV> that)
- {
- this.accessor = that.accessor;
- this.keyConverter = that.keyConverter;
- this.valueConverter = that.valueConverter;
- }
-
- public TypedMap(MapAccessor<IK, IV> accessor, Converter<EK, IK>
keyConverter, Converter<EV, IV> valueConverter)
- {
- if (accessor == null)
- {
- throw new IllegalArgumentException();
- }
- if (keyConverter == null)
- {
- throw new IllegalArgumentException();
- }
- if (valueConverter == null)
- {
- throw new IllegalArgumentException();
- }
-
-
- this.accessor = accessor;
- this.keyConverter = keyConverter;
- this.valueConverter = valueConverter;
- }
-
- public TypedMap(Map<IK, IV> delegate, Converter<EK, IK> keyConv,
Converter<EV, IV> valueConv)
- {
- this(new SimpleMapAccessor<IK, IV>(delegate), keyConv, valueConv);
- }
-
- public Converter<EK, IK> getKeyConverter()
- {
- return keyConverter;
- }
-
- public Converter<EV, IV> getValueConverter()
- {
- return valueConverter;
- }
-
- public final int size()
- {
- return accessor.getMap(false).size();
- }
-
- public final void clear()
- {
- if (!isEmpty())
- {
- accessor.getMap(true).clear();
- }
- }
-
- public final boolean isEmpty()
- {
- return accessor.getMap(false).isEmpty();
- }
-
- public final boolean containsKey(Object key)
- {
- EK ek = (EK)key;
- IK ik = keyConverter.unwrap(ek);
- return accessor.getMap(false).containsKey(ik);
- }
-
- public final Set<EK> keySet()
- {
- return new KeySet();
- }
-
- public EV put(EK ek, EV ev)
- {
- IK ik = keyConverter.unwrap(ek);
- IV iv = valueConverter.unwrap(ev);
- iv = accessor.getMap(true).put(ik, iv);
- if (iv == null)
- {
- ev = null;
- }
- else
- {
- ev = valueConverter.wrap(iv);
- }
- return ev;
- }
-
- public final EV get(Object key)
- {
- EK ek = (EK)key;
- IK ik = keyConverter.unwrap(ek);
- IV iv = accessor.getMap(false).get(ik);
- EV ev = null;
- if (iv != null)
- {
- ev = valueConverter.wrap(iv);
- }
- return ev;
- }
-
- public final EV remove(Object key)
- {
- EK ek = (EK)key;
- IK ik = keyConverter.unwrap(ek);
- IV iv = accessor.getMap(true).remove(ik);
- EV ev = null;
- if (iv != null)
- {
- ev = valueConverter.wrap(iv);
- }
- return ev;
- }
-
- public final boolean containsValue(Object value)
- {
- EV ev = (EV)value;
- IV iv = valueConverter.unwrap(ev);
- return accessor.getMap(false).containsValue(iv);
- }
-
- public final Set<Entry<EK, EV>> entrySet()
- {
- return new TypedEntrySet();
- }
-
- public void putAll(Map<? extends EK, ? extends EV> em)
- {
- Map<IK, IV> im = convert(em);
- accessor.getMap(true).putAll(im);
- }
-
- public Collection<EV> values()
- {
- return new ValueCollection();
- }
-
- /** Compare to parameters objects. */
- public boolean equals(Object o)
- {
- if (o == this)
- {
- return true;
- }
- if (o instanceof Map)
- {
- Map<EK, EV> that = (Map<EK,EV>)o;
- Map<IK, IV> delegate = this.accessor.getMap(false);
-
- // Must have same sizes
- if (that.size() != delegate.size())
- {
- return false;
- }
-
- //
- for (Entry<EK, EV> thatEntry : that.entrySet())
- {
-
- EK thatKey = thatEntry.getKey();
- EV thatValue = thatEntry.getValue();
-
- //
- try
- {
- // Unwrap key, mostly for checking its type is correct
- keyConverter.unwrap(thatKey);
-
- // Unwrap value
- IV iv = valueConverter.unwrap(thatValue);
-
- // Get the internal value
- IV internalValue = delegate.get(thatKey);
-
- // Perform value comparison
- if (!valueConverter.safeEquals(internalValue, iv))
- {
- return false;
- }
- }
- catch (IllegalArgumentException e)
- {
- return false;
- }
- catch (ClassCastException e)
- {
- return false;
- }
- catch (NullPointerException e)
- {
- return false;
- }
- }
-
- //
- return true;
- }
-
- //
- return false;
- }
-
- public String toString()
- {
- return accessor.getMap(false).toString();
- }
-
- /**
- * Validates and unwraps the map.
- *
- * @param t
- * @return
- * @throws IllegalArgumentException
- * @throws NullPointerException
- * @throws ClassCastException
- */
- protected final Map<IK, IV> convert(Map<? extends EK, ? extends EV> t)
throws IllegalArgumentException, NullPointerException, ClassCastException
- {
- if (t == null)
- {
- throw new NullPointerException("No null map can be accepted");
- }
- Map<IK, IV> u = new HashMap<IK, IV>(t.size());
- for (Entry<? extends EK, ? extends EV> entry : t.entrySet())
- {
- IK ik = keyConverter.unwrap(entry.getKey());
- IV iv = valueConverter.unwrap(entry.getValue());
- u.put(ik, iv);
- }
- return u;
- }
-
- /**
- * Replace the content with the new map which is validated before replacement.
- *
- * @param map the replacement map
- * @throws ClassCastException
- * @throws NullPointerException
- * @throws IllegalArgumentException
- */
- public void replace(Map<EK, EV> map) throws ClassCastException,
NullPointerException, IllegalArgumentException
- {
- if (!map.isEmpty())
- {
- Map<IK, IV> tmp = convert(map);
-
- //
- Map<IK, IV> delegate = accessor.getMap(true);
- delegate.clear();
- delegate.putAll(tmp);
- }
- }
-
- /**
- * Validate the content.
- *
- * @throws ClassCastException
- * @throws NullPointerException
- * @throws IllegalArgumentException
- */
- public void validate() throws ClassCastException, NullPointerException,
IllegalArgumentException
- {
- for (Entry<IK, IV> entry : accessor.getMap(false).entrySet())
- {
- keyConverter.wrap(entry.getKey());
- valueConverter.wrap(entry.getValue());
- }
- }
-
- public class KeySet implements Set<EK>
- {
-
- /** . */
- private final Set<IK> delegate;
-
- public KeySet()
- {
- this.delegate = TypedMap.this.accessor.getMap(false).keySet();
- }
-
- public int size()
- {
- return delegate.size();
- }
-
- public void clear()
- {
- if (!isEmpty())
- {
- delegate.clear();
- }
- }
-
- public boolean isEmpty()
- {
- return delegate.isEmpty();
- }
-
- public boolean contains(Object o)
- {
- EK ek;
- try
- {
- ek = (EK)o;
- }
- catch (ClassCastException e)
- {
- return false;
- }
- try
- {
- IK ik = keyConverter.getInternal(ek);
- return TypedMap.this.accessor.getMap(false).containsKey(ik);
- }
- catch (IllegalArgumentException e)
- {
- return false;
- }
- catch (ClassCastException e)
- {
- return false;
- }
- catch (NullPointerException e)
- {
- return false;
- }
- }
-
- public Object[] toArray()
- {
- throw new NotYetImplemented("TypedEntrySet.toArray()");
- }
-
- public boolean add(EK ek)
- {
- throw new NotYetImplemented("TypedEntrySet.add(Object o)");
- }
-
- public boolean remove(Object o)
- {
- throw new NotYetImplemented("TypedEntrySet.remove(Object o)");
- }
-
- public boolean containsAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.addAll(Collection c)");
- }
-
- public boolean addAll(Collection<? extends EK> eks)
- {
- throw new NotYetImplemented("TypedEntrySet.containsAll(Collection
c)");
- }
-
- public boolean removeAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.removeAll(Collection c)");
- }
-
- public boolean retainAll(Collection<?> c)
- {
- if (c == null)
- {
- throw new NullPointerException();
- }
-
- //
- boolean changed = false;
- for (Iterator i = iterator(); i.hasNext();)
- {
- Object key = i.next();
- if (!c.contains(key))
- {
- i.remove();
- changed = true;
- }
- }
- return changed;
- }
-
- public Iterator<EK> iterator()
- {
- return new KeyIterator();
- }
-
- public <EK> EK[] toArray(EK a[])
- {
- throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
- }
-
- public class KeyIterator implements Iterator<EK>
- {
-
- /** . */
- private final Iterator<IK> delegate;
-
- public KeyIterator()
- {
- this.delegate = KeySet.this.delegate.iterator();
- }
-
- public void remove()
- {
- delegate.remove();
- }
-
- public boolean hasNext()
- {
- return delegate.hasNext();
- }
-
- public EK next()
- {
- IK ik = delegate.next();
- return keyConverter.wrap(ik);
- }
- }
- }
-
- public class ValueCollection implements Collection<EV>
- {
-
- /** . */
- private final Collection<IV> delegate;
-
- public ValueCollection()
- {
- this.delegate = TypedMap.this.accessor.getMap(false).values();
- }
-
- public int size()
- {
- return delegate.size();
- }
-
- public void clear()
- {
- delegate.clear();
- }
-
- public boolean isEmpty()
- {
- return delegate.isEmpty();
- }
-
- public Object[] toArray()
- {
- throw new NotYetImplemented("TypedEntrySet.toArray()");
- }
-
- public boolean add(EV ev)
- {
- throw new UnsupportedOperationException();
- }
-
- public boolean contains(Object o)
- {
- throw new NotYetImplemented("TypedEntrySet.contains(Object o)");
- }
-
- public boolean containsAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.containsAll(Collection
c)");
- }
-
- public <T> T[] toArray(T[] ts)
- {
- throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
- }
-
- public Iterator<EV> iterator()
- {
- return new ValueIterator();
- }
-
- public boolean remove(Object o)
- {
- throw new UnsupportedOperationException();
- }
-
- public boolean addAll(Collection<? extends EV> evs)
- {
- throw new UnsupportedOperationException();
- }
-
- public boolean removeAll(Collection<?> objects)
- {
- throw new UnsupportedOperationException();
- }
-
- public boolean retainAll(Collection<?> objects)
- {
- throw new UnsupportedOperationException();
- }
-
- public class ValueIterator implements Iterator<EV>
- {
-
- /** . */
- private final Iterator<IV> delegate;
-
- public ValueIterator()
- {
- this.delegate = ValueCollection.this.delegate.iterator();
- }
-
- public void remove()
- {
- delegate.remove();
- }
-
- public boolean hasNext()
- {
- return delegate.hasNext();
- }
-
- public EV next()
- {
- IV iv = delegate.next();
- return valueConverter.wrap(iv);
- }
- }
- }
-
- public class TypedEntrySet implements Set<Entry<EK, EV>>
- {
-
- /** . */
- private final Set<Entry<IK, IV>> delegate;
-
- public TypedEntrySet()
- {
- this.delegate = TypedMap.this.accessor.getMap(false).entrySet();
- }
-
- public int size()
- {
- return delegate.size();
- }
-
- public void clear()
- {
- if (!isEmpty())
- {
- delegate.clear();
- }
- }
-
- public boolean isEmpty()
- {
- return delegate.isEmpty();
- }
-
- public Object[] toArray()
- {
- throw new NotYetImplemented("TypedEntrySet.toArray()");
- }
-
- public boolean add(Entry<EK, EV> ekevEntry)
- {
- throw new NotYetImplemented("TypedEntrySet.add(Object o)");
- }
-
- public boolean contains(Object o)
- {
- throw new NotYetImplemented("TypedEntrySet.contains(Object o)");
- }
-
- public boolean remove(Object o)
- {
- throw new NotYetImplemented("TypedEntrySet.remove(Object o)");
- }
-
- public boolean addAll(Collection<? extends Entry<EK, EV>> entries)
- {
- throw new NotYetImplemented("TypedEntrySet.addAll(Collection c)");
- }
-
- public boolean containsAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.containsAll(Collection
c)");
- }
-
- public boolean removeAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.removeAll(Collection c)");
- }
-
- public boolean retainAll(Collection<?> objects)
- {
- throw new NotYetImplemented("TypedEntrySet.retainAll(Collection c)");
- }
-
- public <T> T[] toArray(T[] ts)
- {
- throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
- }
-
- public Iterator<Entry<EK, EV>> iterator()
- {
- return new TypedEntryIterator();
- }
-
- public class TypedEntryIterator implements Iterator<Entry<EK, EV>>
- {
-
- /** . */
- private final Iterator<Entry<IK, IV>> delegate;
-
- public TypedEntryIterator()
- {
- this.delegate = TypedEntrySet.this.delegate.iterator();
- }
-
- public void remove()
- {
- delegate.remove();
- }
-
- public boolean hasNext()
- {
- return delegate.hasNext();
- }
-
- public Entry<EK, EV> next()
- {
- Entry<IK, IV> entry = delegate.next();
- return new TypedEntry(entry);
- }
- }
-
- public class TypedEntry implements Entry<EK, EV>
- {
-
- /** . */
- private final Entry<IK, IV> delegate;
-
- public TypedEntry(Entry<IK, IV> delegate)
- {
- this.delegate = delegate;
- }
-
- public int hashCode()
- {
- return delegate.hashCode();
- }
-
- public boolean equals(Object obj)
- {
- return delegate.equals(obj);
- }
-
- public String toString()
- {
- return delegate.toString();
- }
-
- public EK getKey()
- {
- IK ik = delegate.getKey();
- return keyConverter.wrap(ik);
- }
-
- public EV getValue()
- {
- IV iv = delegate.getValue();
- return valueConverter.wrap(iv);
- }
-
- public EV setValue(Object value)
- {
- EV ev = (EV)value;
- IV iv = valueConverter.unwrap(ev);
- iv = delegate.setValue(iv);
- return valueConverter.wrap(iv);
- }
- }
- }
-}
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -26,6 +26,7 @@
import org.jboss.portal.common.util.ParameterMap;
import org.jboss.portal.common.util.Tools;
import org.jboss.portal.common.junit.ExtendedAssert;
+import org.jboss.portal.common.io.IOTools;
import java.util.HashMap;
import java.util.Set;
@@ -33,6 +34,7 @@
import java.util.Map;
import java.util.Arrays;
import java.util.List;
+import java.io.IOException;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -250,7 +252,7 @@
param.getValue(null);
fail("Expected IllegalArgumentException");
}
- catch (NullPointerException e)
+ catch (IllegalArgumentException e)
{
}
}
@@ -268,7 +270,7 @@
param.setValue(null, "b");
fail("Expected IllegalArgumentException");
}
- catch (NullPointerException e)
+ catch (IllegalArgumentException e)
{
}
}
@@ -319,7 +321,7 @@
param.setValues(null, new String[]{"a"});
fail("Expected IllegalArgumentException");
}
- catch (NullPointerException e)
+ catch (IllegalArgumentException e)
{
}
}
@@ -331,7 +333,7 @@
param.setValues("a", null);
fail("Expected IllegalArgumentException");
}
- catch (NullPointerException e)
+ catch (IllegalArgumentException e)
{
}
}
@@ -487,6 +489,27 @@
assertNull(param.getValue("a"));
}
+ private void checkSerializable(ParameterMap parameters) throws IOException
+ {
+ ParameterMap copy = IOTools.clone(parameters);
+ assertTrue(copy.equals(parameters));
+ }
+
+ public void testSerializable() throws IOException
+ {
+ ParameterMap p1 = new ParameterMap();
+ ParameterMap p2 = new ParameterMap();
+ p2.setValues("foo", new String[]{"foo1"});
+ ParameterMap p3 = new ParameterMap();
+ p3.setValues("foo", new String[]{"foo1"});
+ p3.setValues("bar", new String[]{"bar1","bar2"});
+
+ //
+ checkSerializable(p1);
+ checkSerializable(p2);
+ checkSerializable(p3);
+ }
+
public Class[] buildExceptionClasses()
{
return new Class[]
Deleted:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java 2008-01-20
18:09:36 UTC (rev 9540)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -1,462 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This 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 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software 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 *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
-package org.jboss.portal.test.common.util;
-
-import junit.framework.TestCase;
-import org.jboss.portal.common.util.CollectionBuilder;
-import org.jboss.portal.common.util.TypedMap;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class TypedMapTestCase extends TestCase
-{
-
- private StringToInteger sti;
-
- private Map<String, Integer> delegate;
-
- private StringToIntegerMap map;
-
- public TypedMapTestCase(String name)
- {
- super(name);
- }
-
- protected void setUp() throws Exception
- {
- delegate = new HashMap<String, Integer>();
- sti = new StringToInteger();
- map = new StringToIntegerMap(delegate, sti);
- }
-
- public void testGetWithBrokenGetInternalValue()
- {
- sti.internalValueReturnsNull = true;
- map.get("abc");
- delegate.put("abc", new Integer(0));
- map.get("abc");
- }
-
- public void testPutWithBrokenGetInternalValue()
- {
- sti.internalValueReturnsNull = true;
- try
- {
- map.put("abc", "0");
- fail();
- }
- catch (IllegalArgumentException e)
- {
- }
- }
-
- public void testRemoveWithBrokenGetInternalValue()
- {
- sti.internalValueReturnsNull = true;
- map.remove("abc");
- }
-
- public void testRemoveWithBrokenGetExternalValue()
- {
- sti.externalValueReturnsNull = true;
- map.remove("abc");
- delegate.put("abc", new Integer(0));
- try
- {
- map.remove("abc");
- fail();
- }
- catch (IllegalStateException e)
- {
- }
- }
-
- public void testGetWithBrokenGetExternalValue()
- {
- sti.externalValueReturnsNull = true;
- map.get("abc");
- delegate.put("abc", new Integer(0));
- try
- {
- map.get("abc");
- fail();
- }
- catch (IllegalStateException e)
- {
- }
- }
-
- public void testPutWithBrokenGetExternalValue()
- {
- sti.externalValueReturnsNull = true;
- map.put("abc", "0");
- try
- {
- map.put("abc", "0");
- fail();
- }
- catch (IllegalStateException e)
- {
- }
- }
-
- public void testGetWithInvalidInternalValue()
- {
- ((Map)delegate).put("abc", "0");
- try
- {
- map.get("abc");
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testPutWithInvalidInternalValue()
- {
- ((Map)delegate).put("abc", "0");
- try
- {
- map.put("abc", "0");
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testRemoveWithInvalidInternalValue()
- {
- ((Map)delegate).put("abc", "0");
- try
- {
- map.remove("abc");
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testRemove()
- {
- assertNull(map.remove("abc"));
- delegate.put("abc", new Integer(0));
- assertEquals("0", map.remove("abc"));
- assertTrue(delegate.isEmpty());
- }
-
- public void testPut()
- {
- map.put("abc", "0");
- assertEquals(Collections.singletonMap("abc", new Integer(0)), delegate);
- }
-
- public void testGet()
- {
- assertNull(map.get("abc"));
- delegate.put("abc", new Integer(0));
- assertEquals(Collections.singletonMap("abc", "0"), map);
- }
-
- public void testContainsKeyWithInvalidKey()
- {
- try
- {
- map.containsKey(null);
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- map.containsKey(new Object());
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testContainsValueWithInvalidValue()
- {
- try
- {
- map.containsValue(null);
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- map.containsValue(new Object());
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testRemoveWithInvalidKey()
- {
- try
- {
- map.remove(null);
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- map.remove(new Object());
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testGetWithInvalidKey()
- {
- try
- {
- map.get(null);
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- map.get(new Object());
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testPutWithInvalidKey()
- {
- try
- {
- map.put(null, "0");
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- ((Map)map).put(new Object(), "0");
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testWithPutInvalidValue()
- {
- try
- {
- map.put("", null);
- fail();
- }
- catch (NullPointerException expected)
- {
- }
- try
- {
- ((Map)map).put("", new Object());
- fail();
- }
- catch (ClassCastException expected)
- {
- }
- }
-
- public void testEquals()
- {
- Map<String, Integer> leftDelegate = new HashMap<String, Integer>();
- StringToIntegerMap left = new StringToIntegerMap(leftDelegate, sti);
- leftDelegate.put("abc", new Integer(0));
- Map right = new HashMap();
-
- //
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
-
- //
- right.put("abc", new Object());
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
-
- //
- right.put("abc", "abc");
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
-
- //
- right.put("abc", "0");
- assertTrue(left.equals(right));
- assertTrue(right.equals(left));
-
- //
- right.put("def", "1");
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
-
- //
- right.remove("def");
- right.put(null, "0");
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
-
- //
- right.remove(null);
- right.put("def", null);
- assertFalse(left.equals(right));
- assertFalse(right.equals(left));
- }
-
- public void testEntrySetRetainAll()
- {
- Map right = new HashMap();
- right.put("abc", new Integer(0));
- right.put("def", new Integer(1));
- right.put("ghi", new Integer(2));
-
- //
- Map<String, Integer> leftDelegate = new HashMap<String, Integer>();
- StringToIntegerMap left = new StringToIntegerMap(leftDelegate, sti);
- leftDelegate.putAll(right);
-
- try
- {
- left.keySet().retainAll(null);
- fail("Was expecting NPE");
- }
- catch (NullPointerException expected)
- {
- }
-
- //
- boolean changed =
left.keySet().retainAll(CollectionBuilder.hashSet().add("abc").add("def").add("ghi").get());
- assertFalse(changed);
- assertEquals(right, leftDelegate);
-
- //
- changed =
left.keySet().retainAll(CollectionBuilder.hashSet().add("def").get());
- assertTrue(changed);
- right.remove("abc");
- right.remove("ghi");
- assertEquals(right, leftDelegate);
- }
-
- public static class StringToIntegerMap extends TypedMap<String, String, String,
Integer>
- {
- public StringToIntegerMap(Map<String, Integer> map, StringToInteger sti)
- {
- super(map, new StringToString(), sti);
- }
- }
-
- private static class StringToString extends TypedMap.Converter<String, String>
- {
- protected String getInternal(String external) throws IllegalArgumentException,
ClassCastException
- {
- return external;
- }
- protected String getExternal(String internal)
- {
- return internal;
- }
- protected boolean equals(String left, String right)
- {
- return left.equals(right);
- }
- }
-
- private static class StringToInteger extends TypedMap.Converter<String,
Integer>
- {
-
- /** . */
- boolean internalValueReturnsNull = false;
-
- /** . */
- boolean externalValueReturnsNull = false;
-
- protected Integer getInternal(String external) throws IllegalArgumentException,
ClassCastException
- {
- assertNotNull(external);
-
- //
- if (internalValueReturnsNull)
- {
- return null;
- }
- try
- {
- return new Integer(external);
- }
- catch (NumberFormatException e)
- {
- IllegalArgumentException iae = new IllegalArgumentException();
- iae.initCause(e);
- throw iae;
- }
- }
-
- protected String getExternal(Integer internal)
- {
- assertNotNull(internal);
-
- //
- if (externalValueReturnsNull)
- {
- return null;
- }
-
- //
- return internal.toString();
- }
-
- protected boolean equals(Integer left, Integer right)
- {
- assertNotNull(left);
- assertNotNull(right);
- return left.intValue() == right.intValue();
- }
- }
-}
\ No newline at end of file
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/CustomRuntimeException.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/CustomRuntimeException.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/CustomRuntimeException.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,31 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class CustomRuntimeException extends RuntimeException
+{
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/KeyConverter.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/KeyConverter.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/KeyConverter.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,122 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import org.jboss.portal.common.util.AbstractTypedMap;
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class KeyConverter extends AbstractTypedMap.Converter<String, Long>
+{
+
+ public static final String NULL = "null";
+ public static final String UNCHECKED = "unchecked";
+ public static final String IAE = "iae";
+ public static final String CCE = "cce";
+
+ protected Long getInternal(String external) throws IllegalArgumentException,
ClassCastException
+ {
+ Assert.assertNotNull(external);
+
+ //
+ if (NULL.equals(external))
+ {
+ return null;
+ }
+ if (UNCHECKED.equals(external))
+ {
+ throw new CustomRuntimeException();
+ }
+ if (IAE.equals(external))
+ {
+ throw new IllegalArgumentException();
+ }
+ if (CCE.equals(external))
+ {
+ throw new ClassCastException();
+ }
+
+ //
+ if ("zero".equals(external))
+ {
+ return (long)0;
+ }
+ else if ("one".equals(external))
+ {
+ return (long)1;
+ }
+ else if ("two".equals(external))
+ {
+ return (long)2;
+ }
+ else if ("three".equals(external))
+ {
+ return (long)3;
+ }
+
+ //
+ throw new AssertionFailedError();
+ }
+
+ protected String getExternal(Long internal)
+ {
+ Assert.assertNotNull(internal);
+
+ //
+ if (NULL.equals(internal))
+ {
+ return null;
+ }
+ if (UNCHECKED.equals(internal))
+ {
+ throw new RuntimeException();
+ }
+
+ //
+ switch (internal.intValue())
+ {
+ case 0:
+ return "zero";
+ case 1:
+ return "one";
+ case 2:
+ return "two";
+ case 3:
+ return "three";
+ }
+
+ //
+ throw new AssertionFailedError();
+ }
+
+ protected boolean equals(Long left, Long right)
+ {
+ Assert.assertNotNull(left);
+ Assert.assertNotNull(right);
+ return left.equals(right);
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/StringToIntegerMap.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/StringToIntegerMap.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/StringToIntegerMap.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import org.jboss.portal.common.util.AbstractTypedMap;
+import org.jboss.portal.common.util.TypedMap;
+
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class StringToIntegerMap extends TypedMap<String, String, Long, Integer>
+{
+ public StringToIntegerMap(Map<Long, Integer> map)
+ {
+ super(map, new KeyConverter(), new ValueConverter());
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapEntrySetTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapEntrySetTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapEntrySetTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,697 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.Iterator;
+
+import org.jboss.portal.common.util.Tools;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class TypedMapEntrySetTestCase extends TestCase
+{
+
+ /** . */
+ private static final Integer ZERO = 0;
+
+ /** . */
+ private static final Integer ONE = 1;
+
+ /** . */
+ private static final Integer TWO = 2;
+
+ /** . */
+ private Map<Long, Integer> delegate;
+
+ /** . */
+ private Map delegate2;
+
+ /** . */
+ private StringToIntegerMap map;
+
+ /** . */
+ private Map map2;
+
+ /** . */
+ private Set<Map.Entry<String,String>> entries;
+
+ /** . */
+ private Set<Map.Entry<String,String>> uentries;
+
+ /** . */
+ private Map<Long, Integer> expected;
+
+ public TypedMapEntrySetTestCase(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ delegate = new HashMap<Long, Integer>();
+ delegate2 = delegate;
+ map = new StringToIntegerMap(delegate);
+ map2 = map;
+
+ entries = map.entrySet();
+ uentries = entries;
+
+ map.put("zero", "0");
+ map.put("one", "1");
+
+ expected = new HashMap<Long, Integer>();
+ expected.put((long)0, 0);
+ expected.put((long)1, 1);
+ }
+
+ public void testCannotAdd()
+ {
+ try
+ {
+ entries.add(new ExternalEntry("zero", "0"));
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ entries.addAll(Tools.toSet(new ExternalEntry("zero",
"0")));
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContains()
+ {
+ assertEquals(true, entries.contains(new ExternalEntry("zero",
"0")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.contains(new ExternalEntry("zero",
"3")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.contains(new ExternalEntry("three",
"3")));
+ assertEquals(expected, delegate);
+ }
+
+ public void testContainsAll()
+ {
+ assertEquals(true, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "0"), new ExternalEntry("one",
"1"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "3"), new ExternalEntry("one",
"1"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, entries.containsAll(Tools.toSet()));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "0"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "0"), new ExternalEntry("one",
"1"), new ExternalEntry("three", "3"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "0"), new ExternalEntry("one",
"1"), new ExternalEntry("zero", "3"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("zero", "3"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("one", "1"), new ExternalEntry("three",
"3"))));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, entries.containsAll(Tools.toSet(new
ExternalEntry("three", "3"))));
+ assertEquals(expected, delegate);
+ }
+
+ //
+
+ public void testContainsNullArgument()
+ {
+ try
+ {
+ entries.contains(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllNullArgument()
+ {
+ try
+ {
+ entries.containsAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry(null, "0"),
null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero", null),
null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testContainsArgumentWithWrongClass()
+ {
+ try
+ {
+ uentries.contains(new Object());
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ uentries.containsAll(Tools.toList("zero", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testContainsArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ entries.contains(new ExternalEntry("one", "" +
ValueConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.contains(new ExternalEntry(KeyConverter.CCE, "1"));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry(KeyConverter.CCE, "1")));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry("one", "" + ValueConverter.CCE)));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testContainsArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ entries.contains(new ExternalEntry(KeyConverter.IAE, "1"));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.contains(new ExternalEntry("one", "" +
ValueConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry(KeyConverter.IAE, "1")));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry("one", "" + ValueConverter.IAE)));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testContainsArgumentConvertedToNull()
+ {
+ try
+ {
+ entries.contains(new ExternalEntry("one", "" +
ValueConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.contains(new ExternalEntry(KeyConverter.NULL, "1"));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConvertedToNull()
+ {
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry(KeyConverter.NULL, "1")));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry("one", "" + ValueConverter.NULL)));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testContainsArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ entries.contains(new ExternalEntry(KeyConverter.UNCHECKED , "1"));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.contains(new ExternalEntry("one", "" +
ValueConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry(KeyConverter.UNCHECKED, "1")));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ entries.containsAll(Tools.toList(new ExternalEntry("zero",
"0"), new ExternalEntry("one", "" +
ValueConverter.UNCHECKED)));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testSize()
+ {
+ assertEquals(2, entries.size());
+ }
+
+ public void testClear()
+ {
+ entries.clear();
+ delegate.clear();
+ assertEquals(0, delegate.size());
+ }
+
+ public void testEquals()
+ {
+ // todo
+ }
+
+ public void testHashCode()
+ {
+ // todo
+ }
+
+ public void testToArray()
+ {
+ Object[] a = entries.toArray();
+ assertNotNull(a);
+ assertEquals(Object[].class, a.getClass());
+ assertEquals(2, a.length);
+ Map.Entry<String, String>[] tmp = new Map.Entry[a.length];
+ System.arraycopy(a, 0, tmp, 0, a.length);
+ assertTrue(map.equals(map(tmp)));
+ assertEquals(expected, delegate);
+
+ //
+ try
+ {
+ entries.toArray(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ uentries.toArray(new Integer[2]);
+ fail();
+ }
+ catch (ArrayStoreException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ Map.Entry[] strings1 = new Map.Entry[1];
+ Map.Entry[] strings2 = entries.toArray(strings1);
+ assertNotSame(strings1, strings2);
+ assertEquals(map, map(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new Map.Entry[2];
+ strings2 = entries.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(map, map(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new Map.Entry[3];
+ strings2 = entries.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(map, map(strings2));
+ assertEquals(expected, delegate);
+ }
+
+ private <K, V> Map<K, V> map(Map.Entry<K, V> entries[])
+ {
+ Map<K, V> tmp = new HashMap<K, V>();
+ for (Map.Entry<K, V> entry : entries)
+ {
+ if (entry != null)
+ {
+ tmp.put(entry.getKey(), entry.getValue());
+ }
+ }
+ return tmp;
+ }
+
+ private <K, V> Map<K, V> map(Iterator<Map.Entry<K, V>>
iterator)
+ {
+ Map<K, V> tmp = new HashMap<K, V>();
+ while (iterator.hasNext())
+ {
+ Map.Entry<K, V> entry = iterator.next();
+ if (entry != null)
+ {
+ tmp.put(entry.getKey(), entry.getValue());
+ }
+ }
+ return tmp;
+ }
+
+ public void testIterator()
+ {
+ Iterator<Map.Entry<String, String>> i = entries.iterator();
+ assertEquals(map, map(i));
+ assertEquals(expected, delegate);
+
+ //
+ i = entries.iterator();
+ for (Iterator<Map.Entry<String, String>> j =
entries.iterator();j.hasNext();)
+ {
+ if ("zero".equals(j.next().getKey()))
+ {
+ j.remove();
+ }
+ }
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ }
+
+ public void testBlah()
+ {
+
+ Map.Entry<String, String> entry = getEntry("zero");
+
+ assertEquals("zero", entry.getKey());
+ assertEquals("0", entry.getValue());
+
+ assertEquals("0", entry.setValue("1"));
+
+ expected.put((long)0, 1);
+ assertEquals(expected, delegate);
+
+ //
+ try
+ {
+ entry.setValue("" + ValueConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ entry.setValue("" + ValueConverter.IAE);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ entry.setValue("" + ValueConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ entry.setValue("" + ValueConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ entry.setValue(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ delegate.put((long)0, ValueConverter.NULL);
+ try
+ {
+ entry.getValue();
+ fail();
+ }
+ catch (IllegalStateException e)
+ {
+ }
+
+ //
+ delegate.put((long)0, ValueConverter.IAE);
+ try
+ {
+ entry.getValue();
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ private Map.Entry<String, String> getEntry(String key)
+ {
+ for (Map.Entry<String, String> entry : entries)
+ {
+ if (key.equals(entry.getKey()))
+ {
+ return entry;
+ }
+ }
+ return null;
+ }
+
+ private static class ExternalEntry implements Map.Entry<String, String>
+ {
+
+ /** . */
+ private final String key;
+
+ /** . */
+ private String value;
+
+ private ExternalEntry(String key, String value)
+ {
+ this.key = key;
+ this.value = value;
+ }
+
+ public String getKey()
+ {
+ return key;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+
+ public String setValue(String value)
+ {
+ String oldValue = this.value;
+ this.value = value;
+ return oldValue;
+ }
+ }
+}
\ No newline at end of file
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,753 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.Iterator;
+
+import org.jboss.portal.common.util.Tools;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class TypedMapKeySetTestCase extends TestCase
+{
+
+ /** . */
+ private static final Integer ZERO = 0;
+
+ /** . */
+ private static final Integer ONE = 1;
+
+ /** . */
+ private static final Integer TWO = 2;
+
+ /** . */
+ private Map<Long, Integer> delegate;
+
+ /** . */
+ private Map delegate2;
+
+ /** . */
+ private StringToIntegerMap map;
+
+ /** . */
+ private Map map2;
+
+ /** . */
+ private Set<String> keys;
+
+ /** . */
+ private Set ukeys;
+
+ /** . */
+ private Map<Long, Integer> expected;
+
+ public TypedMapKeySetTestCase(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ delegate = new HashMap<Long, Integer>();
+ delegate2 = delegate;
+ map = new StringToIntegerMap(delegate);
+ map2 = map;
+
+ keys = map.keySet();
+ ukeys = keys;
+
+ map.put("zero", "0");
+ map.put("one", "1");
+
+ expected = new HashMap<Long, Integer>();
+ expected.put((long)0, 0);
+ expected.put((long)1, 1);
+ }
+
+ public void testCannotAdd()
+ {
+ try
+ {
+ keys.add("abc");
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ keys.addAll(Tools.toSet("abc"));
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemove()
+ {
+ assertEquals(true, keys.remove("zero"));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ }
+
+ public void testContains()
+ {
+ assertEquals(true, keys.contains("zero"));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, keys.contains("three"));
+ assertEquals(expected, delegate);
+ }
+
+ public void testRemoveAll()
+ {
+ assertEquals(false, keys.removeAll(Tools.toSet()));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, keys.removeAll(Tools.toSet("three")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, keys.removeAll(Tools.toSet("zero")));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ expected.put((long)0, 0);
+
+ //
+ assertEquals(true, keys.removeAll(Tools.toSet("zero",
"three")));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ expected.put((long)0, 0);
+
+ //
+ assertEquals(true, keys.removeAll(Tools.toSet("zero",
"one")));
+ expected.clear();
+ assertEquals(expected, delegate);
+ }
+
+ public void testRetainAll()
+ {
+ assertEquals(false, keys.retainAll(Tools.toSet("zero",
"one")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, keys.retainAll(Tools.toSet("zero")));
+ expected.remove((long)1);
+ assertEquals(expected, delegate);
+ delegate.put((long)1, 1);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true, keys.retainAll(Tools.toSet("three")));
+ expected.clear();
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ delegate.put((long)1, 1);
+ expected.put((long)0, 0);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true,
keys.retainAll(Tools.toSet("zero","three")));
+ expected.remove((long)1);
+ assertEquals(expected, delegate);
+ delegate.put((long)1, 1);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true, keys.retainAll(Tools.toSet()));
+ expected.clear();
+ assertEquals(expected, delegate);
+ }
+
+ public void testContainsAll()
+ {
+ assertEquals(true, keys.containsAll(Tools.toSet("zero",
"one")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, keys.containsAll(Tools.toSet()));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, keys.containsAll(Tools.toSet("zero")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, keys.containsAll(Tools.toSet("zero", "one",
"three")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, keys.containsAll(Tools.toSet("one",
"three")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, keys.containsAll(Tools.toSet("three")));
+ assertEquals(expected, delegate);
+ }
+
+ //
+
+ public void testRemoveNullArgument()
+ {
+ try
+ {
+ keys.remove(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsNullArgument()
+ {
+ try
+ {
+ keys.contains(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllNullArgument()
+ {
+ try
+ {
+ keys.removeAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ keys.removeAll(Tools.toList("zero", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllNullArgument()
+ {
+ try
+ {
+ keys.retainAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ keys.retainAll(Tools.toList("zero", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllNullArgument()
+ {
+ try
+ {
+ keys.containsAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ keys.containsAll(Tools.toList("zero", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentWithWrongClass()
+ {
+ try
+ {
+ ukeys.remove(new Object());
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentWithWrongClass()
+ {
+ try
+ {
+ ukeys.contains(new Object());
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ ukeys.removeAll(Tools.toList("zero", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ ukeys.retainAll(Tools.toList("zero", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ ukeys.containsAll(Tools.toList("zero", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ keys.remove(KeyConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ keys.contains(KeyConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ keys.removeAll(Tools.toList("zero", KeyConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ keys.retainAll(Tools.toList("zero", KeyConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ keys.containsAll(Tools.toList("zero", KeyConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ keys.remove(KeyConverter.IAE);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ keys.contains(KeyConverter.IAE);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ keys.removeAll(Tools.toList("zero", KeyConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ keys.retainAll(Tools.toList("zero", KeyConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ keys.containsAll(Tools.toList("zero", KeyConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConvertedToNull()
+ {
+ try
+ {
+ keys.remove(KeyConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConvertedToNull()
+ {
+ try
+ {
+ keys.contains(KeyConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConvertedToNull()
+ {
+ try
+ {
+ keys.removeAll(Tools.toList("zero", KeyConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConvertedToNull()
+ {
+ try
+ {
+ keys.retainAll(Tools.toList("zero", KeyConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConvertedToNull()
+ {
+ try
+ {
+ keys.containsAll(Tools.toList("zero", KeyConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ keys.remove(KeyConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ keys.contains(KeyConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ keys.removeAll(Tools.toList("zero", KeyConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ keys.retainAll(Tools.toList("zero", KeyConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ keys.containsAll(Tools.toList("zero", KeyConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testSize()
+ {
+ assertEquals(2, keys.size());
+ }
+
+ public void testClear()
+ {
+ keys.clear();
+ delegate.clear();
+ assertEquals(0, delegate.size());
+ }
+
+ public void testEquals()
+ {
+ // todo
+ }
+
+ public void testHashCode()
+ {
+ // todo
+ }
+
+ public void testToArray()
+ {
+ Object[] a = keys.toArray();
+ assertNotNull(a);
+ assertEquals(Object[].class, a.getClass());
+ assertEquals(2, a.length);
+ assertEquals(Tools.toSet("zero","one"), Tools.toSet(a));
+ assertEquals(expected, delegate);
+
+ //
+ try
+ {
+ keys.toArray(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ ukeys.toArray(new Integer[2]);
+ fail();
+ }
+ catch (ArrayStoreException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ String[] strings1 = new String[1];
+ String[] strings2 = keys.toArray(strings1);
+ assertNotSame(strings1, strings2);
+ assertEquals(Tools.toSet("zero","one"),
Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new String[2];
+ strings2 = keys.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(Tools.toSet("zero","one"),
Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new String[3];
+ strings2 = keys.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(Tools.toSet("zero","one", null),
Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+ }
+
+ public void testIterator()
+ {
+ Iterator<String> i = keys.iterator();
+ assertEquals(Tools.toSet("zero","one"), Tools.toSet(i));
+ assertEquals(expected, delegate);
+
+ //
+ i = keys.iterator();
+ for (Iterator<String> j = keys.iterator();j.hasNext();)
+ {
+ if ("zero".equals(j.next()))
+ {
+ j.remove();
+ }
+ }
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ }
+
+
+
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,687 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import java.util.Set;
+
+import org.jboss.portal.common.util.CollectionBuilder;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class TypedMapTestCase extends TestCase
+{
+
+ /** . */
+ private static final Integer ZERO = 0;
+
+ /** . */
+ private static final Integer ONE = 1;
+
+ /** . */
+ private static final Integer TWO = 2;
+
+ /** . */
+ private Map<Long, Integer> delegate;
+
+ /** . */
+ private Map delegate2;
+
+ /** . */
+ private StringToIntegerMap map;
+
+ /** . */
+ private Map map2;
+
+ public TypedMapTestCase(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ delegate = new HashMap<Long, Integer>();
+ delegate2 = delegate;
+ map = new StringToIntegerMap(delegate);
+ map2 = map;
+ }
+
+ // Basic operations
*************************************************************************************************
+
+ public void testGet()
+ {
+ assertNull(map.get("zero"));
+ delegate.put((long)0, ZERO);
+ assertEquals(Collections.singletonMap("zero", "0"), map);
+ }
+
+ public void testPut()
+ {
+ map.put("zero", "0");
+ assertEquals(Collections.singletonMap((long)0, ZERO), delegate);
+ }
+
+ public void testRemove()
+ {
+ assertNull(map.remove("zero"));
+ delegate.put((long)0, ZERO);
+ assertEquals("0", map.remove("zero"));
+ assertTrue(delegate.isEmpty());
+ }
+
+ //
+
+ public void testGetWithWrongKeyArgumentClass()
+ {
+ try
+ {
+ map.get(new Object());
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ public void testPutWithWrongKeyArgumentClass()
+ {
+ try
+ {
+ map2.put(new Object(), "0");
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ public void testRemoveWithWrongKeyArgumentClass()
+ {
+ try
+ {
+ map.remove(new Object());
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ public void testContainsKeyWithWrongKeyArgumentClass()
+ {
+ try
+ {
+ map.containsKey(new Object());
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ //
+
+ public void testGetInternalValueConversionThrowsUncheckedException()
+ {
+ delegate.put((long)0, ValueConverter.UNCHECKED);
+ try
+ {
+ map.get("zero");
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(ValueConverter.UNCHECKED, delegate.get((long)0));
+ }
+ }
+
+ public void testPutInternalValueConversionThrowsUncheckedException()
+ {
+ delegate.put((long)0, ValueConverter.UNCHECKED);
+ try
+ {
+ map.put("zero", "0");
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(ValueConverter.UNCHECKED, delegate.get((long)0));
+ }
+ }
+
+ public void testRemoveInternalValueConversionThrowsUncheckedException()
+ {
+ delegate.put((long)0, ValueConverter.UNCHECKED);
+ try
+ {
+ map.remove("zero");
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(ValueConverter.UNCHECKED, delegate.get((long)0));
+ }
+ }
+
+ //
+
+ public void testGetKeyArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.get(KeyConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ public void testPutKeyArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.put("" + KeyConverter.UNCHECKED, "0");
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ public void testRemoveKeyArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.remove("" + KeyConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ public void testContainsKeyKeyArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.containsKey("" + KeyConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ //
+
+ public void testPutWithWrongValueArgumentClass()
+ {
+ delegate.put((long)0, ZERO);
+ try
+ {
+ map2.put("zero", new Object());
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ assertEquals(1, delegate.size());
+ assertEquals(ZERO, delegate.get((long)0));
+ }
+ }
+
+ public void testContainsValueWithWrongValueArgumentClass()
+ {
+ try
+ {
+ map.containsValue(new Object());
+ fail();
+ }
+ catch (ClassCastException expected)
+ {
+ }
+ }
+
+ //
+
+ public void testPutValueArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.put("zero", "" + ValueConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ public void testContainsValueValueArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ map.containsValue("" + ValueConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(0, delegate.size());
+ }
+ }
+
+ //
+
+ public void testGetNullKeyArgument()
+ {
+ try
+ {
+ map.get(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testPutNullKeyArgument()
+ {
+ try
+ {
+ map.put(null, "0");
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testRemoveNullKeyArgument()
+ {
+ try
+ {
+ map.remove(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testContainsKeyNullKeyArgument()
+ {
+ try
+ {
+ map.remove(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ //
+
+ public void testPutNullValueArgument()
+ {
+ try
+ {
+ map.put("zero", null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.size() == 0);
+ }
+ }
+
+ public void testContainsValueNullValueArgument()
+ {
+ try
+ {
+ map.containsValue(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertTrue(delegate.size() == 0);
+ }
+ }
+
+ //
+
+ public void testGetKeyArgumentConvertedToNull()
+ {
+ try
+ {
+ map.get(KeyConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testPutKeyArgumentConvertedToNull()
+ {
+ try
+ {
+ map.put(KeyConverter.NULL, "0");
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testRemoveKeyArgumentConvertedToNull()
+ {
+ try
+ {
+ map.remove(KeyConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testContainsKeyKeyArgumentConvertedToNull()
+ {
+ try
+ {
+ map.remove(KeyConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ //
+
+ public void testPutValueArgumentConvertedToNull()
+ {
+ try
+ {
+ map.put("zero", "" + ValueConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertFalse(delegate.containsKey((long)0));
+ }
+ }
+
+ public void testContainsValueValueArgumentConvertedToNull()
+ {
+ try
+ {
+ map.containsValue("" + ValueConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertTrue(delegate.size() == 0);
+ }
+ }
+
+ //
+
+ public void testPutValueArgumentThrowsCCE()
+ {
+ try
+ {
+ map.put("zero", "" + ValueConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.size() == 0);
+ }
+ }
+
+ public void testContainsValueValueArgumentThrowsCCE()
+ {
+ try
+ {
+ map.containsValue("" + ValueConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.size() == 0);
+ }
+ }
+
+ //
+
+ public void testGetKeyArgumentThrowsCCE()
+ {
+ try
+ {
+ map.get(KeyConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testPutKeyArgumentThrowsCCE()
+ {
+ try
+ {
+ map.put(KeyConverter.CCE, "0");
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testRemoveKeyArgumentThrowsCCE()
+ {
+ try
+ {
+ map.remove(KeyConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ public void testContainsKeyKeyArgumentThrowsCCE()
+ {
+ try
+ {
+ map.containsKey(KeyConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertTrue(delegate.isEmpty());
+ }
+ }
+
+ //
+
+ public void testGetInternalValueConvertedToNull()
+ {
+ delegate.put((long)0, ValueConverter.NULL);
+ try
+ {
+ map.get("zero");
+ fail();
+ }
+ catch (IllegalStateException e)
+ {
+ assertEquals(ValueConverter.NULL, delegate.get((long)0));
+ }
+ }
+
+ public void testPutInternalValueConvertedToNull()
+ {
+ delegate.put((long)0, ValueConverter.NULL);
+ try
+ {
+ map.put("zero", "0");
+ fail();
+ }
+ catch (IllegalStateException e)
+ {
+ assertEquals(ValueConverter.NULL, delegate.get((long)0));
+ }
+ }
+
+ public void testRemoveInternalValueConvertedToNull()
+ {
+ delegate.put((long)0, ValueConverter.NULL);
+ try
+ {
+ map.remove("zero");
+ fail();
+ }
+ catch (IllegalStateException e)
+ {
+ assertEquals(ValueConverter.NULL, delegate.get((long)0));
+ }
+ }
+
+
+
+
+
+
+
+
+ public void testEquals()
+ {
+ Map<Long, Integer> leftDelegate = new HashMap<Long, Integer>();
+ StringToIntegerMap left = new StringToIntegerMap(leftDelegate);
+ leftDelegate.put((long)0, ZERO);
+ Map<? super Object, ? super Object> right = new HashMap<Object,
Object>();
+
+ //
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+
+ //
+ right.put("zero", new Object());
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+
+ //
+ right.put("abc", "abc");
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+
+ //
+ right.remove("abc");
+ right.put("zero", "0");
+ assertTrue(left.equals(right));
+ assertTrue(right.equals(left));
+
+ //
+ right.put("def", "1");
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+
+ //
+ right.remove("def");
+ right.put(null, "0");
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+
+ //
+ right.remove(null);
+ right.put("def", null);
+ assertFalse(left.equals(right));
+ assertFalse(right.equals(left));
+ }
+
+ public void testEntrySetRetainAll()
+ {
+ Map right = new HashMap();
+ right.put("zero", ZERO);
+ right.put("one", ONE);
+ right.put("two", TWO);
+
+ //
+ Map<Long, Integer> leftTemplate = new HashMap<Long, Integer>();
+ leftTemplate.put((long)0, ZERO);
+ leftTemplate.put((long)1, ONE);
+ leftTemplate.put((long)2, TWO);
+
+ //
+ Map<Long, Integer> leftDelegate = new HashMap<Long,
Integer>(leftTemplate);
+
+ //
+ StringToIntegerMap left = new StringToIntegerMap(leftDelegate);
+
+ try
+ {
+ left.keySet().retainAll(null);
+ fail("Was expecting NPE");
+ }
+ catch (NullPointerException expected)
+ {
+ }
+
+ //
+ boolean changed = left.keySet().retainAll(right.keySet());
+ assertFalse(changed);
+ assertEquals(leftTemplate, leftDelegate);
+
+ //
+ changed =
left.keySet().retainAll(CollectionBuilder.hashSet().add("one").get());
+ assertTrue(changed);
+ right.remove("zero");
+ right.remove("two");
+ leftTemplate.remove((long)0);
+ leftTemplate.remove((long)2);
+ assertEquals(leftTemplate, leftDelegate);
+ }
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapValuesTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapValuesTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapValuesTestCase.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,755 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.Iterator;
+import java.util.Collection;
+
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.common.NotYetImplemented;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class TypedMapValuesTestCase extends TestCase
+{
+
+ /** . */
+ private static final Integer ZERO = 0;
+
+ /** . */
+ private static final Integer ONE = 1;
+
+ /** . */
+ private static final Integer TWO = 2;
+
+ /** . */
+ private Map<Long, Integer> delegate;
+
+ /** . */
+ private Map delegate2;
+
+ /** . */
+ private StringToIntegerMap map;
+
+ /** . */
+ private Map map2;
+
+ /** . */
+ private Collection<String> values;
+
+ /** . */
+ private Collection uvalues;
+
+ /** . */
+ private Map<Long, Integer> expected;
+
+ public TypedMapValuesTestCase(String name)
+ {
+ super(name);
+ }
+
+ protected void setUp() throws Exception
+ {
+ delegate = new HashMap<Long, Integer>();
+ delegate2 = delegate;
+ map = new StringToIntegerMap(delegate);
+ map2 = map;
+
+ values = map.values();
+ uvalues = values;
+
+ map.put("zero", "0");
+ map.put("one", "1");
+
+ expected = new HashMap<Long, Integer>();
+ expected.put((long)0, 0);
+ expected.put((long)1, 1);
+ }
+
+ public void testCannotAdd()
+ {
+ try
+ {
+ values.add("1");
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ values.addAll(Tools.toSet("1"));
+ fail();
+ }
+ catch (UnsupportedOperationException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemove()
+ {
+ assertEquals(true, values.remove("0"));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+
+ //
+// throw new NotYetImplemented("finish me");
+ }
+
+ public void testContains()
+ {
+ assertEquals(true, values.contains("0"));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, values.contains("3"));
+ assertEquals(expected, delegate);
+ }
+
+ public void testRemoveAll()
+ {
+ assertEquals(false, values.removeAll(Tools.toSet()));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, values.removeAll(Tools.toSet("3")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, values.removeAll(Tools.toSet("0")));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ expected.put((long)0, 0);
+
+ //
+ assertEquals(true, values.removeAll(Tools.toSet("0", "3")));
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ expected.put((long)0, 0);
+
+ //
+ assertEquals(true, values.removeAll(Tools.toSet("0", "1")));
+ expected.clear();
+ assertEquals(expected, delegate);
+ }
+
+ public void testRetainAll()
+ {
+ assertEquals(false, values.retainAll(Tools.toSet("0", "1")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, values.retainAll(Tools.toSet("0")));
+ expected.remove((long)1);
+ assertEquals(expected, delegate);
+ delegate.put((long)1, 1);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true, values.retainAll(Tools.toSet("3")));
+ expected.clear();
+ assertEquals(expected, delegate);
+ delegate.put((long)0, 0);
+ delegate.put((long)1, 1);
+ expected.put((long)0, 0);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true, values.retainAll(Tools.toSet("0","3")));
+ expected.remove((long)1);
+ assertEquals(expected, delegate);
+ delegate.put((long)1, 1);
+ expected.put((long)1, 1);
+
+ //
+ assertEquals(true, values.retainAll(Tools.toSet()));
+ expected.clear();
+ assertEquals(expected, delegate);
+ }
+
+ public void testContainsAll()
+ {
+ assertEquals(true, values.containsAll(Tools.toSet("0", "1")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, values.containsAll(Tools.toSet()));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(true, values.containsAll(Tools.toSet("0")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, values.containsAll(Tools.toSet("0", "1",
"3")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, values.containsAll(Tools.toSet("1",
"3")));
+ assertEquals(expected, delegate);
+
+ //
+ assertEquals(false, values.containsAll(Tools.toSet("3")));
+ assertEquals(expected, delegate);
+ }
+
+ //
+
+ public void testRemoveNullArgument()
+ {
+ try
+ {
+ values.remove(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsNullArgument()
+ {
+ try
+ {
+ values.contains(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllNullArgument()
+ {
+ try
+ {
+ values.removeAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ values.removeAll(Tools.toList("0", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllNullArgument()
+ {
+ try
+ {
+ values.retainAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ values.retainAll(Tools.toList("0", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllNullArgument()
+ {
+ try
+ {
+ values.containsAll(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ try
+ {
+ values.containsAll(Tools.toList("0", null));
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentWithWrongClass()
+ {
+ try
+ {
+ uvalues.remove(new Object());
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentWithWrongClass()
+ {
+ try
+ {
+ uvalues.contains(new Object());
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ uvalues.removeAll(Tools.toList("0", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ uvalues.retainAll(Tools.toList("0", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void _testContainsAllNullArgumentWithWrongClass()
+ {
+ try
+ {
+ uvalues.containsAll(Tools.toList("zero", new Object()));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ values.remove("" + ValueConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ values.contains("" + ValueConverter.CCE);
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ values.removeAll(Tools.toList("0", "" +
ValueConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ values.retainAll(Tools.toList("0", "" +
ValueConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsCCE()
+ {
+ try
+ {
+ values.containsAll(Tools.toList("0", "" +
ValueConverter.CCE));
+ fail();
+ }
+ catch (ClassCastException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ values.remove("" + ValueConverter.IAE);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ values.contains("" + ValueConverter.IAE);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ values.removeAll(Tools.toList("0", "" +
ValueConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ values.retainAll(Tools.toList("0", "" +
ValueConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsIAE()
+ {
+ try
+ {
+ values.containsAll(Tools.toList("0", "" +
ValueConverter.IAE));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConvertedToNull()
+ {
+ try
+ {
+ values.remove("" + ValueConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsArgumentConvertedToNull()
+ {
+ try
+ {
+ values.contains("" + ValueConverter.NULL);
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConvertedToNull()
+ {
+ try
+ {
+ values.removeAll(Tools.toList("ze0ro", "" +
ValueConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConvertedToNull()
+ {
+ try
+ {
+ values.retainAll(Tools.toList("0", ""+
ValueConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConvertedToNull()
+ {
+ try
+ {
+ values.containsAll(Tools.toList("0", "" +
ValueConverter.NULL));
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testRemoveArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ values.remove("" + ValueConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContiansArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ values.contains("" + ValueConverter.UNCHECKED);
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRemoveAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ values.removeAll(Tools.toList("0", "" +
ValueConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testRetainAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ values.retainAll(Tools.toList("0", "" +
ValueConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ public void testContainsAllArgumentConversionThrowsUncheckedException()
+ {
+ try
+ {
+ values.containsAll(Tools.toList("0", "" +
ValueConverter.UNCHECKED));
+ fail();
+ }
+ catch (CustomRuntimeException e)
+ {
+ assertEquals(expected, delegate);
+ }
+ }
+
+ //
+
+ public void testSize()
+ {
+ assertEquals(2, values.size());
+ }
+
+ public void testClear()
+ {
+ values.clear();
+ delegate.clear();
+ assertEquals(0, delegate.size());
+ }
+
+ public void _testEquals()
+ {
+ // todo
+ }
+
+ public void _testHashCode()
+ {
+ // todo
+ }
+
+ public void testToArray()
+ {
+ Object[] a = values.toArray();
+ assertNotNull(a);
+ assertEquals(Object[].class, a.getClass());
+ assertEquals(2, a.length);
+ assertEquals(Tools.toSet("0","1"), Tools.toSet(a));
+ assertEquals(expected, delegate);
+
+ //
+ try
+ {
+ values.toArray(null);
+ fail();
+ }
+ catch (NullPointerException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ try
+ {
+ uvalues.toArray(new Integer[2]);
+ fail();
+ }
+ catch (ArrayStoreException e)
+ {
+ assertEquals(expected, delegate);
+ }
+
+ //
+ String[] strings1 = new String[1];
+ String[] strings2 = values.toArray(strings1);
+ assertNotSame(strings1, strings2);
+ assertEquals(Tools.toSet("0","1"), Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new String[2];
+ strings2 = values.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(Tools.toSet("0","1"), Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+
+ //
+ strings1 = new String[3];
+ strings2 = values.toArray(strings1);
+ assertSame(strings1, strings2);
+ assertEquals(Tools.toSet("0","1", null),
Tools.toSet(strings2));
+ assertEquals(expected, delegate);
+ }
+
+ public void testIterator()
+ {
+ Iterator<String> i = values.iterator();
+ assertEquals(Tools.toSet("0","1"), Tools.toSet(i));
+ assertEquals(expected, delegate);
+
+ //
+ i = values.iterator();
+ for (Iterator<String> j = values.iterator();j.hasNext();)
+ {
+ if ("0".equals(j.next()))
+ {
+ j.remove();
+ }
+ }
+ expected.remove((long)0);
+ assertEquals(expected, delegate);
+ }
+}
\ No newline at end of file
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/ValueConverter.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/ValueConverter.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/ValueConverter.java 2008-01-20
22:13:06 UTC (rev 9541)
@@ -0,0 +1,112 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This 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 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util.typedmap;
+
+import org.jboss.portal.common.util.AbstractTypedMap;
+import junit.framework.Assert;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class ValueConverter extends AbstractTypedMap.Converter<String, Integer>
+{
+
+ public static final Integer NULL = 10;
+ public static final Integer UNCHECKED = 11;
+ public static final Integer IAE = 12;
+ public static final Integer CCE = 13;
+
+
+ protected Integer getInternal(String external) throws IllegalArgumentException,
ClassCastException
+ {
+ Assert.assertNotNull(external);
+
+ //
+ int i;
+ try
+ {
+ i = new Integer(external);
+ }
+ catch (NumberFormatException e)
+ {
+ IllegalArgumentException iae = new IllegalArgumentException();
+ iae.initCause(e);
+ throw iae;
+ }
+
+ //
+ if (NULL == i)
+ {
+ return null;
+ }
+ if (UNCHECKED == i)
+ {
+ throw new CustomRuntimeException();
+ }
+ if (IAE == i)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (CCE == i)
+ {
+ throw new ClassCastException();
+ }
+
+ //
+ return i;
+ }
+
+ protected String getExternal(Integer internal)
+ {
+ Assert.assertNotNull(internal);
+
+ //
+ if (NULL.equals(internal))
+ {
+ return null;
+ }
+ if (UNCHECKED.equals(internal))
+ {
+ throw new CustomRuntimeException();
+ }
+ if (IAE.equals(internal))
+ {
+ throw new IllegalArgumentException();
+ }
+ if (CCE.equals(internal))
+ {
+ throw new ClassCastException();
+ }
+
+ //
+ return internal.toString();
+ }
+
+ protected boolean equals(Integer left, Integer right)
+ {
+ Assert.assertNotNull(left);
+ Assert.assertNotNull(right);
+ return left.intValue() == right.intValue();
+ }
+}