You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by ng...@apache.org on 2009/06/23 07:38:19 UTC
svn commit: r787553 [33/34] - in /incubator/wink/contrib/ibm-jaxrs/tests: ./
fvt/ fvt/demo/ fvt/demo/jaxrs/ fvt/demo/jaxrs/cache/
fvt/demo/jaxrs/cache/server/ fvt/demo/jaxrs/cache/test/
fvt/demo/jaxrs/datasource/ fvt/demo/jaxrs/datasource/server/ fvt/d...
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/ServletTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/ServletTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/ServletTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/ServletTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,306 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web;
+
+import java.io.ByteArrayInputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MultivaluedMap;
+
+import junit.framework.TestCase;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.integration.ApplicationProvider;
+import com.ibm.ws.jaxrs.integration.IntegrationRegistry;
+import com.ibm.ws.jaxrs.web.mock.ServletConfigImpl;
+import com.ibm.ws.jaxrs.web.mock.ServletRequest;
+import com.ibm.ws.jaxrs.web.mock.ServletResponse;
+
+/**
+ * Tests for the RESTServlet
+ *
+ */
+public class ServletTests extends TestCase {
+
+ private static final String sampleContent = "<sample:address xmlns:sample=\"http://test.jaxrs.ibm.com/sample/address\">"
+ + "<sample:firstName>Johnny</sample:firstName>"
+ + "<sample:lastName>Sampleton</sample:lastName>"
+ + "</sample:address>";
+
+ public void testBuildRESTInfo() throws Exception {
+ RESTServlet servlet = new RESTServlet();
+ String servletName = "Calendar";
+ Map<String, String> initParams = new HashMap<String, String>();
+ initParams.put("javax.ws.rs.Application", CalendarApplication.class
+ .getName());
+ ServletConfig config = new ServletConfigImpl(servletName, initParams);
+ try {
+ servlet.init(config);
+ } catch (ServletException e) {
+ e.printStackTrace();
+ fail(e.toString());
+ }
+ ApplicationProvider appProvider = (ApplicationProvider) IntegrationRegistry
+ .getIntegrationProvider(config, ApplicationProvider.class);
+ assertNotNull(appProvider);
+ assertNotNull(appProvider.getApplicationClasses(null));
+ assertEquals(1, appProvider.getApplicationClasses(null).size());
+ assertEquals(CalendarApplication.class, appProvider
+ .getApplicationClasses(null).get(0).getClass());
+ }
+
+ public void testCreateDummyServletRequest() throws Exception {
+ ByteArrayInputStream bais = new ByteArrayInputStream(sampleContent
+ .getBytes());
+ String url = "http://localhost:9080/addressbook/Sampleton";
+
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put(ServletRequest.CONTENT_TYPE, "text/xml");
+
+ ServletRequest request = new ServletRequest(url, bais, props);
+
+ ServletInputStream sis = request.getInputStream();
+ assertTrue(sis != null);
+
+ // Let's make sure that the underlying DummyServletInputStream is
+ // working
+ byte[] bytes = new byte[sis.available()];
+ sis.read(bytes, 0, sis.available());
+ assertTrue("The length of the content did not match. Found ["
+ + bytes.length + "] " + "expected ["
+ + sampleContent.getBytes().length + "]",
+ bytes.length == sampleContent.getBytes().length);
+
+ assertTrue(request.getContentType() != null);
+ assertTrue(request.getContentType().equals("text/xml"));
+ }
+
+ public void testCreateServletResponse() throws Exception {
+ StringWriter sw = new StringWriter();
+ ServletResponse response = new ServletResponse(sw);
+ PrintWriter pw = response.getWriter();
+ pw.write(sampleContent);
+ assertEquals(sw.toString(), sampleContent);
+ }
+
+ public void testInitializeQueryStringValues() throws Exception {
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put(ServletRequest.QUERY_STRING, "id=007&name=Bond");
+ ServletRequest request = new ServletRequest(
+ "http://localhost:8080/resource?id=007&name=Bond", null, props);
+ StringWriter sw = new StringWriter();
+ ServletResponse response = new ServletResponse(sw);
+ RESTContext context = new RESTContext();
+ RESTServlet servlet = new RESTServlet();
+ servlet.initContext(request, response, context);
+ MultivaluedMap<String, String> queryValues = (MultivaluedMap<String, String>) context
+ .getProperty(ContextConstants.QUERY_STRING_DECODED_VALUES);
+ assertNotNull(queryValues);
+ List<String> values = queryValues.get("id");
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("007", values.get(0));
+ values = queryValues.get("name");
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("Bond", values.get(0));
+
+ // now let's try a case when there are multiple query string values for the same id
+ props.put(ServletRequest.QUERY_STRING,
+ "id=007&name=Bond&name=James+Bond");
+ request = new ServletRequest(
+ "http://localhost:8080/resource?id=007&name=Bond&name=James+Bond",
+ null, props);
+ sw = new StringWriter();
+ response = new ServletResponse(sw);
+ context = new RESTContext();
+ servlet = new RESTServlet();
+ servlet.initContext(request, response, context);
+ queryValues = (MultivaluedMap<String, String>) context
+ .getProperty(ContextConstants.QUERY_STRING_DECODED_VALUES);
+ assertNotNull(queryValues);
+ values = queryValues.get("id");
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("007", values.get(0));
+ values = queryValues.get("name");
+ assertNotNull(values);
+ assertEquals(2, values.size());
+ assertEquals("Bond", values.get(0));
+ assertEquals("James Bond", values.get(1));
+ }
+
+ public void testInitializeMatrixParams() throws Exception {
+ Map<String, Object> props = new HashMap<String, Object>();
+ ServletRequest request = new ServletRequest(
+ "http://localhost:8080/resource/;id=007;"
+ + "last%20name=Bond;first%20name=James", null, props);
+ StringWriter sw = new StringWriter();
+ ServletResponse response = new ServletResponse(sw);
+ RESTContext context = new RESTContext();
+ RESTServlet servlet = new RESTServlet();
+ servlet.initContext(request, response, context);
+ MultivaluedMap<String, String> matrixParams = (MultivaluedMap<String, String>) context
+ .getProperty(ContextConstants.MATRIX_PARAMETER_VALUES_DECODED);
+ assertNotNull(matrixParams);
+ List<String> paramValue = matrixParams.get("id");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("007", paramValue.get(0));
+
+ paramValue = matrixParams.get("last name");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("Bond", paramValue.get(0));
+
+ paramValue = matrixParams.get("first name");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("James", paramValue.get(0));
+
+ matrixParams = (MultivaluedMap<String, String>) context
+ .getProperty(ContextConstants.MATRIX_PARAMETER_VALUES_ENCODED);
+ assertNotNull(matrixParams);
+
+ paramValue = matrixParams.get("id");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("007", paramValue.get(0));
+
+ paramValue = matrixParams.get("last%20name");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("Bond", paramValue.get(0));
+
+ paramValue = matrixParams.get("first%20name");
+ assertNotNull(paramValue);
+ assertEquals(1, paramValue.size());
+ assertEquals("James", paramValue.get(0));
+ }
+
+ public void testInitializeHttpHeaders() throws Exception {
+ Map<String, Object> props = new HashMap<String, Object>();
+ Map<String, List<String>> httpHeaders = new HashMap<String, List<String>>();
+
+ List<String> hValues = new LinkedList<String>();
+ hValues.add("09/22/08");
+ httpHeaders.put("request-date", hValues);
+
+ hValues = new LinkedList<String>();
+ hValues.add("true");
+ httpHeaders.put("auto-cache", hValues);
+
+ hValues = new LinkedList<String>();
+ hValues.add("12");
+ hValues.add("10");
+ httpHeaders.put("banned-ids", hValues);
+
+ props.put(ServletRequest.HTTP_HEADERS, httpHeaders);
+ ServletRequest request = new ServletRequest(
+ "http://localhost:8080/resource/", null, props);
+ StringWriter sw = new StringWriter();
+ ServletResponse response = new ServletResponse(sw);
+ RESTContext context = new RESTContext();
+ RESTServlet servlet = new RESTServlet();
+ servlet.initContext(request, response, context);
+ MultivaluedMap<String, String> headers = (MultivaluedMap<String, String>) context
+ .getProperty(ContextConstants.HTTP_HEADER_VALUES);
+ assertNotNull(headers);
+
+ List<String> values = headers.get("request-date");
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("09/22/08", values.get(0));
+
+ values = headers.get("auto-cache");
+ assertNotNull(values);
+ assertEquals(1, values.size());
+ assertEquals("true", values.get(0));
+
+ values = headers.get("banned-ids");
+ assertNotNull(values);
+ assertEquals(2, values.size());
+ assertEquals("12", values.get(0));
+ assertEquals("10", values.get(1));
+ }
+
+ public void testInitializeCookies() throws Exception {
+ Map<String, Object> props = new HashMap<String, Object>();
+ Cookie[] reqCookies = new Cookie[] {
+ new Cookie("cookie1", "cookieValue1"),
+ new Cookie("cookie2", "cookieValue2") };
+ props.put(ServletRequest.COOKIES, reqCookies);
+ ServletRequest request = new ServletRequest(
+ "http://localhost:8080/resource/", null, props);
+ StringWriter sw = new StringWriter();
+ ServletResponse response = new ServletResponse(sw);
+ RESTContext context = new RESTContext();
+ RESTServlet servlet = new RESTServlet();
+ servlet.initContext(request, response, context);
+ Map<String, javax.ws.rs.core.Cookie> cookies = (Map<String, javax.ws.rs.core.Cookie>) context
+ .getProperty(ContextConstants.HTTP_COOKIES);
+ assertNotNull(cookies);
+
+ javax.ws.rs.core.Cookie cookie1 = cookies.get("cookie1");
+ assertNotNull(cookie1);
+ assertEquals("cookieValue1", cookie1.getValue());
+
+ javax.ws.rs.core.Cookie cookie2 = cookies.get("cookie2");
+ assertNotNull(cookie2);
+ assertEquals("cookieValue2", cookie2.getValue());
+ }
+
+ @Path(value = "/calendar")
+ public static class Calendar {
+
+ @GET
+ public String getDate() {
+ java.util.Calendar javaCalendar = java.util.Calendar.getInstance();
+ return javaCalendar.getTime().toString();
+ }
+ }
+
+ public static class CalendarApplication extends Application {
+
+ @Override
+ public Set<Class<?>> getClasses() {
+ Set<Class<?>> set = new HashSet<Class<?>>();
+ set.add(Calendar.class);
+ return set;
+ }
+
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/asm/ScanTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/asm/ScanTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/asm/ScanTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/asm/ScanTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.asm;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBContext;
+
+import junit.framework.TestCase;
+
+import com.ibm.rest.test.utils.TestUtils;
+
+public class ScanTests extends TestCase {
+
+ public void testScanSingle() throws Exception {
+ String sep = java.io.File.separator;
+ JAXRSAnnotationScanner scanner = new JAXRSAnnotationScanner();
+ String path = TestUtils.getBaseDirectory() + sep + "build" + sep
+ + "test-classes" + sep
+ + MyResource.class.getName().replace(".", sep) + ".class";
+ File file = new File(path);
+ URL url = file.toURI().toURL();
+ List<InputStream> classInputs = new ArrayList<InputStream>();
+ classInputs.add(url.openStream());
+ Application app = scanner.scan(classInputs, Thread.currentThread()
+ .getContextClassLoader());
+ assertNotNull(app);
+ Set<Class<?>> classes = app.getClasses();
+ assertNotNull(classes);
+ assertEquals(1, classes.size());
+ assertTrue(classes.contains(MyResource.class));
+ }
+
+ public void testScanMultiple() throws Exception {
+ String sep = java.io.File.separator;
+ List<InputStream> classInputs = new ArrayList<InputStream>();
+ JAXRSAnnotationScanner scanner = new JAXRSAnnotationScanner();
+
+ // get the streams
+ String path = TestUtils.getBaseDirectory() + sep + "build" + sep
+ + "test-classes" + sep
+ + MyResource.class.getName().replace(".", sep) + ".class";
+ File file = new File(path);
+ URL url = file.toURI().toURL();
+ classInputs.add(url.openStream());
+
+ path = TestUtils.getBaseDirectory() + sep + "build" + sep
+ + "test-classes" + sep
+ + MyOtherResource.class.getName().replace(".", sep) + ".class";
+ file = new File(path);
+ url = file.toURI().toURL();
+ classInputs.add(url.openStream());
+
+ path = TestUtils.getBaseDirectory() + sep + "build" + sep
+ + "test-classes" + sep
+ + MyOwnProvider.class.getName().replace(".", sep) + ".class";
+ file = new File(path);
+ url = file.toURI().toURL();
+ classInputs.add(url.openStream());
+
+ Application app = scanner.scan(classInputs, Thread.currentThread()
+ .getContextClassLoader());
+ assertNotNull(app);
+ Set<Class<?>> classes = app.getClasses();
+ assertNotNull(classes);
+ assertEquals(3, classes.size());
+ assertTrue(classes.contains(MyResource.class));
+ assertTrue(classes.contains(MyOtherResource.class));
+ assertTrue(classes.contains(MyOwnProvider.class));
+ }
+
+ @Path(value = "/myresource")
+ public static class MyResource {
+
+ }
+
+ @Path(value = "/myotheresource")
+ public static class MyOtherResource {
+
+ }
+
+ @Provider
+ public static class MyOwnProvider implements ContextResolver<JAXBContext> {
+
+ public JAXBContext getContext(Class<?> arg0) {
+ return null;
+ }
+
+ }
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/LocaleEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/LocaleEnumeration.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/LocaleEnumeration.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/LocaleEnumeration.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Locale;
+
+public class LocaleEnumeration implements Enumeration<Locale> {
+
+ private Iterator<Locale> locales;
+
+ public LocaleEnumeration(Iterator<Locale> locales) {
+ this.locales = locales;
+ }
+
+ public boolean hasMoreElements() {
+ return locales.hasNext();
+ }
+
+ public Locale nextElement() {
+ return locales.next();
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/Principal.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/Principal.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/Principal.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/Principal.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+public class Principal implements java.security.Principal {
+
+ private String name;
+
+ public Principal(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletConfigImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletConfigImpl.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletConfigImpl.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletConfigImpl.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+/**
+ * Mock implementation for the ServletConfig interface. This is used to unit
+ * test our RESTServlet functionality.
+ *
+ */
+public class ServletConfigImpl implements ServletConfig {
+
+ private String servletName;
+
+ private Map<String, String> initParams;
+
+ public ServletConfigImpl(String servletName) {
+ this(servletName, null);
+ }
+
+ public ServletConfigImpl(String servletName, Map<String, String> initParams) {
+ this.servletName = servletName;
+ this.initParams = initParams != null ? initParams
+ : new HashMap<String, String>();
+ }
+
+ public String getInitParameter(String paramName) {
+ return initParams.get(paramName);
+ }
+
+ public Enumeration getInitParameterNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServletContext getServletContext() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServletName() {
+ return servletName;
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletContextImpl.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletContextImpl.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletContextImpl.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.EnumSet;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.DispatcherType;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+// Mock ServletContext implementation for testing
+public class ServletContextImpl implements javax.servlet.ServletContext {
+
+ private String contextPath;
+
+ public ServletContextImpl(String contextPath) {
+ this.contextPath = contextPath;
+ }
+
+ public void addFilter(String arg0, String arg1, String arg2, Map<String, String> arg3)
+ throws IllegalArgumentException, IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addFilterMapping(String arg0, String[] arg1, String[] arg2, EnumSet<DispatcherType> arg3, boolean arg4)
+ throws IllegalArgumentException, IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addServlet(String arg0, String arg1, String arg2, Map<String, String> arg3, int arg4)
+ throws IllegalArgumentException, IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addServletMapping(String arg0, String[] arg1)
+ throws IllegalArgumentException, IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public Object getAttribute(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getAttributeNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public javax.servlet.ServletContext getContext(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getContextPath() {
+ return contextPath;
+ }
+
+ public String getInitParameter(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getInitParameterNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMajorVersion() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getMimeType(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMinorVersion() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public RequestDispatcher getNamedDispatcher(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRealPath(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public RequestDispatcher getRequestDispatcher(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public URL getResource(String arg0) throws MalformedURLException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public InputStream getResourceAsStream(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set getResourcePaths(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServerInfo() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Servlet getServlet(String arg0) throws ServletException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServletContextName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServletNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServlets() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void log(Exception arg0, String arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(String arg0, Throwable arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removeAttribute(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setAttribute(String arg0, Object arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletInputStream.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletInputStream.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletInputStream.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletInputStream.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ServletInputStream extends javax.servlet.ServletInputStream {
+
+ private InputStream content;
+
+ public ServletInputStream(InputStream is) {
+ content = is;
+ }
+
+ @Override
+ public int readLine(byte[] b, int offset, int length) throws IOException {
+ return content.read(b, offset, length);
+ }
+
+ @Override
+ public int read() throws IOException {
+ return content.read();
+ }
+
+ @Override
+ public int available() throws IOException {
+ return content.available();
+ }
+
+ @Override
+ public void close() throws IOException {
+ content.close();
+ }
+
+ @Override
+ public synchronized void mark(int readlimit) {
+ content.mark(readlimit);
+ }
+
+ @Override
+ public boolean markSupported() {
+ return content.markSupported();
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ return content.read(b, off, len);
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return content.read(b);
+ }
+
+ @Override
+ public synchronized void reset() throws IOException {
+ content.reset();
+ }
+
+ @Override
+ public long skip(long n) throws IOException {
+ return content.skip(n);
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletOutputStream.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletOutputStream.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletOutputStream.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+public class ServletOutputStream extends javax.servlet.ServletOutputStream {
+
+ private StringWriter sw;
+
+ public ServletOutputStream(StringWriter sw) {
+ this.sw = sw;
+ }
+
+ public StringWriter getWriter() {
+ return sw;
+ }
+
+ public void close() throws IOException {
+ // TODO Auto-generated method stub
+ super.close();
+ }
+
+ public void flush() throws IOException {
+ // TODO Auto-generated method stub
+ super.flush();
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ sw.write(new String(b), off, len);
+ }
+
+ public void write(byte[] b) throws IOException {
+ sw.write(new String(b));
+ }
+
+ public void write(int b) throws IOException {
+ sw.write(b);
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletRequest.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletRequest.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletRequest.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletRequest.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,462 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+public class ServletRequest implements HttpServletRequest {
+
+ public static final String CONTENT_TYPE = "contentType";
+
+ public static final String AUTH_TYPE = "authType";
+
+ public static final String PATH_INFO = "pathInfo";
+
+ public static final String REQUEST_URI = "requestURI";
+
+ public static final String IS_SECURE = "isSecure";
+
+ public static final String USER_PRINCIPAL = "userPrincipal";
+
+ public static final String ROLES_FOR_USER = "rolesForUser";
+
+ public static final String CONTEXT_PATH = "contextPath";
+
+ public static final String CHAR_ENCODING = "charEncoding";
+
+ public static final String QUERY_STRING = "queryString";
+
+ public static final String METHOD = "method";
+
+ public static final String HTTP_HEADERS = "HTTP_HEADERS";
+
+ public static final String COOKIES = "COOKIES";
+
+ public static final String HTTP_DATE_HEADERS = "HTTP_DATE_HEADERS";
+
+ public static final String LOCALE = "LOCALE";
+
+ public static final String LOCALES = "LOCALES";
+
+ public static final String FORM_PARAMS = "formParams";
+
+ private Map<String, Object> properties;
+
+ private String requestURL;
+
+ private ServletInputStream stream;
+
+ public ServletRequest(Map<String, Object> properties) {
+ this.properties = properties;
+ }
+
+ public ServletRequest(String url, InputStream content, Map<String, Object> p) {
+ stream = new ServletInputStream(content);
+ requestURL = url;
+ properties = p;
+ }
+
+ public String getAuthType() {
+ return (String) properties.get(AUTH_TYPE);
+ }
+
+ public String getContextPath() {
+ return (String) properties.get(CONTEXT_PATH);
+ }
+
+ public Cookie[] getCookies() {
+ return (Cookie[]) properties.get(ServletRequest.COOKIES);
+ }
+
+ public long getDateHeader(String header) {
+ Map<String, Long> dateHeaders = (Map<String, Long>) properties
+ .get(ServletRequest.HTTP_DATE_HEADERS);
+ return dateHeaders != null ? dateHeaders.get(header) : null;
+ }
+
+ public String getHeader(String key) {
+ String value = null;
+ Map<String, List<String>> headers = (Map<String, List<String>>) properties
+ .get(HTTP_HEADERS);
+ if (headers != null) {
+ value = headers.get(key) != null ? headers.get(key).get(0) : null;
+ }
+ return value;
+ }
+
+ public Enumeration getHeaderNames() {
+ StringTokenizer tokens = null;
+ Map<String, List<String>> headers = (Map<String, List<String>>) properties
+ .get(HTTP_HEADERS);
+ if (headers != null && headers.keySet() != null) {
+ Iterator<String> keys = headers.keySet().iterator();
+ StringBuffer sb = new StringBuffer();
+ while (keys.hasNext()) {
+ sb.append(keys.next() + ";");
+ }
+ tokens = new StringTokenizer(sb.toString().trim(), ";");
+ }
+ return tokens;
+ }
+
+ public Enumeration getHeaders(String key) {
+ StringTokenizer tokens = null;
+ Map<String, List<String>> headers = (Map<String, List<String>>) properties
+ .get(HTTP_HEADERS);
+ if (headers != null && headers.get(key) != null) {
+ Iterator<String> values = headers.get(key).iterator();
+ StringBuffer sb = new StringBuffer();
+ while (values.hasNext()) {
+ sb.append(values.next() + ";");
+ }
+ tokens = new StringTokenizer(sb.toString().trim(), ";");
+ }
+ return tokens;
+ }
+
+ public int getIntHeader(String arg0) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getMethod() {
+ return (String) properties.get(METHOD);
+ }
+
+ public String getPathInfo() {
+ return (String) properties.get(PATH_INFO);
+ }
+
+ public String getPathTranslated() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getQueryString() {
+ return (String) properties.get(QUERY_STRING);
+ }
+
+ public String getRemoteUser() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRequestedSessionId() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRequestURI() {
+ String pathInfo = (String) properties.get(PATH_INFO);
+ if (pathInfo != null) {
+ if (pathInfo.indexOf("?") != -1) {
+ pathInfo = pathInfo.substring(0, pathInfo.indexOf("?"));
+ }
+ if (pathInfo.indexOf(";") != -1) {
+ pathInfo = pathInfo.substring(0, pathInfo.indexOf(";"));
+ }
+ }
+ return pathInfo;
+ }
+
+ public StringBuffer getRequestURL() {
+ return new StringBuffer(requestURL);
+ }
+
+ public String getServletPath() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public HttpSession getSession() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public HttpSession getSession(boolean arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Principal getUserPrincipal() {
+ return (Principal) properties.get(USER_PRINCIPAL);
+ }
+
+ public boolean isRequestedSessionIdFromCookie() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isRequestedSessionIdFromUrl() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isRequestedSessionIdFromURL() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isRequestedSessionIdValid() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isUserInRole(String role) {
+ List<String> roles = (List<String>) properties.get(ROLES_FOR_USER);
+ if (roles == null) {
+ return false;
+ } else {
+ return roles.contains(role);
+ }
+ }
+
+ public void complete() throws IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public Object getAttribute(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getAttributeNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getCharacterEncoding() {
+ return (String) properties.get(CHAR_ENCODING);
+ }
+
+ public int getContentLength() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getContentType() {
+ return (String) properties.get(CONTENT_TYPE);
+ }
+
+ public javax.servlet.ServletInputStream getInputStream() throws IOException {
+ return stream;
+ }
+
+ public String getLocalAddr() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Locale getLocale() {
+ return (Locale) properties.get(ServletRequest.LOCALE);
+ }
+
+ public Enumeration getLocales() {
+ return (Enumeration) properties.get(ServletRequest.LOCALES);
+ }
+
+ public String getLocalName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getLocalPort() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getParameter(String key) {
+ Map<String, List<String>> formParams = (Map<String, List<String>>) properties
+ .get(FORM_PARAMS);
+ if (formParams != null && formParams.get(key) != null
+ && formParams.get(key).size() > 0) {
+ return formParams.get(key).get(0);
+ }
+ return null;
+ }
+
+ public Map getParameterMap() {
+ return (Map<String, List<String>>) properties.get(FORM_PARAMS);
+ }
+
+ public Enumeration getParameterNames() {
+ Map<String, List<String>> formParams = (Map<String, List<String>>) properties
+ .get(FORM_PARAMS);
+ if (formParams != null) {
+ Iterator<String> keys = formParams.keySet().iterator();
+ StringBuffer buffer = new StringBuffer();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ buffer.append(key + ";");
+ }
+ return new StringTokenizer(buffer.toString(), ";");
+ }
+ return null;
+ }
+
+ public String[] getParameterValues(String key) {
+ Map<String, List<String>> formParams = (Map<String, List<String>>) properties
+ .get(FORM_PARAMS);
+ if (formParams != null && formParams.get(key) != null
+ && formParams.get(key).size() > 0) {
+ List<String> formValues = formParams.get(key);
+ String[] arrayValue = new String[formValues.size()];
+ return formValues.toArray(arrayValue);
+ }
+ return null;
+ }
+
+ public String getProtocol() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public BufferedReader getReader() throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRealPath(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRemoteAddr() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRemoteHost() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getRemotePort() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public RequestDispatcher getRequestDispatcher(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getScheme() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServerName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getServerPort() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public ServletContext getServletContext() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServletResponse getServletResponse() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public boolean isInitial() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isResumed() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isSecure() {
+ Object obj = properties.get(IS_SECURE);
+ if (obj != null) {
+ return (Boolean) obj;
+ }
+ return Boolean.FALSE;
+ }
+
+ public boolean isSuspended() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isTimeout() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void removeAttribute(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void resume() throws IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setAttribute(String key, Object value) {
+ properties.put(key, value);
+ }
+
+ public void setCharacterEncoding(String arg0)
+ throws UnsupportedEncodingException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void suspend() throws IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void suspend(long arg0) throws IllegalStateException {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletResponse.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletResponse.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletResponse.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/mock/ServletResponse.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.mock;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+// Mock object for unit testing
+public class ServletResponse implements HttpServletResponse {
+
+ private StringWriter sw;
+
+ private PrintWriter pw;
+
+ private int status;
+
+ private com.ibm.ws.jaxrs.web.mock.ServletOutputStream os;
+
+ private Map<String, List<String>> headers = new HashMap<String, List<String>>();
+
+ public ServletResponse(StringWriter sw) {
+ this.sw = sw;
+ pw = new PrintWriter(sw);
+ os = new com.ibm.ws.jaxrs.web.mock.ServletOutputStream(this.sw);
+ }
+
+ public void addCookie(Cookie arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addDateHeader(String arg0, long arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addHeader(String key, String value) {
+ List<String> values = headers.get(key);
+ if (values == null) {
+ values = new LinkedList<String>();
+ headers.put(key, values);
+ }
+ values.add(value);
+ }
+
+ public String getHeader(String key) {
+ if (headers.get(key) != null)
+ return headers.get(key).get(0);
+
+ return null;
+ }
+
+ public List<String> getHeaders(String key) {
+ return headers.get(key);
+ }
+
+ public void addIntHeader(String arg0, int arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean containsHeader(String key) {
+ return headers.containsKey(key);
+ }
+
+ public String encodeRedirectUrl(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String encodeRedirectURL(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String encodeUrl(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String encodeURL(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void sendError(int arg0, String arg1) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void sendError(int arg0) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void sendRedirect(String arg0) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setDateHeader(String arg0, long arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setHeader(String key, String value) {
+ List<String> values = headers.get(key);
+ if (values == null) {
+ values = new LinkedList<String>();
+ headers.put(key, values);
+ }
+ values.add(value);
+ }
+
+ public void setIntHeader(String arg0, int arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setStatus(int arg0, String arg1) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setStatus(int status) {
+ this.status = status;
+ }
+
+ public int getStatus() {
+ return status;
+ }
+
+ public void disable() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void enable() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void flushBuffer() throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public int getBufferSize() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getCharacterEncoding() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getContentType() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Locale getLocale() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServletOutputStream getOutputStream() throws IOException {
+ return os;
+ }
+
+ public PrintWriter getWriter() throws IOException {
+ return pw;
+ }
+
+ public boolean isCommitted() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean isDisabled() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void reset() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void resetBuffer() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setBufferSize(int arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setCharacterEncoding(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setContentLength(int arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setContentType(String arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setLocale(Locale arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServerHost.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServerHost.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServerHost.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServerHost.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.samples;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.ws.rs.Path;
+
+public class ServerHost {
+
+ private int port = 7080;
+ private ServletHost sh = null;
+ private String path = "";
+
+ public ServerHost() {
+ }
+
+ public ServerHost(int port) {
+ this.port = port;
+ }
+
+ public void setPath(String path) {
+ this.path = path;
+ }
+
+ public void init() throws Exception {
+ Class[] ca = getJAXRSClasses(path);
+ if (ca.length == 0) {
+ System.out.println("No Classes Found with JAX-RS annotations");
+ System.exit(0);
+ }
+ sh = new ServletHost(port, ca);
+ sh.init();
+ }
+
+ public void waitForEnter() {
+ System.out.println("Hit Enter to terminate the Server ");
+ // open up standard input
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+ try {
+ br.readLine();
+ } catch (IOException ioe) {
+ System.out.println("IO Error - Ending the Server");
+ System.exit(1);
+ }
+ }
+
+ public static void main(String[] args) {
+
+ try {
+ ServerHost sh = new ServerHost();
+ // sh.setPath("com.ibm.rest");
+ sh.init();
+ sh.waitForEnter();
+
+ System.out.println("Ending the Server");
+ System.exit(0);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ /*
+ * Get the list of JAX-RS annotated classes...
+ */
+ public static Class[] getJAXRSClasses(String packageName)
+ throws ClassNotFoundException, IOException {
+ Class[] ca = getClasses(packageName);
+ ArrayList<Class> result = new ArrayList<Class>();
+ for (int i = 0; i < ca.length; i++) {
+ if (ca[i].getAnnotation(Path.class) != null) {
+ System.out.println("Hosting JAX-RS Resource " + ca[i]);
+ result.add(ca[i]);
+ }
+ }
+
+ Class[] ret = new Class[result.size()];
+ return result.toArray(ret);
+ }
+
+ /**
+ * Scans all classes accessible from the context class loader which belong
+ * to the given package and subpackages.
+ *
+ * @param packageName
+ * The base package
+ * @return The classes
+ * @throws ClassNotFoundException
+ * @throws IOException
+ */
+ private static Class[] getClasses(String packageName)
+ throws ClassNotFoundException, IOException {
+ ClassLoader classLoader = Thread.currentThread()
+ .getContextClassLoader();
+ assert classLoader != null;
+ if (packageName == null)
+ packageName = "";
+ String path = packageName.replace('.', '/');
+ Enumeration<URL> resources = classLoader.getResources(path);
+ ArrayList<File> dirs = new ArrayList<File>();
+ while (resources.hasMoreElements()) {
+ URL resource = resources.nextElement();
+ dirs.add(new File(resource.getFile()));
+ }
+ ArrayList<Class> classes = new ArrayList<Class>();
+ for (File directory : dirs) {
+ classes.addAll(findClasses(directory, packageName));
+ }
+ return classes.toArray(new Class[classes.size()]);
+ }
+
+ /**
+ * Recursive method used to find all classes in a given directory and
+ * subdirs.
+ *
+ * @param directory
+ * The base directory
+ * @param packageName
+ * The package name for classes found inside the base directory
+ * @return The classes
+ * @throws ClassNotFoundException
+ */
+ private static List<Class> findClasses(File directory, String packageName)
+ throws ClassNotFoundException {
+ List<Class> classes = new ArrayList<Class>();
+ if (!directory.exists()) {
+ return classes;
+ }
+ File[] files = directory.listFiles();
+ for (File file : files) {
+ if (file.isDirectory()) {
+ assert !file.getName().contains(".");
+ if (packageName.equals(""))
+ classes.addAll(findClasses(file, file.getName()));
+ else
+ classes.addAll(findClasses(file, packageName + "."
+ + file.getName()));
+ } else if (file.getName().endsWith(".class")) {
+ try {
+ classes.add(Class.forName(packageName
+ + '.'
+ + file.getName().substring(0,
+ file.getName().length() - 6)));
+ } catch (NoClassDefFoundError e) {
+
+ }
+ }
+ }
+ return classes;
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServletHost.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServletHost.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServletHost.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/web/samples/ServletHost.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.ws.jaxrs.web.samples;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.nio.SelectChannelConnector;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.DefaultServlet;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+import com.ibm.ws.jaxrs.web.RESTServlet;
+
+/**
+ * Integration with Jetty
+ *
+ */
+public class ServletHost {
+
+ private Class<?>[] resourceClasses;
+
+ private int port;
+
+ private Server server;
+
+ private String applicationClassName;
+
+ private String contextRoot;
+
+ public ServletHost(int port, Class<?>... resourceClasses) {
+ this.port = port;
+ this.resourceClasses = resourceClasses;
+ this.applicationClassName = null;
+ server = new Server();
+ SelectChannelConnector connector = new SelectChannelConnector();
+ connector.setPort(port);
+ server.setConnectors(new Connector[] { connector });
+ }
+
+ public ServletHost(int port, String applicationClassName) {
+ this(port, applicationClassName, null);
+ }
+
+ public ServletHost(int port, String applicationClassName, String contextRoot) {
+ this.port = port;
+ this.resourceClasses = null;
+ this.applicationClassName = applicationClassName;
+ this.contextRoot = contextRoot;
+ server = new Server();
+ SelectChannelConnector connector = new SelectChannelConnector();
+ connector.setPort(port);
+ server.setConnectors(new Connector[] { connector });
+ }
+
+ public void init() throws Exception {
+ Context root = new Context(server, contextRoot != null ? contextRoot
+ : "/", Context.SESSIONS);
+
+ Map<String, String> initParams = new HashMap<String, String>();
+ if (applicationClassName != null) {
+ ServletHolder holder = new ServletHolder(new RESTServlet());
+ holder.setInitParameter("javax.ws.rs.Application",
+ applicationClassName);
+ initParams.put("javax.ws.rs.Application", applicationClassName);
+ root.addServlet(holder, "/*");
+ } else {
+ root.addServlet(
+ new ServletHolder(new RESTServlet(resourceClasses)), "/*");
+ }
+ root.setInitParams(initParams);
+ server.addHandler(root);
+ server.start();
+ }
+
+ /**
+ * This allows for the configuration of a servlet that will handle serving
+ * static content like HTML pages.
+ *
+ */
+ public void addFileServlet(String contextRoot, String resourceBase) {
+ Context root = new Context(server, contextRoot, Context.SESSIONS);
+ ServletHolder holder = new ServletHolder(new DefaultServlet());
+ root.addServlet(holder, "/*");
+ root.setResourceBase(resourceBase);
+ server.addHandler(root);
+ }
+
+ public void shutdown() throws Exception {
+ server.stop();
+ }
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/wsspi/jaxrs/DateHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/wsspi/jaxrs/DateHandlerTest.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/wsspi/jaxrs/DateHandlerTest.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/wsspi/jaxrs/DateHandlerTest.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ibm.wsspi.jaxrs;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+import com.ibm.wsspi.jaxrs.http.DateHandler;
+
+public class DateHandlerTest extends TestCase {
+
+ private static final String WED_15_NOV_1995_06_25_24_CST = "Wed, 15 Nov 1995 06:25:24 CST";
+
+ public void testFormatterLosesTimeZoneInfoOnParse() throws ParseException {
+ /*
+ * this test is just making sure that the behavior is understood for DateFormat.parse()
+ * After calling parse(), the timezone and other information for the latest parsed object is used
+ * on subsequent calls to DateFormat.
+ *
+ * If this test fails, that's ok as long as the other ones pass.
+ */
+ DateFormat format = new SimpleDateFormat(
+ "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
+
+ Date d = format.parse(WED_15_NOV_1995_06_25_24_CST);
+
+ format.setLenient(false);
+ format.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ assertEquals("Wed, 15 Nov 1995 12:25:24 GMT", format.format(d));
+ assertEquals("Wed, 15 Nov 1995 12:25:24 GMT", format.format(d));
+
+ /* let's do this again */
+ Date d2 = format.parse("Thu, 16 Nov 1995 08:39:42 CST");
+
+ format.setLenient(false);
+ format.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ assertEquals("Wed, 15 Nov 1995 12:25:24 GMT", format.format(d));
+ assertEquals("Wed, 15 Nov 1995 12:25:24 GMT", format.format(d));
+
+ assertEquals("Thu, 16 Nov 1995 14:39:42 GMT", format.format(d2));
+ assertEquals("Wed, 15 Nov 1995 12:25:24 GMT", format.format(d));
+
+ /*
+ * after calling parse, the DateFormat uses the time zone of the parsed object.
+ */
+
+ /* let's do this one more time */
+ format.parse("Fri, 17 Nov 1995 09:40:53 EST");
+
+ assertEquals("Wed, 15 Nov 1995 07:25:24 EST", format.format(d));
+ assertEquals("Wed, 15 Nov 1995 07:25:24 EST", format.format(d));
+
+ try {
+ format.parse("17 Nov 1995 09:40:53 EST");
+ fail("Lost the pattern");
+ } catch (ParseException e) {
+ /* good */
+ }
+ }
+
+ public void testMultipleInvocations() throws ParseException {
+ for (int c = 0; c < 10; ++c) {
+ assertEquals(
+ "Wed, 15 Nov 1995 12:25:24 GMT",
+ DateHandler
+ .formatDateInRFC1123Format(DateHandler
+ .formatStringToDateInRFC1123Only(WED_15_NOV_1995_06_25_24_CST)));
+ }
+ }
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/DelegateTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/DelegateTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/DelegateTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/DelegateTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.impl;
+
+import javax.ws.rs.core.Cookie;
+import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
+
+import junit.framework.TestCase;
+
+/**
+ * These tests will test the functionality of the various
+ * HeaderDelegate implementations.
+ *
+ */
+public class DelegateTests extends TestCase {
+
+ // Ensure null input to the fromString and toString methods
+ // always results in an IllegalArgumentException
+ public void testBadInput() throws Exception {
+ IllegalArgumentException iae = null;
+ HeaderDelegate delegate = null;
+
+ // test the CacheControlHeaderProvider
+ try {
+ delegate = new CacheControlHeaderProvider();
+ delegate.fromString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ try {
+ delegate = new CacheControlHeaderProvider();
+ delegate.toString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+
+ // test the CookieHeaderProvider
+ try {
+ delegate = new CookieHeaderProvider();
+ delegate.fromString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ try {
+ delegate = new CookieHeaderProvider();
+ delegate.toString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+
+ // test the EntityTagHeaderProvider
+ try {
+ delegate = new EntityTagHeaderProvider();
+ delegate.fromString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ try {
+ delegate = new EntityTagHeaderProvider();
+ delegate.toString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+
+ // test the MediaTypeHeaderProvider
+ try {
+ delegate = new MediaTypeHeaderProvider();
+ delegate.fromString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ try {
+ delegate = new MediaTypeHeaderProvider();
+ delegate.toString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+
+ // test the NewCookieHeaderProvider
+ try {
+ delegate = new NewCookieHeaderProvider();
+ delegate.fromString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ try {
+ delegate = new NewCookieHeaderProvider();
+ delegate.toString(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+ }
+
+ // Ensure we get a version even when one is not specified
+ public void testParseCookieStringNoVersion() throws Exception {
+ CookieHeaderProvider provider = new CookieHeaderProvider();
+ Cookie cookie = provider.fromString("NAME_1=Value_1;");
+ assertNotNull(cookie);
+ assertEquals(0, cookie.getVersion());
+ assertEquals("NAME_1", cookie.getName());
+ assertEquals("Value_1", cookie.getValue());
+ }
+
+ // Another cookie parsing test, this time the version is included
+ public void testParseCookieString() throws Exception {
+ CookieHeaderProvider provider = new CookieHeaderProvider();
+ Cookie cookie = provider
+ .fromString("$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"");
+ assertNotNull(cookie);
+ assertEquals(1, cookie.getVersion());
+ assertEquals("/acme", cookie.getPath());
+ assertEquals("Customer", cookie.getName());
+ assertEquals("WILE_E_COYOTE", cookie.getValue());
+ }
+
+}
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/ResponseTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/ResponseTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/ResponseTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/org/apache/cxf/jaxrs/impl/ResponseTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,264 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.impl;
+
+import java.net.URI;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.NewCookie;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Variant;
+
+import junit.framework.TestCase;
+
+public class ResponseTests extends TestCase {
+
+ public void testBuildResponseWithVariant() throws Exception {
+ Response resp = Response.ok("blah",
+ new Variant(null, new Locale("en", "US"), null)).build();
+ String cl = (String) resp.getMetadata().getFirst("Content-Language");
+ assertEquals("en_US", cl);
+ }
+
+ public void testBuildResponseCookies() throws Exception {
+ NewCookie cookie = new NewCookie("cookie1", "value1");
+ Response resp = Response.ok().cookie(cookie).build();
+ assertNotNull(resp.getMetadata().getFirst("Set-Cookie"));
+ String cookieStr = (String) resp.getMetadata().getFirst("Set-Cookie");
+ assertEquals(cookie.toString(), cookieStr);
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().cookie(cookie).cookie((NewCookie) null).build();
+ assertNull(resp.getMetadata().getFirst("Set-Cookie"));
+ }
+
+ public void testBuildResponseContentType() throws Exception {
+ MediaType type = new MediaType("text", "xml");
+ Response resp = Response.ok().type(type).build();
+ assertNotNull(resp.getMetadata().getFirst("Content-Type"));
+ assertEquals("text/xml", resp.getMetadata().getFirst("Content-Type"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().type(type).type((MediaType) null).build();
+ assertNull(resp.getMetadata().getFirst("Content-Type"));
+
+ String typeStr = "text/xml";
+ resp = Response.ok().type(typeStr).build();
+ assertNotNull(resp.getMetadata().getFirst("Content-Type"));
+ assertEquals("text/xml", resp.getMetadata().getFirst("Content-Type"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().type(typeStr).type((String) null).build();
+ assertNull(resp.getMetadata().getFirst("Content-Type"));
+ }
+
+ public void testBuildVariants() throws Exception {
+ Variant variant = new Variant(new MediaType("text", "xml"), new Locale(
+ "en", "US"), null);
+ Response resp = Response.ok().variant(variant).build();
+
+ // make sure null has the intended effect
+ variant = new Variant(new MediaType("text", "xml"), new Locale("en",
+ "US"), null);
+ resp = Response.ok().variant(variant).variants(null).build();
+ List<Object> varies = resp.getMetadata().get("Vary");
+ assertNull(varies);
+ }
+
+ public void testBuildResponseContentLanguage() throws Exception {
+ Locale locale = new Locale("en", "US");
+ Response resp = Response.ok().language(locale).build();
+ assertNotNull(resp.getMetadata().getFirst("Content-Language"));
+ assertEquals("en_US", resp.getMetadata().getFirst("Content-Language"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().language(locale).language((Locale) null).build();
+ assertNull(resp.getMetadata().getFirst("Content-Language"));
+
+ String localeStr = "en_US";
+ resp = Response.ok().language(localeStr).build();
+ assertNotNull(resp.getMetadata().getFirst("Content-Language"));
+ assertEquals("en_US", resp.getMetadata().getFirst("Content-Language"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().language(localeStr).language((String) null)
+ .build();
+ assertNull(resp.getMetadata().getFirst("Content-Language"));
+ }
+
+ public void testBuildResponseLocation() throws Exception {
+ URI uri = new URI("http://www.testrest.com");
+ Response resp = Response.ok().location(uri).build();
+ assertNotNull(resp.getMetadata().getFirst("Location"));
+ assertEquals(uri.toString(), resp.getMetadata().getFirst("Location"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().location(uri).location(null).build();
+ assertNull(resp.getMetadata().getFirst("Location"));
+ }
+
+ public void testBuildResponseContentLocation() throws Exception {
+ URI uri = new URI("http://www.testrest.com");
+ Response resp = Response.ok().contentLocation(uri).build();
+ assertNotNull(resp.getMetadata().getFirst("Content-Location"));
+ assertEquals(uri.toString(), resp.getMetadata().getFirst(
+ "Content-Location"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().contentLocation(uri).contentLocation(null).build();
+ assertNull(resp.getMetadata().getFirst("Content-Location"));
+ }
+
+ public void testBuildResponseETag() throws Exception {
+ EntityTag eTag = new EntityTag("blah");
+ Response resp = Response.ok().tag(eTag).build();
+ assertNotNull(resp.getMetadata().getFirst("ETag"));
+ assertEquals(eTag.toString(), resp.getMetadata().getFirst("ETag"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().tag(eTag).tag((EntityTag) null).build();
+ assertNull(resp.getMetadata().getFirst("ETag"));
+
+ String eTagStr = "blah";
+ resp = Response.ok().tag(eTagStr).build();
+ assertNotNull(resp.getMetadata().getFirst("ETag"));
+ assertEquals(eTagStr, resp.getMetadata().getFirst("ETag"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().tag(eTagStr).tag((String) null).build();
+ assertNull(resp.getMetadata().getFirst("ETag"));
+ }
+
+ public void testBuildResponseLastModified() throws Exception {
+ final SimpleDateFormat rfc1123Format = new SimpleDateFormat(
+ "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ Date date = Calendar.getInstance().getTime();
+ Response resp = Response.ok().lastModified(date).build();
+ assertNotNull(resp.getMetadata().getFirst("Last-Modified"));
+ assertEquals(rfc1123Format.format(date), resp.getMetadata().getFirst(
+ "Last-Modified"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().lastModified(date).lastModified(null).build();
+ assertNull(resp.getMetadata().getFirst("Last-Modified"));
+ }
+
+ public void testBuildResponseExpires() throws Exception {
+ Date date = Calendar.getInstance().getTime();
+ Response resp = Response.ok().expires(date).build();
+ assertNotNull(resp.getMetadata().getFirst("Expires"));
+ assertEquals(date.toString(), resp.getMetadata().getFirst("Expires"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().expires(date).expires(null).build();
+ assertNull(resp.getMetadata().getFirst("Expires"));
+ }
+
+ public void testBuildResponseCacheControl() throws Exception {
+ CacheControl cache = new CacheControl();
+ cache.setMaxAge(100);
+ Response resp = Response.ok().cacheControl(cache).build();
+ assertNotNull(resp.getMetadata().getFirst("Cache-Control"));
+ assertEquals(cache.toString(), resp.getMetadata().getFirst(
+ "Cache-Control"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().cacheControl(cache).cacheControl(null).build();
+ assertNull(resp.getMetadata().getFirst("Cache-Control"));
+ }
+
+ public void testBuildResponseHeader() throws Exception {
+ Response resp = Response.ok().header("header1", "value1").build();
+ assertNotNull(resp.getMetadata().getFirst("header1"));
+ assertEquals("value1", resp.getMetadata().getFirst("header1"));
+
+ // now make sure passing null has the intended effect
+ resp = Response.ok().header("header1", "value1")
+ .header("header1", null).build();
+ assertNull(resp.getMetadata().getFirst("header1"));
+ }
+
+ public void testBuildResponseInvalidStatus() throws Exception {
+ IllegalArgumentException iae = null;
+ try {
+ Response.ok().status(null);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+ iae = null;
+
+ // now make sure status is in valid range
+ try {
+ Response.ok().status(99);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNotNull(iae);
+
+ iae = null;
+ try {
+ Response.ok().status(600);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+
+ iae = null;
+ try {
+ Response.ok().status(599);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNull(iae);
+
+ try {
+ Response.ok().status(100);
+ } catch (IllegalArgumentException e) {
+ iae = e;
+ }
+ assertNull(iae);
+ }
+
+ public void testResponseWithVariantOk() throws Exception {
+ String string = "blah";
+ Variant variant = new Variant(new MediaType("text", "xml"), new Locale(
+ "en", "US"), null);
+ Response resp = Response.ok(string, variant).build();
+ assertEquals(200, resp.getStatus());
+ }
+
+ public void testResponseWithNotModified() throws Exception {
+ Response resp = Response.notModified("blah").build();
+ Object obj = resp.getMetadata().getFirst("ETAG");
+ assertNotNull(obj);
+ assertEquals("blah", obj);
+ }
+
+}