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/09/19 21:38:26 UTC
svn commit: r816947 [2/3] - in
/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest:
environment/servlets/ httpheaders/ providers/ request/ securitycontext/
uriinfo/
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/WinkProvidersMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/WinkProvidersMethodsTest.java?rev=816947&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/WinkProvidersMethodsTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/WinkProvidersMethodsTest.java Sat Sep 19 19:38:24 2009
@@ -0,0 +1,892 @@
+/*
+ * 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.providers;
+
+import java.io.IOException;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Providers;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpException;
+import org.apache.wink.client.ClientResponse;
+import org.apache.wink.client.RestClient;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class WinkProvidersMethodsTest extends TestCase {
+
+ private static String getBaseURI() {
+ if (ServerEnvironmentInfo.isRestFilterUsed()) {
+ return ServerEnvironmentInfo.getBaseURI();
+ }
+ return ServerEnvironmentInfo.getBaseURI() + "/providers";
+ }
+
+ protected RestClient client;
+
+ @Override
+ public void setUp() {
+ client = new RestClient();
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getContextResolver(Class, javax.ws.rs.core.MediaType)}
+ * will return null when a {@link ContextResolver} is not provided that
+ * matches the requested Context type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoMatch() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Throwable&mediaType=*%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("nullcontextresolver", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a
+ * {@link Providers#getContextResolver(Class, javax.ws.rs.core.MediaType)}
+ * will return a single context resolver when a single matching
+ * {@link ContextResolver} is provided by the application.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchSingle() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=*%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver", response
+ .getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will not be returned when given a non-compatible media type
+ * (my/type).
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoMatchBySpecificMediaType() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=my%2Ftype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("nullcontextresolver", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the specific text/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaType() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver", response
+ .getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the wildcard/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaTypeTypeWildcard() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=*%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver", response
+ .getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the text/* type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaTypeSubtypeWildcard() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=text%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver", response
+ .getEntity(String.class));
+ }
+
+ // /**
+ // * Tests that when finding a {@link ContextResolver} both the application
+ // * provided and runtime provided context resolvers are searched. Invokes
+ // * with a specific JAXB class and verifies that the final context is a
+ // * JAXBContext. In this case, the runtime provided context resolver is
+ // used.
+ // *
+ // * @throws HttpException
+ // * @throws IOException
+ // */
+ // public void
+ // testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeWildcardInvokeWithClassInvokeRuntimeProvided()
+ // throws HttpException, IOException {
+ // HttpClient client = new HttpClient();
+ //
+ // GetMethod getMethod =
+ // new GetMethod(
+ // getBaseURI() +
+ // "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=*%2F*&invokeWithClassName=org.apache.wink.itest.providers.otherxml.OtherRootElement");
+ // try {
+ // client.executeMethod(getMethod);
+ // assertEquals(200, getMethod.getStatusCode());
+ // assertTrue(getMethod.getResponseBodyAsString(),
+ // getMethod.getResponseBodyAsString()
+ // .contains("JAXBContext"));
+ // } finally {
+ // getMethod.releaseConnection();
+ // }
+ // }
+
+ /**
+ * Tests that when finding a {@link ContextResolver} both the application
+ * provided and runtime provided context resolvers are searched. Invokes
+ * with a specific JAXB class and verifies that the final context is a
+ * JAXBContext. In this case, the application provided context resolver is
+ * used.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeWildcardInvokeWithClassInvokeApplicationProvided()
+ throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=*%2F*&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertTrue(response.getEntity(String.class), response.getEntity(String.class)
+ .contains("JAXBContext"));
+ }
+
+ // /**
+ // * Tests that when there are multiple {@link ContextResolver}s that could
+ // * respond to a given type, that a proxy is returned that will call all of
+ // * them.
+ // *
+ // * @throws HttpException
+ // * @throws IOException
+ // */
+ // public void
+ // testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificTextXML()
+ // throws HttpException, IOException {
+ // HttpClient client = new HttpClient();
+ //
+ // GetMethod getMethod =
+ // new GetMethod(
+ // getBaseURI() +
+ // "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=text%2Fxml");
+ // try {
+ // client.executeMethod(getMethod);
+ // assertEquals(200, getMethod.getStatusCode());
+ // assertTrue(getMethod.getResponseBodyAsString().startsWith("$Proxy"));
+ // } finally {
+ // getMethod.releaseConnection();
+ // }
+ // }
+
+ // /**
+ // * Tests that when the application provided {@link ContextResolver} which
+ // * has a {@link Produces} annotation with text/xml is not the
+ // * ContextResolver returned when searching for application/json media
+ // type.
+ // *
+ // * @throws HttpException
+ // * @throws IOException
+ // */
+ // public void
+ // testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificApplicationJSON()
+ // throws HttpException, IOException {
+ // HttpClient client = new HttpClient();
+ //
+ // GetMethod getMethod =
+ // new GetMethod(
+ // getBaseURI() +
+ // "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=application%2Fjson");
+ // try {
+ // client.executeMethod(getMethod);
+ // assertEquals(200, getMethod.getStatusCode());
+ // assertTrue(getMethod.getResponseBodyAsString().contains("JAXBContext"));
+ // } finally {
+ // getMethod.releaseConnection();
+ // }
+ // }
+
+ // /**
+ // * Tests that when the application provided {@link ContextResolver} which
+ // * has a {@link Produces} annotation with text/xml is not called when an
+ // * application/json is searched. This method should be able to invoke the
+ // * runtime provided JAXBContext ContextResolver but return null.
+ // *
+ // * @throws HttpException
+ // * @throws IOException
+ // */
+ // public void
+ // testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificApplicationJSONInvokeNegative()
+ // throws HttpException, IOException {
+ // HttpClient client = new HttpClient();
+ //
+ // GetMethod getMethod =
+ // new GetMethod(
+ // getBaseURI() +
+ // "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=application%2Fjson&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement");
+ // try {
+ // client.executeMethod(getMethod);
+ // assertEquals(200, getMethod.getStatusCode());
+ // assertEquals("null", getMethod.getResponseBodyAsString());
+ // } finally {
+ // getMethod.releaseConnection();
+ // }
+ // }
+
+ /**
+ * Tests that when the application provided {@link ContextResolver} which
+ * has a {@link Produces} annotation with text/xml is called when an
+ * text/xml is searched. This method should be able to invoke the
+ * application provided JAXBContext ContextResolver and return a
+ * JAXBContext.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificTextXMLInvoke()
+ throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=text%2Fxml&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertTrue(response.getEntity(String.class), response.getEntity(String.class)
+ .contains("JAXBContext"));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/xml {@link Produces}
+ * annotation will match a (specific)/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesWildcardType() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=abcd%2Fxml&invokeWithClassName=java.lang.Short&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("allwildcardshort", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a text/wildcard
+ * {@link Produces} annotation will match a text/(specific) type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesWildcardSubtype()
+ throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fabcd&invokeWithClassName=java.lang.Short&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("textwildcardonly", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/wildcard
+ * {@link Produces} annotation will match a (specific)/(specific) type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesAllWildcard() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=abcd%2Fdef&invokeWithClassName=java.lang.Short&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("allwildcardshort", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a match with multiple {@link ContextResolver}s with the same
+ * media type and generic type will use a proxy that finds the single
+ * resolver that returns a non-null.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesFindOne() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Integer&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("integerxmlonly", response.getEntity(String.class));
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Long&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("longxml2only", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a match with multiple {@link ContextResolver}s will return the
+ * most specific responses before the wildcard responses.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchAnyMoreSpecificThanWildcards() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Short&returnToStringValue=true")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertTrue(responseBody, "shortxmlandjson".equals(responseBody) || "shortxml2only"
+ .equals(responseBody)
+ || "shortxmlonly".equals(responseBody));
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/wildcard
+ * {@link Produces} annotation will match any media type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoProducesMatchNotExpectedMediaType() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=my%2Ftype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyStringContextForAllWildcard", responseBody);
+ }
+
+ /**
+ * Tests that a Provider can return an {@link ExceptionMapper}.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperAppSuppliedProvider() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/exception?className=org.apache.wink.itest.providers.MyException")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.ExceptionMapperForMyException", responseBody);
+ }
+
+ /**
+ * Tests that a {@link Providers} will return the correct exception mapper
+ * given an exception that is a sub-class of the {@link ExceptionMapper}
+ * class.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperAppSuppliedInheritanceTree() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/exception?className=org.apache.wink.itest.providers.MyException2")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.ExceptionMapperForMyException", responseBody);
+ }
+
+ /**
+ * Tests that a {@link Providers} will return a null value when given a
+ * class that does not have an {@link ExceptionMapper}.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperNoMatching() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/exception?className=java.lang.Throwable")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("null", responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body writers.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByProducesMediaType() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ responseBody
+ .contains("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber") || responseBody
+ .contains("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForInteger"));
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body writers and will
+ * respect more specific types over wildcards.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByProducesMediaTypeWithWildcards()
+ throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fmytype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterApplicationWildcardForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=mytype%2Fmysubtype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterWildcardForShort",
+ responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will find MessageBodyWriters that have inherited the MessageBodyWriter
+ * interface.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyWriterWhichInheritsWriterInterface() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.util.List&mediaType=abcd%2Fefgh")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterInherited", responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will filter out writers that do not match the isWritable method.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByIsWritable() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ "org.apache.wink.itest.providers.MyMessageBodyWriterJSONForInteger"
+ .equals(responseBody) || "org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber"
+ .equals(responseBody));
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Long&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ "org.apache.wink.itest.providers.MyMessageBodyWriterJSONForLong"
+ .equals(responseBody) || "org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber"
+ .equals(responseBody));
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Long&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will use the application provided MessageBodyWriters before runtime
+ * provided.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterUserPrecedenceOverRuntime() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.String&mediaType=text%2Fplain")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.MyStringWriterForStrings", responseBody);
+ }
+
+ /**
+ * Tests that a null is returned when calling
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * if there are no suitable MessageBodyWriters.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterNoMatching() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Float&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("nullwriter", responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Exception&mediaType=*%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.common.internal.providers.entity.FormatedExceptionProvider",
+ responseBody);
+ }
+
+ /**
+ * Tests that a null is returned when calling
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * if there are no suitable MessageBodyReader.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyReaderNoMatching() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Float&mediaType=mynonexistenttype%2Fmysubtype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("nullreader", responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Exception&mediaType=*%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("nullreader", responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will filter out writers that do not match the isWritable method.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByIsReadable() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ "org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForInteger"
+ .equals(responseBody) || "org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber"
+ .equals(responseBody));
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Long&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ "org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForLong"
+ .equals(responseBody) || "org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber"
+ .equals(responseBody));
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Long&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body readers.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByConsumesMediaType() throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=text%2Fxml")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertTrue(responseBody,
+ responseBody
+ .contains("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber") || responseBody
+ .contains("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForInteger"));
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body readers and will
+ * respect more specific types over wildcards.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByConsumesMediaTypeWithWildcards()
+ throws HttpException, IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fjson")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2F*")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fmytype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderApplicationWildcardForShort",
+ responseBody);
+
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=mytype%2Fmysubtype")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderWildcardForShort",
+ responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will use the application provided MessageBodyReaders before runtime
+ * provided.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderUserPrecedenceOverRuntime() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.String&mediaType=text%2Fplain")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderForStrings",
+ responseBody);
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will find MessageBodyReaders that have inherited the MessageBodyReader
+ * interface.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyReaderWhichInheritsReaderInterface() throws HttpException,
+ IOException {
+ ClientResponse response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.util.Set&mediaType=tuv%2Fwxyz")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ String responseBody = response.getEntity(String.class);
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderInherited",
+ responseBody);
+ response =
+ client
+ .resource(getBaseURI() + "/context/providers/messagebodyreader?className=java.util.List&mediaType=tuv%2Fwxyz")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ responseBody = response.getEntity(String.class);
+ assertEquals("nullreader", responseBody);
+ }
+}
Propchange: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/WinkProvidersMethodsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java?rev=816947&r1=816946&r2=816947&view=diff
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java (original)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java Sat Sep 19 19:38:24 2009
@@ -76,7 +76,7 @@
asctimeDateFormatWithOneDigit.setTimeZone(TimeZone.getTimeZone("GMT"));
}
- public String getBaseURI() {
+ private static String getBaseURI() {
if(ServerEnvironmentInfo.isRestFilterUsed()) {
return ServerEnvironmentInfo.getBaseURI();
}
@@ -423,7 +423,7 @@
* @throws HttpException
* @throws IOException
*/
- public void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ private void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException,
IOException {
HttpClient client = new HttpClient();
@@ -592,7 +592,7 @@
* @throws HttpException
* @throws IOException
*/
- public void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ private void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException,
IOException {
HttpClient client = new HttpClient();
final String justTheTag = etag;
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkApacheRequestMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkApacheRequestMethodsTest.java?rev=816947&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkApacheRequestMethodsTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkApacheRequestMethodsTest.java Sat Sep 19 19:38:24 2009
@@ -0,0 +1,31 @@
+/*
+ * 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.request;
+
+import org.apache.wink.client.ApacheHttpClientConfig;
+import org.apache.wink.client.RestClient;
+
+public class WinkApacheRequestMethodsTest extends WinkRequestMethodsTest {
+
+ @Override
+ public void setUp() {
+ client = new RestClient(new ApacheHttpClientConfig());
+ }
+}
Propchange: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkApacheRequestMethodsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkRequestMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkRequestMethodsTest.java?rev=816947&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkRequestMethodsTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkRequestMethodsTest.java Sat Sep 19 19:38:24 2009
@@ -0,0 +1,621 @@
+/*
+ * 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.request;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpException;
+import org.apache.wink.client.ClientResponse;
+import org.apache.wink.client.Resource;
+import org.apache.wink.client.RestClient;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class WinkRequestMethodsTest extends TestCase {
+
+ final private static SimpleDateFormat rfc1123Format =
+ new SimpleDateFormat(
+ "EEE, dd MMM yyyy HH:mm:ss zzz",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat rfc850Format =
+ new SimpleDateFormat(
+ "EEEE, dd-MMM-yy HH:mm:ss zzz",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat asctimeDateFormat =
+ new SimpleDateFormat(
+ "EEE MMM dd HH:mm:ss yyyy",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat asctimeDateFormatWithOneDigit =
+ new SimpleDateFormat(
+ "EEE MMM d HH:mm:ss yyyy",
+ Locale.ENGLISH);
+
+ {
+ /*
+ * the implementation allows you to set a different time zone on the
+ * requests for If-Modified-Since headers and it will do the
+ * "right thing" (this is more leniant). However, asctime does not have
+ * a time zone specified so it is assumed that all datetimes are in
+ * GMT/UTC so the tests have to assume that the GMT time zone is used.
+ */
+ asctimeDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ asctimeDateFormatWithOneDigit.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
+ private static String getBaseURI() {
+ if (ServerEnvironmentInfo.isRestFilterUsed()) {
+ return ServerEnvironmentInfo.getBaseURI();
+ }
+ return ServerEnvironmentInfo.getBaseURI() + "/request";
+ }
+
+ protected RestClient client;
+
+ @Override
+ public void setUp() {
+ client = new RestClient();
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the RFC 1123 date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingRFC1123Format() throws HttpException,
+ IOException {
+ checkIfModifiedSinceUsingSuppliedDateFormat(rfc1123Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the RFC 850 date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingRFC850Format() throws HttpException,
+ IOException {
+ checkIfModifiedSinceUsingSuppliedDateFormat(rfc850Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the Asctime date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingAscTimeFormat() throws HttpException,
+ IOException {
+ SimpleDateFormat formatter =
+ (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < 10)
+ ? asctimeDateFormatWithOneDigit : asctimeDateFormat;
+ checkIfModifiedSinceUsingSuppliedDateFormat(formatter);
+ }
+
+ private void checkIfModifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
+ throws IOException, HttpException {
+ Date d2 = new Date(System.currentTimeMillis() - 120000);
+ Date d = new Date(System.currentTimeMillis() - 60000);
+ String date = DateFormat.getDateTimeInstance().format(d);
+ /*
+ * sets a last modified date
+ */
+ Resource dateResource = client.resource(getBaseURI() + "/context/request/date");
+ ClientResponse response = dateResource.contentType("text/string").put(date);
+ assertEquals(204, response.getStatusCode());
+
+ response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d)).get();
+ /*
+ * verifies that if the exact date is sent in and used in
+ * If-Modified-Since header, then the server will be ok and that it will
+ * return 304
+ */
+ assertEquals(304, response.getStatusCode());
+
+ /*
+ * verifies that if no If-Modified-Since header is sent, then the server
+ * will be ok and the Request instance won't build a response.
+ */
+ dateResource = client.resource(getBaseURI() + "/context/request/date");
+ response = dateResource.get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders()
+ .getFirst(HttpHeaders.LAST_MODIFIED));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+
+ /*
+ * verifies that using Last-Modified response header sent by server as
+ * If-Modified-Since request header, then the server will return a 304
+ */
+ String lastModified = response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
+ dateResource = client.resource(getBaseURI() + "/context/request/date");
+ response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, lastModified).get();
+ assertEquals(304, response.getStatusCode());
+
+ /*
+ * verifies that using a If-Modified-Since earlier than the
+ * Last-Modified response header sent by server then the server will
+ * return a 200 with entity
+ */
+ dateResource = client.resource(getBaseURI() + "/context/request/date");
+ response = dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(d2)).get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders()
+ .getFirst(HttpHeaders.LAST_MODIFIED));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+
+ /*
+ * verifies that using a If-Modified-Since later than the Last-Modified
+ * response header sent by server, then the server will return a 304
+ */
+ dateResource = client.resource(getBaseURI() + "/context/request/date");
+ response =
+ dateResource.header(HttpHeaders.IF_MODIFIED_SINCE, formatter.format(new Date())).get();
+ assertEquals(304, response.getStatusCode());
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using RFC 1123.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingRFC1123() throws HttpException, IOException {
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(rfc1123Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using RFC 850.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingRFC850() throws HttpException, IOException {
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(rfc850Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using Asctime.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingAscTime() throws HttpException, IOException {
+ SimpleDateFormat dateFormat =
+ (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < 10)
+ ? asctimeDateFormatWithOneDigit : asctimeDateFormat;
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(dateFormat);
+ }
+
+ private void checkIfUnmodifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
+ throws IOException, HttpException {
+ Date d2 = new Date(System.currentTimeMillis() - 120000);
+ Date d = new Date(System.currentTimeMillis() - 60000);
+ String date = DateFormat.getDateTimeInstance().format(d);
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/request/date").contentType("text/string")
+ .put(date);
+ assertEquals(204, response.getStatusCode());
+
+ /*
+ * verifies that if the exact date is sent in and used in
+ * If-Unmodified-Since header, then the server will be ok and that it
+ * will return 200
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/date")
+ .header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(d)).get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders().getFirst("Last-Modified"));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+
+ /*
+ * verifies that if no If-Unmodified-Since header is sent, then the
+ * server will be ok and the Request instance won't build a response.
+ */
+ response = client.resource(getBaseURI() + "/context/request/date").get();
+
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders()
+ .getFirst(HttpHeaders.LAST_MODIFIED));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+
+ /*
+ * verifies that using Last-Modified response header sent by server as
+ * If-Unmodified-Since request header, then the server will return the
+ * entity
+ */
+ String lastModified = response.getHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
+ response =
+ client.resource(getBaseURI() + "/context/request/date")
+ .header(HttpHeaders.IF_UNMODIFIED_SINCE, lastModified).get();
+
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders()
+ .getFirst(HttpHeaders.LAST_MODIFIED));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+
+ /*
+ * verifies that using a If-Unmodified-Since earlier than the
+ * Last-Modified response header sent by server then the server will
+ * return a 412
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/date")
+ .header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(d2)).get();
+ assertEquals(412, response.getStatusCode());
+
+ response =
+ client.resource(getBaseURI() + "/context/request/date")
+ .header(HttpHeaders.IF_UNMODIFIED_SINCE, formatter.format(new Date())).get();
+ /*
+ * verifies that using a If-Unmodified-Since later than the
+ * Last-Modified response header sent by server, then the server will
+ * return 200 and the entity
+ */
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), response.getEntity(String.class));
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), response.getHeaders()
+ .getFirst(HttpHeaders.LAST_MODIFIED));
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header and a strong ETag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfMatchStrong() throws HttpException, IOException {
+ try {
+ checkETagIfMatch("\"myentitytagABCXYZ\"", false);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header and a weak ETag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfMatchWeak() throws HttpException, IOException {
+ try {
+ checkETagIfMatch("\"myentitytagABCXYZ\"", true);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ private void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ IOException {
+ final String justTheTag = etag;
+ final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
+ String isWeak = isEntityTagWeak ? "true" : "false";
+
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/request/etag").contentType("text/string")
+ .put(setETag);
+ assertEquals(204, response.getStatusCode());
+
+ response =
+ client.resource(getBaseURI() + "/context/request/etag").header(HttpHeaders.IF_MATCH,
+ setETag).get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that a request without an If-Match header will still proceed
+ */
+ response = client.resource(getBaseURI() + "/context/request/etag").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that a misquoted entity tag is not a valid entity tag
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_MATCH, setETag.substring(1, setETag.length() - 1)).get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that a misquoted entity tag is not a valid entity tag
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_MATCH, setETag.substring(0, setETag.length() - 1)).get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that a misquoted entity tag is not a valid entity tag
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_MATCH, setETag.substring(1, setETag.length())).get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that if an etag is sent that does not match the server etag,
+ * that a 412 is returned
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag").header(HttpHeaders.IF_MATCH,
+ "\"someothervalue\"")
+ .get();
+ assertEquals(412, response.getStatusCode());
+
+ /*
+ * verifies that if multiple etags are sent that do not match the server
+ * etag, that a 412 is returned
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_MATCH, "\"austin\", \"powers\"").get();
+ assertEquals(412, response.getStatusCode());
+
+ /*
+ * verifies that if multiple etags are sent that do not match the server
+ * etag, that a 412 is returned
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag").header(HttpHeaders.IF_MATCH,
+ "\"austin\", " + setETag
+ + " , \"powers\"")
+ .get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header with strong entity tag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfNoneMatchStrong() throws HttpException, IOException {
+ checkETagIfNoneMatch("\"myentitytagABCXYZ\"", false);
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header with weak entity tag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfNoneMatchWeak() throws HttpException, IOException {
+ checkETagIfNoneMatch("\"myentitytagABCXYZ\"", true);
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ private void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ IOException {
+ final String justTheTag = etag;
+ final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
+ String isWeak = isEntityTagWeak ? "true" : "false";
+
+ /*
+ * sets an entity tag
+ */
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/request/etag").contentType("text/string")
+ .put(setETag);
+ assertEquals(204, response.getStatusCode());
+
+ /*
+ * verifies that if the exact etag is sent in, then the response is a
+ * 304
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, setETag).get();
+ assertEquals(304, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+
+ /*
+ * verifies that if a "*" etag is sent in, then the response returns a
+ * 304
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"*\"").get();
+ assertEquals(304, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+
+ /*
+ * verifies that if a "*" etag is sent in, then the response returns a
+ * 412
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"*\"").post(null);
+ assertEquals(412, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+
+ /*
+ * verifies that if the set etag is sent in, then the response returns a
+ * 412
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, setETag).post(null);
+ assertEquals(412, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+
+ /*
+ * verifies that a request without an If-None-Match header will still
+ * proceed
+ */
+ response = client.resource(getBaseURI() + "/context/request/etag").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that a request without an If-None-Match header will still
+ * proceed
+ */
+ response = client.resource(getBaseURI() + "/context/request/etag").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that an unquoted entity tag is invalid
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, setETag.substring(1, setETag.length() - 1))
+ .get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that a misquoted entity tag is invalid
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, setETag.substring(0, setETag.length() - 1))
+ .get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that a misquoted entity tag is invalid
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, setETag.substring(1, setETag.length())).get();
+ assertEquals(400, response.getStatusCode());
+
+ /*
+ * verifies that if an etag is sent that does not match the server etag,
+ * that request is allowed to proceed
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"someothervalue\"").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that if multiple etags are sent that do not match the server
+ * etag, that the request is allowed to proceed
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"austin\", \"powers\"").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that if multiple etags are sent that do not match the server
+ * etag, then a 200 and the request entity is returned
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"austin\", \"powers\"").post(null);
+ assertEquals(200, response.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, response.getEntity(String.class));
+
+ /*
+ * verifies that if multiple etags are sent that do match the server
+ * etag, that a 304 is returned
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"austin\", " + setETag + " , \"powers\"")
+ .get();
+ assertEquals(304, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+
+ /*
+ * verifies that a request with an If-None-Match header will fail
+ */
+ response =
+ client.resource(getBaseURI() + "/context/request/etag")
+ .header(HttpHeaders.IF_NONE_MATCH, "\"austin\", " + setETag + " , \"powers\"")
+ .post(null);
+ assertEquals(412, response.getStatusCode());
+ assertEquals(setETag, response.getHeaders().getFirst(HttpHeaders.ETAG));
+ }
+
+ // TODO: add selectVariant tests by querying the various
+ // /context/request/variant/* paths
+
+}
Propchange: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/WinkRequestMethodsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/SecurityContextTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/SecurityContextTest.java?rev=816947&r1=816946&r2=816947&view=diff
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/SecurityContextTest.java (original)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/SecurityContextTest.java Sat Sep 19 19:38:24 2009
@@ -42,6 +42,12 @@
return ServerEnvironmentInfo.getBaseURI() + "/securitycontext";
}
+ private HttpClient client;
+
+ public void setUp() {
+ client = new HttpClient();
+ }
+
/**
* Tests that a security context can be injected via a parameter.
*
@@ -50,8 +56,6 @@
* @throws JAXBException
*/
public void testSecurityContextParamResource() throws HttpException, IOException, JAXBException {
- HttpClient client = new HttpClient();
-
GetMethod getMethod = new GetMethod(getBaseURI() + "/context/securitycontext/param");
try {
client.executeMethod(getMethod);
@@ -83,8 +87,6 @@
*/
public void testSecurityContextConstructorResource() throws HttpException, IOException,
JAXBException {
- HttpClient client = new HttpClient();
-
GetMethod getMethod = new GetMethod(getBaseURI() + "/context/securitycontext/constructor");
try {
client.executeMethod(getMethod);
@@ -115,8 +117,6 @@
* @throws JAXBException
*/
public void testSecurityContextBeanResource() throws HttpException, IOException, JAXBException {
- HttpClient client = new HttpClient();
-
GetMethod getMethod = new GetMethod(getBaseURI() + "/context/securitycontext/bean");
try {
client.executeMethod(getMethod);
@@ -146,8 +146,6 @@
* @throws HttpException
*/
public void testSecurityContextNotBeanResource() throws HttpException, IOException {
- HttpClient client = new HttpClient();
-
GetMethod getMethod =
new GetMethod(getBaseURI() + "/context/securitycontext/notbeanmethod");
try {
@@ -167,8 +165,6 @@
* @throws JAXBException
*/
public void testSecurityContextFieldResource() throws HttpException, IOException, JAXBException {
- HttpClient client = new HttpClient();
-
GetMethod getMethod = new GetMethod(getBaseURI() + "/context/securitycontext/field");
try {
client.executeMethod(getMethod);
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkApacheSecurityContextTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkApacheSecurityContextTest.java?rev=816947&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkApacheSecurityContextTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkApacheSecurityContextTest.java Sat Sep 19 19:38:24 2009
@@ -0,0 +1,32 @@
+/*
+ * 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.securitycontext;
+
+import org.apache.wink.client.ApacheHttpClientConfig;
+import org.apache.wink.client.RestClient;
+
+public class WinkApacheSecurityContextTest extends WinkSecurityContextTest {
+
+ @Override
+ public void setUp() {
+ client = new RestClient(new ApacheHttpClientConfig());
+ }
+
+}
Propchange: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkApacheSecurityContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkSecurityContextTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkSecurityContextTest.java?rev=816947&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkSecurityContextTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkSecurityContextTest.java Sat Sep 19 19:38:24 2009
@@ -0,0 +1,167 @@
+/*
+ * 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.securitycontext;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpException;
+import org.apache.wink.client.ClientResponse;
+import org.apache.wink.client.RestClient;
+import org.apache.wink.itest.securitycontext.xml.ObjectFactory;
+import org.apache.wink.itest.securitycontext.xml.SecurityContextInfo;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class WinkSecurityContextTest extends TestCase {
+
+ private static String getBaseURI() {
+ if (ServerEnvironmentInfo.isRestFilterUsed()) {
+ return ServerEnvironmentInfo.getBaseURI();
+ }
+ return ServerEnvironmentInfo.getBaseURI() + "/securitycontext";
+ }
+
+ protected RestClient client;
+
+ @Override
+ public void setUp() {
+ client = new RestClient();
+ }
+
+ /**
+ * Tests that a security context can be injected via a parameter.
+ *
+ * @throws IOException
+ * @throws HttpException
+ * @throws JAXBException
+ */
+ public void testSecurityContextParamResource() throws HttpException, IOException, JAXBException {
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/securitycontext/param").get();
+
+ assertEquals(200, response.getStatusCode());
+
+ JAXBContext context = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
+ SecurityContextInfo secContextInfo =
+ (SecurityContextInfo)context.createUnmarshaller().unmarshal(response
+ .getEntity(InputStream.class));
+ assertNotNull(secContextInfo);
+ assertEquals(false, secContextInfo.isSecure());
+ assertEquals(false, secContextInfo.isUserInRoleAdmin());
+ assertEquals(false, secContextInfo.isUserInRoleNull());
+ assertEquals(false, secContextInfo.isUserInRoleUser());
+ assertEquals("null", secContextInfo.getUserPrincipal());
+ assertNull(secContextInfo.getAuthScheme());
+ }
+
+ /**
+ * Tests that a security context can be injected via a constructor.
+ *
+ * @throws IOException
+ * @throws HttpException
+ * @throws JAXBException
+ */
+ public void testSecurityContextConstructorResource() throws HttpException, IOException,
+ JAXBException {
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/securitycontext/constructor").get();
+
+ assertEquals(200, response.getStatusCode());
+
+ JAXBContext context = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
+ SecurityContextInfo secContextInfo =
+ (SecurityContextInfo)context.createUnmarshaller().unmarshal(response
+ .getEntity(InputStream.class));
+ assertNotNull(secContextInfo);
+ assertEquals(false, secContextInfo.isSecure());
+ assertEquals(false, secContextInfo.isUserInRoleAdmin());
+ assertEquals(false, secContextInfo.isUserInRoleNull());
+ assertEquals(false, secContextInfo.isUserInRoleUser());
+ assertEquals("null", secContextInfo.getUserPrincipal());
+ assertNull(secContextInfo.getAuthScheme());
+ }
+
+ /**
+ * Tests that a security context can be injected via a bean method.
+ *
+ * @throws IOException
+ * @throws HttpException
+ * @throws JAXBException
+ */
+ public void testSecurityContextBeanResource() throws HttpException, IOException, JAXBException {
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/securitycontext/bean").get();
+ assertEquals(200, response.getStatusCode());
+ JAXBContext context = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
+ SecurityContextInfo secContextInfo =
+ (SecurityContextInfo)context.createUnmarshaller().unmarshal(response
+ .getEntity(InputStream.class));
+ assertNotNull(secContextInfo);
+ assertEquals(false, secContextInfo.isSecure());
+ assertEquals(false, secContextInfo.isUserInRoleAdmin());
+ assertEquals(false, secContextInfo.isUserInRoleNull());
+ assertEquals(false, secContextInfo.isUserInRoleUser());
+ assertEquals("null", secContextInfo.getUserPrincipal());
+ assertNull(secContextInfo.getAuthScheme());
+ }
+
+ /**
+ * Tests that a security context will not be injected into non-bean methods.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testSecurityContextNotBeanResource() throws HttpException, IOException {
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/securitycontext/notbeanmethod").get();
+ assertEquals(200, response.getStatusCode());
+ assertEquals("false", response.getEntity(String.class));
+ }
+
+ /**
+ * Tests that a security context can be injected via a member field.
+ *
+ * @throws IOException
+ * @throws HttpException
+ * @throws JAXBException
+ */
+ public void testSecurityContextFieldResource() throws HttpException, IOException, JAXBException {
+ ClientResponse response =
+ client.resource(getBaseURI() + "/context/securitycontext/field").get();
+ assertEquals(200, response.getStatusCode());
+
+ JAXBContext context = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
+ SecurityContextInfo secContextInfo =
+ (SecurityContextInfo)context.createUnmarshaller().unmarshal(response
+ .getEntity(InputStream.class));
+ assertNotNull(secContextInfo);
+ assertEquals(false, secContextInfo.isSecure());
+ assertEquals(false, secContextInfo.isUserInRoleAdmin());
+ assertEquals(false, secContextInfo.isUserInRoleNull());
+ assertEquals(false, secContextInfo.isUserInRoleUser());
+ assertEquals("null", secContextInfo.getUserPrincipal());
+ assertNull(secContextInfo.getAuthScheme());
+ }
+}
Propchange: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/securitycontext/WinkSecurityContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native