[seam-commits] Seam SVN: r10691 - in modules/trunk: mock and 10 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Wed Apr 29 01:52:55 EDT 2009
Author: dan.j.allen
Date: 2009-04-29 01:52:55 -0400 (Wed, 29 Apr 2009)
New Revision: 10691
Added:
modules/trunk/mock/
modules/trunk/mock/pom.xml
modules/trunk/mock/src/
modules/trunk/mock/src/main/
modules/trunk/mock/src/main/java/
modules/trunk/mock/src/main/java/org/
modules/trunk/mock/src/main/java/org/jboss/
modules/trunk/mock/src/main/java/org/jboss/seam/
modules/trunk/mock/src/main/java/org/jboss/seam/mock/
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplication.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplicationFactory.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockExternalContext.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContext.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContextFactory.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycle.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycleFactory.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockNavigationHandler.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockRenderKit.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseStateManager.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseWriter.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockStateManager.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockViewHandler.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/
modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletRequest.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletResponse.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpSession.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockServletContext.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/
modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/EnumerationIterator.java
modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/IteratorEnumeration.java
modules/trunk/mock/src/main/resources/
Log:
mock module
Property changes on: modules/trunk/mock
___________________________________________________________________
Name: svn:ignore
+ target
Added: modules/trunk/mock/pom.xml
===================================================================
--- modules/trunk/mock/pom.xml (rev 0)
+++ modules/trunk/mock/pom.xml 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,39 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>seam-parent</artifactId>
+ <groupId>org.jboss.seam</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>seam-mock</artifactId>
+ <packaging>jar</packaging>
+ <version>3.0.0-SNAPSHOT</version>
+ <name>Seam Mock Objects Module</name>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>javax.el</groupId>
+ <artifactId>el-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.faces</groupId>
+ <artifactId>jsf-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplication.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplication.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplication.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,421 @@
+package org.jboss.seam.mock.faces;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.el.CompositeELResolver;
+import javax.el.ExpressionFactory;
+import javax.faces.FacesException;
+import javax.faces.application.Application;
+import javax.faces.application.NavigationHandler;
+import javax.faces.application.StateManager;
+import javax.faces.application.ViewHandler;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIOutput;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.BigDecimalConverter;
+import javax.faces.convert.BigIntegerConverter;
+import javax.faces.convert.BooleanConverter;
+import javax.faces.convert.ByteConverter;
+import javax.faces.convert.CharacterConverter;
+import javax.faces.convert.Converter;
+import javax.faces.convert.DoubleConverter;
+import javax.faces.convert.FloatConverter;
+import javax.faces.convert.IntegerConverter;
+import javax.faces.convert.LongConverter;
+import javax.faces.convert.ShortConverter;
+import javax.faces.el.EvaluationException;
+import javax.faces.el.MethodBinding;
+import javax.faces.el.PropertyResolver;
+import javax.faces.el.ReferenceSyntaxException;
+import javax.faces.el.ValueBinding;
+import javax.faces.el.VariableResolver;
+import javax.faces.event.ActionListener;
+import javax.faces.validator.Validator;
+
+ at SuppressWarnings("deprecation")
+public class MockApplication extends Application
+{
+
+ private javax.el.CompositeELResolver elResolver;
+ private javax.el.CompositeELResolver additionalResolvers;
+ private Collection locales;
+
+ public MockApplication()
+ {
+ elResolver = new CompositeELResolver();
+ additionalResolvers = new CompositeELResolver();
+ elResolver.add(additionalResolvers);
+ //elResolver.add(EL.EL_RESOLVER);
+ }
+
+ @Override
+ public Object evaluateExpressionGet(FacesContext context, String expression, Class type) throws javax.el.ELException
+ {
+ return getExpressionFactory().createValueExpression(context.getELContext(), expression, type).getValue(context.getELContext());
+ }
+
+ @Override
+ public void addELContextListener(javax.el.ELContextListener elcl)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void addELResolver(javax.el.ELResolver r)
+ {
+ additionalResolvers.add(r);
+ }
+
+ @Override
+ public UIComponent createComponent(javax.el.ValueExpression ve, FacesContext context, String id) throws FacesException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public javax.el.ELContextListener[] getELContextListeners()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public javax.el.ELResolver getELResolver()
+ {
+ return elResolver;
+ }
+
+ @Override
+ public java.util.ResourceBundle getResourceBundle(FacesContext context, String string)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removeELContextListener(javax.el.ELContextListener elcl)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public ActionListener getActionListener()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setActionListener(ActionListener listener)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private Locale defaultLocale = Locale.ENGLISH;
+
+ @Override
+ public Locale getDefaultLocale()
+ {
+ return defaultLocale;
+ }
+
+ @Override
+ public void setDefaultLocale(Locale locale)
+ {
+ defaultLocale = locale;
+ }
+
+ @Override
+ public String getDefaultRenderKitId()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setDefaultRenderKitId(String rk)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private String msgBundleName;
+
+ @Override
+ public String getMessageBundle()
+ {
+ return msgBundleName;
+ }
+
+ @Override
+ public void setMessageBundle(String bundleName)
+ {
+ this.msgBundleName = bundleName;
+ }
+
+ private NavigationHandler navigationHandler = null; //new SeamNavigationHandler( new MockNavigationHandler() );
+
+ @Override
+ public NavigationHandler getNavigationHandler()
+ {
+ return navigationHandler;
+ }
+
+ @Override
+ public void setNavigationHandler(NavigationHandler navigationHandler)
+ {
+ this.navigationHandler = navigationHandler;
+ }
+
+ @Override
+ public PropertyResolver getPropertyResolver()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setPropertyResolver(PropertyResolver pr)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private VariableResolver variableResolver = /*new SeamVariableResolver(*/ new VariableResolver() {
+ @Override
+ public Object resolveVariable(FacesContext ctx, String name) throws EvaluationException
+ {
+ return null;
+ }
+ } /*)*/;
+
+ @Override
+ public VariableResolver getVariableResolver()
+ {
+ return variableResolver;
+ }
+
+ @Override
+ public void setVariableResolver(VariableResolver variableResolver)
+ {
+ this.variableResolver = variableResolver;
+ }
+
+ private ViewHandler viewHandler = null; //new SeamViewHandler( new MockViewHandler() );
+
+ @Override
+ public ViewHandler getViewHandler()
+ {
+ return viewHandler;
+ }
+
+ @Override
+ public void setViewHandler(ViewHandler viewHandler)
+ {
+ this.viewHandler = viewHandler;
+ }
+
+ private StateManager stateManager = null; //new SeamStateManager( new MockStateManager() );
+
+ @Override
+ public StateManager getStateManager()
+ {
+ return stateManager;
+ }
+
+ @Override
+ public void setStateManager(StateManager stateManager)
+ {
+ this.stateManager = stateManager;
+ }
+
+ @Override
+ public void addComponent(String name, String x)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public UIComponent createComponent(String name) throws FacesException
+ {
+ // Best guess component creation with a dummy component if it can't be found
+ if (name.startsWith("org.jboss.seam.mail.ui") || name.startsWith("org.jboss.seam.excel.ui"))
+ {
+ try
+ {
+ return (UIComponent) Class.forName(name).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new UnsupportedOperationException("Unable to create component " + name);
+ }
+ }
+ else
+ {
+ // Oh well, can't simply create the component so put a dummy one in its place
+ return new UIOutput();
+ }
+ }
+
+ @Override
+ public UIComponent createComponent(ValueBinding vb, FacesContext fc, String x)
+ throws FacesException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Iterator getComponentTypes()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private final Map<Class, Converter> converters = new HashMap<Class, Converter>();
+ {
+ converters.put(Integer.class, new IntegerConverter());
+ converters.put(Long.class, new LongConverter());
+ converters.put(Float.class, new FloatConverter());
+ converters.put(Double.class, new DoubleConverter());
+ converters.put(Boolean.class, new BooleanConverter());
+ converters.put(Short.class, new ShortConverter());
+ converters.put(Byte.class, new ByteConverter());
+ converters.put(Character.class, new CharacterConverter());
+ converters.put(BigDecimal.class, new BigDecimalConverter());
+ converters.put(BigInteger.class, new BigIntegerConverter());
+ }
+
+ private final Map<String, Converter> convertersById = new HashMap<String, Converter>();
+ {
+ convertersById.put(IntegerConverter.CONVERTER_ID, new IntegerConverter());
+ convertersById.put(LongConverter.CONVERTER_ID, new LongConverter());
+ convertersById.put(FloatConverter.CONVERTER_ID, new FloatConverter());
+ convertersById.put(DoubleConverter.CONVERTER_ID, new DoubleConverter());
+ convertersById.put(BooleanConverter.CONVERTER_ID, new BooleanConverter());
+ convertersById.put(ShortConverter.CONVERTER_ID, new ShortConverter());
+ convertersById.put(ByteConverter.CONVERTER_ID, new ByteConverter());
+ convertersById.put(CharacterConverter.CONVERTER_ID, new CharacterConverter());
+ convertersById.put(BigDecimalConverter.CONVERTER_ID, new BigDecimalConverter());
+ convertersById.put(BigIntegerConverter.CONVERTER_ID, new BigIntegerConverter());
+ }
+
+ @Override
+ public void addConverter(String id, String converterClass)
+ {
+ convertersById.put(id, instantiateConverter(converterClass));
+ }
+
+ @Override
+ public void addConverter(Class type, String converterClass)
+ {
+ converters.put(type, instantiateConverter(converterClass));
+ }
+
+ private Converter instantiateConverter(String converterClass)
+ {
+ try
+ {
+ return (Converter) Class.forName(converterClass).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public Converter createConverter(String id)
+ {
+ return convertersById.get(id);
+ }
+
+ @Override
+ public Converter createConverter(Class clazz)
+ {
+ return converters.get(clazz);
+ }
+
+ @Override
+ public Iterator getConverterIds()
+ {
+ return convertersById.keySet().iterator();
+ }
+
+ @Override
+ public Iterator getConverterTypes()
+ {
+ return converters.keySet().iterator();
+ }
+
+ @Override
+ public MethodBinding createMethodBinding(String expression, Class[] params)
+ throws ReferenceSyntaxException
+ {
+ return null; //new UnifiedELMethodBinding(expression, params);
+
+ }
+
+ @Override
+ public ValueBinding createValueBinding(String expression)
+ throws ReferenceSyntaxException
+ {
+ return null; //new UnifiedELValueBinding(expression);
+ }
+
+ @Override
+ public Iterator getSupportedLocales()
+ {
+ if (locales == null)
+ {
+ return Collections.singleton(defaultLocale).iterator();
+ }
+ else
+ {
+ return locales.iterator();
+ }
+ }
+
+ @Override
+ public void setSupportedLocales(Collection locales)
+ {
+ this.locales = locales;
+ }
+
+ private final Map<String, Validator> validatorsById = new HashMap<String, Validator>();
+
+ @Override
+ public void addValidator(String id, String validatorClass)
+ {
+ validatorsById.put(id, instantiateValidator(validatorClass));
+ }
+
+ private Validator instantiateValidator(String validatorClass)
+ {
+ try
+ {
+ return (Validator) Class.forName(validatorClass).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public Validator createValidator(String id) throws FacesException
+ {
+ return validatorsById.get(id);
+ }
+
+ @Override
+ public Iterator getValidatorIds()
+ {
+ return validatorsById.keySet().iterator();
+ }
+
+ @Override
+ public ExpressionFactory getExpressionFactory()
+ {
+ return null; //SeamExpressionFactory.INSTANCE;
+ }
+
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplicationFactory.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplicationFactory.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockApplicationFactory.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,40 @@
+package org.jboss.seam.mock.faces;
+
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+
+/**
+ * An mock implementation of the JSF ApplicationFactory which returns a mock
+ * Application wrapped in a SeamApplication. This class can be registered with
+ * JSF to allow JSF to be used formally in a test environment as follows:
+ *
+ * <code>
+ * FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+ * "org.jboss.seam.mock.faces.MockApplicationFactory");
+ * Application application = ((ApplicationFactory) FactoryFinder
+ * .getFactory(FactoryFinder.APPLICATION_FACTORY)).getApplication();
+ * </code>
+ *
+ * @author Dan Allen
+ */
+public class MockApplicationFactory extends ApplicationFactory
+{
+ private Application application;
+
+ @Override
+ public Application getApplication()
+ {
+// if (application == null)
+// {
+// application = new SeamApplication();
+// }
+ return application;
+ }
+
+ @Override
+ public void setApplication(Application application)
+ {
+ this.application = application;
+ }
+
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockExternalContext.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockExternalContext.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockExternalContext.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,560 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.seam.mock.faces;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.Principal;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import org.jboss.seam.mock.servlet.MockHttpServletRequest;
+import org.jboss.seam.mock.servlet.MockHttpServletResponse;
+import org.jboss.seam.mock.servlet.MockHttpSession;
+import org.jboss.seam.mock.servlet.MockServletContext;
+import org.jboss.seam.mock.util.EnumerationIterator;
+
+/**
+ * @author Gavin King
+ * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
+ * @version $Revision: 9684 $
+ */
+public class MockExternalContext extends ExternalContext
+{
+ private ServletContext context;
+
+ private HttpServletRequest request;
+
+ private HttpServletResponse response;
+
+ public MockExternalContext()
+ {
+ this.context = new MockServletContext();
+ this.request = new MockHttpServletRequest(new MockHttpSession(context));
+ this.response = new MockHttpServletResponse();
+ }
+
+ public MockExternalContext(ServletContext context)
+ {
+ this.context = context;
+ this.request = new MockHttpServletRequest(new MockHttpSession(context));
+ this.response = new MockHttpServletResponse();
+ }
+
+ public MockExternalContext(ServletContext context, HttpSession session)
+ {
+ this.context = context;
+ this.request = new MockHttpServletRequest(session);
+ this.response = new MockHttpServletResponse();
+ }
+
+ public MockExternalContext(ServletContext context, HttpServletRequest request)
+ {
+ this.context = context;
+ this.request = request;
+ this.response = new MockHttpServletResponse();
+ }
+
+ public MockExternalContext(ServletContext context, HttpServletRequest request,
+ HttpServletResponse response)
+ {
+ this.context = context;
+ this.request = request;
+ this.response = response;
+ }
+
+ public MockExternalContext(HttpServletRequest request)
+ {
+ this.request = request;
+ }
+
+ @Override
+ public void dispatch(String url) throws IOException
+ {
+
+ }
+
+ @Override
+ public String encodeActionURL(String url)
+ {
+ return encodeURL(url);
+ }
+
+ @Override
+ public String encodeNamespace(String ns)
+ {
+ return ns;
+ }
+
+ @Override
+ public String encodeResourceURL(String url)
+ {
+ return encodeURL(url);
+ }
+
+ @Override
+ public Map getApplicationMap()
+ {
+ return new AttributeMap()
+ {
+ @Override
+ public Enumeration keys()
+ {
+ return context.getAttributeNames();
+ }
+
+ @Override
+ public Object getAttribute(String key)
+ {
+ return context.getAttribute(key);
+ }
+
+ @Override
+ public void setAttribute(String key, Object value)
+ {
+ context.setAttribute(key, value);
+ }
+
+ @Override
+ public void removeAttribute(String key)
+ {
+ context.removeAttribute(key);
+ }
+ };
+ }
+
+ @Override
+ public String getAuthType()
+ {
+ return request.getAuthType();
+ }
+
+ @Override
+ public Object getContext()
+ {
+ return context;
+ }
+
+ @Override
+ public String getInitParameter(String name)
+ {
+ return context.getInitParameter(name);
+ }
+
+ @Override
+ public Map getInitParameterMap()
+ {
+ Map result = new HashMap();
+ Enumeration e = context.getInitParameterNames();
+ while (e.hasMoreElements())
+ {
+ String name = (String) e.nextElement();
+ result.put(name, context.getInitParameter(name));
+ }
+ return result;
+ }
+
+ @Override
+ public String getRemoteUser()
+ {
+ return request.getRemoteUser();
+ }
+
+ @Override
+ public Object getRequest()
+ {
+ return request;
+ }
+
+ @Override
+ public String getRequestContextPath()
+ {
+ String path = (String) request.getAttribute("org.jboss.seam.web.requestContextPath");
+ return path!=null ? path : request.getContextPath();
+ }
+
+ @Override
+ public Map getRequestCookieMap()
+ {
+ Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
+
+ if (request != null && request.getCookies() != null)
+ {
+ for (Cookie cookie : request.getCookies())
+ {
+ cookieMap.put(cookie.getName(), cookie);
+ }
+ }
+
+ return cookieMap;
+ }
+
+ @Override
+ public Map getRequestHeaderMap()
+ {
+ Map result = new HashMap();
+ Enumeration<String> names = request.getHeaderNames();
+ while (names.hasMoreElements())
+ {
+ String name = names.nextElement();
+ result.put(name, request.getHeader(name));
+ }
+ return result;
+ }
+
+ @Override
+ public Map getRequestHeaderValuesMap()
+ {
+ Map<String, String[]> result = new HashMap<String, String[]>();
+ Enumeration<String> en = request.getHeaderNames();
+ while (en.hasMoreElements())
+ {
+ String header = en.nextElement();
+ List<String> headerList = Collections.list(request.getHeaders(header));
+ String[] headers = new String[headerList.size()];
+ for (int i = 0; i < headerList.size(); i++)
+ {
+ headers[i] = headerList.get(i);
+ }
+ result.put(header, headers);
+ }
+ return result;
+ }
+
+ @Override
+ public Locale getRequestLocale()
+ {
+ return Locale.ENGLISH;
+ }
+
+ @Override
+ public Iterator getRequestLocales()
+ {
+ return Collections.singleton(Locale.ENGLISH).iterator();
+ }
+
+ @Override
+ public Map getRequestMap()
+ {
+ return new AttributeMap()
+ {
+ @Override
+ public Enumeration keys()
+ {
+ return request.getAttributeNames();
+ }
+
+ @Override
+ public Object getAttribute(String key)
+ {
+ return request.getAttribute(key);
+ }
+
+ @Override
+ public void setAttribute(String key, Object value)
+ {
+ request.setAttribute(key, value);
+ }
+
+ @Override
+ public void removeAttribute(String key)
+ {
+ request.removeAttribute(key);
+ }
+ };
+ }
+
+ @Override
+ public Map getRequestParameterMap()
+ {
+ Map map = new HashMap();
+ Enumeration<String> names = request.getParameterNames();
+ while (names.hasMoreElements())
+ {
+ String name = names.nextElement();
+ map.put(name, request.getParameter(name));
+ }
+ return map;
+ }
+
+ @Override
+ public Iterator getRequestParameterNames()
+ {
+ return request.getParameterMap().keySet().iterator();
+ }
+
+ @Override
+ public Map getRequestParameterValuesMap()
+ {
+ return request.getParameterMap();
+ }
+
+ @Override
+ public String getRequestPathInfo()
+ {
+ String path = (String) request.getAttribute("org.jboss.seam.web.requestPathInfo");
+ return path!=null ? path : request.getPathInfo();
+ }
+
+ @Override
+ public String getRequestServletPath()
+ {
+ String path = (String) request.getAttribute("org.jboss.seam.web.requestServletPath");
+ return path!=null ? path : request.getServletPath();
+ }
+
+ @Override
+ public URL getResource(String name) throws MalformedURLException
+ {
+ return context.getResource(name);
+ }
+
+ @Override
+ public InputStream getResourceAsStream(String name)
+ {
+ return context.getResourceAsStream(name);
+ }
+
+ @Override
+ public Set getResourcePaths(String name)
+ {
+ return context.getResourcePaths(name);
+ }
+
+ @Override
+ public Object getResponse()
+ {
+ return response;
+ }
+
+ @Override
+ public Object getSession(boolean create)
+ {
+ return request.getSession();
+ }
+
+ @Override
+ public Map getSessionMap()
+ {
+ final HttpSession session = request.getSession(true); //TODO: create the session lazily, RI should do that to
+ return new AttributeMap()
+ {
+ @Override
+ public Enumeration keys()
+ {
+ return session.getAttributeNames();
+ }
+
+ @Override
+ public Object getAttribute(String key)
+ {
+ return session.getAttribute(key);
+ }
+
+ @Override
+ public void setAttribute(String key, Object value)
+ {
+ session.setAttribute(key, value);
+ }
+
+ @Override
+ public void removeAttribute(String key)
+ {
+ session.removeAttribute(key);
+ }
+ };
+ }
+
+ static abstract class AttributeMap implements Map
+ {
+
+ public abstract Enumeration keys();
+
+ public Object get(Object key)
+ {
+ return getAttribute((String) key);
+ }
+
+ public Object put(Object key, Object value)
+ {
+ Object result = get(key);
+ setAttribute((String) key, value);
+ return result;
+ }
+
+ public void clear()
+ {
+ Enumeration e = keys();
+ while (e.hasMoreElements())
+ {
+ remove(e.nextElement());
+ }
+ }
+
+ public boolean containsKey(Object key)
+ {
+ Enumeration e = keys();
+ while (e.hasMoreElements())
+ {
+ if (key.equals(e.nextElement())) return true;
+ }
+ return false;
+ }
+
+ public boolean containsValue(Object value)
+ {
+ Enumeration e = keys();
+ while (e.hasMoreElements())
+ {
+ if (value.equals(get(e.nextElement()))) return true;
+ }
+ return false;
+ }
+
+ public Set entrySet()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public abstract Object getAttribute(String key);
+
+ public boolean isEmpty()
+ {
+ return size() == 0;
+ }
+
+ public Set keySet()
+ {
+ return new AbstractSet()
+ {
+
+ @Override
+ public Iterator iterator()
+ {
+ return new EnumerationIterator(keys());
+ }
+
+ @Override
+ public int size()
+ {
+ return AttributeMap.this.size();
+ }
+
+ };
+ }
+
+ public abstract void setAttribute(String key, Object value);
+
+ public abstract void removeAttribute(String key);
+
+ public void putAll(Map t)
+ {
+ for (Map.Entry me : (Set<Map.Entry>) t.entrySet())
+ {
+ put(me.getKey(), me.getValue());
+ }
+ }
+
+ public Object remove(Object key)
+ {
+ Object result = getAttribute((String) key);
+ removeAttribute((String) key);
+ return result;
+ }
+
+ public int size()
+ {
+ int i = 0;
+ Enumeration e = keys();
+ while (e.hasMoreElements())
+ {
+ e.nextElement();
+ i++;
+ }
+ return i;
+ }
+
+ public Collection values()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
+ @Override
+ public Principal getUserPrincipal()
+ {
+ return request.getUserPrincipal();
+ }
+
+ @Override
+ public boolean isUserInRole(String role)
+ {
+ return request.isUserInRole(role);
+ }
+
+ @Override
+ public void log(String message, Throwable t)
+ {
+
+ }
+
+ @Override
+ public void log(String t)
+ {
+ }
+
+ @Override
+ public void redirect(String url) throws IOException
+ {
+ response.sendRedirect(url);
+ FacesContext.getCurrentInstance().responseComplete();
+ }
+
+ /**
+ * @since 1.2
+ */
+ @Override
+ public String getResponseContentType()
+ {
+ return response.getContentType();
+ }
+
+ /**
+ * Attempt to encode the URL, falling back to
+ * an identity function if the response has
+ * not been set on this mock context. This
+ * functionality is needed in order for
+ * the ExceptionFilter to maintain the session id
+ * when url rewriting is used.
+ */
+ protected String encodeURL(String url)
+ {
+ if (response != null) {
+ String encodedUrl = response.encodeURL(url);
+ url = (encodedUrl != null ? encodedUrl : url);
+ }
+ return url;
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContext.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContext.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContext.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,388 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * $Id$
+ */
+package org.jboss.seam.mock.faces;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import java.util.NoSuchElementException;
+import javax.el.ELContext;
+import javax.faces.FactoryFinder;
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+import javax.faces.application.FacesMessage;
+import javax.faces.application.FacesMessage.Severity;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseStream;
+import javax.faces.context.ResponseWriter;
+import javax.faces.event.PhaseId;
+import javax.faces.render.RenderKit;
+import javax.faces.render.RenderKitFactory;
+
+/**
+ * @author Gavin King
+ * @author Thomas Heute
+ * @author Dan Allen
+ */
+public class MockFacesContext extends FacesContext
+{
+ private Application application;
+
+ private UIViewRoot viewRoot;
+
+ /**
+ * Store mapping of clientId to ArrayList of FacesMessage instances. The
+ * null key is used to represent FacesMessage instances that are not
+ * associated with a clientId instance.
+ */
+ private final Map<String, List<FacesMessage>> messages = new LinkedHashMap<String, List<FacesMessage>>();
+
+ private ExternalContext externalContext;
+
+ private ResponseWriter responseWriter;
+
+ private RenderKitFactory renderKitFactory;
+
+ private ELContext elContext;
+
+ private boolean renderResponse;
+
+ private boolean responseComplete;
+
+ private PhaseId currentPhaseId;
+
+ public MockFacesContext()
+ {
+ }
+
+ public MockFacesContext(Application application)
+ {
+ this.application = application;
+ }
+
+ public MockFacesContext(ExternalContext externalContext, Application application)
+ {
+ this.externalContext = externalContext;
+ this.application = application;
+ }
+
+ // Create a MockFacesContext using a ApplicationFactory to get the
+ // Application
+ public MockFacesContext(ExternalContext externalContext)
+ {
+ application = ((ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY)).getApplication();
+ renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+ this.externalContext = externalContext;
+ }
+
+ @Override
+ public Application getApplication()
+ {
+ return application;
+ }
+
+ @Override
+ public Iterator getClientIdsWithMessages()
+ {
+ return messages.keySet().iterator();
+ }
+
+ @Override
+ public ExternalContext getExternalContext()
+ {
+ return externalContext;
+ }
+
+ @Override
+ public Severity getMaximumSeverity()
+ {
+ Severity max = null;
+ for (List<FacesMessage> messagesForKey : messages.values())
+ {
+ for (FacesMessage msg : messagesForKey)
+ {
+ if (msg.getSeverity() == FacesMessage.SEVERITY_FATAL)
+ {
+ return FacesMessage.SEVERITY_FATAL;
+ }
+ else if (max == null || msg.getSeverity().compareTo(max) > 0)
+ {
+ max = msg.getSeverity();
+ }
+ }
+ }
+ return max;
+ }
+
+ @Override
+ public Iterator getMessages()
+ {
+ return messages.size() > 0 ? new FacesMessagesIterator(messages) : Collections.<FacesMessage>emptyList().iterator();
+ }
+
+ @Override
+ public Iterator getMessages(String clientId)
+ {
+ List<FacesMessage> messagesForKey = messages.get(clientId);
+ return messagesForKey != null ? messagesForKey.iterator() : Collections.<FacesMessage>emptyList().iterator();
+ }
+
+ @Override
+ public List<FacesMessage> getMessageList()
+ {
+ List<FacesMessage> aggregatedMessages = new ArrayList<FacesMessage>();
+ for (List<FacesMessage> messagesForKey : messages.values())
+ {
+ aggregatedMessages.addAll(messagesForKey);
+ }
+ return Collections.unmodifiableList(aggregatedMessages);
+ }
+
+ @Override
+ public List<FacesMessage> getMessageList(String clientId)
+ {
+ return messages.containsKey(clientId) ? Collections.unmodifiableList(messages.get(clientId)) : Collections.<FacesMessage>emptyList();
+ }
+
+ @Override
+ public RenderKit getRenderKit()
+ {
+ if (getViewRoot() == null || getViewRoot().getRenderKitId() == null)
+ {
+ return MockRenderKit.INSTANCE;
+ }
+ else
+ {
+ return renderKitFactory.getRenderKit(this, getViewRoot().getRenderKitId());
+ }
+ }
+
+ @Override
+ public boolean getRenderResponse()
+ {
+ return renderResponse;
+ }
+
+ @Override
+ public boolean getResponseComplete()
+ {
+ return responseComplete;
+ }
+
+ @Override
+ public ResponseStream getResponseStream()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setResponseStream(ResponseStream stream)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public ResponseWriter getResponseWriter()
+ {
+ return responseWriter;
+ }
+
+ @Override
+ public void setResponseWriter(ResponseWriter writer)
+ {
+ responseWriter = writer;
+ }
+
+ @Override
+ public UIViewRoot getViewRoot()
+ {
+ return viewRoot;
+ }
+
+ @Override
+ public void setViewRoot(UIViewRoot vr)
+ {
+ viewRoot = vr;
+ }
+
+ @Override
+ public void addMessage(String clientId, FacesMessage msg)
+ {
+ if (!messages.containsKey(clientId))
+ {
+ List<FacesMessage> messagesForKey = new ArrayList<FacesMessage>(1);
+ messagesForKey.add(msg);
+ messages.put(clientId, messagesForKey);
+ }
+ else
+ {
+ messages.get(clientId).add(msg);
+ }
+ }
+
+ @Override
+ public void release()
+ {
+ setCurrentInstance(null);
+ MockFacesContextFactory.setFacesContext(null);
+ }
+
+ @Override
+ public void renderResponse()
+ {
+ renderResponse = true;
+ }
+
+ @Override
+ public void responseComplete()
+ {
+ responseComplete = true;
+ }
+
+ @Override
+ public PhaseId getCurrentPhaseId()
+ {
+ return currentPhaseId;
+ }
+
+ @Override
+ public void setCurrentPhaseId(PhaseId phaseId)
+ {
+ this.currentPhaseId = phaseId;
+ }
+
+ public MockFacesContext setCurrent()
+ {
+ setCurrentInstance(this);
+
+ MockFacesContextFactory.setFacesContext(this);
+ return this;
+ }
+
+ public MockFacesContext createViewRoot()
+ {
+ viewRoot = new UIViewRoot();
+ viewRoot.setRenderKitId(getApplication().getViewHandler().calculateRenderKitId(this));
+ return this;
+ }
+
+ @Override
+ public ELContext getELContext()
+ {
+ /*
+ if (elContext == null)
+ {
+ elContext = EL.createELContext(EL.createELContext(), getApplication().getELResolver());
+ elContext.putContext(FacesContext.class, this);
+ }
+ return elContext;
+ */
+ return elContext;
+ }
+
+ public void setELContext(ELContext elContext)
+ {
+ this.elContext = elContext;
+ }
+
+ private static final class FacesMessagesIterator implements Iterator<FacesMessage>
+ {
+ private Map<String, List<FacesMessage>> messages;
+
+ private int keyIndex = -1;
+
+ private int numKeys;
+
+ private Iterator<FacesMessage> messagesForKey;
+
+ private Iterator<String> keys;
+
+ FacesMessagesIterator(Map<String, List<FacesMessage>> messages)
+ {
+ this.messages = messages;
+ numKeys = messages.size();
+ keys = messages.keySet().iterator();
+ }
+
+ public boolean hasNext()
+ {
+ if (keyIndex == -1)
+ {
+ keyIndex++;
+ messagesForKey = messages.get(keys.next()).iterator();
+ }
+ while (!messagesForKey.hasNext())
+ {
+ keyIndex++;
+ if ((keyIndex) < numKeys)
+ {
+ messagesForKey = messages.get(keys.next()).iterator();
+ }
+ else
+ {
+ return false;
+ }
+ }
+ return messagesForKey.hasNext();
+ }
+
+ public FacesMessage next()
+ {
+ if (keyIndex >= numKeys)
+ {
+ throw new NoSuchElementException();
+ }
+ if (messagesForKey != null && messagesForKey.hasNext())
+ {
+ return messagesForKey.next();
+ }
+ else
+ {
+ if (!this.hasNext())
+ {
+ throw new NoSuchElementException();
+ }
+ else
+ {
+ return messagesForKey.next();
+ }
+ }
+ }
+
+ public void remove()
+ {
+ if (keyIndex == -1)
+ {
+ throw new IllegalStateException();
+ }
+ messagesForKey.remove();
+ }
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContextFactory.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContextFactory.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockFacesContextFactory.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,27 @@
+package org.jboss.seam.mock.faces;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.lifecycle.Lifecycle;
+
+public class MockFacesContextFactory extends FacesContextFactory
+{
+ private static FacesContext facesContext;
+
+ @Override
+ public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException
+ {
+ return facesContext;
+ }
+
+ public static void setFacesContext(FacesContext facesContext)
+ {
+ MockFacesContextFactory.facesContext = facesContext;
+ }
+
+ public static FacesContext getFacesContext()
+ {
+ return MockFacesContextFactory.facesContext;
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycle.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycle.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycle.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,49 @@
+//$Id: MockLifecycle.java 8195 2008-05-15 13:25:37Z pete.muir at jboss.org $
+package org.jboss.seam.mock.faces;
+
+import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
+import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseListener;
+import javax.faces.lifecycle.Lifecycle;
+
+public class MockLifecycle extends Lifecycle
+{
+ public static final Lifecycle INSTANCE = new MockLifecycle();
+
+ public MockLifecycle()
+ {
+ MockLifecycleFactory.setLifecycle(this);
+ FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY, MockLifecycleFactory.class.getName());
+ }
+
+ @Override
+ public void addPhaseListener(PhaseListener pl)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void execute(FacesContext ctx) throws FacesException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public PhaseListener[] getPhaseListeners()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void removePhaseListener(PhaseListener pl)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void render(FacesContext ctx) throws FacesException
+ {
+ throw new UnsupportedOperationException();
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycleFactory.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycleFactory.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockLifecycleFactory.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,40 @@
+package org.jboss.seam.mock.faces;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import javax.faces.lifecycle.Lifecycle;
+import javax.faces.lifecycle.LifecycleFactory;
+
+public class MockLifecycleFactory extends LifecycleFactory
+{
+ private static Lifecycle lifecycle;
+
+ public static void setLifecycle(Lifecycle lifecycle)
+ {
+ MockLifecycleFactory.lifecycle = lifecycle;
+ }
+
+ public static Lifecycle getLifecycle()
+ {
+ return MockLifecycleFactory.lifecycle;
+ }
+
+ @Override
+ public void addLifecycle(String lifecycleId, Lifecycle lifecycle)
+ {
+ throw new IllegalArgumentException("Not supported by mock");
+ }
+
+ @Override
+ public Lifecycle getLifecycle(String lifecycleId)
+ {
+ return lifecycle;
+ }
+
+ @Override
+ public Iterator<String> getLifecycleIds()
+ {
+ return Arrays.asList(DEFAULT_LIFECYCLE).iterator();
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockNavigationHandler.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockNavigationHandler.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockNavigationHandler.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,12 @@
+package org.jboss.seam.mock.faces;
+
+import javax.faces.application.NavigationHandler;
+import javax.faces.context.FacesContext;
+
+public class MockNavigationHandler extends NavigationHandler
+{
+ @Override
+ public void handleNavigation(FacesContext context, String action, String outcome)
+ {
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockRenderKit.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockRenderKit.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockRenderKit.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,45 @@
+package org.jboss.seam.mock.faces;
+
+import java.io.OutputStream;
+import java.io.Writer;
+
+import javax.faces.context.ResponseStream;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.RenderKit;
+import javax.faces.render.Renderer;
+import javax.faces.render.ResponseStateManager;
+
+public class MockRenderKit extends RenderKit
+{
+ public static final MockRenderKit INSTANCE = new MockRenderKit();
+
+ @Override
+ public void addRenderer(String x, String y, Renderer renderer)
+ {
+ // Do nothing
+ }
+
+ @Override
+ public Renderer getRenderer(String x, String y)
+ {
+ return null;
+ }
+
+ @Override
+ public ResponseStateManager getResponseStateManager()
+ {
+ return new MockResponseStateManager();
+ }
+
+ @Override
+ public ResponseWriter createResponseWriter(Writer writer, String x, String y)
+ {
+ return new MockResponseWriter();
+ }
+
+ @Override
+ public ResponseStream createResponseStream(OutputStream stream)
+ {
+ throw new UnsupportedOperationException();
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseStateManager.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseStateManager.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseStateManager.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,28 @@
+package org.jboss.seam.mock.faces;
+
+import java.io.IOException;
+
+import javax.faces.application.StateManager.SerializedView;
+import javax.faces.context.FacesContext;
+import javax.faces.render.ResponseStateManager;
+
+ at SuppressWarnings("deprecation")
+public class MockResponseStateManager extends ResponseStateManager
+{
+ @Override
+ public Object getComponentStateToRestore(FacesContext ctx)
+ {
+ return new Object();
+ }
+
+ @Override
+ public Object getTreeStructureToRestore(FacesContext ctx, String x)
+ {
+ return new Object();
+ }
+
+ @Override
+ public void writeState(FacesContext ctx, SerializedView viewState) throws IOException
+ {
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseWriter.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseWriter.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockResponseWriter.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,158 @@
+package org.jboss.seam.mock.faces;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.ResponseWriter;
+
+public class MockResponseWriter extends ResponseWriter
+{
+ private Writer writer;
+
+ public MockResponseWriter(Writer writer)
+ {
+ this.writer = writer;
+ }
+
+ public MockResponseWriter()
+ {
+ this.writer = new StringWriter();
+ }
+
+ @Override
+ public ResponseWriter cloneWithWriter(Writer writer)
+ {
+ return new MockResponseWriter(writer);
+ }
+
+ @Override
+ public void endDocument() throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void endElement(String element) throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ writer.flush();
+
+ }
+
+ @Override
+ public String getCharacterEncoding()
+ {
+ return null;
+ }
+
+ @Override
+ public String getContentType()
+ {
+ return null;
+ }
+
+ @Override
+ public void startDocument() throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void startElement(String element, UIComponent component) throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void writeAttribute(String attribute, Object object, String string) throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void writeComment(Object object) throws IOException
+ {
+ // TODO Do nothing
+ }
+
+ @Override
+ public void writeText(Object value, String string) throws IOException
+ {
+ if (value == null)
+ {
+ throw new NullPointerException("Text must not be null.");
+ }
+ String strValue = value.toString();
+ write(strValue);
+ }
+
+ @Override
+ public void writeText(char[] chars, int start, int end) throws IOException
+ {
+ if (chars == null)
+ {
+ throw new NullPointerException("cbuf name must not be null");
+ }
+ if (chars.length < start + end)
+ {
+ throw new IndexOutOfBoundsException((start + end) + " > " + chars.length);
+ }
+ String strValue = new String(chars, start, end);
+ write(strValue);
+ }
+
+ @Override
+ public void writeURIAttribute(String attribute, Object object, String string) throws IOException
+ {
+ // Do nothing
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ writer.close();
+
+ }
+
+ @Override
+ public void write(char[] chars, int start, int end) throws IOException
+ {
+ writer.write(chars, start, end);
+ }
+
+ @Override
+ public void write(String str) throws IOException
+ {
+ writer.write(str);
+ }
+
+ @Override
+ public void write(int c) throws IOException
+ {
+ writer.write(c);
+ }
+
+ @Override
+ public void write(char cbuf[]) throws IOException
+ {
+ writer.write(cbuf);
+ }
+
+ @Override
+ public void write(String str, int off, int len) throws IOException
+ {
+ writer.write(str, off, len);
+ }
+
+ public Writer getWriter()
+ {
+ return this.writer;
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockStateManager.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockStateManager.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockStateManager.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,63 @@
+package org.jboss.seam.mock.faces;
+
+import java.io.IOException;
+
+import javax.faces.application.StateManager;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+ at SuppressWarnings("deprecation")
+public class MockStateManager extends StateManager
+{
+ @Override
+ public Object saveView(FacesContext ctx)
+ {
+ return null;
+ }
+
+ @Override
+ public void writeState(FacesContext ctx, Object state) throws IOException
+ {
+ }
+
+ @Override
+ public SerializedView saveSerializedView(FacesContext ctx)
+ {
+ return null;
+ }
+
+ @Override
+ protected Object getTreeStructureToSave(FacesContext ctx)
+ {
+ return null;
+ }
+
+ @Override
+ protected Object getComponentStateToSave(FacesContext ctx)
+ {
+ return null;
+ }
+
+ @Override
+ public void writeState(FacesContext ctx, SerializedView sv)
+ throws IOException
+ {
+ }
+
+ @Override
+ public UIViewRoot restoreView(FacesContext ctx, String x, String y)
+ {
+ return null;
+ }
+
+ @Override
+ protected UIViewRoot restoreTreeStructure(FacesContext ctx, String x, String y)
+ {
+ return null;
+ }
+
+ @Override
+ protected void restoreComponentState(FacesContext ctx, UIViewRoot viewRoot, String x)
+ {
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockViewHandler.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockViewHandler.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/faces/MockViewHandler.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,89 @@
+package org.jboss.seam.mock.faces;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import javax.faces.FacesException;
+import javax.faces.application.ViewHandler;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+public class MockViewHandler extends ViewHandler
+{
+ @Override
+ public Locale calculateLocale(FacesContext ctx)
+ {
+ return Locale.getDefault();
+ }
+
+ @Override
+ public String calculateRenderKitId(FacesContext ctx)
+ {
+ return null;
+ }
+
+ @Override
+ public UIViewRoot createView(FacesContext ctx, String viewId)
+ {
+ UIViewRoot viewRoot = new UIViewRoot();
+ viewRoot.setViewId(viewId);
+ //TODO: set locale?
+ return viewRoot;
+ }
+
+ @Override
+ public String getActionURL(FacesContext ctx, String viewId)
+ {
+ String contextPath = ctx.getExternalContext().getRequestContextPath();
+ String pathInfo = ctx.getExternalContext().getRequestPathInfo();
+ String servletPath = ctx.getExternalContext().getRequestServletPath();
+
+ if (pathInfo == null || pathInfo.length() == 0)
+ {
+ int sploc = servletPath.lastIndexOf('.');
+ if (sploc < 0)
+ {
+ throw new IllegalArgumentException("no file extension in servlet path: " + servletPath);
+ }
+ return contextPath + getViewIdSansSuffix(viewId) + servletPath.substring(sploc);
+
+ }
+ else
+ {
+ return contextPath + (servletPath != null ? servletPath : "") + viewId;
+ }
+ }
+
+ private static String getViewIdSansSuffix(String viewId)
+ {
+ int loc = viewId.lastIndexOf('.');
+ if (loc < 0)
+ {
+ throw new IllegalArgumentException("no file extension in view id: " + viewId);
+ }
+ return viewId.substring(0, loc);
+ }
+
+ @Override
+ public String getResourceURL(FacesContext ctx, String url)
+ {
+ return url;
+ }
+
+ @Override
+ public void renderView(FacesContext ctx, UIViewRoot viewRoot)
+ throws IOException, FacesException
+ {
+ }
+
+ @Override
+ public UIViewRoot restoreView(FacesContext ctx, String id)
+ {
+ return null;
+ }
+
+ @Override
+ public void writeState(FacesContext ctx) throws IOException
+ {
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletRequest.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletRequest.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletRequest.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,394 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.seam.mock.servlet;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import org.jboss.seam.mock.util.IteratorEnumeration;
+
+/**
+ * @author Gavin King
+ * @author Thomas Heute
+ */
+public class MockHttpServletRequest implements HttpServletRequest
+{
+
+ private Map<String, String[]> parameters = new HashMap<String, String[]>();
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+ private HttpSession session;
+ private Map<String, String[]> headers = new HashMap<String, String[]>();
+ private String principalName;
+ private Set<String> principalRoles;
+ private Cookie[] cookies;
+ private String method;
+ private Enumeration locales;
+
+ public MockHttpServletRequest(HttpSession session)
+ {
+ this(session, null, new HashSet<String>());
+ }
+
+ public MockHttpServletRequest(HttpSession session, String principalName, Set<String> principalRoles)
+ {
+ this(session, principalName, principalRoles, new Cookie[] {}, null);
+ }
+
+ public MockHttpServletRequest(HttpSession session, String principalName, Set<String> principalRoles, Cookie[] cookies, String method)
+ {
+ this.session = session;
+ this.principalName = principalName;
+ this.principalRoles = principalRoles;
+ this.cookies = cookies;
+ this.method = method;
+ // The 1.2 RI NPEs if this header isn't present
+ headers.put("Accept", new String[0]);
+ locales = new IteratorEnumeration(new ArrayList().iterator());
+ }
+
+ public Map<String, String[]> getParameters()
+ {
+ return parameters;
+ }
+
+ public Map<String, Object> getAttributes()
+ {
+ return attributes;
+ }
+
+ public String getAuthType()
+ {
+ //TODO
+ return null;
+ }
+
+ public Cookie[] getCookies()
+ {
+ return cookies;
+ }
+
+ public long getDateHeader(String arg0)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getHeader(String header)
+ {
+ String[] values = headers.get(header);
+ return values==null || values.length==0 ? null : values[0];
+ }
+
+ public Enumeration getHeaders(String header)
+ {
+ return new IteratorEnumeration( Arrays.asList( headers.get(header) ).iterator() );
+ }
+
+ public Enumeration getHeaderNames()
+ {
+ return new IteratorEnumeration( headers.keySet().iterator() );
+ }
+
+ public int getIntHeader(String header)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getMethod()
+ {
+ return method;
+ }
+
+ public String getPathInfo()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getPathTranslated()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getContextPath()
+ {
+ return "/project";
+ }
+
+ public String getQueryString()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getRemoteUser()
+ {
+ return principalName;
+ }
+
+ public boolean isUserInRole(String role)
+ {
+ return principalRoles.contains(role);
+ }
+
+ public Principal getUserPrincipal()
+ {
+ return principalName==null ? null :
+ new Principal()
+ {
+ public String getName()
+ {
+ return principalName;
+ }
+ };
+ }
+
+ public String getRequestedSessionId()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getRequestURI()
+ {
+ return "http://localhost:8080/myproject/page.seam";
+ }
+
+ public StringBuffer getRequestURL()
+ {
+ return new StringBuffer( getRequestURI() );
+ }
+
+ public String getServletPath()
+ {
+ return "/page.seam";
+ }
+
+ public HttpSession getSession(boolean create)
+ {
+ return session;
+ }
+
+ public HttpSession getSession()
+ {
+ return getSession(true);
+ }
+
+ public boolean isRequestedSessionIdValid()
+ {
+ return true;
+ }
+
+ public boolean isRequestedSessionIdFromCookie()
+ {
+ return true;
+ }
+
+ public boolean isRequestedSessionIdFromURL()
+ {
+ return false;
+ }
+
+ public boolean isRequestedSessionIdFromUrl()
+ {
+ return false;
+ }
+
+ public Object getAttribute(String att)
+ {
+ return attributes.get(att);
+ }
+
+ public Enumeration getAttributeNames()
+ {
+ return new IteratorEnumeration( attributes.keySet().iterator() );
+ }
+
+ public String getCharacterEncoding()
+ {
+ //TODO
+ return null;
+ }
+
+ public void setCharacterEncoding(String enc)
+ throws UnsupportedEncodingException
+ {
+ //TODO
+
+ }
+
+ public int getContentLength()
+ {
+ //TODO
+ return 0;
+ }
+
+ public String getContentType()
+ {
+ //TODO
+ return null;
+ }
+
+ public ServletInputStream getInputStream() throws IOException
+ {
+ //TODO
+ return null;
+ }
+
+ public String getParameter(String param)
+ {
+ String[] values = parameters.get(param);
+ return values==null || values.length==0 ? null : values[0];
+ }
+
+ public Enumeration getParameterNames()
+ {
+ return new IteratorEnumeration( parameters.keySet().iterator() );
+ }
+
+ public String[] getParameterValues(String param)
+ {
+ return parameters.get(param);
+ }
+
+ public Map getParameterMap()
+ {
+ return parameters;
+ }
+
+ public String getProtocol()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getScheme()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getServerName()
+ {
+ //TODO
+ return null;
+ }
+
+ public int getServerPort()
+ {
+ //TODO
+ return 0;
+ }
+
+ public BufferedReader getReader() throws IOException
+ {
+ //TODO
+ return null;
+ }
+
+ public String getRemoteAddr()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getRemoteHost()
+ {
+ //TODO
+ return null;
+ }
+
+ public void setAttribute(String att, Object value)
+ {
+ if (value==null)
+ {
+ attributes.remove(value);
+ }
+ else
+ {
+ attributes.put(att, value);
+ }
+ }
+
+ public void removeAttribute(String att)
+ {
+ attributes.remove(att);
+ }
+
+ public Locale getLocale()
+ {
+ //TODO
+ return null;
+ }
+
+ public Enumeration getLocales()
+ {
+ return locales;
+ }
+
+ public boolean isSecure()
+ {
+ //TODO
+ return false;
+ }
+
+ public RequestDispatcher getRequestDispatcher(String path)
+ {
+ //TODO
+ return null;
+ }
+
+ public String getRealPath(String path)
+ {
+ //TODO
+ return null;
+ }
+
+ public int getRemotePort()
+ {
+ //TODO
+ return 0;
+ }
+
+ public String getLocalName()
+ {
+ //TODO
+ return null;
+ }
+
+ public String getLocalAddr()
+ {
+ //TODO
+ return null;
+ }
+
+ public int getLocalPort()
+ {
+ //TODO
+ return 0;
+ }
+
+ public Map<String, String[]> getHeaders()
+ {
+ return headers;
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletResponse.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletResponse.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpServletResponse.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,153 @@
+package org.jboss.seam.mock.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Locale;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+public class MockHttpServletResponse implements HttpServletResponse
+{
+ public void addCookie(Cookie cookie)
+ {
+ }
+
+ public void addDateHeader(String arg0, long arg1)
+ {
+ }
+
+ public void addHeader(String arg0, String arg1)
+ {
+ }
+
+ public void addIntHeader(String arg0, int arg1)
+ {
+ }
+
+ public boolean containsHeader(String arg0)
+ {
+ return false;
+ }
+
+ public String encodeRedirectURL(String arg0)
+ {
+ return null;
+ }
+
+ public String encodeRedirectUrl(String arg0)
+ {
+ return null;
+ }
+
+ public String encodeURL(String url)
+ {
+ return url;
+ }
+
+ @Deprecated
+ public String encodeUrl(String url)
+ {
+ return encodeURL(url);
+ }
+
+ public void sendError(int arg0) throws IOException
+ {
+ }
+
+ public void sendError(int arg0, String arg1) throws IOException
+ {
+ }
+
+ public void sendRedirect(String arg0) throws IOException
+ {
+ }
+
+ public void setDateHeader(String arg0, long arg1)
+ {
+ }
+
+ public void setHeader(String arg0, String arg1)
+ {
+ }
+
+ public void setIntHeader(String arg0, int arg1)
+ {
+ }
+
+ public void setStatus(int arg0)
+ {
+ }
+
+ public void setStatus(int arg0, String arg1)
+ {
+ }
+
+ public void flushBuffer() throws IOException
+ {
+ }
+
+ public int getBufferSize()
+ {
+ return 0;
+ }
+
+ public String getCharacterEncoding()
+ {
+ return null;
+ }
+
+ public String getContentType()
+ {
+ return null;
+ }
+
+ public Locale getLocale()
+ {
+ return null;
+ }
+
+ public ServletOutputStream getOutputStream() throws IOException
+ {
+ return null;
+ }
+
+ public PrintWriter getWriter() throws IOException
+ {
+ return null;
+ }
+
+ public boolean isCommitted()
+ {
+ return false;
+ }
+
+ public void reset()
+ {
+ }
+
+ public void resetBuffer()
+ {
+ }
+
+ public void setBufferSize(int arg0)
+ {
+ }
+
+ public void setCharacterEncoding(String arg0)
+ {
+ }
+
+ public void setContentLength(int arg0)
+ {
+ }
+
+ public void setContentType(String arg0)
+ {
+ }
+
+ public void setLocale(Locale arg0)
+ {
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpSession.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpSession.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockHttpSession.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,148 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.seam.mock.servlet;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+import org.jboss.seam.mock.util.IteratorEnumeration;
+
+/**
+ * @author Gavin King
+ * @author Thomas Heute
+ * @version $Revision: 9668 $
+ */
+ at SuppressWarnings("deprecation")
+public class MockHttpSession implements HttpSession
+{
+
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+ private boolean isInvalid;
+ private ServletContext servletContext;
+
+ public MockHttpSession() {}
+
+ public MockHttpSession(ServletContext servletContext)
+ {
+ this.servletContext = servletContext;
+ }
+
+ public boolean isInvalid()
+ {
+ return isInvalid;
+ }
+
+ public long getCreationTime()
+ {
+ return 0;
+ }
+
+ public String getId()
+ {
+ return null;
+ }
+
+ public long getLastAccessedTime()
+ {
+ return 0;
+ }
+
+ private int maxInactiveInterval;
+
+ public void setMaxInactiveInterval(int max)
+ {
+ maxInactiveInterval = max;
+ }
+
+ public int getMaxInactiveInterval()
+ {
+ return maxInactiveInterval;
+ }
+
+ public HttpSessionContext getSessionContext()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object getAttribute(String att)
+ {
+ return attributes.get(att);
+ }
+
+ public Object getValue(String att)
+ {
+ return getAttribute(att);
+ }
+
+ public Enumeration getAttributeNames()
+ {
+ return new IteratorEnumeration( attributes.keySet().iterator() );
+ }
+
+ public String[] getValueNames()
+ {
+ return attributes.keySet().toArray( new String[0] );
+ }
+
+ public void setAttribute(String att, Object value)
+ {
+ if (value==null)
+ {
+ attributes.remove(att);
+ }
+ else
+ {
+ attributes.put(att, value);
+ }
+ }
+
+ public void putValue(String att, Object value)
+ {
+ setAttribute(att, value);
+ }
+
+ public void removeAttribute(String att)
+ {
+ attributes.remove(att);
+ }
+
+ public void removeValue(String att)
+ {
+ removeAttribute(att);
+ }
+
+ public void invalidate()
+ {
+ attributes.clear();
+ isInvalid = true;
+ }
+
+ public boolean isNew()
+ {
+ return false;
+ }
+
+ public Map<String, Object> getAttributes()
+ {
+ return attributes;
+ }
+
+ public ServletContext getServletContext()
+ {
+ return servletContext;
+ }
+
+ /*public void clear() {
+ attributes.clear();
+ isInvalid = false;
+ }*/
+
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockServletContext.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockServletContext.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/servlet/MockServletContext.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,312 @@
+//$Id: MockServletContext.java 9513 2008-11-06 03:09:55Z shane.bryzak at jboss.com $
+package org.jboss.seam.mock.servlet;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.jboss.seam.mock.util.IteratorEnumeration;
+
+public class MockServletContext implements ServletContext
+{
+ private Map<String, String> initParameters = new HashMap<String, String>();
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+ private File webappRoot;
+ private File webInfRoot;
+ private File webInfClassesRoot;
+
+ public MockServletContext()
+ {
+ try
+ {
+ URL webxml = getClass().getResource("/WEB-INF/web.xml");
+ if (webxml != null)
+ {
+ webInfRoot = new File(webxml.toURI()).getParentFile();
+ if (webInfRoot != null)
+ {
+ webInfClassesRoot = new File(webInfRoot.getParentFile().getPath() + "/classes");
+ webappRoot = webInfRoot.getParentFile();
+ }
+ // call processing of context parameters
+ processContextParameters(webxml);
+ }
+ else
+ {
+ webappRoot = new File(getClass().getResource("/.").toURI());
+ }
+ }
+ catch (URISyntaxException e)
+ {
+ System.out.println("Unable to find web.xml:" + e.getMessage());
+ }
+ }
+
+ private void processContextParameters(URL webXML)
+ {
+ /*
+ try
+ {
+ Element root = XML.getRootElementSafely(webXML.openStream());
+ for (Element element : (List<Element>) root.elements("context-param"))
+ {
+ getInitParameters().put(element.elementText("param-name"), element.elementText("param-value"));
+ }
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Error parsing web.xml", e);
+ }
+ catch (DocumentException e)
+ {
+ throw new RuntimeException("Error parsing web.xml", e);
+ }
+ */
+ }
+
+ public Map<String, String> getInitParameters()
+ {
+ return initParameters;
+ }
+
+ public Map<String, Object> getAttributes()
+ {
+ return attributes;
+ }
+
+ public ServletContext getContext(String name)
+ {
+ return this;
+ }
+
+ public int getMajorVersion()
+ {
+ return 2;
+ }
+
+ public int getMinorVersion()
+ {
+ return 4;
+ }
+
+ public String getMimeType(String arg0)
+ {
+ return null;
+ }
+
+ public Set getResourcePaths(String name)
+ {
+ Enumeration<URL> enumeration = null;
+ try
+ {
+ enumeration = getClass().getClassLoader().getResources("WEB-INF");
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Error finding webroot.", e);
+ }
+ Set<String> result = new HashSet<String>();
+ while (enumeration.hasMoreElements())
+ {
+ URL url = enumeration.nextElement();
+ File rootFile = new File(url.getPath()).getParentFile();
+ File newFile = new File(rootFile.getPath() + name);
+ File[] files = newFile.listFiles();
+ if (files != null)
+ {
+ addPaths(result, files, rootFile.getPath());
+ }
+ }
+ return result;
+ }
+
+ private static void addPaths(Set<String> result, File[] files, String rootPath)
+ {
+ for (File file : files)
+ {
+ String filePath = file.getPath().substring(rootPath.length()).replace('\\', '/');
+ if (file.isDirectory())
+ {
+ result.add(filePath + "/");
+ }
+ else
+ {
+ result.add(filePath);
+ }
+ }
+ }
+
+ /**
+ * Get the URL for a particular resource that is relative to the web app root
+ * directory.
+ *
+ * @param name The name of the resource to get
+ * @return The resource, or null if resource not found
+ * @throws MalformedURLException If the URL is invalid
+ */
+ public URL getResource(String name) throws MalformedURLException
+ {
+ File file = getFile(name, webappRoot);
+
+ if (file == null)
+ {
+ file = getFile(name, webInfRoot);
+ }
+
+ if (file == null)
+ {
+ file = getFile(name, webInfClassesRoot);
+ }
+
+ if (file != null)
+ {
+ return file.toURI().toURL();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private static File getFile(String name, File root)
+ {
+ if (root == null)
+ {
+ return null;
+ }
+
+ if (name.startsWith("/"))
+ {
+ name = name.substring(1);
+ }
+
+ File f = new File(root, name);
+ if (!f.exists())
+ {
+ return null;
+ }
+ else
+ {
+ return f;
+ }
+ }
+
+ public InputStream getResourceAsStream(String name)
+ {
+ return getClass().getResourceAsStream(name);
+ }
+
+ public RequestDispatcher getRequestDispatcher(String url)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public RequestDispatcher getNamedDispatcher(String name)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Servlet getServlet(String name) throws ServletException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Enumeration getServlets()
+ {
+ return null;
+ }
+
+ public Enumeration getServletNames()
+ {
+ return null;
+ }
+
+ public void log(String msg)
+ {
+ }
+
+ public void log(Exception ex, String msg)
+ {
+ }
+
+ public void log(String msg, Throwable ex)
+ {
+ }
+
+ public String getRealPath(String relativePath)
+ {
+ if (webappRoot != null)
+ {
+ return webappRoot.getAbsolutePath() + relativePath;
+ }
+ else
+ {
+ return relativePath;
+ }
+ }
+
+ public String getServerInfo()
+ {
+ return null;
+ }
+
+ public String getInitParameter(String param)
+ {
+ return initParameters.get(param);
+ }
+
+ public Enumeration getInitParameterNames()
+ {
+ return new IteratorEnumeration(initParameters.keySet().iterator());
+ }
+
+ public Object getAttribute(String att)
+ {
+ return attributes.get(att);
+ }
+
+ public Enumeration getAttributeNames()
+ {
+ return new IteratorEnumeration(attributes.keySet().iterator());
+ }
+
+ public void setAttribute(String att, Object value)
+ {
+ if (value == null)
+ {
+ attributes.remove(value);
+ }
+ else
+ {
+ attributes.put(att, value);
+ }
+ }
+
+ public void removeAttribute(String att)
+ {
+ attributes.remove(att);
+ }
+
+ public String getServletContextName()
+ {
+ return "Mock";
+ }
+
+ public String getContextPath()
+ {
+ return null;
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/EnumerationIterator.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/EnumerationIterator.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/EnumerationIterator.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,29 @@
+package org.jboss.seam.mock.util;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+
+public class EnumerationIterator<T> implements Iterator<T>
+{
+ private Enumeration e;
+
+ public EnumerationIterator(Enumeration e)
+ {
+ this.e = e;
+ }
+
+ public boolean hasNext()
+ {
+ return e.hasMoreElements();
+ }
+
+ public T next()
+ {
+ return (T) e.nextElement();
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+}
Added: modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/IteratorEnumeration.java
===================================================================
--- modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/IteratorEnumeration.java (rev 0)
+++ modules/trunk/mock/src/main/java/org/jboss/seam/mock/util/IteratorEnumeration.java 2009-04-29 05:52:55 UTC (rev 10691)
@@ -0,0 +1,25 @@
+//$Id: IteratorEnumeration.java 5629 2007-06-29 00:41:37Z gavin $
+package org.jboss.seam.mock.util;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+
+public class IteratorEnumeration implements Enumeration
+{
+ private Iterator iterator;
+
+ public IteratorEnumeration(Iterator iterator)
+ {
+ this.iterator = iterator;
+ }
+
+ public boolean hasMoreElements()
+ {
+ return iterator.hasNext();
+ }
+
+ public Object nextElement()
+ {
+ return iterator.next();
+ }
+}
More information about the seam-commits
mailing list