diff options
author | Thomas Watson | 2015-04-10 18:26:18 +0000 |
---|---|---|
committer | Thomas Watson | 2015-04-10 18:26:18 +0000 |
commit | e80907c516213af2f6edf7d8f1ef98c567e580c7 (patch) | |
tree | 281125e5fb8f35a7254ab8b9f2359ab9e2e4032e | |
parent | 1c563754214b5956d40c981819d35e66da461d4f (diff) | |
download | rt.equinox.bundles-e80907c516213af2f6edf7d8f1ef98c567e580c7.tar.gz rt.equinox.bundles-e80907c516213af2f6edf7d8f1ef98c567e580c7.tar.xz rt.equinox.bundles-e80907c516213af2f6edf7d8f1ef98c567e580c7.zip |
Bug 464334 - [http whiteboard] should throwI20150414-1400I20150414-0800
UnsupportedOperationException for various ServletContext methods
2 files changed, 107 insertions, 2 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java index 6d7f229f7..bd1b287b8 100644 --- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java +++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java @@ -13,7 +13,8 @@ package org.eclipse.equinox.http.servlet.tests; import java.io.FileNotFoundException; import java.io.IOException; - +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.CookieHandler; import java.net.CookieManager; import java.net.CookiePolicy; @@ -30,12 +31,14 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.Servlet; import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextListener; import javax.servlet.ServletException; @@ -1269,6 +1272,64 @@ public class ServletTest extends TestCase { Assert.assertEquals(expected, actual); } + public void testServletContextUnsupportedOperations() { + final AtomicReference<ServletContext> contextHolder = new AtomicReference<ServletContext>(); + Servlet unsupportedServlet = new HttpServlet() { + private static final long serialVersionUID = 1L; + @Override + public void init(ServletConfig config) throws ServletException { + contextHolder.set(config.getServletContext()); + } + }; + + ServiceRegistration<Servlet> servletReg = null; + Dictionary<String, Object> servletProps = new Hashtable<String, Object>(); + servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/sessions"); + try { + servletReg = getBundleContext().registerService(Servlet.class, unsupportedServlet, servletProps); + } catch (Exception e) { + fail("Unexpected exception: " + e); + } finally { + if (servletReg != null) { + servletReg.unregister(); + } + } + ServletContext context = contextHolder.get(); + assertNotNull("Null context.", context); + for(Method m : getUnsupportedMethods()) { + checkMethod(m, context); + } + } + + private void checkMethod(Method m, ServletContext context) throws RuntimeException { + Class<?>[] types = m.getParameterTypes(); + Object[] params = new Object[types.length]; + try { + m.invoke(context, params); + fail("Expected an exception."); + } catch (IllegalAccessException e) { + fail("unexpected: " + e); + } catch (InvocationTargetException e) { + Throwable cause = e.getTargetException(); + if (!(cause instanceof UnsupportedOperationException)) { + fail("unexpected exception for " + m.getName() + ": " + cause); + } + } + + } + + static private List<Method> getUnsupportedMethods() { + List<Method> methods = new ArrayList<Method>(); + Class<ServletContext> contextClass = ServletContext.class; + for(Method m : contextClass.getMethods()) { + String name = m.getName(); + if (name.equals("addFilter") || name.equals("addListener") || name.equals("addServlet") || name.equals("createFilter") || name.equals("createListener") || name.equals("createServlet") | name.equals("declareRoles")) { + methods.add(m); + } + } + return methods; + } + public void test_ServletContextHelper1() throws Exception { Bundle bundle = installBundle(ServletTest.TEST_BUNDLE_1); try { diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java index 817d12cc4..e0f497df0 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java @@ -255,7 +255,51 @@ public class ServletContextAdaptor { } } - public void setAttribute(String attributeName, Object attributeValue) { + public void addFilter(String arg1, Class<? extends Filter> arg2) { + throw new UnsupportedOperationException(); + } + public void addFilter(String arg1, String arg2) { + throw new UnsupportedOperationException(); + } + public void addFilter(String arg1, Filter arg2) { + throw new UnsupportedOperationException(); + } + + public void addListener(Class<?> arg1){ + throw new UnsupportedOperationException(); + } + public void addListener(String arg1){ + throw new UnsupportedOperationException(); + } + public void addListener(EventListener arg1){ + throw new UnsupportedOperationException(); + } + + public void addServlet(String arg1, Class<? extends Servlet> arg2) { + throw new UnsupportedOperationException(); + } + public void addServlet(String arg1, String arg2) { + throw new UnsupportedOperationException(); + } + public void addServlet(String arg1, Servlet arg2) { + throw new UnsupportedOperationException(); + } + + public void createFilter(Class<?> arg1) { + throw new UnsupportedOperationException(); + } + public void createServlet(Class<?> arg1) { + throw new UnsupportedOperationException(); + } + public void createListener(Class<?> arg1) { + throw new UnsupportedOperationException(); + } + + public void declareRoles(String... arg1) { + throw new UnsupportedOperationException(); + } + + public void setAttribute(String attributeName, Object attributeValue) { Dictionary<String, Object> attributes = getContextAttributes(); boolean added = (attributes.get(attributeName) == null); attributes.put(attributeName, attributeValue); |