incubator-wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ngalla...@apache.org
Subject svn commit: r787553 [26/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...
Date Tue, 23 Jun 2009 05:38:19 GMT
Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/core/UriInfoTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/core/UriInfoTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/core/UriInfoTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/core/UriInfoTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,425 @@
+/*
+ * 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.core;
+
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+
+import junit.framework.TestCase;
+
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.web.ServletTestFramework;
+import com.ibm.ws.jaxrs.web.mock.ServletRequest;
+import com.ibm.ws.jaxrs.web.mock.ServletResponse;
+
+/**
+ * This will verify the functionality in our UriInfo implementation.
+ *
+ */
+public class UriInfoTests extends TestCase {
+
+    public void testGetAbsolutePath() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments?id=sales&org=worldwide";
+
+        // don't include the query parameters in what we compare the result to
+        String absURL = "http://localhost:9080/resources/departments";
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        URI uri = uriInfo.getAbsolutePath();
+        assertNotNull(uri);
+        assertEquals(absURL, uri.toString());
+    }
+
+    public void testGetAbsolutePathBuilder() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments?id=sales&org=worldwide";
+
+        // don't include the query parameters in what we compare the result to
+        String absURL = "http://localhost:9080/resources/departments";
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
+        assertNotNull(uriBuilder);
+        assertEquals(absURL, uriBuilder.build().toString());
+    }
+
+    public void testGetBaseUri() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put(ServletRequest.PATH_INFO, "/departments");
+        String url = "http://localhost:9080/resources/departments";
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        URI baseUri = uriInfo.getBaseUri();
+        assertNotNull(baseUri);
+        assertEquals("http://localhost:9080/resources", baseUri.toString());
+    }
+
+    public void testGetBaseUriBuilder() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments";
+        props.put(ServletRequest.CONTEXT_PATH, "/resources");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
+        assertNotNull(baseUriBuilder);
+        assertEquals("http://localhost:9080/resources", baseUriBuilder.build()
+                .toString());
+    }
+
+    public void testGetPath() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments";
+        props.put(ServletRequest.PATH_INFO, "/departments");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        String path = uriInfo.getPath();
+        assertNotNull(path);
+        assertEquals("/departments", path);
+    }
+
+    public void testGetPathWithEarlierContextRootString() throws Exception {
+        // in this case, the context root just happens to have a substring
+        // that matches the real path
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments/endofcontextroot/departments";
+        props.put(ServletRequest.PATH_INFO, "/departments");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        String path = uriInfo.getPath();
+        assertNotNull(path);
+        assertEquals("/departments", path);
+    }
+
+    public void testGetPathSegments() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departmentsByName/sales";
+        props.put(ServletRequest.PATH_INFO, "/departmentsByName/sales");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        List<PathSegment> segments = uriInfo.getPathSegments();
+        assertNotNull(segments);
+        assertEquals(2, segments.size());
+        PathSegment segment = segments.get(0);
+        assertEquals("departmentsByName", segment.getPath());
+        segment = segments.get(1);
+        assertEquals("sales", segment.getPath());
+
+    }
+
+    public void testGetRequestUri() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments/sales?id=sales&org=worldwide";
+        props.put(ServletRequest.PATH_INFO, "/departments/sales");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        URI uri = uriInfo.getRequestUri();
+        assertNotNull(uri);
+        assertEquals("http://localhost:9080/resources/departments/sales", uri
+                .toString());
+    }
+
+    public void testGetRequestUriBuilder() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments/sales?id=sales&org=worldwide";
+        props.put(ServletRequest.PATH_INFO, "/departments/sales");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        UriBuilder uriBuilder = uriInfo.getRequestUriBuilder();
+        assertNotNull(uriBuilder);
+        assertEquals("http://localhost:9080/resources/departments/sales",
+                uriBuilder.build().toString());
+    }
+
+    public void testGetQueryParameters() throws Exception {
+        RESTContext context = new RESTContext();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments";
+        props.put(ServletRequest.QUERY_STRING, "id=tech sales&org=worldwide");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        MultivaluedMap<String, String> queryParams = uriInfo
+                .getQueryParameters();
+        assertNotNull(queryParams);
+        List<String> paramValues = queryParams.get("id");
+        assertNotNull(paramValues);
+        assertEquals(1, paramValues.size());
+        assertEquals("tech sales", paramValues.get(0));
+        paramValues = queryParams.get("org");
+        assertNotNull(paramValues);
+        assertEquals(1, paramValues.size());
+        assertEquals("worldwide", paramValues.get(0));
+
+        // let's test multiple query param keys with different values
+        url = "http://localhost:9080/resources/departments";
+        props.put(ServletRequest.QUERY_STRING,
+                "id=tech sales&org=worldwide&id=north america");
+        request = new ServletRequest(url, null, props);
+        response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        uriInfo = new UriInfoImpl(context);
+        queryParams = uriInfo.getQueryParameters();
+        assertNotNull(queryParams);
+        paramValues = queryParams.get("id");
+        assertNotNull(paramValues);
+        assertEquals(2, paramValues.size());
+        assertEquals("tech sales", paramValues.get(0));
+        assertEquals("north america", paramValues.get(1));
+        paramValues = queryParams.get("org");
+        assertNotNull(paramValues);
+        assertEquals(1, paramValues.size());
+        assertEquals("worldwide", paramValues.get(0));
+    }
+
+    public void testGetPathParameters() throws Exception {
+        RESTContext context = new RESTContext();
+        MultivaluedMap<String, String> templateParams = new MetadataMap<String, String>();
+        Map<String, Object> props = new HashMap<String, Object>();
+        String url = "http://localhost:9080/resources/departments/northamerica/texas/sales";
+        props.put(ServletRequest.CONTEXT_PATH, "/resources");
+        props.put(ServletRequest.PATH_INFO,
+                "/departments/northamerica/texas/sales");
+        props.put(ServletRequest.CHAR_ENCODING, "UTF-8");
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        templateParams.add("region", "northamerica");
+        templateParams.add("state", "texas");
+        templateParams.add("departmentName", "sales");
+        context.setProperty(ContextConstants.TEMPLATE_VALUES, templateParams);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
+        assertNotNull(pathParams);
+        assertEquals(3, pathParams.size());
+        String paramValue = pathParams.get("region").get(0);
+        assertNotNull(paramValue);
+        assertEquals("northamerica", paramValue);
+        paramValue = pathParams.get("state").get(0);
+        assertNotNull(paramValue);
+        assertEquals("texas", paramValue);
+        paramValue = pathParams.get("departmentName").get(0);
+        assertNotNull(paramValue);
+        assertEquals("sales", paramValue);
+    }
+
+    public void testGetMatchedUris() throws Exception {
+
+        String url = "http://localhost:9080/resources/departments/northamerica/texas/sales";
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put(ServletRequest.CHAR_ENCODING, "UTF-8");
+
+        // simple test with one matched URI
+        RESTContext context = new RESTContext();
+        ServletRequest request = new ServletRequest(url, null, props);
+        ServletResponse response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        List<String> matchedList = new LinkedList<String>();
+        matchedList.add("foo");
+        context.setProperty(ContextConstants.MATCHED_URIS, matchedList);
+
+        UriInfo uriInfo = new UriInfoImpl(context);
+        List<String> matchedUris = uriInfo.getMatchedURIs();
+        assertNotNull(matchedUris);
+        assertEquals(1, matchedUris.size());
+        assertEquals("foo", matchedUris.get(0));
+
+        // two matched URIs
+        context = new RESTContext();
+        request = new ServletRequest(url, null, props);
+        response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        matchedList = new LinkedList<String>();
+        matchedList.add("foo");
+        matchedList.add("foo/entry");
+        context.setProperty(ContextConstants.MATCHED_URIS, matchedList);
+        uriInfo = new UriInfoImpl(context);
+        matchedUris = uriInfo.getMatchedURIs();
+        assertNotNull(matchedUris);
+        assertEquals(2, matchedUris.size());
+        assertEquals("foo/entry", matchedUris.get(0));
+        assertEquals("foo", matchedUris.get(1));
+
+        // matched uri to subresource locator
+        context = new RESTContext();
+        request = new ServletRequest(url, null, props);
+        response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        matchedList = new LinkedList<String>();
+        matchedList.add("foo");
+        matchedList.add("foo/bar/1");
+        context.setProperty(ContextConstants.MATCHED_URIS, matchedList);
+        uriInfo = new UriInfoImpl(context);
+        matchedUris = uriInfo.getMatchedURIs();
+        assertNotNull(matchedUris);
+        assertEquals(2, matchedUris.size());
+        assertEquals("foo/bar/1", matchedUris.get(0));
+        assertEquals("foo", matchedUris.get(1));
+
+        // ensure that we properly decode
+        context = new RESTContext();
+        request = new ServletRequest(url, null, props);
+        response = new ServletResponse(new StringWriter());
+        ServletTestFramework.initContext(request, response, context);
+        matchedList = new LinkedList<String>();
+        matchedList.add("foo");
+        matchedList.add("foo/bar/1%20A");
+        context.setProperty(ContextConstants.MATCHED_URIS, matchedList);
+        context.setProperty(ContextConstants.SERVLET_REQUEST, request);
+        uriInfo = new UriInfoImpl(context);
+        matchedUris = uriInfo.getMatchedURIs();
+        assertNotNull(matchedUris);
+        assertEquals(2, matchedUris.size());
+        assertEquals("foo/bar/1 A", matchedUris.get(0));
+        assertEquals("foo", matchedUris.get(1));
+
+        // ensure the underlying list is read-only
+        Exception roException = null;
+        try {
+            matchedUris.remove(0);
+        } catch (Exception e) {
+            roException = e;
+        }
+        assertNotNull(roException);
+    }
+
+    public void testGetMatchedUrisEncoded() {
+        RESTContext context = new RESTContext();
+        List<String> matchedList = new LinkedList<String>();
+        matchedList.add("foo");
+        matchedList.add("foo/bar/1%20A");
+        context.setProperty(ContextConstants.MATCHED_URIS, matchedList);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        List<String> matchedUris = uriInfo.getMatchedURIs(false);
+        assertNotNull(matchedUris);
+        assertEquals(2, matchedUris.size());
+        assertEquals("foo/bar/1%20A", matchedUris.get(0));
+        assertEquals("foo", matchedUris.get(1));
+    }
+
+    public void testGetMatchedResources() {
+
+        // test simple case of one matched resource
+        List<Object> matchedRes = new LinkedList<Object>();
+        matchedRes.add(Foo.class);
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MATCHED_RESOURCES, matchedRes);
+        UriInfo uriInfo = new UriInfoImpl(context);
+        List<Object> matchedResources = uriInfo.getMatchedResources();
+        assertNotNull(matchedResources);
+        assertEquals(1, matchedResources.size());
+        assertEquals(Foo.class, matchedResources.get(0));
+
+        // test multiple entries
+        matchedRes = new LinkedList<Object>();
+        matchedRes.add(Foo.class);
+        matchedRes.add(Bar.class);
+        context = new RESTContext();
+        context.setProperty(ContextConstants.MATCHED_RESOURCES, matchedRes);
+        uriInfo = new UriInfoImpl(context);
+        matchedResources = uriInfo.getMatchedResources();
+        assertNotNull(matchedResources);
+        assertEquals(2, matchedResources.size());
+        assertEquals(Bar.class, matchedResources.get(0));
+        assertEquals(Foo.class, matchedResources.get(1));
+
+        // make sure the list is unmodifiable
+        Exception roException = null;
+        try {
+            matchedResources.remove(0);
+        } catch (Exception e) {
+            roException = e;
+        }
+        assertNotNull(roException);
+    }
+
+    @Path(value = "/foo")
+    public static class Foo {
+
+        @GET
+        public void get() {
+
+        }
+
+        @Path(value = "/bar/{id}")
+        public Bar getBar(String id) {
+            return null;
+        }
+
+        @POST
+        @Path(value = "/entry")
+        public void post(String entry) {
+
+        }
+    }
+
+    public static class Bar {
+
+        @GET
+        public void show() {
+
+        }
+
+    }
+
+}

Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/ext/ProvidersTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/ext/ProvidersTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/ext/ProvidersTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/ext/ProvidersTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,207 @@
+/*
+ * 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.ext;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import javax.ws.rs.ext.Providers;
+import javax.xml.bind.JAXBContext;
+
+import junit.framework.TestCase;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.metadata.RESTMetaData;
+import com.ibm.ws.jaxrs.model.ApplicationProcessor;
+import com.ibm.ws.jaxrs.model.JAXRSInfoOutput;
+
+/**
+ * This will verify the functionality of our Providers implementation.
+ *
+ */
+public class ProvidersTests extends TestCase {
+
+    private RESTContext context = new RESTContext();
+
+    @Override
+    public void setUp() {
+        context.setProperty(ContextConstants.REST_METADATA, new RESTMetaData());
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                "tempKey");
+    }
+
+    public void testGetSystemExceptionMapper() {
+        Providers providers = new ProvidersImpl(context);
+        ExceptionMapper<WebApplicationException> mapper = providers
+                .getExceptionMapper(WebApplicationException.class);
+        assertNotNull(mapper);
+    }
+
+    public void testGetSystemContextResolver() {
+        Providers providers = new ProvidersImpl(context);
+        ContextResolver<JAXBContext> jaxbResolver = providers
+                .getContextResolver(JAXBContext.class, new MediaType("text",
+                        "xml"));
+        assertNotNull(jaxbResolver);
+    }
+
+    public void testGetSystemMessageBodyReader() {
+        Providers providers = new ProvidersImpl(context);
+        MessageBodyReader<String> reader = providers.getMessageBodyReader(
+                String.class, null, null, new MediaType("text", "xml"));
+        assertNotNull(reader);
+    }
+
+    public void testGetSystemMessageBodyWriter() {
+        Providers providers = new ProvidersImpl(context);
+        MessageBodyWriter<String> writer = providers.getMessageBodyWriter(
+                String.class, null, null, new MediaType("text", "xml"));
+        assertNotNull(writer);
+    }
+
+    public void testGetUserProviders() throws Exception {
+        final List<Object> userProviders = new LinkedList<Object>();
+        userProviders.add(new CustomEntityProvider());
+        userProviders.add(new CustomExceptionMapper());
+        userProviders.add(new CustomContextResolver());
+        ApplicationProcessor processor = new ApplicationProcessor(null, Thread
+                .currentThread().getContextClassLoader());
+        JAXRSInfoOutput output = processor
+                .processApplication(new Application() {
+
+                    @Override
+                    public Set<Class<?>> getClasses() {
+                        return null;
+                    }
+
+                    @Override
+                    public Set<Object> getSingletons() {
+                        return new HashSet<Object>(userProviders);
+                    }
+
+                });
+
+        RESTMetaData metadata = new RESTMetaData();
+        metadata.getProviderInfoList().addAll(output.getProviderInfoList());
+        context.setProperty(ContextConstants.REST_METADATA, metadata);
+
+        ProvidersImpl providers = new ProvidersImpl(context);
+        ContextResolver<CustomContext> customResolver = providers
+                .getContextResolver(CustomContext.class, null);
+        assertNotNull(customResolver);
+        assertEquals(CustomContextResolver.class, customResolver.getClass());
+
+        ExceptionMapper<CustomException> mapper = providers
+                .getExceptionMapper(CustomException.class);
+        assertNotNull(mapper);
+        assertEquals(CustomExceptionMapper.class, mapper.getClass());
+
+        MessageBodyReader<CustomObject> reader = providers
+                .getMessageBodyReader(CustomObject.class, null, null,
+                        new MediaType("application", "custom"));
+        assertNotNull(reader);
+        assertEquals(CustomEntityProvider.class, reader.getClass());
+
+        MessageBodyWriter<CustomObject> writer = providers
+                .getMessageBodyWriter(CustomObject.class, null, null,
+                        new MediaType("application", "custom"));
+        assertNotNull(writer);
+        assertEquals(CustomEntityProvider.class, writer.getClass());
+    }
+
+    public class CustomObject {
+
+    }
+
+    public class CustomContext {
+
+    }
+
+    public class CustomException extends Exception {
+
+    }
+
+    @Provider
+    @Produces("application/custom")
+    @Consumes("application/custom")
+    public static class CustomEntityProvider implements MessageBodyReader<CustomObject>, MessageBodyWriter<CustomObject> {
+
+        public long getSize(CustomObject arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+            return 0;
+        }
+
+        public boolean isWriteable(Class<?> clazz, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return clazz == CustomObject.class;
+        }
+
+        public void writeTo(CustomObject arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4, MultivaluedMap<String, Object> arg5, OutputStream arg6)
+                throws IOException, WebApplicationException {
+        }
+
+        public boolean isReadable(Class<?> clazz, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return clazz == CustomObject.class;
+        }
+
+        public CustomObject readFrom(Class<CustomObject> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
+                throws IOException, WebApplicationException {
+            return null;
+        }
+
+    }
+
+    @Provider
+    public static class CustomContextResolver implements ContextResolver<CustomContext> {
+
+        public CustomContext getContext(Class<?> arg0) {
+            return null;
+        }
+
+    }
+
+    @Provider
+    public static class CustomExceptionMapper implements ExceptionMapper<CustomException> {
+
+        public Response toResponse(CustomException arg0) {
+            return null;
+        }
+
+    }
+
+}

Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/ContextValidationHandlerTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/ContextValidationHandlerTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/ContextValidationHandlerTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/ContextValidationHandlerTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,223 @@
+/*
+ * 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.handler.internal;
+
+import java.io.ByteArrayOutputStream;
+
+import junit.framework.TestCase;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.engine.RESTConstants;
+
+public class ContextValidationHandlerTests extends TestCase {
+
+    // Ensure path information is available on RESTContext
+    public void testValidatePathInfo() throws Exception {
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        Exception e = null;
+        ContextValidator handler = new ContextValidator();
+
+        // first make sure if nothing is set we throw an exception
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty("something", "somevalue");
+
+        // now let's check when we have a property, but no path information
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/resources/parkinglot/10/floor/1");
+        context.setProperty(ContextConstants.HTTP_REQUEST_CONTEXT_PATH,
+                "resources");
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM,
+                new ByteArrayOutputStream());
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                new Object());
+
+        // now let's set the request URL and context path and make sure the path gets set
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+        String pathInfo = (String) context
+                .getProperty(ContextConstants.HTTP_PATH_INFO);
+        assertEquals("/parkinglot/10/floor/1", pathInfo);
+
+        // now let's make sure when the path information is set the validation is successful
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+    }
+
+    // Ensure HTTP method is available on RESTContext
+    public void testValidateHttpMethod() throws Exception {
+        ContextValidator handler = new ContextValidator();
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/somepath");
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM,
+                new ByteArrayOutputStream());
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/resources/somepath");
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                new Object());
+        Exception e = null;
+
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+    }
+
+    // Ensure OutputStream is available on RESTContext
+    public void testValidateOutputStream() throws Exception {
+        ContextValidator handler = new ContextValidator();
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/somepath");
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/resources/somepath");
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                new Object());
+        Exception e = null;
+
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM,
+                new ByteArrayOutputStream());
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+    }
+
+    // Ensure full request URL is available on RESTContext
+    public void testValidateRequestURL() throws Exception {
+        ContextValidator handler = new ContextValidator();
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/somepath");
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM,
+                new ByteArrayOutputStream());
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                new Object());
+        Exception e = null;
+
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/resources/somepath");
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+    }
+
+    // Ensure full request URL is available on RESTContext
+    public void testValidateIntegrationRegistrationKey() throws Exception {
+        ContextValidator handler = new ContextValidator();
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/somepath");
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/resources/somepath");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM,
+                new ByteArrayOutputStream());
+        Exception e = null;
+
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNotNull(e);
+        e = null;
+
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                new Object());
+        try {
+            handler.invoke(context);
+        } catch (IllegalArgumentException iae) {
+            e = iae;
+        }
+        assertNull(e);
+    }
+
+}

Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/MetadataHandlerTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/MetadataHandlerTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/MetadataHandlerTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/handler/internal/MetadataHandlerTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,130 @@
+/*
+ * 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.handler.internal;
+
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+
+import junit.framework.TestCase;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.engine.RESTConstants;
+import com.ibm.ws.jaxrs.integration.ApplicationProvider;
+import com.ibm.ws.jaxrs.integration.DefaultApplication;
+import com.ibm.ws.jaxrs.integration.IntegrationRegistry;
+import com.ibm.ws.jaxrs.integration.MetaDataProvider;
+import com.ibm.ws.jaxrs.metadata.RESTMetaData;
+import com.ibm.ws.jaxrs.web.ServletMetadataProvider;
+
+public class MetadataHandlerTests extends TestCase {
+
+    public void testBuildMetadataWithCache() throws Exception {
+        Object integrationRegistrationKey = new Object();
+
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.METADATA_KEY, "key");
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestApplicationProvider());
+        IntegrationRegistry
+                .addIntegrationProvider(integrationRegistrationKey,
+                        MetaDataProvider.class, new ServletMetadataProvider());
+
+        MetadataLocator handler = new MetadataLocator();
+        handler.invoke(context);
+        RESTMetaData metaData = (RESTMetaData) context
+                .getProperty(ContextConstants.REST_METADATA);
+        assertNotNull(metaData);
+        assertNotNull(metaData.getClassInfoList());
+        assertEquals(1, metaData.getClassInfoList().size());
+
+        // make sure the second invoke results in the same cache being returned since we have
+        // the same key set on the RESTContext
+        handler.invoke(context);
+        RESTMetaData newMetaData = (RESTMetaData) context
+                .getProperty(ContextConstants.REST_METADATA);
+        assertTrue("Expected same instance of RESTMetaData on second invoke",
+                metaData.equals(newMetaData));
+    }
+
+    public void testBuildMetadataWithNoCache() throws Exception {
+        Object integrationRegistrationKey = new Object();
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.MESSAGE_FLOW,
+                RESTConstants.MESSAGE_FLOW_INBOUND);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestApplicationProvider());
+        IntegrationRegistry
+                .addIntegrationProvider(integrationRegistrationKey,
+                        MetaDataProvider.class, new ServletMetadataProvider());
+
+        MetadataLocator handler = new MetadataLocator();
+        handler.invoke(context);
+        RESTMetaData metaData = (RESTMetaData) context
+                .getProperty(ContextConstants.REST_METADATA);
+        assertNotNull(metaData);
+        assertNotNull(metaData.getClassInfoList());
+        assertEquals(1, metaData.getClassInfoList().size());
+
+        // make sure the second invoke results in the same cache being returned since we have
+        // the same key set on the RESTContext
+        handler.invoke(context);
+        RESTMetaData newMetaData = (RESTMetaData) context
+                .getProperty(ContextConstants.REST_METADATA);
+        assertFalse(
+                "Expected different instance of RESTMetaData on second invoke",
+                metaData.equals(newMetaData));
+    }
+
+    public static class TestApplicationProvider implements ApplicationProvider {
+
+        public List<Application> getApplicationClasses(RESTContext context)
+                throws Exception {
+            List<Application> applications = new LinkedList<Application>();
+            Set<Class<?>> classes = new HashSet<Class<?>>();
+            classes.add(TestResource.class);
+            applications.add(new DefaultApplication(classes, null));
+            return applications;
+        }
+
+    }
+
+    @Path(value = "/test")
+    public static class TestResource {
+
+        @GET
+        public String get() {
+            return "Hello";
+        }
+
+    }
+}

Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/IntegrationTests.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/IntegrationTests.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/IntegrationTests.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,481 @@
+/*
+ * 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.integration;
+
+import java.io.ByteArrayInputStream;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+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.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+
+import com.ibm.ws.jaxrs.context.ContextConstants;
+import com.ibm.ws.jaxrs.context.RESTContext;
+import com.ibm.ws.jaxrs.engine.RESTEngine;
+import com.ibm.ws.jaxrs.web.mock.Principal;
+import com.ibm.ws.jaxrs.web.mock.ServletOutputStream;
+import com.ibm.ws.jaxrs.web.mock.ServletRequest;
+
+/**
+ * Collection of tests to verify the integration capabilities of the
+ * consumers of the IBM JAX-RS runtime.
+ *
+ */
+public class IntegrationTests extends TestCase {
+
+    // Test designed to ensure that a request can be received given a minimal set of inputs.
+    public void testSparseRequest() throws Exception {
+        List<Application> apps = new LinkedList<Application>();
+        apps.add(new MyApplication());
+
+        Object integrationRegistrationKey = new Object();
+
+        // register the ApplicationProvider
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestAppProvider(apps));
+        StringWriter sw = new StringWriter();
+        ServletOutputStream os = new ServletOutputStream(sw);
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/resource");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        RESTContext respContext = null;
+        try {
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        Response response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // make sure the content length was set
+        Integer length = (Integer) respContext
+                .getProperty(ContextConstants.RESPONSE_CONTENT_LENGTH);
+        assertNotNull(length);
+        assertTrue(
+                "Response content length was less than than or equal to zero",
+                length > 0);
+
+        // get the entity that was written to the OutputStream
+        String resp = sw.toString();
+        assertNotNull("Return content was null", resp);
+        assertEquals("Hello!", resp);
+
+        IntegrationRegistry.removeIntegrationProvider(
+                integrationRegistrationKey, null);
+    }
+
+    // Tests that minimal information can be sent when a request needs HTTP request header information.
+    public void testHeaderInjection() throws Exception {
+        List<Application> apps = new LinkedList<Application>();
+        apps.add(new MyApplication());
+
+        Object integrationRegistrationKey = new Object();
+
+        // register the ApplicationProvider
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestAppProvider(apps));
+        StringWriter sw = new StringWriter();
+        ServletOutputStream os = new ServletOutputStream(sw);
+        RESTContext context = new RESTContext();
+
+        // build HTTP header info
+        MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
+        headers.putSingle("customHeader".toLowerCase(), "customValue");
+
+        context.setProperty(ContextConstants.HTTP_HEADER_VALUES, headers);
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO,
+                "/resource/headers/customHeader");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource/"
+                        + "headers/customHeader");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        RESTContext respContext = null;
+        try {
+
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        Response response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // make sure the content length was set
+        Integer length = (Integer) respContext
+                .getProperty(ContextConstants.RESPONSE_CONTENT_LENGTH);
+        assertNotNull(length);
+        assertTrue(
+                "Response content length was less than than or equal to zero",
+                length > 0);
+
+        // get the entity that was written to the OutputStream
+        String resp = (String) response.getEntity();
+        assertNotNull("Entity of javax.ws.rs.core.Response was null", resp);
+        assertEquals("customValue", resp);
+
+        IntegrationRegistry.removeIntegrationProvider(
+                integrationRegistrationKey, null);
+    }
+
+    // Tests that minimal information can be sent when a request needs security context information.
+    public void testSecurityInjection() throws Exception {
+        List<Application> apps = new LinkedList<Application>();
+        apps.add(new MyApplication());
+
+        Object integrationRegistrationKey = new Object();
+
+        // register the ApplicationProvider
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestAppProvider(apps));
+        StringWriter sw = new StringWriter();
+        ServletOutputStream os = new ServletOutputStream(sw);
+        RESTContext context = new RESTContext();
+
+        // build the security principal
+        Principal principal = new Principal("testUser1");
+
+        context.setProperty(ContextConstants.USER_PRINCIPAL, principal);
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO,
+                "/resource/security/principal");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource/"
+                        + "security/principal");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        RESTContext respContext = null;
+        try {
+
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        Response response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // get the entity that was written to the OutputStream
+        String resp = (String) response.getEntity();
+        assertNotNull("Entity of javax.ws.rs.core.Response was null", resp);
+        assertEquals("testUser1", resp);
+
+        // make sure the content length was set
+        Integer length = (Integer) respContext
+                .getProperty(ContextConstants.RESPONSE_CONTENT_LENGTH);
+        assertNotNull(length);
+        assertTrue(
+                "Response content length was less than than or equal to zero",
+                length > 0);
+
+        // the SecurityContext.isUserInRole still requires an HttpServletRequest object
+        Map<String, Object> props = new HashMap<String, Object>();
+        List<String> usersInRole = new LinkedList<String>();
+        usersInRole.add("testUser1");
+        props.put(ServletRequest.ROLES_FOR_USER, usersInRole);
+        ServletRequest request = new ServletRequest(props);
+
+        sw = new StringWriter();
+        os = new ServletOutputStream(sw);
+        context = new RESTContext();
+        context.setProperty(ContextConstants.SERVLET_REQUEST, request);
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO,
+                "/resource/security/userinrole/testUser1");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource/"
+                        + "security/userinrole/testUser1");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        respContext = null;
+        try {
+
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // make sure the content length was set
+        length = (Integer) respContext
+                .getProperty(ContextConstants.RESPONSE_CONTENT_LENGTH);
+        assertNotNull(length);
+        assertTrue(
+                "Response content length was less than than or equal to zero",
+                length > 0);
+
+        // get the entity that was written to the OutputStream
+        resp = (String) response.getEntity();
+        assertNotNull("Entity of javax.ws.rs.core.Response was null", resp);
+        assertTrue("The expected response was 'true' but was: " + resp, resp
+                .equalsIgnoreCase("true"));
+
+        // just to make sure nothing is set
+        IntegrationRegistry.removeIntegrationProvider(
+                integrationRegistrationKey, null);
+    }
+
+    // Tests we can send a post request with body data by simply setting the InputStream on
+    // the request RESTContext
+    public void testPostWithData() {
+        List<Application> apps = new LinkedList<Application>();
+        apps.add(new MyApplication());
+
+        Object integrationRegistrationKey = new Object();
+
+        // register the ApplicationProvider
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestAppProvider(apps));
+        StringWriter sw = new StringWriter();
+        ServletOutputStream os = new ServletOutputStream(sw);
+        RESTContext context = new RESTContext();
+
+        // setup the InputStream
+        String input = "Hello Post!";
+        ByteArrayInputStream is = new ByteArrayInputStream(input.getBytes());
+
+        // both an InputStream and Content-Type are needed for requests with data
+        context.setProperty(ContextConstants.REQUEST_INPUT_STREAM, is);
+        context.setProperty(ContextConstants.CONTENT_TYPE, "text/xml");
+        context.setProperty(ContextConstants.HTTP_METHOD, "POST");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/resource");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        RESTContext respContext = null;
+        try {
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        Response response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // check that the header was added to the response
+        Object data = response.getMetadata().getFirst("postedData");
+        assertNotNull("Expected 'postedData' header not found in response",
+                data);
+        assertEquals("Hello Post!", data);
+
+        // just to make sure nothing is set
+        IntegrationRegistry.removeIntegrationProvider(
+                integrationRegistrationKey, null);
+    }
+
+    // Verify a custom ResponseWriter can be supplied
+    public void testCustomResponseWriter() {
+        List<Application> apps = new LinkedList<Application>();
+        apps.add(new MyApplication());
+
+        Object integrationRegistrationKey = new Object();
+
+        // register the ApplicationProvider
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ApplicationProvider.class, new TestAppProvider(apps));
+
+        // register our ResponseWriter
+        IntegrationRegistry.addIntegrationProvider(integrationRegistrationKey,
+                ResponseWriter.class, new MyResponseWriter());
+
+        StringWriter sw = new StringWriter();
+        ServletOutputStream os = new ServletOutputStream(sw);
+        RESTContext context = new RESTContext();
+        context.setProperty(ContextConstants.HTTP_METHOD, "GET");
+        context.setProperty(ContextConstants.HTTP_PATH_INFO, "/resource");
+        context.setProperty(ContextConstants.HTTP_REQUEST_URL,
+                "http://localhost:7070/context/resource");
+        context.setProperty(ContextConstants.RESPONSE_OUTPUT_STREAM, os);
+        context.setProperty(ContextConstants.INTEGRATION_REGISTRATION_KEY,
+                integrationRegistrationKey);
+        RESTContext respContext = null;
+        try {
+            // invoke the RESTEngine
+            respContext = RESTEngine.invoke(context);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail(t.toString());
+        }
+
+        // get the javax.ws.rs.core.Response from the response RESTContext
+        Response response = (Response) respContext
+                .getProperty(ContextConstants.RESPONSE);
+        assertNotNull(
+                "Expected javax.ws.rs.core.Response not found on RESTContext",
+                response);
+
+        // make sure the content length was set
+        Integer length = (Integer) respContext
+                .getProperty(ContextConstants.RESPONSE_CONTENT_LENGTH);
+        assertNotNull(length);
+        // zero here because our custom writer does not write the response to the stream
+        assertTrue("Response content length was greater than zero", length
+                .intValue() == 0);
+
+        // get the entity that was written to the OutputStream
+        String resp = (String) response.getEntity();
+        assertNotNull("Entity of javax.ws.rs.core.Response was null", resp);
+        assertEquals("Hello!", resp);
+
+        // verify we did not write anything to the stream
+        assertEquals("", sw.toString());
+
+        // just to make sure nothing is set
+        IntegrationRegistry.removeIntegrationProvider(
+                integrationRegistrationKey, null);
+    }
+
+    public static class MyApplication extends Application {
+
+        public Set<Class<?>> getClasses() {
+            Set<Class<?>> classes = new HashSet<Class<?>>();
+            classes.add(MyResource.class);
+            return classes;
+        }
+
+    }
+
+    public static class MyResponseWriter extends DefaultResponseWriter {
+
+        public void writeWithEntityProvider(RESTContext context, MessageBodyWriter writer, Object responseObject, Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream os)
+                throws Exception {
+
+            // do nothing here, don't want to serialize
+            return;
+        }
+
+    }
+
+    @Path(value = "/resource")
+    public static class MyResource {
+
+        @Context
+        HttpHeaders headers;
+
+        @Context
+        SecurityContext security;
+
+        @Context
+        UriInfo uriInfo;
+
+        @GET
+        public String sayHello() {
+            return "Hello!";
+        }
+
+        @GET
+        @Path(value = "/headers/{id}")
+        public String getHeader(@PathParam(value = "id") String headerId) {
+            List<String> values = headers.getRequestHeader(headerId);
+            return (values == null || values.size() == 0) ? null : values
+                    .get(0);
+        }
+
+        @GET
+        @Path(value = "/security/principal")
+        public String getPrincipal() {
+            return security.getUserPrincipal().getName();
+        }
+
+        @GET
+        @Path(value = "/security/userinrole/{user}")
+        public String getUserInRole(@PathParam(value = "user") String user) {
+            boolean userInRole = security.isUserInRole(user);
+            return String.valueOf(userInRole);
+        }
+
+        @POST
+        public Response postString(String postedString) {
+            ResponseBuilder rb = Response.ok();
+            Response response = rb.build();
+            response.getMetadata().putSingle("postedData", postedString);
+            return response;
+        }
+    }
+
+}

Added: incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/TestAppProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/TestAppProvider.java?rev=787553&view=auto
==============================================================================
--- incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/TestAppProvider.java (added)
+++ incubator/wink/contrib/ibm-jaxrs/tests/unittests/com/ibm/ws/jaxrs/integration/TestAppProvider.java Tue Jun 23 05:37:57 2009
@@ -0,0 +1,41 @@
+/*
+ * 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.integration;
+
+import java.util.List;
+
+import javax.ws.rs.core.Application;
+
+import com.ibm.ws.jaxrs.context.RESTContext;
+
+public class TestAppProvider implements ApplicationProvider {
+
+    private List<Application> applications;
+
+    public TestAppProvider(List<Application> applications) {
+        this.applications = applications;
+    }
+
+    public List<Application> getApplicationClasses(RESTContext context)
+            throws Exception {
+        return applications;
+    }
+
+}



Mime
View raw message