Per CONTRIBUTING, convert text files to use LF instead of CRLF
It is easier to standardise this now in one commit rather than during later bulk changes where it would complicate the review process. This is literally the result of running "dos2unix" on appropriate files. There is no attempt here to likewise standardise the spaces/tabs conventions in the codebase.
This commit is contained in:
@@ -1,45 +1,45 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A simple subinterface of {@link Map} that exposes a mutex that application code can synchronize on.
|
||||
* <p>
|
||||
* Expected to be implemented by Maps that are backed by shared objects that require synchronization between multiple
|
||||
* threads. An example would be the HTTP session map.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface SharedMap<K, V> extends Map<K, V> {
|
||||
|
||||
/**
|
||||
* Returns the shared mutex that may be synchronized on using a synchronized block. The returned mutex is guaranteed
|
||||
* to be non-null.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* <pre>
|
||||
* synchronized (sharedMap.getMutex()) {
|
||||
* // do synchronized work
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @return the mutex
|
||||
*/
|
||||
Object getMutex();
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A simple subinterface of {@link Map} that exposes a mutex that application code can synchronize on.
|
||||
* <p>
|
||||
* Expected to be implemented by Maps that are backed by shared objects that require synchronization between multiple
|
||||
* threads. An example would be the HTTP session map.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface SharedMap<K, V> extends Map<K, V> {
|
||||
|
||||
/**
|
||||
* Returns the shared mutex that may be synchronized on using a synchronized block. The returned mutex is guaranteed
|
||||
* to be non-null.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* <pre>
|
||||
* synchronized (sharedMap.getMutex()) {
|
||||
* // do synchronized work
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @return the mutex
|
||||
*/
|
||||
Object getMutex();
|
||||
}
|
||||
|
||||
@@ -1,105 +1,105 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* A map decorator that implements <code>SharedMap</code>. By default, simply returns the map itself as the mutex.
|
||||
* Subclasses may override to return a different mutex object.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class SharedMapDecorator<K, V> implements SharedMap<K, V>, Serializable {
|
||||
|
||||
/**
|
||||
* The wrapped, target map.
|
||||
*/
|
||||
private Map<K, V> map;
|
||||
|
||||
/**
|
||||
* Creates a new shared map decorator.
|
||||
* @param map the map that is shared by multiple threads, to be synced
|
||||
*/
|
||||
public SharedMapDecorator(Map<K, V> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
// implementing Map
|
||||
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
public V get(Object key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends K, ? extends V> map) {
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
public V remove(Object key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
// implementing SharedMap
|
||||
|
||||
public Object getMutex() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("map", map).append("mutex", getMutex()).toString();
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* A map decorator that implements <code>SharedMap</code>. By default, simply returns the map itself as the mutex.
|
||||
* Subclasses may override to return a different mutex object.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class SharedMapDecorator<K, V> implements SharedMap<K, V>, Serializable {
|
||||
|
||||
/**
|
||||
* The wrapped, target map.
|
||||
*/
|
||||
private Map<K, V> map;
|
||||
|
||||
/**
|
||||
* Creates a new shared map decorator.
|
||||
* @param map the map that is shared by multiple threads, to be synced
|
||||
*/
|
||||
public SharedMapDecorator(Map<K, V> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
// implementing Map
|
||||
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
public V get(Object key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends K, ? extends V> map) {
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
public V remove(Object key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
// implementing SharedMap
|
||||
|
||||
public Object getMutex() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("map", map).append("mutex", getMutex()).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,289 +1,289 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Base class for map adapters whose keys are String values. Concrete classes need only implement the abstract hook
|
||||
* methods defined by this class.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public abstract class StringKeyedMapAdapter<V> implements Map<String, V> {
|
||||
|
||||
private Set<String> keySet;
|
||||
|
||||
private Collection<V> values;
|
||||
|
||||
private Set<Entry<String, V>> entrySet;
|
||||
|
||||
// implementing Map
|
||||
|
||||
public void clear() {
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
removeAttribute(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return getAttribute(key.toString()) != null;
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
Object aValue = getAttribute(it.next());
|
||||
if (value.equals(aValue)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Set<Entry<String, V>> entrySet() {
|
||||
return (entrySet != null) ? entrySet : (entrySet = new EntrySet());
|
||||
}
|
||||
|
||||
public V get(Object key) {
|
||||
return getAttribute(key.toString());
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return !getAttributeNames().hasNext();
|
||||
}
|
||||
|
||||
public Set<String> keySet() {
|
||||
return (keySet != null) ? keySet : (keySet = new KeySet());
|
||||
}
|
||||
|
||||
public V put(String key, V value) {
|
||||
String stringKey = String.valueOf(key);
|
||||
V previousValue = getAttribute(stringKey);
|
||||
setAttribute(stringKey, value);
|
||||
return previousValue;
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends String, ? extends V> map) {
|
||||
for (Entry<? extends String, ? extends V> entry : map.entrySet()) {
|
||||
setAttribute(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public V remove(Object key) {
|
||||
String stringKey = key.toString();
|
||||
V retval = getAttribute(stringKey);
|
||||
removeAttribute(stringKey);
|
||||
return retval;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
int size = 0;
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
size++;
|
||||
it.next();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
return (values != null) ? values : (values = new Values());
|
||||
}
|
||||
|
||||
// hook methods
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Gets a value associated with a key.
|
||||
* @param key the key to lookup
|
||||
* @return the associated value, or null if none
|
||||
*/
|
||||
protected abstract V getAttribute(String key);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Puts a key-value pair in the map, overwriting
|
||||
* any possible earlier value associated with the same key.
|
||||
* @param key the key to associate the value with
|
||||
* @param value the value to associate with the key
|
||||
*/
|
||||
protected abstract void setAttribute(String key, V value);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Removes a key and its associated value from the
|
||||
* map.
|
||||
* @param key the key to remove
|
||||
*/
|
||||
protected abstract void removeAttribute(String key);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Returns an enumeration listing all keys known to
|
||||
* the map.
|
||||
* @return the key enumeration
|
||||
*/
|
||||
protected abstract Iterator<String> getAttributeNames();
|
||||
|
||||
// internal helper classes
|
||||
|
||||
private abstract class AbstractSet<T> extends java.util.AbstractSet<T> {
|
||||
public boolean isEmpty() {
|
||||
return StringKeyedMapAdapter.this.isEmpty();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return StringKeyedMapAdapter.this.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
StringKeyedMapAdapter.this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private class KeySet extends AbstractSet<String> {
|
||||
public Iterator<String> iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return StringKeyedMapAdapter.this.containsKey(o);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
return StringKeyedMapAdapter.this.remove(o) != null;
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class AbstractKeyIterator {
|
||||
private final Iterator<String> it = getAttributeNames();
|
||||
|
||||
private String currentKey;
|
||||
|
||||
public void remove() {
|
||||
if (currentKey == null) {
|
||||
throw new NoSuchElementException("You must call next() at least once");
|
||||
}
|
||||
StringKeyedMapAdapter.this.remove(currentKey);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return it.hasNext();
|
||||
}
|
||||
|
||||
protected String nextKey() {
|
||||
return currentKey = it.next();
|
||||
}
|
||||
}
|
||||
|
||||
private class KeyIterator extends AbstractKeyIterator implements Iterator<String> {
|
||||
public String next() {
|
||||
return nextKey();
|
||||
}
|
||||
}
|
||||
|
||||
private class Values extends AbstractSet<V> {
|
||||
public Iterator<V> iterator() {
|
||||
return new ValuesIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return StringKeyedMapAdapter.this.containsValue(o);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
for (Iterator<V> it = iterator(); it.hasNext();) {
|
||||
if (o.equals(it.next())) {
|
||||
it.remove();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private class ValuesIterator extends AbstractKeyIterator implements Iterator<V> {
|
||||
public V next() {
|
||||
return StringKeyedMapAdapter.this.get(nextKey());
|
||||
}
|
||||
}
|
||||
|
||||
private class EntrySet extends AbstractSet<Entry<String, V>> {
|
||||
public Iterator<Entry<String, V>> iterator() {
|
||||
return new EntryIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
Entry<String, V> entry = getAsEntry(o);
|
||||
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
|
||||
return false;
|
||||
}
|
||||
V valueFromThisMap = StringKeyedMapAdapter.this.get(entry.getKey());
|
||||
return entry.getValue().equals(valueFromThisMap);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
Entry<String, V> entry = getAsEntry(o);
|
||||
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
|
||||
return false;
|
||||
}
|
||||
V valueFromThisMap = StringKeyedMapAdapter.this.get(entry.getKey());
|
||||
if (!entry.getValue().equals(valueFromThisMap)) {
|
||||
return false;
|
||||
}
|
||||
return StringKeyedMapAdapter.this.remove(entry.getKey()) != null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Entry<String, V> getAsEntry(Object o) {
|
||||
if (o instanceof Entry) {
|
||||
return (Entry<String, V>) o;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class EntryIterator extends AbstractKeyIterator implements Iterator<Entry<String, V>> {
|
||||
public Entry<String, V> next() {
|
||||
return new EntrySetEntry(nextKey());
|
||||
}
|
||||
}
|
||||
|
||||
private class EntrySetEntry implements Entry<String, V> {
|
||||
private final String currentKey;
|
||||
|
||||
public EntrySetEntry(String currentKey) {
|
||||
this.currentKey = currentKey;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return currentKey;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return StringKeyedMapAdapter.this.get(currentKey);
|
||||
}
|
||||
|
||||
public V setValue(V value) {
|
||||
return StringKeyedMapAdapter.this.put(currentKey, value);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.collection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Base class for map adapters whose keys are String values. Concrete classes need only implement the abstract hook
|
||||
* methods defined by this class.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public abstract class StringKeyedMapAdapter<V> implements Map<String, V> {
|
||||
|
||||
private Set<String> keySet;
|
||||
|
||||
private Collection<V> values;
|
||||
|
||||
private Set<Entry<String, V>> entrySet;
|
||||
|
||||
// implementing Map
|
||||
|
||||
public void clear() {
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
removeAttribute(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return getAttribute(key.toString()) != null;
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
Object aValue = getAttribute(it.next());
|
||||
if (value.equals(aValue)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Set<Entry<String, V>> entrySet() {
|
||||
return (entrySet != null) ? entrySet : (entrySet = new EntrySet());
|
||||
}
|
||||
|
||||
public V get(Object key) {
|
||||
return getAttribute(key.toString());
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return !getAttributeNames().hasNext();
|
||||
}
|
||||
|
||||
public Set<String> keySet() {
|
||||
return (keySet != null) ? keySet : (keySet = new KeySet());
|
||||
}
|
||||
|
||||
public V put(String key, V value) {
|
||||
String stringKey = String.valueOf(key);
|
||||
V previousValue = getAttribute(stringKey);
|
||||
setAttribute(stringKey, value);
|
||||
return previousValue;
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends String, ? extends V> map) {
|
||||
for (Entry<? extends String, ? extends V> entry : map.entrySet()) {
|
||||
setAttribute(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public V remove(Object key) {
|
||||
String stringKey = key.toString();
|
||||
V retval = getAttribute(stringKey);
|
||||
removeAttribute(stringKey);
|
||||
return retval;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
int size = 0;
|
||||
for (Iterator<String> it = getAttributeNames(); it.hasNext();) {
|
||||
size++;
|
||||
it.next();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
return (values != null) ? values : (values = new Values());
|
||||
}
|
||||
|
||||
// hook methods
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Gets a value associated with a key.
|
||||
* @param key the key to lookup
|
||||
* @return the associated value, or null if none
|
||||
*/
|
||||
protected abstract V getAttribute(String key);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Puts a key-value pair in the map, overwriting
|
||||
* any possible earlier value associated with the same key.
|
||||
* @param key the key to associate the value with
|
||||
* @param value the value to associate with the key
|
||||
*/
|
||||
protected abstract void setAttribute(String key, V value);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Removes a key and its associated value from the
|
||||
* map.
|
||||
* @param key the key to remove
|
||||
*/
|
||||
protected abstract void removeAttribute(String key);
|
||||
|
||||
/**
|
||||
* Hook method that needs to be implemented by concrete subclasses. Returns an enumeration listing all keys known to
|
||||
* the map.
|
||||
* @return the key enumeration
|
||||
*/
|
||||
protected abstract Iterator<String> getAttributeNames();
|
||||
|
||||
// internal helper classes
|
||||
|
||||
private abstract class AbstractSet<T> extends java.util.AbstractSet<T> {
|
||||
public boolean isEmpty() {
|
||||
return StringKeyedMapAdapter.this.isEmpty();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return StringKeyedMapAdapter.this.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
StringKeyedMapAdapter.this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private class KeySet extends AbstractSet<String> {
|
||||
public Iterator<String> iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return StringKeyedMapAdapter.this.containsKey(o);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
return StringKeyedMapAdapter.this.remove(o) != null;
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class AbstractKeyIterator {
|
||||
private final Iterator<String> it = getAttributeNames();
|
||||
|
||||
private String currentKey;
|
||||
|
||||
public void remove() {
|
||||
if (currentKey == null) {
|
||||
throw new NoSuchElementException("You must call next() at least once");
|
||||
}
|
||||
StringKeyedMapAdapter.this.remove(currentKey);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return it.hasNext();
|
||||
}
|
||||
|
||||
protected String nextKey() {
|
||||
return currentKey = it.next();
|
||||
}
|
||||
}
|
||||
|
||||
private class KeyIterator extends AbstractKeyIterator implements Iterator<String> {
|
||||
public String next() {
|
||||
return nextKey();
|
||||
}
|
||||
}
|
||||
|
||||
private class Values extends AbstractSet<V> {
|
||||
public Iterator<V> iterator() {
|
||||
return new ValuesIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return StringKeyedMapAdapter.this.containsValue(o);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
for (Iterator<V> it = iterator(); it.hasNext();) {
|
||||
if (o.equals(it.next())) {
|
||||
it.remove();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private class ValuesIterator extends AbstractKeyIterator implements Iterator<V> {
|
||||
public V next() {
|
||||
return StringKeyedMapAdapter.this.get(nextKey());
|
||||
}
|
||||
}
|
||||
|
||||
private class EntrySet extends AbstractSet<Entry<String, V>> {
|
||||
public Iterator<Entry<String, V>> iterator() {
|
||||
return new EntryIterator();
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
Entry<String, V> entry = getAsEntry(o);
|
||||
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
|
||||
return false;
|
||||
}
|
||||
V valueFromThisMap = StringKeyedMapAdapter.this.get(entry.getKey());
|
||||
return entry.getValue().equals(valueFromThisMap);
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
Entry<String, V> entry = getAsEntry(o);
|
||||
if (entry == null || entry.getKey() == null || entry.getValue() == null) {
|
||||
return false;
|
||||
}
|
||||
V valueFromThisMap = StringKeyedMapAdapter.this.get(entry.getKey());
|
||||
if (!entry.getValue().equals(valueFromThisMap)) {
|
||||
return false;
|
||||
}
|
||||
return StringKeyedMapAdapter.this.remove(entry.getKey()) != null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Entry<String, V> getAsEntry(Object o) {
|
||||
if (o instanceof Entry) {
|
||||
return (Entry<String, V>) o;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class EntryIterator extends AbstractKeyIterator implements Iterator<Entry<String, V>> {
|
||||
public Entry<String, V> next() {
|
||||
return new EntrySetEntry(nextKey());
|
||||
}
|
||||
}
|
||||
|
||||
private class EntrySetEntry implements Entry<String, V> {
|
||||
private final String currentKey;
|
||||
|
||||
public EntrySetEntry(String currentKey) {
|
||||
this.currentKey = currentKey;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return currentKey;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return StringKeyedMapAdapter.this.get(currentKey);
|
||||
}
|
||||
|
||||
public V setValue(V value) {
|
||||
return StringKeyedMapAdapter.this.put(currentKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.convert.service;
|
||||
|
||||
import org.springframework.binding.convert.converters.Converter;
|
||||
|
||||
/**
|
||||
* Package private converter that is a "no op".
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
class NoOpConverter implements Converter {
|
||||
|
||||
private Class<?> sourceClass;
|
||||
|
||||
private Class<?> targetClass;
|
||||
|
||||
/**
|
||||
* Create a "no op" converter from given source to given target class.
|
||||
*/
|
||||
public NoOpConverter(Class<?> sourceClass, Class<?> targetClass) {
|
||||
this.sourceClass = sourceClass;
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
public Class<?> getSourceClass() {
|
||||
return sourceClass;
|
||||
}
|
||||
|
||||
public Class<?> getTargetClass() {
|
||||
return targetClass;
|
||||
}
|
||||
|
||||
public Object convertSourceToTargetClass(Object source, Class<?> targetClass) {
|
||||
return source;
|
||||
}
|
||||
|
||||
public boolean isTwoWay() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object convertTargetToSourceClass(Object target, Class<?> sourceClass) {
|
||||
return target;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.convert.service;
|
||||
|
||||
import org.springframework.binding.convert.converters.Converter;
|
||||
|
||||
/**
|
||||
* Package private converter that is a "no op".
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
class NoOpConverter implements Converter {
|
||||
|
||||
private Class<?> sourceClass;
|
||||
|
||||
private Class<?> targetClass;
|
||||
|
||||
/**
|
||||
* Create a "no op" converter from given source to given target class.
|
||||
*/
|
||||
public NoOpConverter(Class<?> sourceClass, Class<?> targetClass) {
|
||||
this.sourceClass = sourceClass;
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
public Class<?> getSourceClass() {
|
||||
return sourceClass;
|
||||
}
|
||||
|
||||
public Class<?> getTargetClass() {
|
||||
return targetClass;
|
||||
}
|
||||
|
||||
public Object convertSourceToTargetClass(Object source, Class<?> targetClass) {
|
||||
return source;
|
||||
}
|
||||
|
||||
public boolean isTwoWay() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object convertTargetToSourceClass(Object target, Class<?> sourceClass) {
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A settable expression that adds non-null values to a collection.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class CollectionAddingExpression implements Expression {
|
||||
|
||||
/**
|
||||
* The expression that resolves a mutable collection reference.
|
||||
*/
|
||||
private Expression collectionExpression;
|
||||
|
||||
/**
|
||||
* Creates a collection adding property expression.
|
||||
* @param collectionExpression the collection expression
|
||||
*/
|
||||
public CollectionAddingExpression(Expression collectionExpression) {
|
||||
this.collectionExpression = collectionExpression;
|
||||
}
|
||||
|
||||
public Object getValue(Object context) throws EvaluationException {
|
||||
return collectionExpression.getValue(context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setValue(Object context, Object value) throws EvaluationException {
|
||||
Object result = getValue(context);
|
||||
if (result == null) {
|
||||
throw new EvaluationException(context.getClass(), collectionExpression.getExpressionString(),
|
||||
"Unable to access collection value for expression '" + collectionExpression.getExpressionString()
|
||||
+ "'", new IllegalStateException(
|
||||
"The collection expression evaluated to a [null] reference"));
|
||||
}
|
||||
Assert.isInstanceOf(Collection.class, result, "Not a collection: ");
|
||||
if (value != null) {
|
||||
// add the value to the collection
|
||||
((Collection<Object>) result).add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Class<?> getValueType(Object context) {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
public String getExpressionString() {
|
||||
return collectionExpression.getExpressionString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("collectionExpression", collectionExpression).toString();
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A settable expression that adds non-null values to a collection.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class CollectionAddingExpression implements Expression {
|
||||
|
||||
/**
|
||||
* The expression that resolves a mutable collection reference.
|
||||
*/
|
||||
private Expression collectionExpression;
|
||||
|
||||
/**
|
||||
* Creates a collection adding property expression.
|
||||
* @param collectionExpression the collection expression
|
||||
*/
|
||||
public CollectionAddingExpression(Expression collectionExpression) {
|
||||
this.collectionExpression = collectionExpression;
|
||||
}
|
||||
|
||||
public Object getValue(Object context) throws EvaluationException {
|
||||
return collectionExpression.getValue(context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setValue(Object context, Object value) throws EvaluationException {
|
||||
Object result = getValue(context);
|
||||
if (result == null) {
|
||||
throw new EvaluationException(context.getClass(), collectionExpression.getExpressionString(),
|
||||
"Unable to access collection value for expression '" + collectionExpression.getExpressionString()
|
||||
+ "'", new IllegalStateException(
|
||||
"The collection expression evaluated to a [null] reference"));
|
||||
}
|
||||
Assert.isInstanceOf(Collection.class, result, "Not a collection: ");
|
||||
if (value != null) {
|
||||
// add the value to the collection
|
||||
((Collection<Object>) result).add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Class<?> getValueType(Object context) {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
public String getExpressionString() {
|
||||
return collectionExpression.getExpressionString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("collectionExpression", collectionExpression).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
/*
|
||||
* Copyright 2004-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.convert.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.binding.convert.ConversionExecutionException;
|
||||
import org.springframework.binding.convert.converters.StringToDate;
|
||||
|
||||
public class StaticConversionExecutorImplTests {
|
||||
|
||||
private StaticConversionExecutor conversionExecutor;
|
||||
|
||||
/*
|
||||
* Copyright 2004-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.convert.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.binding.convert.ConversionExecutionException;
|
||||
import org.springframework.binding.convert.converters.StringToDate;
|
||||
|
||||
public class StaticConversionExecutorImplTests {
|
||||
|
||||
private StaticConversionExecutor conversionExecutor;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
StringToDate stringToDate = new StringToDate();
|
||||
conversionExecutor = new StaticConversionExecutor(String.class, Date.class, stringToDate);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
StringToDate stringToDate = new StringToDate();
|
||||
conversionExecutor = new StaticConversionExecutor(String.class, Date.class, stringToDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeConversion() {
|
||||
assertTrue(conversionExecutor.execute("2008-10-10").getClass().equals(Date.class));
|
||||
}
|
||||
|
||||
public void testTypeConversion() {
|
||||
assertTrue(conversionExecutor.execute("2008-10-10").getClass().equals(Date.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssignmentCompatibleTypeConversion() {
|
||||
java.sql.Date date = new java.sql.Date(123L);
|
||||
try {
|
||||
assertSame(date, conversionExecutor.execute(date));
|
||||
fail("Should have failed");
|
||||
} catch (ConversionExecutionException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testAssignmentCompatibleTypeConversion() {
|
||||
java.sql.Date date = new java.sql.Date(123L);
|
||||
try {
|
||||
assertSame(date, conversionExecutor.execute(date));
|
||||
fail("Should have failed");
|
||||
} catch (ConversionExecutionException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertNull() {
|
||||
assertNull(conversionExecutor.execute(null));
|
||||
}
|
||||
|
||||
public void testConvertNull() {
|
||||
assertNull(conversionExecutor.execute(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIllegalType() {
|
||||
try {
|
||||
conversionExecutor.execute(new StringBuilder());
|
||||
fail();
|
||||
} catch (ConversionExecutionException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
public void testIllegalType() {
|
||||
try {
|
||||
conversionExecutor.execute(new StringBuilder());
|
||||
fail();
|
||||
} catch (ConversionExecutionException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
/*
|
||||
* Copyright 2004-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for {@link MethodInvocationException}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
*/
|
||||
public class MethodInvocationExceptionTests {
|
||||
|
||||
/*
|
||||
* Copyright 2004-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test case for {@link MethodInvocationException}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
*/
|
||||
public class MethodInvocationExceptionTests {
|
||||
|
||||
@Test
|
||||
public void testGetTargetException() {
|
||||
// runtime exception
|
||||
IllegalArgumentException iae = new IllegalArgumentException("test");
|
||||
MethodInvocationException ex = testException(iae);
|
||||
assertSame(iae, ex.getTargetException());
|
||||
|
||||
// exception
|
||||
IOException ioe = new IOException("test");
|
||||
ex = testException(ioe);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
|
||||
// nested
|
||||
InvocationTargetException ite = new InvocationTargetException(ioe);
|
||||
ex = testException(ite);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
|
||||
// deep nesting
|
||||
ite = new InvocationTargetException(new InvocationTargetException(ioe));
|
||||
ex = testException(ite);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private MethodInvocationException testException(Throwable cause) {
|
||||
return new MethodInvocationException(new MethodSignature("test"), null, cause);
|
||||
}
|
||||
}
|
||||
public void testGetTargetException() {
|
||||
// runtime exception
|
||||
IllegalArgumentException iae = new IllegalArgumentException("test");
|
||||
MethodInvocationException ex = testException(iae);
|
||||
assertSame(iae, ex.getTargetException());
|
||||
|
||||
// exception
|
||||
IOException ioe = new IOException("test");
|
||||
ex = testException(ioe);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
|
||||
// nested
|
||||
InvocationTargetException ite = new InvocationTargetException(ioe);
|
||||
ex = testException(ite);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
|
||||
// deep nesting
|
||||
ite = new InvocationTargetException(new InvocationTargetException(ioe));
|
||||
ex = testException(ite);
|
||||
assertSame(ioe, ex.getTargetException());
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private MethodInvocationException testException(Throwable cause) {
|
||||
return new MethodInvocationException(new MethodSignature("test"), null, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,98 +1,98 @@
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.binding.expression.support.StaticExpression;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.binding.method.MethodInvoker}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class MethodInvokerTests {
|
||||
|
||||
private MethodInvoker methodInvoker;
|
||||
|
||||
/*
|
||||
* Copyright 2004-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.binding.expression.support.StaticExpression;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.binding.method.MethodInvoker}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class MethodInvokerTests {
|
||||
|
||||
private MethodInvoker methodInvoker;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
this.methodInvoker = new MethodInvoker();
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
this.methodInvoker = new MethodInvoker();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvocationTargetException() {
|
||||
try {
|
||||
methodInvoker.invoke(new MethodSignature("test"), new TestObject(), null);
|
||||
fail();
|
||||
} catch (MethodInvocationException e) {
|
||||
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
|
||||
assertEquals("just testing", e.getTargetException().getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvocationTargetException() {
|
||||
try {
|
||||
methodInvoker.invoke(new MethodSignature("test"), new TestObject(), null);
|
||||
fail();
|
||||
} catch (MethodInvocationException e) {
|
||||
assertTrue(e.getTargetException() instanceof IllegalArgumentException);
|
||||
assertEquals("just testing", e.getTargetException().getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidMethod() {
|
||||
try {
|
||||
methodInvoker.invoke(new MethodSignature("bogus"), new TestObject(), null);
|
||||
fail();
|
||||
} catch (MethodInvocationException e) {
|
||||
assertTrue(e.getTargetException() instanceof InvalidMethodKeyException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidMethod() {
|
||||
try {
|
||||
methodInvoker.invoke(new MethodSignature("bogus"), new TestObject(), null);
|
||||
fail();
|
||||
} catch (MethodInvocationException e) {
|
||||
assertTrue(e.getTargetException() instanceof InvalidMethodKeyException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanArg() {
|
||||
Parameters parameters = new Parameters();
|
||||
Bean bean = new Bean();
|
||||
parameters.add(new Parameter(Bean.class, new StaticExpression(bean)));
|
||||
MethodSignature method = new MethodSignature("testBeanArg", parameters);
|
||||
assertSame(bean, methodInvoker.invoke(method, new TestObject(), null));
|
||||
}
|
||||
|
||||
public void testBeanArg() {
|
||||
Parameters parameters = new Parameters();
|
||||
Bean bean = new Bean();
|
||||
parameters.add(new Parameter(Bean.class, new StaticExpression(bean)));
|
||||
MethodSignature method = new MethodSignature("testBeanArg", parameters);
|
||||
assertSame(bean, methodInvoker.invoke(method, new TestObject(), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveArg() {
|
||||
Parameters parameters = new Parameters();
|
||||
parameters.add(new Parameter(Boolean.class, new StaticExpression(true)));
|
||||
MethodSignature method = new MethodSignature("testPrimitiveArg", parameters);
|
||||
assertEquals(Boolean.TRUE, methodInvoker.invoke(method, new TestObject(), null));
|
||||
}
|
||||
|
||||
static class TestObject {
|
||||
|
||||
public void test() {
|
||||
throw new IllegalArgumentException("just testing");
|
||||
}
|
||||
|
||||
public Object testBeanArg(Bean bean) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public boolean testPrimitiveArg(boolean primitive) {
|
||||
return primitive;
|
||||
}
|
||||
}
|
||||
|
||||
static class Bean {
|
||||
String value;
|
||||
}
|
||||
}
|
||||
public void testPrimitiveArg() {
|
||||
Parameters parameters = new Parameters();
|
||||
parameters.add(new Parameter(Boolean.class, new StaticExpression(true)));
|
||||
MethodSignature method = new MethodSignature("testPrimitiveArg", parameters);
|
||||
assertEquals(Boolean.TRUE, methodInvoker.invoke(method, new TestObject(), null));
|
||||
}
|
||||
|
||||
static class TestObject {
|
||||
|
||||
public void test() {
|
||||
throw new IllegalArgumentException("just testing");
|
||||
}
|
||||
|
||||
public Object testBeanArg(Bean bean) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public boolean testPrimitiveArg(boolean primitive) {
|
||||
return primitive;
|
||||
}
|
||||
}
|
||||
|
||||
static class Bean {
|
||||
String value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user