mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 11:35:29 -04:00
TMS-XXXX: New map adapters for servlet context and request attributes + minor API tweaks.
This commit is contained in:
parent
dd0f382d3c
commit
1ffe694538
@ -1,7 +1,5 @@
|
|||||||
package com.twelvemonkeys.servlet;
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
import com.twelvemonkeys.util.CollectionUtil;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,88 +9,53 @@ import java.util.*;
|
|||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
* @version $Id: AbstractServletMapAdapter.java#1 $
|
* @version $Id: AbstractServletMapAdapter.java#1 $
|
||||||
*/
|
*/
|
||||||
abstract class AbstractServletMapAdapter extends AbstractMap<String, List<String>> {
|
abstract class AbstractServletMapAdapter<T> extends AbstractMap<String, T> {
|
||||||
// TODO: This map is now a little too lazy.. Should cache entries too (instead?) !
|
// TODO: This map is now a little too lazy.. Should cache entries!
|
||||||
|
private transient Set<Entry<String, T>> entries;
|
||||||
private final static List<String> NULL_LIST = new ArrayList<String>();
|
|
||||||
|
|
||||||
private transient Map<String, List<String>> cache = new HashMap<String, List<String>>();
|
|
||||||
private transient int size = -1;
|
|
||||||
private transient AbstractSet<Entry<String, List<String>>> entries;
|
|
||||||
|
|
||||||
protected abstract Iterator<String> keysImpl();
|
protected abstract Iterator<String> keysImpl();
|
||||||
|
|
||||||
protected abstract Iterator<String> valuesImpl(String pName);
|
protected abstract T valueImpl(String pName);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> get(final Object pKey) {
|
public T get(final Object pKey) {
|
||||||
if (pKey instanceof String) {
|
if (pKey instanceof String) {
|
||||||
return getValues((String) pKey);
|
return valueImpl((String) pKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getValues(final String pName) {
|
|
||||||
List<String> values = cache.get(pName);
|
|
||||||
|
|
||||||
if (values == null) {
|
|
||||||
//noinspection unchecked
|
|
||||||
Iterator<String> headers = valuesImpl(pName);
|
|
||||||
|
|
||||||
if (headers == null) {
|
|
||||||
cache.put(pName, NULL_LIST);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
values = toList(headers);
|
|
||||||
cache.put(pName, values);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return values == NULL_LIST ? null : values;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> toList(final Iterator<String> pValues) {
|
|
||||||
List<String> list = new ArrayList<String>();
|
|
||||||
CollectionUtil.addAll(list, pValues);
|
|
||||||
return Collections.unmodifiableList(list);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
if (size == -1) {
|
// Avoid creating expensive entry set for computing size
|
||||||
computeSize();
|
int size = 0;
|
||||||
|
|
||||||
|
for (Iterator<String> names = keysImpl(); names.hasNext(); names.next()) {
|
||||||
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void computeSize() {
|
public Set<Entry<String, T>> entrySet() {
|
||||||
size = 0;
|
|
||||||
|
|
||||||
for (Iterator<String> names = keysImpl(); names.hasNext(); names.next()) {
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<Entry<String, List<String>>> entrySet() {
|
|
||||||
if (entries == null) {
|
if (entries == null) {
|
||||||
entries = new AbstractSet<Entry<String, List<String>>>() {
|
entries = new AbstractSet<Entry<String, T>>() {
|
||||||
public Iterator<Entry<String, List<String>>> iterator() {
|
public Iterator<Entry<String, T>> iterator() {
|
||||||
return new Iterator<Entry<String, List<String>>>() {
|
return new Iterator<Entry<String, T>>() {
|
||||||
Iterator<String> headerNames = keysImpl();
|
Iterator<String> keys = keysImpl();
|
||||||
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return headerNames.hasNext();
|
return keys.hasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entry<String, List<String>> next() {
|
public Entry<String, T> next() {
|
||||||
// TODO: Replace with cached lookup
|
// TODO: Replace with cached lookup
|
||||||
return new HeaderEntry(headerNames.next());
|
return new HeaderEntry(keys.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove() {
|
public void remove() {
|
||||||
throw new UnsupportedOperationException();
|
keys.remove();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -106,34 +69,35 @@ abstract class AbstractServletMapAdapter extends AbstractMap<String, List<String
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class HeaderEntry implements Entry<String, List<String>> {
|
private class HeaderEntry implements Entry<String, T> {
|
||||||
String headerName;
|
final String key;
|
||||||
|
|
||||||
public HeaderEntry(String pHeaderName) {
|
public HeaderEntry(final String pKey) {
|
||||||
headerName = pHeaderName;
|
key = pKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getKey() {
|
||||||
return headerName;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getValue() {
|
public T getValue() {
|
||||||
return get(headerName);
|
return get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> setValue(List<String> pValue) {
|
public T setValue(final T pValue) {
|
||||||
throw new UnsupportedOperationException();
|
// Write-through if supported
|
||||||
|
return put(key, pValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
List<String> value;
|
T value = getValue();
|
||||||
return (headerName == null ? 0 : headerName.hashCode()) ^
|
return (key == null ? 0 : key.hashCode()) ^
|
||||||
((value = getValue()) == null ? 0 : value.hashCode());
|
(value == null ? 0 : value.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object pOther) {
|
public boolean equals(final Object pOther) {
|
||||||
if (pOther == this) {
|
if (pOther == this) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ServletAttributesMap
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: ServletAttributesMap.java,v 1.0 01.03.13 10:34 haraldk Exp$
|
||||||
|
*/
|
||||||
|
class ServletAttributesMapAdapter extends AbstractServletMapAdapter<Object> {
|
||||||
|
private final ServletContext context;
|
||||||
|
private final ServletRequest request;
|
||||||
|
|
||||||
|
ServletAttributesMapAdapter(final ServletContext context) {
|
||||||
|
this(notNull(context), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
ServletAttributesMapAdapter(final ServletRequest request) {
|
||||||
|
this(null, notNull(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServletAttributesMapAdapter(final ServletContext context, final ServletRequest request) {
|
||||||
|
this.context = context;
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private Enumeration<String> getAttributeNames() {
|
||||||
|
return context != null ? context.getAttributeNames() : request.getAttributeNames();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getAttribute(final String name) {
|
||||||
|
return context != null ? context.getAttribute(name) : request.getAttribute(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object setAttribute(String name, Object value) {
|
||||||
|
Object oldValue = getAttribute(name);
|
||||||
|
|
||||||
|
if (context != null) {
|
||||||
|
context.setAttribute(name, value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
request.setAttribute(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object removeAttribute(String name) {
|
||||||
|
Object oldValue = getAttribute(name);
|
||||||
|
|
||||||
|
if (context != null) {
|
||||||
|
context.removeAttribute(name);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
request.removeAttribute(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Iterator<String> keysImpl() {
|
||||||
|
final Enumeration<String> keys = getAttributeNames();
|
||||||
|
return new Iterator<String>() {
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return keys.hasMoreElements();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String next() {
|
||||||
|
key = keys.nextElement();
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
// Support removal of attribute through key iterator
|
||||||
|
removeAttribute(key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object valueImpl(String pName) {
|
||||||
|
return getAttribute(pName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object put(String key, Object value) {
|
||||||
|
return setAttribute(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object remove(Object key) {
|
||||||
|
return key instanceof String ? removeAttribute((String) key) : null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package com.twelvemonkeys.servlet;
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
import com.twelvemonkeys.lang.Validate;
|
|
||||||
import com.twelvemonkeys.util.CollectionUtil;
|
import com.twelvemonkeys.util.CollectionUtil;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.Enumeration;
|
import java.util.*;
|
||||||
import java.util.Iterator;
|
|
||||||
|
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ServletHeadersMapAdapter
|
* ServletHeadersMapAdapter
|
||||||
@ -14,24 +14,29 @@ import java.util.Iterator;
|
|||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
* @version $Id: ServletHeadersMapAdapter.java#1 $
|
* @version $Id: ServletHeadersMapAdapter.java#1 $
|
||||||
*/
|
*/
|
||||||
class ServletHeadersMapAdapter extends AbstractServletMapAdapter {
|
class ServletHeadersMapAdapter extends AbstractServletMapAdapter<List<String>> {
|
||||||
|
|
||||||
protected final HttpServletRequest request;
|
protected final HttpServletRequest request;
|
||||||
|
|
||||||
public ServletHeadersMapAdapter(HttpServletRequest pRequest) {
|
public ServletHeadersMapAdapter(final HttpServletRequest pRequest) {
|
||||||
request = Validate.notNull(pRequest, "request");
|
request = notNull(pRequest, "request");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Iterator<String> valuesImpl(String pName) {
|
protected List<String> valueImpl(final String pName) {
|
||||||
//noinspection unchecked
|
@SuppressWarnings("unchecked")
|
||||||
Enumeration<String> headers = request.getHeaders(pName);
|
Enumeration<String> headers = request.getHeaders(pName);
|
||||||
return headers == null ? null : CollectionUtil.iterator(headers);
|
return headers == null ? null : toList(CollectionUtil.iterator(headers));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> toList(final Iterator<String> pValues) {
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
CollectionUtil.addAll(list, pValues);
|
||||||
|
return Collections.unmodifiableList(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Iterator<String> keysImpl() {
|
protected Iterator<String> keysImpl() {
|
||||||
//noinspection unchecked
|
@SuppressWarnings("unchecked")
|
||||||
Enumeration<String> headerNames = request.getHeaderNames();
|
Enumeration<String> headerNames = request.getHeaderNames();
|
||||||
return headerNames == null ? null : CollectionUtil.iterator(headerNames);
|
return headerNames == null ? null : CollectionUtil.iterator(headerNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package com.twelvemonkeys.servlet;
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
import com.twelvemonkeys.lang.Validate;
|
|
||||||
import com.twelvemonkeys.util.CollectionUtil;
|
import com.twelvemonkeys.util.CollectionUtil;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ServletParametersMapAdapter
|
* ServletParametersMapAdapter
|
||||||
@ -14,23 +17,23 @@ import java.util.Iterator;
|
|||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
* @version $Id: ServletParametersMapAdapter.java#1 $
|
* @version $Id: ServletParametersMapAdapter.java#1 $
|
||||||
*/
|
*/
|
||||||
class ServletParametersMapAdapter extends AbstractServletMapAdapter {
|
class ServletParametersMapAdapter extends AbstractServletMapAdapter<List<String>> {
|
||||||
|
// TODO: Be able to piggyback on HttpServletRequest.getParameterMap when available?
|
||||||
|
|
||||||
protected final HttpServletRequest request;
|
protected final ServletRequest request;
|
||||||
|
|
||||||
public ServletParametersMapAdapter(HttpServletRequest pRequest) {
|
public ServletParametersMapAdapter(final ServletRequest pRequest) {
|
||||||
request = Validate.notNull(pRequest, "request");
|
request = notNull(pRequest, "request");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Iterator<String> valuesImpl(String pName) {
|
protected List<String> valueImpl(String pName) {
|
||||||
String[] values = request.getParameterValues(pName);
|
String[] values = request.getParameterValues(pName);
|
||||||
return values == null ? null : CollectionUtil.iterator(values);
|
return values == null ? null : Arrays.asList(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Iterator<String> keysImpl() {
|
protected Iterator<String> keysImpl() {
|
||||||
//noinspection unchecked
|
@SuppressWarnings("unchecked")
|
||||||
Enumeration<String> names = request.getParameterNames();
|
Enumeration<String> names = request.getParameterNames();
|
||||||
return names == null ? null : CollectionUtil.iterator(names);
|
return names == null ? null : CollectionUtil.iterator(names);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -50,7 +50,7 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* Various servlet related helper methods.
|
* Various servlet related helper methods.
|
||||||
*
|
*
|
||||||
* @author Harald Kuhr
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @author Eirik Torske
|
* @author Eirik Torske
|
||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
* @version $Id: ServletUtil.java#3 $
|
* @version $Id: ServletUtil.java#3 $
|
||||||
@ -544,7 +544,7 @@ public final class ServletUtil {
|
|||||||
/**
|
/**
|
||||||
* Returns a {@code URL} containing the real path for a given virtual
|
* Returns a {@code URL} containing the real path for a given virtual
|
||||||
* path, on URL form.
|
* path, on URL form.
|
||||||
* Note that this mehtod will return {@code null} for all the same reasons
|
* Note that this method will return {@code null} for all the same reasons
|
||||||
* as {@code ServletContext.getRealPath(java.lang.String)} does.
|
* as {@code ServletContext.getRealPath(java.lang.String)} does.
|
||||||
*
|
*
|
||||||
* @param pContext the servlet context
|
* @param pContext the servlet context
|
||||||
@ -634,13 +634,30 @@ public final class ServletUtil {
|
|||||||
return new ServletConfigMapAdapter(pContext);
|
return new ServletConfigMapAdapter(pContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO?
|
/**
|
||||||
// public static Map<String, ?> attributesAsMap(final ServletContext pContext) {
|
* Creates an <em>modifiable</em> {@code Map} view of the given
|
||||||
// }
|
* {@code ServletContext}s attributes.
|
||||||
//
|
*
|
||||||
// public static Map<String, ?> attributesAsMap(final ServletRequest pRequest) {
|
* @param pContext the servlet context
|
||||||
// }
|
* @return a {@code Map} view of the attributes
|
||||||
//
|
* @throws IllegalArgumentException if {@code pContext} is {@code null}
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> attributesAsMap(final ServletContext pContext) {
|
||||||
|
return new ServletAttributesMapAdapter(pContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an <em>modifiable</em> {@code Map} view of the given
|
||||||
|
* {@code ServletRequest}s attributes.
|
||||||
|
*
|
||||||
|
* @param pRequest the servlet request
|
||||||
|
* @return a {@code Map} view of the attributes
|
||||||
|
* @throws IllegalArgumentException if {@code pContext} is {@code null}
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> attributesAsMap(final ServletRequest pRequest) {
|
||||||
|
return new ServletAttributesMapAdapter(pRequest);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an unmodifiable {@code Map} view of the given
|
* Creates an unmodifiable {@code Map} view of the given
|
||||||
* {@code HttpServletRequest}s request parameters.
|
* {@code HttpServletRequest}s request parameters.
|
||||||
@ -649,7 +666,7 @@ public final class ServletUtil {
|
|||||||
* @return a {@code Map} view of the request parameters
|
* @return a {@code Map} view of the request parameters
|
||||||
* @throws IllegalArgumentException if {@code pRequest} is {@code null}
|
* @throws IllegalArgumentException if {@code pRequest} is {@code null}
|
||||||
*/
|
*/
|
||||||
public static Map<String, List<String>> parametersAsMap(final HttpServletRequest pRequest) {
|
public static Map<String, List<String>> parametersAsMap(final ServletRequest pRequest) {
|
||||||
return new ServletParametersMapAdapter(pRequest);
|
return new ServletParametersMapAdapter(pRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
|
import com.twelvemonkeys.util.MapAbstractTestCase;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ServletConfigMapAdapterTestCase
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @version $Id: ServletAttributesMapAdapterTestCase.java#1 $
|
||||||
|
*/
|
||||||
|
public class ServletAttributesMapAdapterContextTest extends MapAbstractTestCase {
|
||||||
|
private static final String ATTRIB_VALUE_ETAG = "\"1234567890abcdef\"";
|
||||||
|
private static final Date ATTRIB_VALUE_DATE = new Date();
|
||||||
|
private static final List<Integer> ATTRIB_VALUE_FOO = Arrays.asList(1, 2);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTestSerialization() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAllowNullKey() {
|
||||||
|
return false; // Makes no sense...
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAllowNullValue() {
|
||||||
|
return false; // Should be allowed, but the tests don't handle the put(foo, null) == remove(foo) semantics
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map makeEmptyMap() {
|
||||||
|
MockServletContextImpl context = mock(MockServletContextImpl.class, Mockito.CALLS_REAL_METHODS);
|
||||||
|
context.attributes = createAttributes(false);
|
||||||
|
|
||||||
|
return new ServletAttributesMapAdapter(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map makeFullMap() {
|
||||||
|
MockServletContextImpl context = mock(MockServletContextImpl.class, Mockito.CALLS_REAL_METHODS);
|
||||||
|
context.attributes = createAttributes(true);
|
||||||
|
|
||||||
|
return new ServletAttributesMapAdapter(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> createAttributes(boolean initialValues) {
|
||||||
|
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
|
||||||
|
|
||||||
|
if (initialValues) {
|
||||||
|
String[] sampleKeys = (String[]) getSampleKeys();
|
||||||
|
for (int i = 0; i < sampleKeys.length; i++) {
|
||||||
|
map.put(sampleKeys[i], getSampleValues()[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getSampleKeys() {
|
||||||
|
return new String[] {"Date", "ETag", "X-Foo"};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getSampleValues() {
|
||||||
|
return new Object[] {ATTRIB_VALUE_DATE, ATTRIB_VALUE_ETAG, ATTRIB_VALUE_FOO};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getNewSampleValues() {
|
||||||
|
// Needs to be same length but different values
|
||||||
|
return new Object[] {new Date(-1l), "foo/bar", Arrays.asList(2, 3, 4)};
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void testMapPutNullValue() {
|
||||||
|
// Special null semantics
|
||||||
|
resetFull();
|
||||||
|
|
||||||
|
int size = map.size();
|
||||||
|
String key = getClass().getName() + ".someNewKey";
|
||||||
|
map.put(key, null);
|
||||||
|
assertEquals(size, map.size());
|
||||||
|
assertFalse(map.containsKey(key));
|
||||||
|
|
||||||
|
map.put(getSampleKeys()[0], null);
|
||||||
|
assertEquals(size - 1, map.size());
|
||||||
|
assertFalse(map.containsKey(getSampleKeys()[0]));
|
||||||
|
|
||||||
|
map.remove(getSampleKeys()[1]);
|
||||||
|
assertEquals(size - 2, map.size());
|
||||||
|
assertFalse(map.containsKey(getSampleKeys()[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static abstract class MockServletContextImpl implements ServletContext {
|
||||||
|
Map<String, Object> attributes;
|
||||||
|
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
return attributes.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumeration getAttributeNames() {
|
||||||
|
return Collections.enumeration(attributes.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttribute(String name, Object o) {
|
||||||
|
if (o == null) {
|
||||||
|
attributes.remove(name);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.put(name, o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAttribute(String name) {
|
||||||
|
attributes.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.servlet;
|
||||||
|
|
||||||
|
import com.twelvemonkeys.util.MapAbstractTestCase;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ServletConfigMapAdapterTestCase
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @version $Id: ServletAttributesMapAdapterTestCase.java#1 $
|
||||||
|
*/
|
||||||
|
public class ServletAttributesMapAdapterRequestTest extends MapAbstractTestCase {
|
||||||
|
private static final String ATTRIB_VALUE_ETAG = "\"1234567890abcdef\"";
|
||||||
|
private static final Date ATTRIB_VALUE_DATE = new Date();
|
||||||
|
private static final List<Integer> ATTRIB_VALUE_FOO = Arrays.asList(1, 2);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTestSerialization() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAllowNullKey() {
|
||||||
|
return false; // Makes no sense...
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAllowNullValue() {
|
||||||
|
return false; // Should be allowed, but the tests don't handle the put(foo, null) == remove(foo) semantics
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map makeEmptyMap() {
|
||||||
|
MockServletRequestImpl request = mock(MockServletRequestImpl.class, Mockito.CALLS_REAL_METHODS);
|
||||||
|
request.attributes = createAttributes(false);
|
||||||
|
|
||||||
|
return new ServletAttributesMapAdapter(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map makeFullMap() {
|
||||||
|
MockServletRequestImpl request = mock(MockServletRequestImpl.class, Mockito.CALLS_REAL_METHODS);
|
||||||
|
request.attributes = createAttributes(true);
|
||||||
|
|
||||||
|
return new ServletAttributesMapAdapter(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> createAttributes(boolean initialValues) {
|
||||||
|
Map<String, Object> map = new ConcurrentHashMap<String, Object>();
|
||||||
|
|
||||||
|
if (initialValues) {
|
||||||
|
String[] sampleKeys = (String[]) getSampleKeys();
|
||||||
|
for (int i = 0; i < sampleKeys.length; i++) {
|
||||||
|
map.put(sampleKeys[i], getSampleValues()[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getSampleKeys() {
|
||||||
|
return new String[] {"Date", "ETag", "X-Foo"};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getSampleValues() {
|
||||||
|
return new Object[] {ATTRIB_VALUE_DATE, ATTRIB_VALUE_ETAG, ATTRIB_VALUE_FOO};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getNewSampleValues() {
|
||||||
|
// Needs to be same length but different values
|
||||||
|
return new Object[] {new Date(-1l), "foo/bar", Arrays.asList(2, 3, 4)};
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void testMapPutNullValue() {
|
||||||
|
// Special null semantics
|
||||||
|
resetFull();
|
||||||
|
|
||||||
|
int size = map.size();
|
||||||
|
String key = getClass().getName() + ".someNewKey";
|
||||||
|
map.put(key, null);
|
||||||
|
assertEquals(size, map.size());
|
||||||
|
assertFalse(map.containsKey(key));
|
||||||
|
|
||||||
|
map.put(getSampleKeys()[0], null);
|
||||||
|
assertEquals(size - 1, map.size());
|
||||||
|
assertFalse(map.containsKey(getSampleKeys()[0]));
|
||||||
|
|
||||||
|
map.remove(getSampleKeys()[1]);
|
||||||
|
assertEquals(size - 2, map.size());
|
||||||
|
assertFalse(map.containsKey(getSampleKeys()[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static abstract class MockServletRequestImpl implements ServletRequest {
|
||||||
|
Map<String, Object> attributes;
|
||||||
|
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
return attributes.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumeration getAttributeNames() {
|
||||||
|
return Collections.enumeration(attributes.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttribute(String name, Object o) {
|
||||||
|
if (o == null) {
|
||||||
|
attributes.remove(name);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attributes.put(name, o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAttribute(String name) {
|
||||||
|
attributes.remove(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@ import java.net.MalformedURLException;
|
|||||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/test/java/com/twelvemonkeys/servlet/ServletConfigMapAdapterTestCase.java#3 $
|
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/test/java/com/twelvemonkeys/servlet/ServletConfigMapAdapterTestCase.java#3 $
|
||||||
*/
|
*/
|
||||||
public abstract class ServletConfigMapAdapterTestCase extends MapAbstractTestCase {
|
public abstract class ServletConfigMapAdapterTest extends MapAbstractTestCase {
|
||||||
|
|
||||||
public boolean isPutAddSupported() {
|
public boolean isPutAddSupported() {
|
||||||
return false;
|
return false;
|
||||||
@ -148,7 +148,7 @@ public abstract class ServletConfigMapAdapterTestCase extends MapAbstractTestCas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class ServletConfigMapTestCase extends ServletConfigMapAdapterTestCase {
|
public static final class ServletConfigMapTestCase extends ServletConfigMapAdapterTest {
|
||||||
|
|
||||||
public Map makeEmptyMap() {
|
public Map makeEmptyMap() {
|
||||||
ServletConfig config = new TestConfig();
|
ServletConfig config = new TestConfig();
|
||||||
@ -162,7 +162,7 @@ public abstract class ServletConfigMapAdapterTestCase extends MapAbstractTestCas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class FilterConfigMapTestCase extends ServletConfigMapAdapterTestCase {
|
public static final class FilterConfigMapTestCase extends ServletConfigMapAdapterTest {
|
||||||
|
|
||||||
public Map makeEmptyMap() {
|
public Map makeEmptyMap() {
|
||||||
FilterConfig config = new TestConfig();
|
FilterConfig config = new TestConfig();
|
||||||
@ -176,7 +176,7 @@ public abstract class ServletConfigMapAdapterTestCase extends MapAbstractTestCas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class ServletContextMapTestCase extends ServletConfigMapAdapterTestCase {
|
public static final class ServletContextMapTestCase extends ServletConfigMapAdapterTest {
|
||||||
|
|
||||||
public Map makeEmptyMap() {
|
public Map makeEmptyMap() {
|
||||||
ServletContext config = new TestConfig();
|
ServletContext config = new TestConfig();
|
@ -17,7 +17,7 @@ import static org.mockito.Mockito.when;
|
|||||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @version $Id: ServletHeadersMapAdapterTestCase.java#1 $
|
* @version $Id: ServletHeadersMapAdapterTestCase.java#1 $
|
||||||
*/
|
*/
|
||||||
public class ServletHeadersMapAdapterTestCase extends MapAbstractTestCase {
|
public class ServletHeadersMapAdapterTest extends MapAbstractTestCase {
|
||||||
private static final List<String> HEADER_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
|
private static final List<String> HEADER_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
|
||||||
private static final List<String> HEADER_VALUE_DATE = Arrays.asList(new Date().toString());
|
private static final List<String> HEADER_VALUE_DATE = Arrays.asList(new Date().toString());
|
||||||
private static final List<String> HEADER_VALUE_FOO = Arrays.asList("one", "two");
|
private static final List<String> HEADER_VALUE_FOO = Arrays.asList("one", "two");
|
@ -17,7 +17,7 @@ import static org.mockito.Mockito.when;
|
|||||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/test/java/com/twelvemonkeys/servlet/ServletParametersMapAdapterTestCase.java#1 $
|
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-servlet/src/test/java/com/twelvemonkeys/servlet/ServletParametersMapAdapterTestCase.java#1 $
|
||||||
*/
|
*/
|
||||||
public class ServletParametersMapAdapterTestCase extends MapAbstractTestCase {
|
public class ServletParametersMapAdapterTest extends MapAbstractTestCase {
|
||||||
private static final List<String> PARAM_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
|
private static final List<String> PARAM_VALUE_ETAG = Arrays.asList("\"1234567890abcdef\"");
|
||||||
private static final List<String> PARAM_VALUE_DATE = Arrays.asList(new Date().toString());
|
private static final List<String> PARAM_VALUE_DATE = Arrays.asList(new Date().toString());
|
||||||
private static final List<String> PARAM_VALUE_FOO = Arrays.asList("one", "two");
|
private static final List<String> PARAM_VALUE_FOO = Arrays.asList("one", "two");
|
Loading…
x
Reference in New Issue
Block a user