Author: max_shaposhnik
Date: 2009-09-10 09:37:40 -0400 (Thu, 10 Sep 2009)
New Revision: 168
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensetiveMap.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensitiveMultivaluedMap.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletRequest.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletResponse.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpSession.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockPrincipal.java
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockServletContext.java
Removed:
ws/trunk/testframework/src/main/java/org/exoplatform/testframework/
Log:
EXOJCR-152
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensetiveMap.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensetiveMap.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensetiveMap.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id: CaseInsensetiveMap.java 167 2009-09-10 13:14:45Z max_shaposhnik $
+ */
+public class CaseInsensetiveMap<T> extends java.util.HashMap<String, T>
+{
+
+ private static final long serialVersionUID = -8562529039657285360L;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean containsKey(Object key)
+ {
+ return super.containsKey(getKey(key));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public T get(Object key)
+ {
+ return super.get(getKey(key));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public T put(String key, T value)
+ {
+ return super.put(getKey(key), value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public T remove(Object key)
+ {
+ return super.remove(getKey(key));
+ }
+
+ private String getKey(Object key)
+ {
+ if (key == null)
+ {
+ return null;
+ }
+ return key.toString().toLowerCase();
+ }
+
+}
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensitiveMultivaluedMap.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensitiveMultivaluedMap.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/CaseInsensitiveMultivaluedMap.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id: CaseInsensitiveMap.java 2822 2009-08-27 14:14:41Z andrew00x $
+ */
+public class CaseInsensitiveMultivaluedMap<T> extends HashMap<String,
List<T>>
+{
+
+ private static final long serialVersionUID = 6637313979061607685L;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean containsKey(Object key)
+ {
+ return super.containsKey(getKey(key));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<T> get(Object key)
+ {
+ return getList(getKey(key));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<T> put(String key, List<T> value)
+ {
+ return super.put(getKey(key), value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<T> remove(Object key)
+ {
+ return super.remove(getKey(key));
+ }
+
+ public T getFirst(String key)
+ {
+ List<T> l = getList(key);
+ if (l.size() == 0)
+ return null;
+ return l.get(0);
+ }
+
+ private List<T> getList(String key)
+ {
+ List<T> l = super.get(getKey(key));
+ if (l == null)
+ l = new ArrayList<T>();
+ put(key, l);
+ return l;
+ }
+
+ private String getKey(Object key)
+ {
+ if (key == null)
+ {
+ return null;
+ }
+ return key.toString().toLowerCase();
+ }
+
+}
\ No newline at end of file
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletRequest.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletRequest.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletRequest.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,720 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+/**
+ * The Class MockHttpServletRequest.
+ *
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id: $
+ */
+
+@SuppressWarnings("unchecked")
+public class MockHttpServletRequest implements HttpServletRequest
+{
+
+ /** HTTP method. */
+ private String method;
+
+ /** Length. */
+ private int length;
+
+ /** Request url. */
+ private String requestURL;
+
+ /** Data. */
+ private InputStream data;
+
+ /** Headers. */
+ private CaseInsensitiveMultivaluedMap<String> headers = new
CaseInsensitiveMultivaluedMap<String>();
+
+ /** The parameters. */
+ private Map<String, List<String>> parameters = new HashMap<String,
List<String>>();
+
+ /** The session. */
+ private HttpSession session;
+
+ /** The locale. */
+ private Locale locale;
+
+ /** The secure. */
+ private boolean secure;
+
+ /** The Constant p. */
+ private static final Pattern p =
Pattern.compile("http://([^:]+?):([^/]+?)/([^/]+?)/(.*?)");
+
+ /** The attributes. */
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+
+ private Principal principal;
+
+ /**
+ * Instantiates a new mock http servlet request.
+ *
+ * @param url
+ * the url
+ * @param data
+ * the data
+ * @param length
+ * the length
+ * @param method
+ * the method
+ * @param headers
+ * the headers
+ */
+ public MockHttpServletRequest(String url, InputStream data, int length, String
method,
+ Map<String, List<String>> headers)
+ {
+ this.requestURL = url;
+ this.data = data;
+ this.length = length;
+ this.method = method;
+ if (headers != null)
+ this.headers.putAll(headers);
+ String queryString = getQueryString();
+ if (queryString != null)
+ {
+ parameters.putAll(parseQueryString(queryString));
+ }
+ session = new MockHttpSession();
+ }
+
+ /**
+ * Reset.
+ */
+ public void reset()
+ {
+ parameters = new HashMap();
+ attributes = new HashMap();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getAttribute(String name)
+ {
+ return attributes.get(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getAttributeNames()
+ {
+ return new EnumerationImpl(attributes.keySet().iterator());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getAuthType()
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getCharacterEncoding()
+ {
+ return "UTF-8";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getContentLength()
+ {
+ return length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getContentType()
+ {
+ return headers.getFirst("content-type");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getContextPath()
+ {
+ Matcher m = p.matcher(requestURL);
+ if (!m.matches())
+ throw new RuntimeException("Unable determine context path.");
+ return '/' + m.group(3);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Cookie[] getCookies()
+ {
+ return new Cookie[0];
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getDateHeader(String name)
+ {
+ if (headers.get(name) != null)
+ return Long.valueOf(headers.getFirst(name));
+ return -1L;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getHeader(String name)
+ {
+ return headers.getFirst(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getHeaderNames()
+ {
+ return new EnumerationImpl(headers.keySet().iterator());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getHeaders(String name)
+ {
+ ArrayList values = (ArrayList)headers.get(name);
+ if (values.size() > 0)
+ return new EnumerationImpl(values.iterator());
+
+ return new EnumerationImpl(Collections.EMPTY_LIST.iterator());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ServletInputStream getInputStream() throws IOException
+ {
+ return new MockServletInputStream(data);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getIntHeader(String name)
+ {
+ if (headers.get(name).size() > 0)
+ return Integer.parseInt(headers.getFirst(name));
+ return -1;
+ }
+
+ /**
+ * Gets the local addr.
+ *
+ * @return the local addr
+ */
+ public String getLocalAddr()
+ {
+ return "127.0.0.1";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Locale getLocale()
+ {
+ return Locale.US;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getLocales()
+ {
+ return null;
+ }
+
+ /**
+ * Gets the local name.
+ *
+ * @return the local name
+ */
+ public String getLocalName()
+ {
+ return "localhost";
+ }
+
+ /**
+ * Gets the local port.
+ *
+ * @return the local port
+ */
+ public int getLocalPort()
+ {
+ return 80;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getMethod()
+ {
+ return method;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getParameter(String name)
+ {
+ Iterator<String> it = parameters.keySet().iterator();
+ while (it.hasNext())
+ {
+ String key = it.next();
+ if (key.equalsIgnoreCase(name))
+ {
+ ArrayList values = (ArrayList)parameters.get(key);
+ if (values != null)
+ return (String)values.get(0);
+ }
+ }
+ return (null);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Map getParameterMap()
+ {
+ return parameters;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getParameterNames()
+ {
+ return new EnumerationImpl(parameters.keySet().iterator());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getParameterValues(String name)
+ {
+ ArrayList<String> arr = new ArrayList<String>();
+ Iterator it = parameters.keySet().iterator();
+ while (it.hasNext())
+ {
+
+ String pname = (String)it.next();
+ if (pname.equalsIgnoreCase(name))
+ arr.add((String)parameters.get(name).get(0));
+ }
+ return arr.toArray(new String[arr.size()]);
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getPathInfo()
+ {
+ Matcher m = p.matcher(requestURL);
+ if (!m.matches())
+ throw new RuntimeException("Unable determine pathInfo.");
+ String p = m.group(4);
+ int q = p.indexOf('?');
+ if (q > 0)
+ {
+ p = p.substring(0, q);
+ }
+ return '/' + p;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getPathTranslated()
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getProtocol()
+ {
+ return "HTTP/1.1";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getQueryString()
+ {
+ if (requestURL == null)
+ return null;
+ int sep = requestURL.lastIndexOf('?');
+ if (sep == -1)
+ return null;
+ return requestURL.substring(sep + 1);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public BufferedReader getReader() throws IOException
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRealPath(String arg0)
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRemoteAddr()
+ {
+ return "127.0.0.1";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRemoteHost()
+ {
+ return "localhost";
+ }
+
+ /**
+ * Gets the remote port.
+ *
+ * @return the remote port
+ */
+ public int getRemotePort()
+ {
+ return 8080;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRemoteUser()
+ {
+ return "root";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public RequestDispatcher getRequestDispatcher(String s)
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRequestedSessionId()
+ {
+ return "sessionId";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRequestURI()
+ {
+ return getContextPath() + getServletPath() + getPathInfo();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public StringBuffer getRequestURL()
+ {
+ if (requestURL == null)
+ return null;
+ return new StringBuffer(requestURL);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getScheme()
+ {
+ return "http";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getServerName()
+ {
+ Matcher m = p.matcher(requestURL);
+ if (!m.matches())
+ throw new RuntimeException("Unable determine server name.");
+ return m.group(1);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getServerPort()
+ {
+ Matcher m = p.matcher(requestURL);
+ if (!m.matches())
+ throw new RuntimeException("Unable determine request URI.");
+ return Integer.valueOf(m.group(2));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getServletPath()
+ {
+ return "";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public HttpSession getSession()
+ {
+ return session;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public HttpSession getSession(boolean b)
+ {
+ return session;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Principal getUserPrincipal()
+ {
+ return principal == null ? principal = new MockPrincipal("root") :
principal;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isRequestedSessionIdFromCookie()
+ {
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isRequestedSessionIdFromUrl()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isRequestedSessionIdFromURL()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isRequestedSessionIdValid()
+ {
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isSecure()
+ {
+ return secure;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isUserInRole(String role)
+ {
+ return "admin".equals(role);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeAttribute(String name)
+ {
+ attributes.remove(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setAttribute(String name, Object object)
+ {
+ attributes.put(name, object);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
+ {
+ }
+
+ /**
+ * Sets the parameter.
+ *
+ * @param name
+ * the name
+ * @param value
+ * the value
+ */
+ public void setParameter(String name, String value)
+ {
+ ArrayList arr = new ArrayList<String>();
+ arr.add(value);
+ parameters.put(name, arr);
+ }
+
+ public static Map<String, List<String>> parseQueryString(String rawQuery)
+ {
+ HashMap<String, List<String>> m = new HashMap<String,
List<String>>();
+ if (rawQuery == null || rawQuery.length() == 0)
+ return m;
+ int p = 0;
+ int n = 0;
+ while (n < rawQuery.length())
+ {
+ n = rawQuery.indexOf('&', p);
+ if (n == -1)
+ n = rawQuery.length();
+
+ String pair = rawQuery.substring(p, n);
+ if (pair.length() == 0)
+ continue;
+
+ String name;
+ String value = ""; // default value
+ int eq = pair.indexOf('=');
+ if (eq == -1) // no value, default is ""
+ name = pair;
+ else
+ {
+ name = pair.substring(0, eq);
+ value = pair.substring(eq + 1);
+ }
+
+ if (m.get(name) == null)
+ {
+ List<String> arr = new ArrayList<String>();
+ arr.add(value);
+ m.put(name, arr);
+ }
+ else
+ {
+ List<String> arr = m.get(name);
+ arr.add(value);
+ }
+ p = n + 1;
+ }
+ return m;
+ }
+}
+
+@SuppressWarnings("unchecked")
+class EnumerationImpl implements Enumeration
+{
+
+ private final Iterator iter;
+
+ public EnumerationImpl(Iterator iter)
+ {
+ this.iter = iter;
+ }
+
+ public boolean hasMoreElements()
+ {
+ return iter.hasNext();
+ }
+
+ public Object nextElement()
+ {
+ return iter.next();
+ }
+}
+
+class MockServletInputStream extends ServletInputStream
+{
+
+ private final InputStream data;
+
+ public MockServletInputStream(InputStream data)
+ {
+ this.data = data;
+ }
+
+ @Override
+ public int read() throws IOException
+ {
+ return data.read();
+ }
+}
\ No newline at end of file
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletResponse.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletResponse.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpServletResponse.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,445 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * The Class MockHttpServletResponse.
+ *
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id: $
+ */
+
+public class MockHttpServletResponse implements HttpServletResponse
+{
+
+ /** The writer. */
+ private PrintWriter writer;
+
+ /** The stream. */
+ private ByteArrayOutputStream stream;
+
+ /** The output. */
+ private ByteArrayServletOutputStream output;
+
+ /** The buffer. */
+ private byte[] buffer = new byte[1024];
+
+ /** The buffer count. */
+ private int bufferCount = 0;
+
+ /** The cookies. */
+ private List<Cookie> cookies = new ArrayList<Cookie>();
+
+ /** The headers. */
+ private HashMap<String, List<String>> headers = new
CaseInsensitiveMultivaluedMap<String>();
+
+ /** The status. */
+ private int status = HttpServletResponse.SC_OK;
+
+ /** The message. */
+ private String message = "";
+
+ /** The locale. */
+ private Locale locale = Locale.getDefault();
+
+ /** The content type. */
+ private String contentType = null;
+
+ /** The content length. */
+ protected int contentLength = -1;
+
+ /** The encoding. */
+ protected String encoding = null;
+
+ /** The date format we will use for creating date headers. */
+ protected static final SimpleDateFormat format = new SimpleDateFormat("EEE, dd
MMM yyyy HH:mm:ss zzz", Locale.US);
+ static
+ {
+ format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ };
+
+ /**
+ * Instantiates a new mock http servlet response.
+ */
+ public MockHttpServletResponse()
+ {
+ stream = new ByteArrayOutputStream();
+ writer = new PrintWriter(stream);
+ output = new ByteArrayServletOutputStream(stream);
+ }
+
+ /**
+ * Gets the output content.
+ *
+ * @return the output content
+ */
+ public String getOutputContent()
+ {
+ return new String(stream.toByteArray());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void flushBuffer() throws IOException
+ {
+ if (bufferCount > 0)
+ {
+ try
+ {
+ output.write(buffer, 0, bufferCount);
+ }
+ finally
+ {
+ bufferCount = 0;
+ }
+ }
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getBufferSize()
+ {
+ return (buffer.length);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ServletOutputStream getOutputStream() throws IOException
+ {
+ return this.output;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public PrintWriter getWriter() throws IOException
+ {
+ return this.writer;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isCommitted()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void reset()
+ {
+ bufferCount = 0;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void resetBuffer()
+ {
+ bufferCount = 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addCookie(Cookie cookie)
+ {
+ cookies.add(cookie);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addDateHeader(String name, long value)
+ {
+ addHeader(name, format.format(new Date(value)));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addHeader(String name, String value)
+ {
+ Iterator it = headers.keySet().iterator();
+ while (it.hasNext())
+ {
+ String key = (String)it.next();
+ if (key.equals(name))
+ {
+ List<String> values = (ArrayList<String>)headers.get(key);
+ if (values != null)
+ {
+ values = new ArrayList<String>();
+ headers.put(name, values);
+ }
+ values.add(value);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addIntHeader(String name, int value)
+ {
+ addHeader(name, "" + value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean containsHeader(String name)
+ {
+ return (headers.get(name) != null);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String encodeRedirectURL(String url)
+ {
+ return url; // TODO encode
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String encodeRedirectUrl(String url)
+ {
+ return url;// TODO encode
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String encodeURL(String url)
+ {
+ return url;// TODO encode
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String encodeUrl(String url)
+ {
+ return url;// TODO encode
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void sendError(int status) throws IOException
+ {
+ sendError(status, "");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void sendError(int status, String message) throws IOException
+ {
+ this.status = status;
+ this.message = message;
+ resetBuffer();
+ try
+ {
+ flushBuffer();
+ }
+ catch (IOException e)
+ {
+
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void sendRedirect(String location) throws IOException
+ {
+ resetBuffer();
+ setStatus(SC_MOVED_TEMPORARILY);
+ setHeader("Location", location);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setDateHeader(String name, long value)
+ {
+ setHeader(name, format.format(new Date(value)));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setHeader(String name, String value)
+ {
+ List<String> values = new ArrayList<String>();
+ values.add(value);
+ headers.put(name, values);
+
+ String match = name.toLowerCase();
+ if (match.equals("content-length"))
+ {
+ int contentLength = -1;
+ contentLength = Integer.parseInt(value);
+ if (contentLength >= 0)
+ setContentLength(contentLength);
+ }
+ else if (match.equals("content-type"))
+ {
+ setContentType(value);
+ }
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setIntHeader(String name, int value)
+ {
+ setHeader(name, "" + value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setStatus(int status)
+ {
+ this.status = status;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setStatus(int status, String message)
+ {
+ this.status = status;
+ this.message = message;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getCharacterEncoding()
+ {
+ if (encoding == null)
+ return ("UTF-8");
+ else
+ return (encoding);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Locale getLocale()
+ {
+ return locale;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setBufferSize(int size)
+ {
+ if (buffer.length >= size)
+ return;
+ buffer = new byte[size];
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setContentLength(int length)
+ {
+ this.contentLength = length;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setContentType(String type)
+ {
+ this.contentType = type;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setLocale(Locale locale)
+ {
+ this.locale = locale;
+ }
+
+ /**
+ * The Class ByteArrayServletOutputStream.
+ */
+ private static class ByteArrayServletOutputStream extends ServletOutputStream
+ {
+
+ /** The baos. */
+ ByteArrayOutputStream baos;
+
+ /**
+ * Instantiates a new byte array servlet output stream.
+ *
+ * @param baos
+ * the baos
+ */
+ public ByteArrayServletOutputStream(ByteArrayOutputStream baos)
+ {
+ this.baos = baos;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void write(int i) throws IOException
+ {
+ baos.write(i);
+ }
+ }
+
+}
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpSession.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpSession.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockHttpSession.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,221 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class MockHttpSession.
+ *
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id: $
+ */
+public class MockHttpSession implements HttpSession
+{
+
+ /** The attributes map. */
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+
+ /** The servlet context. */
+ private ServletContext servletContext;
+
+ /** The is valid. */
+ private boolean isValid = true;
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getCreationTime()
+ {
+ return 0L;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getId()
+ {
+ return "MockSessionId";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public long getLastAccessedTime()
+ {
+ return 0L;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ServletContext getServletContext()
+ {
+ return servletContext;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setMaxInactiveInterval(int i)
+ {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getMaxInactiveInterval()
+ {
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public HttpSessionContext getSessionContext()
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getAttribute(String s)
+ {
+
+ if (!isValid)
+ {
+ throw new IllegalStateException("Cannot call getAttribute() on invalidated
session");
+ }
+ return attributes.get(s);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getValue(String s)
+ {
+ return getAttribute(s);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getAttributeNames()
+ {
+ if (!isValid)
+ {
+ throw new IllegalStateException("Cannot call getAttribute() on invalidated
session");
+ }
+ return new Vector<String>(attributes.keySet()).elements();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getValueNames()
+ {
+ if (!isValid)
+ {
+ throw new IllegalStateException("Cannot call getAttribute() on invalidated
session");
+ }
+ String results[] = new String[0];
+ return ((String[])attributes.keySet().toArray(results));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setAttribute(String s, Object o)
+ {
+ attributes.put(s, o);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void putValue(String s, Object o)
+ {
+ setAttribute(s, o);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeAttribute(String s)
+ {
+ attributes.remove(s);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeValue(String s)
+ {
+ removeAttribute(s);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void invalidate()
+ {
+ if (!isValid)
+ {
+ throw new IllegalStateException("Cannot call invalidate() on invalidated
session");
+ }
+ this.isValid = false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isNew()
+ {
+ return false;
+ }
+
+ /**
+ * Checks if is valid.
+ *
+ * @return true, if is valid
+ */
+ public boolean isValid()
+ {
+ return this.isValid;
+ }
+
+ /**
+ * Sets the valid.
+ *
+ * @param isValid the new valid
+ */
+ public void setValid(boolean isValid)
+ {
+ this.isValid = isValid;
+ }
+}
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockPrincipal.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockPrincipal.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockPrincipal.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+/**
+ * @author Mestrallet Benjamin benjmestrallet(a)users.sourceforge.net
+ */
+public class MockPrincipal implements java.security.Principal
+{
+
+ private final String username;
+
+ public MockPrincipal(String username)
+ {
+ this.username = username;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getName()
+ {
+ return username;
+ }
+}
Added:
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockServletContext.java
===================================================================
---
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockServletContext.java
(rev 0)
+++
ws/trunk/testframework/src/main/java/org/exoplatform/services/test/mock/MockServletContext.java 2009-09-10
13:37:40 UTC (rev 168)
@@ -0,0 +1,378 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.test.mock;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Vector;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+
+/**
+ * The Class MockServletContext.
+ *
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id: $
+ */
+public class MockServletContext implements ServletContext
+{
+
+ /** The name. */
+ private String name;
+
+ /** The init params. */
+ private HashMap<String, String> initParams;
+
+ /** The attributes. */
+ private HashMap<String, Object> attributes;
+
+ /** The context path. */
+ private String contextPath;
+
+ /** The log buffer. */
+ private StringBuffer logBuffer = new StringBuffer();
+
+ /**
+ * Instantiates a new mock servlet context.
+ */
+ public MockServletContext()
+ {
+ this("MockServletContext1");
+ }
+
+ /**
+ * Instantiates a new mock servlet context.
+ *
+ * @param name the name
+ */
+ public MockServletContext(String name)
+ {
+ this.name = name;
+ this.initParams = new HashMap<String, String>();
+ this.attributes = new HashMap<String, Object>();
+ }
+
+ /**
+ * Instantiates a new mock servlet context.
+ *
+ * @param name the name
+ * @param path the path
+ */
+ public MockServletContext(String name, String path)
+ {
+ this(name);
+ contextPath = path;
+ attributes.put("javax.servlet.context.tempdir", path);
+ }
+
+ /**
+ * Sets the name.
+ *
+ * @param name the new name
+ */
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ /**
+ * Gets the log buffer.
+ *
+ * @return the log buffer
+ */
+ public String getLogBuffer()
+ {
+ try
+ {
+ return logBuffer.toString();
+ }
+ finally
+ {
+ logBuffer = new StringBuffer();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ServletContext getContext(String s)
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getMajorVersion()
+ {
+ return 2;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getMinorVersion()
+ {
+ return 4;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getMimeType(String s)
+ {
+ return "text/html";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Set getResourcePaths(String s)
+ {
+
+ if (!s.endsWith("/"))
+ s = s + "/";
+
+ Set<String> set = new HashSet<String>();
+ set.add("/WEB-INF/");
+
+ try
+ {
+ URL url = getResource(s);
+ File dir = new File(url.getPath());
+ if (dir.isDirectory())
+ {
+ File[] arr = dir.listFiles();
+ for (int i = 0; i < arr.length; i++)
+ {
+ File tmp = arr[i];
+ if (tmp.isDirectory())
+ set.add(s + "/" + tmp.getName() + "/");
+ else
+ set.add(s + "/" + tmp.getName());
+ }
+ }
+ }
+ catch (MalformedURLException e)
+ {
+ }
+ return set;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public URL getResource(String s) throws MalformedURLException
+ {
+ String path = "file:" + contextPath + s;
+ URL url = new URL(path);
+ return url;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public InputStream getResourceAsStream(String s)
+ {
+ try
+ {
+ return getResource(s).openStream();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public RequestDispatcher getRequestDispatcher(String s)
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public RequestDispatcher getNamedDispatcher(String s)
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Deprecated
+ public Servlet getServlet(String s) throws ServletException
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Deprecated
+ public Enumeration getServlets()
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Deprecated
+ public Enumeration getServletNames()
+ {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void log(String s)
+ {
+ logBuffer.append(s);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Deprecated
+ public void log(Exception e, String s)
+ {
+ logBuffer.append(s + e.getMessage());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void log(String s, Throwable throwable)
+ {
+ logBuffer.append(s + throwable.getMessage());
+ }
+
+ /**
+ * Sets the context path.
+ *
+ * @param s the new context path
+ */
+ public void setContextPath(String s)
+ {
+ contextPath = s;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getRealPath(String s)
+ {
+ return contextPath + s;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getServerInfo()
+ {
+ return null;
+ }
+
+ /**
+ * Sets the init parameter.
+ *
+ * @param name the name
+ * @param value the value
+ */
+ public void setInitParameter(String name, String value)
+ {
+ initParams.put(name, value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getInitParameter(String name)
+ {
+ return (String)initParams.get(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getInitParameterNames()
+ {
+ Vector<String> keys = new Vector<String>(initParams.keySet());
+ return keys.elements();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getAttribute(String name)
+ {
+ return attributes.get(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Enumeration getAttributeNames()
+ {
+ Vector<String> keys = new Vector<String>(attributes.keySet());
+ return keys.elements();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setAttribute(String name, Object value)
+ {
+ attributes.put(name, value);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeAttribute(String name)
+ {
+ attributes.remove(name);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getServletContextName()
+ {
+ return name;
+ }
+
+}