You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2009/07/07 06:07:59 UTC
svn commit: r791696 [3/4] - in
/incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src:
main/java/org/apache/cxf/ main/java/org/apache/cxf/helpers/
main/java/org/apache/wink/jaxrs/test/providers/readers/ m...
Added: incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/readers/JAXRSMessageBodyReadersTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/readers/JAXRSMessageBodyReadersTest.java?rev=791696&view=auto
==============================================================================
--- incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/readers/JAXRSMessageBodyReadersTest.java (added)
+++ incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/readers/JAXRSMessageBodyReadersTest.java Tue Jul 7 04:07:57 2009
@@ -0,0 +1,1126 @@
+/*
+ * 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.wink.jaxrs.test.providers.readers;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.ext.MessageBodyReader;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSMessageBodyReadersTest extends TestCase {
+
+ public String getBaseURI() {
+ return ServerEnvironmentInfo.getBaseURI() + "/readers";
+ }
+
+ /**
+ * Tests that an improperly formatted request content type is handled.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderImproperlyFormattedContentType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/requestcontenttype");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(500, postMethod.getStatusCode());
+ // assertLogContainsException("java.lang.IllegalArgumentException");
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an empty request content type is handled.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderNoContentType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/requestcontenttype");
+ byte[] requestContent = {0, 0, 0};
+ postMethod
+ .setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(requestContent)));
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ /*
+ * should only invoke hello world
+ */
+ assertEquals("hello world", postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method receives the correct class type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableUnexpectedClassType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/unexpectedclasstype");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(415, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method receives the correct class type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableExpectedClassType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/classtype");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("echo:Helloecho:World", postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method works when there is no generic entity type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableNoGenericEntityType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/nogenericentity");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("echo:Hello\r\nWorld\r\n", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method works when there is no argument type specified on the generic
+ * type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableGenericEntityEmptyType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/genericentityempty");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(415, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method returns true when the expected argument type is specified on the
+ * generic type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableGenericEntityTypeCorrect() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/genericentityqueuestring");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("Hello thereWorld there", postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method returns false when an unexpected argument type is specified on the
+ * generic type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableGenericEntityTypeIncorrect() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/genericentityqueueobject");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(415, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method still works without an annotated entity parameter.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableEntityParameterNotAnnotated() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/notannotatedentity");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ postMethod.addRequestHeader("Accept", "text/plain");
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(415, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is passed a single annotation.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableEntityParameterAnnotated() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/annotatedentity");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ postMethod.addRequestHeader("Accept", "text/plain");
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ assertEquals("Hello thereWorld there", postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is passed multiple annotations.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableEntityParameterAnnotatedMultiple() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/multipleannotatedentity");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ postMethod.addRequestHeader("Accept", "text/plain");
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ assertEquals("Hello thereWorld there", postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is passed an incompatiable media type and does not return true.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableIncorrectMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/mediatype");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "text/plain",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(415, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that the
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is passed the expected media type and reads the data.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableCorrectMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/mediatype");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello\r\nWorld\r\n", "custom/type",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertTrue(response, response.contains("Hello there"));
+ assertTrue(response, response.contains("World there"));
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * When a {@link RuntimeException} is propagated back from
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * , verify that the exception is handled appropriately.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderIsReadableThrowsRuntimeException() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("thrownull", "custom/runtimeexception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(500, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ // assertLogContainsException(response,
+ // "javax.servlet.ServletException");
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("hello world", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:hello world", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * When a {@link WebApplicationException} is propagated back from
+ * {@link MessageBodyReader#isReadable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * , verify that the exception is handled appropriately.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testReaderIsReadableThrowsWebApplicationException() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("thrownull",
+ "custom/webapplicationexception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(478, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("hello world", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:hello world", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
+ * can return a different object based on the class argument.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testReaderReadFromClassType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlylong");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/long", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("" + Long.MAX_VALUE, response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlyinteger");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/int", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("" + Integer.MAX_VALUE, response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
+ * can return a different object based on the generic type argument.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderReadFromGenericType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlygenericlist");
+ postMethod
+ .setRequestEntity(new StringRequestEntity(
+ "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10",
+ "custom/generic", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("listnonspecified:obj:1obj:2obj:3obj:4obj:5obj:6obj:7obj:8obj:9obj:10",
+ response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlygenericliststring");
+ postMethod
+ .setRequestEntity(new StringRequestEntity(
+ "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10",
+ "custom/generic", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("liststring:str:1str:2str:3str:4str:5str:6str:7str:8str:9str:10", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlygenericlistinteger");
+ postMethod
+ .setRequestEntity(new StringRequestEntity(
+ "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10",
+ "custom/generic", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("listinteger:12345678910", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlygenericinteger");
+ postMethod
+ .setRequestEntity(new StringRequestEntity(
+ "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10",
+ "custom/generic", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("integer:55", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
+ * can return a different object based on the annotations argument.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderReadFromAnnotationType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlyshort");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/short", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("" + Short.MAX_VALUE, response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlyshortnoannotation");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/short", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("null", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
+ * can return a different object based on the media type argument.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderReadFromMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlybytemediatype");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/int", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("null", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readdifferentlybytemediatype");
+ postMethod.setRequestEntity(new StringRequestEntity("empty", "custom/byte", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("" + Byte.MAX_VALUE, response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
+ * can use the HttpHeaders.
+ *
+ * @throws IOException
+ */
+ public void testReaderReadFromGetHeader() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World!", "custom/exception",
+ "UTF-8"));
+ postMethod.addRequestHeader("myCustomHeaderToappend", "abcdefgh");
+ postMethod.addRequestHeader("MYCUSTOMHEADERTOAPPEND", "wxyz");
+ postMethod.addRequestHeader("mycustomheadertoappend", "12345");
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World!abcdefghwxyz12345", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that calling {@link InputStream#close()} in the
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.InputStream)}
+ * method will not cause errors.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testReaderReadFromCloseInputStream() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("closeinput", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:closeinput", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that throwing a {@link RuntimeException} in the
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.InputStream)}
+ * method will propagate the exception appropriately.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testReaderReadFromThrowsRuntimeException() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("thrownull", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(500, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ // assertLogContainsException(response,
+ // "javax.servlet.ServletException");
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("hello world", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:hello world", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an IOException triggered by a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.InputStream)}
+ * will propagate appropriately.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderReadFromThrowsIOException() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("ioexception", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(500, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ // assertLogContainsException(response,
+ // "javax.servlet.ServletException");
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("hello world", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:hello world", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a WebApplicationException triggered by a
+ * {@link MessageBodyReader#readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.InputStream)}
+ * will propagate appropriately.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testReaderReadFromThrowsWebApplicationException() throws IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("Hello World", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:Hello World", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("clear", "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("0postReaderReadFrom:clear", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("webapplicationexception",
+ "custom/exception", "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(477, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodyreader/reader/readfrom");
+ postMethod.setRequestEntity(new StringRequestEntity("hello world", "custom/exception",
+ "UTF-8"));
+ try {
+ client.executeMethod(postMethod);
+
+ assertEquals(200, postMethod.getStatusCode());
+
+ String response = postMethod.getResponseBodyAsString();
+ assertEquals("1postReaderReadFrom:hello world", response);
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+}
Added: incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java?rev=791696&view=auto
==============================================================================
--- incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java (added)
+++ incubator/wink/trunk/wink-integration-test/wink-server-integration-test/wink-jaxrs-test-providers/src/test/java/org/apache/wink/jaxrs/test/providers/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java Tue Jul 7 04:07:57 2009
@@ -0,0 +1,426 @@
+/*
+ * 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.wink.jaxrs.test.providers.writerexceptions;
+
+import java.io.IOException;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSMessageBodyWriterExceptionThrownTest extends TestCase {
+
+ public String getBaseURI() {
+ return ServerEnvironmentInfo.getBaseURI() + "/writerexceptions";
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#isWriteable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method which is not mapped to an {@link ExceptionMapper} is still thrown.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testIsWritableExceptionThrownWhichIsNotMappedIsThrownOut() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writable/throwruntime");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(500, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#isWriteable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method which is mapped to an {@link ExceptionMapper} uses the mapper.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testIsWritableExceptionThrownWhichIsMappedUsesExceptionMapper()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writable/thrownull");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(495, postMethod.getStatusCode());
+ assertEquals("Invoked" + NullPointerExceptionMapper.class.getName(), postMethod
+ .getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link WebApplicationException} thrown from the
+ * {@link MessageBodyWriter#isWriteable(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is correctly processed.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testIsWritableWebApplicationExceptionThrown() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writable/throwwebapplicationexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(499, postMethod.getStatusCode());
+ assertEquals("can not write type", postMethod.getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#getSize(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method which is not mapped to an {@link ExceptionMapper} is still thrown.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetSizeExceptionThrownWhichIsNotMappedIsThrownOut() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "getsize/throwruntime");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(500, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#getSize(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method which is mapped to an {@link ExceptionMapper} uses the mapper.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetSizeExceptionThrownWhichIsMappedUsesExceptionMapper() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "getsize/thrownull");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(495, postMethod.getStatusCode());
+ assertEquals("Invoked" + NullPointerExceptionMapper.class.getName(), postMethod
+ .getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link WebApplicationException} thrown from the
+ * {@link MessageBodyWriter#getSize(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * method is correctly processed.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetSizeWebApplicationExceptionThrown() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "getsize/throwwebapplicationexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(499, postMethod.getStatusCode());
+ assertEquals("can not write type", postMethod.getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method which is not mapped to an {@link ExceptionMapper} is still thrown.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToExceptionThrownWhichIsNotMappedIsThrownOut() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writeto/throwruntime");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(500, postMethod.getStatusCode());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method which is mapped to an {@link ExceptionMapper} uses the mapper.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToExceptionThrownWhichIsMappedUsesExceptionMapper() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writeto/thrownull");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(495, postMethod.getStatusCode());
+ assertEquals("Invoked" + NullPointerExceptionMapper.class.getName(), postMethod
+ .getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link WebApplicationException} thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method is correctly processed.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToWebApplicationExceptionThrown() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writeto/throwwebapplicationexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(498, postMethod.getStatusCode());
+ assertEquals("can not write type in writeto", postMethod.getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link IOException} thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method can be mapped correctly.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToIOExceptionThrown() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writeto/throwioexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(455, postMethod.getStatusCode());
+ assertEquals("Invoked" + IOExceptionMapper.class.getName(), postMethod
+ .getResponseBodyAsString());
+ assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method thrown after the stream is committed is handled.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToExceptionThrownWhichIsNotMappedIsThrownOutAfterStreamCommitted()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writetoafterwritten/throwruntime");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("written", postMethod.getResponseBodyAsString());
+ assertEquals("writetoafterwritten/throwruntime", postMethod
+ .getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that an exception thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method which is mapped to an {@link ExceptionMapper} after the stream is
+ * committed is handled.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToExceptionThrownWhichIsMappedUsesExceptionMapperAfterStreamCommitted()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writetoafterwritten/thrownull");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("written", postMethod.getResponseBodyAsString());
+ assertEquals("writetoafterwritten/thrownull", postMethod
+ .getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link WebApplicationException} thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method is correctly processed.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToWebApplicationExceptionThrownAfterStreamCommitted()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writetoafterwritten/throwwebapplicationexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("written", postMethod.getResponseBodyAsString());
+ assertEquals("writetoafterwritten/throwwebapplicationexception", postMethod
+ .getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link IOException} thrown from the
+ * {@link MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)}
+ * method thrown after the stream is committed is handled.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testWriteToIOExceptionThrownAfterStreamCommitted() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ PostMethod postMethod =
+ new PostMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ postMethod.setRequestHeader("Accept", "writetoafterwritten/throwioexception");
+ try {
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("written", postMethod.getResponseBodyAsString());
+ assertEquals("writetoafterwritten/throwioexception", postMethod
+ .getResponseHeader("Content-Type").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a 500 error is returned when a {@link MessageBodyWriter}
+ * cannot be found.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void test500IfWriterNotFound() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/jaxrs/tests/providers/messagebodywriter/writer/messagebodywriterexceptions");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(500, getMethod.getStatusCode());
+ assertEquals("", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+}