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/08/07 05:10:46 UTC

svn commit: r801869 [15/21] - in /incubator/wink/trunk: wink-integration-test/ wink-integration-test/wink-server-integration-test-support/ wink-integration-test/wink-server-integration-test-support/src/main/java/org/apache/wink/test/integration/ wink-i...

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSInputStreamTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSInputStreamTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSInputStreamTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,208 @@
+/*
+ * 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.itest.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Header;
+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.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSInputStreamTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/standard";
+    }
+
+    /**
+     * Tests posting to an InputStream
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostInputStream() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/inputstream");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                 "text/plain"));
+        postMethod.addRequestHeader("Accept", "text/plain");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            InputStream is = postMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+            assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests putting and then getting a byte array.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPutInputStream() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                "bytes/array"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/inputstream");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+
+            String contentType =
+                (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod
+                    .getResponseHeader("Content-Type").getValue();
+            assertNotNull(contentType, contentType);
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests receiving an empty byte array.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException,
+        IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                "any/type"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/inputstream");
+        getMethod.addRequestHeader("Accept", "mytype/subtype");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+            assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests a resource method invoked with a ByteArrayInputStream as a
+     * parameter. This should fail with a 415 since the reader has no way to
+     * necessarily wrap it to the type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testInputStreamImplementation() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod =
+            new PostMethod(getBaseURI() + "/providers/standard/inputstream/subclasses/shouldfail");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                 "any/type"));
+        try {
+            client.executeMethod(postMethod);
+            assertEquals(415, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSMultivaluedMapTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSMultivaluedMapTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSMultivaluedMapTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSMultivaluedMapTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,241 @@
+/*
+ * 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.itest.standard;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Header;
+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.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.itest.standard.ArrayUtils;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSMultivaluedMapTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/standard";
+    }
+
+    /**
+     * Tests posting to a MultivaluedMap with application/x-www-form-urlencoded
+     * request Content-Type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostMultivaluedMap() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/multivaluedmap");
+        postMethod.setRequestEntity(new StringRequestEntity("tuv=wxyz&abcd=",
+                                                            "application/x-www-form-urlencoded",
+                                                            "UTF-8"));
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            InputStream is = postMethod.getResponseBodyAsStream();
+
+            InputStreamReader isr = new InputStreamReader(is);
+            char[] buffer = new char[1];
+            int read = 0;
+            int offset = 0;
+            while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
+                offset += read;
+                if (offset >= buffer.length) {
+                    buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
+                }
+            }
+            char[] carr = ArrayUtils.copyOf(buffer, offset);
+
+            int checkEOF = is.read();
+            assertEquals(-1, checkEOF);
+            String str = new String(carr);
+
+            assertTrue(str, "abcd=&tuv=wxyz".equals(str) || "tuv=wxyz&abcd=".equals(str));
+            assertEquals("application/x-www-form-urlencoded", postMethod
+                .getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            if (contentLengthHeader != null) {
+                // some of the containers can be "smarter" and set the
+                // content-length for us if the payload is small
+                assertEquals("14", contentLengthHeader.getValue());
+            }
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests posting to a MultivaluedMap with a request Content-Type that is not
+     * application/x-www-form-urlencoded.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostMultivaluedMapNotFormURLEncoded() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/multivaluedmap");
+        postMethod
+            .setRequestEntity(new StringRequestEntity("tuv=wxyz&abcd=", "text/plain", "UTF-8"));
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(415, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests posting to a MultivaluedMap with a request Accept type that is not
+     * application/x-www-form-urlencoded.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostMultivaluedMapAcceptNotFormURLEncoded() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod =
+            new PostMethod(getBaseURI() + "/providers/standard/multivaluedmap/noproduces");
+        postMethod.setRequestEntity(new StringRequestEntity("tuv=wxyz&abcd=",
+                                                            "application/x-www-form-urlencoded",
+                                                            "UTF-8"));
+        postMethod.addRequestHeader("Accept", "not/expected");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(500, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests putting and then getting a /multivaluedmap.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPutReader() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/multivaluedmap");
+        putMethod.setRequestEntity(new StringRequestEntity("username=user1&password=user1password",
+                                                           "application/x-www-form-urlencoded",
+                                                           "UTF-8"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/multivaluedmap");
+        getMethod.addRequestHeader("Accept", "application/x-www-form-urlencoded");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            InputStreamReader isr = new InputStreamReader(is);
+            char[] buffer = new char[1];
+            int read = 0;
+            int offset = 0;
+            while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
+                offset += read;
+                if (offset >= buffer.length) {
+                    buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
+                }
+            }
+            char[] carr = ArrayUtils.copyOf(buffer, offset);
+
+            int checkEOF = is.read();
+            assertEquals(-1, checkEOF);
+            String str = new String(carr);
+
+            assertTrue(str,
+                       "username=user1&password=user1password".equals(str) || "password=user1password&username=user1"
+                           .equals(str));
+            assertEquals("application/x-www-form-urlencoded", getMethod
+                .getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            if (contentLengthHeader != null) {
+                // some of the containers can be "smarter" and set the
+                // content-length for us if the payload is small
+                assertEquals("37", contentLengthHeader.getValue());
+            }
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+    //
+    // /**
+    // * Tests a resource method invoked with a MultivaluedMap&lt;String,
+    // * Object&gt; as a parameter. This should fail with a 415 since the reader
+    // * has no way to necessarily wrap it to the type.
+    // *
+    // * @throws HttpException
+    // * @throws IOException
+    // */
+    // public void testMultivaluedMapImplementation() throws HttpException,
+    // IOException {
+    // HttpClient client = new HttpClient();
+    //
+    // PostMethod postMethod = new PostMethod(getBaseURI()
+    // + "/providers/standard/multivaluedmap/subclasses/shouldfail");
+    // byte[] barr = new byte[1000];
+    // Random r = new Random();
+    // r.nextBytes(barr);
+    // postMethod.setRequestEntity(new InputStreamRequestEntity(new
+    // ByteArrayInputStream(barr),
+    // "any/type"));
+    // try {
+    // client.executeMethod(postMethod);
+    // assertEquals(415, postMethod.getStatusCode());
+    // } finally {
+    // postMethod.releaseConnection();
+    // }
+    //
+    // postMethod = new PostMethod(getBaseURI()
+    // + "/providers/standard/multivaluedmap/subclasses/shouldfail");
+    // postMethod.setRequestEntity(new InputStreamRequestEntity(new
+    // ByteArrayInputStream(barr),
+    // "application/x-www-form-urlencoded"));
+    // try {
+    // client.executeMethod(postMethod);
+    // assertEquals(415, postMethod.getStatusCode());
+    // System.out.println(postMethod.getResponseBodyAsString());
+    // } finally {
+    // postMethod.releaseConnection();
+    // }
+    // }
+
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSReaderTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSReaderTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSReaderTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,222 @@
+/*
+ * 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.itest.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Header;
+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.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.itest.standard.ArrayUtils;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSReaderTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/standard";
+    }
+
+    /**
+     * Tests posting to a Reader parameter.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostReader() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/reader");
+        postMethod.setRequestEntity(new StringRequestEntity("abcd", "text/plain", "UTF-8"));
+        postMethod.addRequestHeader("Accept", "text/plain");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            InputStream is = postMethod.getResponseBodyAsStream();
+
+            InputStreamReader isr = new InputStreamReader(is);
+            char[] buffer = new char[1];
+            int read = 0;
+            int offset = 0;
+            while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
+                offset += read;
+                if (offset >= buffer.length) {
+                    buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
+                }
+            }
+            char[] carr = ArrayUtils.copyOf(buffer, offset);
+
+            int checkEOF = is.read();
+            assertEquals(-1, checkEOF);
+            String str = new String(carr);
+
+            assertEquals("abcd", str);
+            assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests putting and then getting a Reader.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPutReader() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/reader");
+        putMethod.setRequestEntity(new StringRequestEntity("wxyz", "char/array", "UTF-8"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/reader");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            InputStreamReader isr = new InputStreamReader(is);
+            char[] buffer = new char[1];
+            int read = 0;
+            int offset = 0;
+            while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
+                offset += read;
+                if (offset >= buffer.length) {
+                    buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
+                }
+            }
+            char[] carr = ArrayUtils.copyOf(buffer, offset);
+
+            int checkEOF = is.read();
+            assertEquals(-1, checkEOF);
+            String str = new String(carr);
+
+            assertEquals("wxyz", str);
+
+            String contentType =
+                (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod
+                    .getResponseHeader("Content-Type").getValue();
+            assertNotNull(contentType, contentType);
+
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException,
+        IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/reader");
+        putMethod.setRequestEntity(new StringRequestEntity("wxyz", "char/array", "UTF-8"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/reader");
+        getMethod.addRequestHeader("Accept", "mytype/subtype");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            InputStreamReader isr = new InputStreamReader(is);
+            char[] buffer = new char[1];
+            int read = 0;
+            int offset = 0;
+            while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
+                offset += read;
+                if (offset >= buffer.length) {
+                    buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
+                }
+            }
+            char[] carr = ArrayUtils.copyOf(buffer, offset);
+
+            int checkEOF = is.read();
+            assertEquals(-1, checkEOF);
+            String str = new String(carr);
+
+            assertEquals("wxyz", str);
+            assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
+
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests a resource method invoked with a BufferedReader as a parameter.
+     * This should fail with a 415 since the reader has no way to necessarily
+     * wrap it to the type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testInputStreamImplementation() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod =
+            new PostMethod(getBaseURI() + "/providers/standard/reader/subclasses/shouldfail");
+        byte[] barr = new byte[1000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                 "any/type"));
+        try {
+            client.executeMethod(postMethod);
+            assertEquals(415, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSSourceTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSSourceTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSSourceTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSSourceTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,229 @@
+/*
+ * 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.itest.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Header;
+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.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSSourceTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/standard";
+    }
+
+    /**
+     * Tests posting to a Source entity parameter with text/xml
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostSourceWithTextXMLMediaType() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/source");
+        postMethod
+            .setRequestEntity(new StringRequestEntity(
+                                                      "<message><user>user1</user><password>user1pwd</password></message>",
+                                                      "text/xml", "UTF-8"));
+        postMethod.addRequestHeader("Accept", "text/xml");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            String str = postMethod.getResponseBodyAsString();
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><user>user1</user><password>user1pwd</password></message>",
+                         str);
+            assertEquals("text/xml", postMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests posting to a Source entity parameter with application/xml as the
+     * media type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostSourceWithApplicationXMLMediaType() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/source");
+        postMethod
+            .setRequestEntity(new StringRequestEntity(
+                                                      "<message><user>user1</user><password>user1pwd</password></message>",
+                                                      "application/xml", "UTF-8"));
+        postMethod.addRequestHeader("Accept", "application/xml");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            String str = postMethod.getResponseBodyAsString();
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><user>user1</user><password>user1pwd</password></message>",
+                         str);
+            assertEquals("application/xml", postMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    // public void testPostSourceWithApplicationWildcardXMLSubtypeMediaType() {
+    // fail();
+    // }
+
+    /**
+     * Tests posting to a Source entity parameter and returning Source entity
+     * response with an unacceptable response media type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostSourceWithNonExpectedAcceptType() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/source");
+        postMethod
+            .setRequestEntity(new StringRequestEntity(
+                                                      "<message><user>user1</user><password>user1pwd</password></message>",
+                                                      "application/xml", "UTF-8"));
+        postMethod.addRequestHeader("Accept", "not/expected");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(500, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests posting to a Source entity parameter and returning Source entity
+     * response with an unacceptable request content-type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostSourceWithNonExpectedRequestContentType() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/source");
+        postMethod
+            .setRequestEntity(new StringRequestEntity(
+                                                      "<message><user>user1</user><password>user1pwd</password></message>",
+                                                      "text/plain", "UTF-8"));
+        postMethod.addRequestHeader("Accept", "application/xml");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(415, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests putting and then getting a source.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPutSource() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/source");
+        putMethod
+            .setRequestEntity(new StringRequestEntity(
+                                                      "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><user>user1</user><password>user1pwd</password></message>",
+                                                      "application/xml", "UTF-8"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/source");
+        try {
+            client.executeMethod(getMethod);
+
+            String str = getMethod.getResponseBodyAsString();
+            assertEquals(str, 200, getMethod.getStatusCode());
+
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><user>user1</user><password>user1pwd</password></message>",
+                         str);
+
+            String contentType =
+                (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod
+                    .getResponseHeader("Content-Type").getValue();
+            assertNotNull(contentType, contentType);
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests a resource method invoked with a SAXSource as a parameter. This
+     * should fail with a 415 since the reader has no way to necessarily wrap it
+     * to the type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testSourceSubclassImplementation() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod =
+            new PostMethod(getBaseURI() + "/providers/standard/source/subclasses/shouldfail");
+        byte[] barr = new byte[1000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                 "application/xml"));
+        try {
+            client.executeMethod(postMethod);
+            assertEquals(415, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSStreamingOutputTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSStreamingOutputTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSStreamingOutputTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/standard/JAXRSStreamingOutputTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,183 @@
+/*
+ * 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.itest.standard;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Header;
+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.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSStreamingOutputTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/standard";
+    }
+
+    /**
+     * Tests posting to a StreamingOutput and then returning StreamingOutput.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPostStreamingOutput() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod =
+            new PostMethod(getBaseURI() + "/providers/standard/streamingoutput");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                 "text/plain"));
+        postMethod.addRequestHeader("Accept", "text/plain");
+        try {
+            client.executeMethod(postMethod);
+
+            assertEquals(200, postMethod.getStatusCode());
+            InputStream is = postMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+            assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests putting and then getting a StreamingOutput.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testPutStreamngOutput() throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                "bytes/array"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+
+            String contentType =
+                (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod
+                    .getResponseHeader("Content-Type").getValue();
+            assertNotNull(contentType, contentType);
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Tests receiving a StreamingOutput with a non-standard content-type.
+     * 
+     * @throws HttpException
+     * @throws IOException
+     */
+    public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException,
+        IOException {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput");
+        byte[] barr = new byte[100000];
+        Random r = new Random();
+        r.nextBytes(barr);
+        putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr),
+                                                                "any/type"));
+        try {
+            client.executeMethod(putMethod);
+            assertEquals(204, putMethod.getStatusCode());
+        } finally {
+            putMethod.releaseConnection();
+        }
+
+        GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput");
+        getMethod.addRequestHeader("Accept", "mytype/subtype");
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            InputStream is = getMethod.getResponseBodyAsStream();
+
+            byte[] receivedBArr = new byte[barr.length];
+            DataInputStream dis = new DataInputStream(is);
+            dis.readFully(receivedBArr);
+
+            int checkEOF = dis.read();
+            assertEquals(-1, checkEOF);
+            for (int c = 0; c < barr.length; ++c) {
+                assertEquals(barr[c], receivedBArr[c]);
+            }
+            assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
+            Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
+            assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(),
+                       contentLengthHeader);
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/subresources/JAXRSExceptionsSubresourcesTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/subresources/JAXRSExceptionsSubresourcesTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/subresources/JAXRSExceptionsSubresourcesTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/subresources/JAXRSExceptionsSubresourcesTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,132 @@
+/*
+ * 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.itest.subresources;
+
+import javax.ws.rs.core.Response.Status;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class JAXRSExceptionsSubresourcesTest extends TestCase {
+
+    public String getBaseURI() {
+        return ServerEnvironmentInfo.getBaseURI() + "/subresourceexceptions/guestbooksubresources";
+    }
+
+    /**
+     * Test the positive workflow where a comment with a message and author is
+     * successfully posted to the Guestbook.
+     * 
+     * @throws Exception
+     */
+    public void testRegularWorkflow() throws Exception {
+        HttpClient client = new HttpClient();
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/commentdata");
+        try {
+            postMethod
+                .setRequestEntity(new StringRequestEntity(
+                                                          "<comment><id>10000</id><author>Anonymous</author><message>Hi there</message></comment>",
+                                                          "text/xml", null));
+            client.executeMethod(postMethod);
+            assertEquals(201, postMethod.getStatusCode());
+        } finally {
+            postMethod.releaseConnection();
+        }
+        String postURILocation = postMethod.getResponseHeader("Location").getValue();
+
+        GetMethod getMethod = new GetMethod(postURILocation);
+        try {
+            client.executeMethod(getMethod);
+            assertEquals(200, getMethod.getStatusCode());
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><comment><author>Anonymous</author><id>10000</id><message>Hi there</message></comment>",
+                         getMethod.getResponseBodyAsString());
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Test that a <code>WebApplicationException</code> thrown from a
+     * sub-resource is still processed properly.
+     * 
+     * @throws Exception
+     */
+    public void testWebApplicationException() throws Exception {
+        HttpClient client = new HttpClient();
+
+        PostMethod postMethod = new PostMethod(getBaseURI() + "/commentdata");
+        try {
+            postMethod.setRequestEntity(new StringRequestEntity("<comment></comment>", "text/xml",
+                                                                null));
+            // postMethod.addRequestHeader("Accept", "text/xml");
+            client.executeMethod(postMethod);
+            assertEquals(Status.BAD_REQUEST.getStatusCode(), postMethod.getStatusCode());
+            assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><commenterror><message>Please include a comment ID, a message, and your name.</message></commenterror>",
+                         postMethod.getResponseBodyAsString());
+        } finally {
+            postMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Test that a checked exception is processed properly.
+     * 
+     * @throws Exception
+     */
+    public void testCheckedException() throws Exception {
+        HttpClient client = new HttpClient();
+
+        PutMethod putMethod = new PutMethod(getBaseURI() + "/commentdata");
+        try {
+            putMethod.setRequestEntity(new StringRequestEntity("<comment></comment>", "text/xml",
+                                                               null));
+            client.executeMethod(putMethod);
+            assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), putMethod.getStatusCode());
+            // assertLogContainsException("jaxrs.tests.exceptions.subresources.server.GuestbookException: Unexpected ID.");
+        } finally {
+            putMethod.releaseConnection();
+        }
+    }
+
+    /**
+     * Test the positive workflow where a comment with a message and author is
+     * successfully posted to the Guestbook.
+     * 
+     * @throws Exception
+     */
+    public void testRuntimeException() throws Exception {
+        HttpClient client = new HttpClient();
+        DeleteMethod deleteMethod = new DeleteMethod(getBaseURI() + "/commentdata/afdsfsdf");
+        try {
+            client.executeMethod(deleteMethod);
+            assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), deleteMethod.getStatusCode());
+            // assertLogContainsException("java.lang.NumberFormatException: For input string: \"afdsfsdf\"");
+        } finally {
+            deleteMethod.releaseConnection();
+        }
+    }
+}

Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-providers/src/test/java/org/apache/wink/itest/writerexceptions/JAXRSMessageBodyWriterExceptionThrownTest.java Fri Aug  7 03:10:22 2009
@@ -0,0 +1,430 @@
+/*
+ * 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.itest.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.itest.writerexceptions.IOExceptionMapper;
+import org.apache.wink.itest.writerexceptions.NullPointerExceptionMapper;
+import org.apache.wink.test.integration.ServerContainerAssertions;
+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());
+            ServerContainerAssertions.assertExceptionBodyFromServer(500, getMethod
+                .getResponseBodyAsString());
+        } finally {
+            getMethod.releaseConnection();
+        }
+    }
+}