You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by ro...@apache.org on 2010/03/02 23:26:30 UTC
svn commit: r918227 -
/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/
Author: rott
Date: Tue Mar 2 22:26:29 2010
New Revision: 918227
URL: http://svn.apache.org/viewvc?rev=918227&view=rev
Log:
jackson configuration tests - thanks to Jesse Ramos for the contribution
Added:
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration2Test.java
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration3Test.java
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfigurationTest.java
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonPOJOTest.java
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration1Test.java
incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration2Test.java
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration2Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration2Test.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration2Test.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration2Test.java Tue Mar 2 22:26:29 2010
@@ -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.providers.jackson.internal;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JacksonDeserializationConfiguration2Test extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {Resource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ jacksonProvider.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false);
+ jacksonProvider.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.USE_BIG_DECIMAL_FOR_FLOATS, true);
+ jacksonProvider.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.USE_BIG_INTEGER_FOR_INTS, true);
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("/resource")
+ public static class Resource {
+
+ @POST
+ @Path("personwithchildren")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public List<String> postPersonWithChildren(Person person) {
+ return person.getChildren();
+ }
+
+ @POST
+ @Path("personwithweight")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public void postPersonWithAgeWeight(Person person) {
+ Object weight = person.getWeight();
+ if(!(weight instanceof BigDecimal))
+ throw new WebApplicationException();
+ Object age = person.getAge();
+ if(!(age instanceof BigInteger))
+ throw new WebApplicationException();
+ }
+ }
+
+ public static class Person {
+ private List<String> children = new ArrayList<String>();
+ private Object weight;
+ private Object age;
+
+ public Object getAge() {
+ return age;
+ }
+
+ public void setAge(Object age) {
+ this.age = age;
+ }
+
+ public List<String> getChildren() {
+ return children;
+ }
+
+ public Object getWeight() {
+ return weight;
+ }
+
+ public void setWeight(Object weight) {
+ this.weight = weight;
+ }
+ }
+
+ public void testPOSTPersonWithChildren() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/resource/personwithchildren",
+ MediaType.APPLICATION_JSON);
+ request.setContentType(MediaType.APPLICATION_JSON);
+ request.setContent("{\"children\":[\"Joe\",\"Sally\",\"Steve\"]}".getBytes());
+ try{
+ invoke(request);
+ fail("ServletException not thrown for missing setter method of children.");
+ } catch(ServletException e) {}
+ }
+
+ public void testPOSTPersonWithAgeWeight() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/resource/personwithweight",
+ MediaType.APPLICATION_JSON);
+ request.setContentType(MediaType.APPLICATION_JSON);
+ request.setContent("{\"weight\":160.333, \"age\":27}".getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(204, response.getStatus());
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration3Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration3Test.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration3Test.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfiguration3Test.java Tue Mar 2 22:26:29 2010
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * 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.providers.jackson.internal;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.providers.json.JSONUtils;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.annotate.JsonIgnore;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.json.JSONObject;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JacksonDeserializationConfiguration3Test extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {Resource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.USE_ANNOTATIONS, false);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.DeserializationConfig.Feature.USE_ANNOTATIONS,
+ false);
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("resource")
+ public static class Resource {
+
+ @POST
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Shape postShape(Shape shape) {
+ return shape;
+ }
+ }
+
+ public static class Shape {
+ private int numSides;
+ private String name;
+
+ public int getNumSides() {
+ return numSides;
+ }
+
+ @JsonIgnore
+ public void setNumSides(int numSides) {
+ this.numSides = numSides;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ }
+
+ public void testPOSTShape() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/resource",
+ MediaType.APPLICATION_JSON);
+ request.setContentType(MediaType.APPLICATION_JSON);
+ request.setContent("{\"numSides\":4, \"name\":\"square\"}".getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ System.out.println(response.getContentAsString());
+ assertTrue(JSONUtils.equals(new JSONObject("{\"numSides\":4, \"name\":\"square\"}"),
+ new JSONObject(response.getContentAsString())));
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfigurationTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfigurationTest.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfigurationTest.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonDeserializationConfigurationTest.java Tue Mar 2 22:26:29 2010
@@ -0,0 +1,153 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ *******************************************************************************/
+
+package org.apache.wink.providers.jackson.internal;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+public class JacksonDeserializationConfigurationTest extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {Resource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.DeserializationConfig.Feature.AUTO_DETECT_SETTERS,
+ false);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.DeserializationConfig.Feature.AUTO_DETECT_FIELDS,
+ false);
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("/resource")
+ public static class Resource {
+
+ @POST
+ @Path("personsetters")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public PersonSetters postPersonSetters(PersonSetters person) {
+ return person;
+ }
+
+ @POST
+ @Path("personfields")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public PersonFields postPersonFields(PersonFields person) {
+ return person;
+ }
+
+ @POST
+ @Path("personchildren")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public PersonSetters postChildren(PersonSetters person) {
+ return person;
+ }
+ }
+
+ public static class PersonSetters {
+ private String first;
+ private String last;
+ private boolean awake = false;
+
+ public boolean isAwake() {
+ return awake;
+ }
+
+ public void setAwake(boolean awake) {
+ this.awake = awake;
+ }
+
+ public String nickname;
+
+ public String getFirst() {
+ return first;
+ }
+
+ public void setFirst(String first) {
+ this.first = first;
+ }
+
+ public String getLast() {
+ return last;
+ }
+
+ public void setLast(String last) {
+ this.last = last;
+ }
+ }
+
+ public static class PersonFields {
+ public String first;
+ public String last;
+ public boolean awake = false;
+ public String nickname;
+ }
+
+ public void testPOSTPersonSetters() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/resource/personsetters",
+ MediaType.APPLICATION_JSON);
+ request.setContentType(MediaType.APPLICATION_JSON);
+ request
+ .setContent("{\"first\":\"firstName\", \"last\":\"lastName\", \"awake\":true, \"nickname\":\"Bill\"}"
+ .getBytes());
+ try {
+ invoke(request);
+ fail("ServletException was not thrown when specifying fields that are not detected.");
+ } catch (ServletException e) {
+ }
+ }
+
+ public void testPOSTPersonFields() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/resource/personfields",
+ MediaType.APPLICATION_JSON);
+ request.setContentType(MediaType.APPLICATION_JSON);
+ request
+ .setContent("{\"first\":\"firstName\", \"last\":\"lastName\", \"awake\":true, \"nickname\":\"Bill\"}"
+ .getBytes());
+ try {
+ invoke(request);
+ fail("ServletException was not thrown when specifying fields that are not detected.");
+ } catch (ServletException e) {
+ }
+ }
+
+}
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonPOJOTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonPOJOTest.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonPOJOTest.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonPOJOTest.java Tue Mar 2 22:26:29 2010
@@ -0,0 +1,448 @@
+/*******************************************************************************
+ * 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.providers.jackson.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.wink.providers.json.JSONUtils;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JacksonPOJOTest extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {POJOResource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("/jackson/pojo")
+ public static class POJOResource {
+
+ @GET
+ @Produces("application/json")
+ @Path("person")
+ public Person getPerson() {
+ Person p = new Person();
+ p.setFirst("first");
+ p.setLast("last");
+ return p;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("person")
+ public Person postPerson(Person p) {
+ return p;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("string")
+ public List<String> getCollection() {
+ List<String> list = new ArrayList<String>();
+ list.add("string1");
+ list.add("");
+ list.add("string3");
+ return list;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("string")
+ public List<String> postCollection(List<String> list) {
+ return list;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("personcollect")
+ public List<Person> getPersonCollection() {
+ List<Person> people = new ArrayList<Person>();
+ Person p = new Person();
+ p.setFirst("first1");
+ p.setLast("last1");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first2");
+ p.setLast("last2");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first3");
+ p.setLast("last3");
+ people.add(p);
+ return people;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("personcollect")
+ public List<Person> postPeopleCollection(List<Person> people) {
+ return people;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("stringarray")
+ public String[] getArray() {
+ String[] list = new String[4];
+ list[0] = "string1";
+ list[1] = "";
+ list[2] = null;
+ list[3] = "string4";
+ return list;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("stringarray")
+ public String[] postArray(String[] list) {
+ return list;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("personarray")
+ public Person[] getPeopleArray() {
+ Person[] people = new Person[3];
+ Person p = new Person();
+ p.setFirst("first1");
+ p.setLast("last1");
+ people[0] = p;
+ p = new Person();
+ p.setFirst("first2");
+ p.setLast("last2");
+ people[1] = p;
+ p = new Person();
+ p.setFirst("first3");
+ p.setLast("last3");
+ people[2] = p;
+ return people;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("personarray")
+ public Person[] postPeopleArray(Person[] people) {
+ return people;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("collectionofcollection")
+ public List<List<Person>> getCollectionofCollection() {
+ List<List<Person>> peopleCollection = new ArrayList<List<Person>>();
+
+ List<Person> people = new ArrayList<Person>();
+ Person p = new Person();
+ p.setFirst("first1");
+ p.setLast("last1");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first2");
+ p.setLast("last2");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first3");
+ p.setLast("last3");
+ people.add(p);
+ peopleCollection.add(people);
+
+ people = new ArrayList<Person>();
+ p = new Person();
+ p.setFirst("first4");
+ p.setLast("last4");
+ people.add(p);
+ people.add(null);
+ p = new Person();
+ p.setFirst("first6");
+ p.setLast("last6");
+ people.add(p);
+ peopleCollection.add(people);
+
+ return peopleCollection;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("collectionofcollection")
+ public List<List<Person>> postCollectionofCollection(List<List<Person>> peopleCollection) {
+ return peopleCollection;
+ }
+
+ @GET
+ @Produces("application/json")
+ @Path("collectionofarray")
+ public List<Person[]> getCollectionofArray() {
+ List<Person[]> peopleCollection = new ArrayList<Person[]>();
+
+ List<Person> people = new ArrayList<Person>();
+ Person p = new Person();
+ p.setFirst("first1");
+ p.setLast("last1");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first2");
+ p.setLast("last2");
+ people.add(p);
+ p = new Person();
+ p.setFirst("first3");
+ p.setLast("last3");
+ people.add(p);
+ peopleCollection.add(people.toArray(new Person[] {}));
+
+ people = new ArrayList<Person>();
+ p = new Person();
+ p.setFirst("first4");
+ p.setLast("last4");
+ people.add(p);
+ people.add(null);
+ p = new Person();
+ p.setFirst("first6");
+ p.setLast("last6");
+ people.add(p);
+ peopleCollection.add(people.toArray(new Person[] {}));
+
+ return peopleCollection;
+ }
+
+ @POST
+ @Produces("application/json")
+ @Consumes("application/json")
+ @Path("collectionofarray")
+ public List<Person[]> postCollectionofArray(List<Person[]> peopleCollection) {
+ return peopleCollection;
+ }
+ }
+
+ public static class Person {
+ String first;
+ String last;
+
+ public String getFirst() {
+ return first;
+ }
+
+ public void setFirst(String first) {
+ this.first = first;
+ }
+
+ public String getLast() {
+ return last;
+ }
+
+ public void setLast(String last) {
+ this.last = last;
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof Person))
+ return false;
+ Person other = (Person)o;
+ return this.first.equals(other.first) && this.last.equals(other.last);
+ }
+ }
+
+ public void testGETPerson() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/jackson/pojo/person",
+ "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils.equals(new JSONObject("{\"first\":\"first\", \"last\":\"last\"}"),
+ new JSONObject(response.getContentAsString())));
+ }
+
+ public void testPOSTPerson() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/jackson/pojo/person",
+ "application/json");
+ request.setContentType("application/json");
+ request.setContent("{\"first\":\"first\", \"last\":\"last\"}".getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils.equals(new JSONObject("{\"first\":\"first\", \"last\":\"last\"}"),
+ new JSONObject(response.getContentAsString())));
+ }
+
+ public void testGETCollection() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/jackson/pojo/string",
+ "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils.equals(new JSONArray("[\"string1\", \"\", \"string3\"]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testPOSTCollection() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/jackson/pojo/string",
+ "application/json");
+ request.setContentType("application/json");
+ request.setContent("[\"string1\", \"\", \"string3\"]".getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils.equals(new JSONArray("[\"string1\", \"\", \"string3\"]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testGETCollectionWithObject() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/jackson/pojo/personcollect",
+ "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testPOSTCollectionWithObject() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/jackson/pojo/personcollect",
+ "application/json");
+ request.setContentType("application/json");
+ request
+ .setContent(("[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}]").getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testGETCollectionWithCollection() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/jackson/pojo/collectionofcollection",
+ "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testPOSTCollectionWithCollection() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/jackson/pojo/collectionofcollection",
+ "application/json");
+ request.setContentType("application/json");
+ request
+ .setContent(("[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]").getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testGETCollectionWithArray() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/jackson/pojo/collectionofarray",
+ "application/json");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]"),
+ new JSONArray(response.getContentAsString())));
+ }
+
+ public void testPOSTCollectionWithArray() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("POST",
+ "/jackson/pojo/collectionofarray",
+ "application/json");
+ request.setContentType("application/json");
+ request
+ .setContent(("[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]").getBytes());
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils
+ .equals(new JSONArray(
+ "[[{\"first\":\"first1\",\"last\":\"last1\"}," + "{\"first\":\"first2\",\"last\":\"last2\"},"
+ + "{\"first\":\"first3\",\"last\":\"last3\"}],"
+ + "[{\"first\":\"first4\",\"last\":\"last4\"},"
+ + "null,"
+ + "{\"first\":\"first6\",\"last\":\"last6\"}]]"),
+ new JSONArray(response.getContentAsString())));
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration1Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration1Test.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration1Test.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration1Test.java Tue Mar 2 22:26:29 2010
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * 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.providers.jackson.internal;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.providers.json.JSONUtils;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.json.JSONObject;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JacksonSerializationConfiguration1Test extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {PersonResource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_FIELDS,
+ false);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_GETTERS,
+ false);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS,
+ false);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
+ false);
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("/person")
+ public static class PersonResource {
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Person getPerson() {
+ Person p = new Person();
+ p.setFirst("firstName");
+ p.setLast("lastName");
+ p.nickname = "nickName";
+ return p;
+ }
+ }
+
+ public static class Person {
+ private String first;
+ private String last;
+ private boolean awake = false;
+
+ public boolean isAwake() {
+ return awake;
+ }
+
+ public void setAwake(boolean awake) {
+ this.awake = awake;
+ }
+
+ public String nickname;
+
+ public String getFirst() {
+ return first;
+ }
+
+ public void setFirst(String first) {
+ this.first = first;
+ }
+
+ public String getLast() {
+ return last;
+ }
+
+ public void setLast(String last) {
+ this.last = last;
+ }
+ }
+
+ public void testGETPerson() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/person",
+ MediaType.APPLICATION_JSON);
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ assertTrue(JSONUtils.equals(new JSONObject("{}"), new JSONObject(response
+ .getContentAsString())));
+ }
+}
Added: incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration2Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration2Test.java?rev=918227&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration2Test.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jackson-provider/src/test/java/org/apache/wink/providers/jackson/internal/JacksonSerializationConfiguration2Test.java Tue Mar 2 22:26:29 2010
@@ -0,0 +1,187 @@
+/*******************************************************************************
+ * 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.providers.jackson.internal;
+
+import java.util.Random;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.providers.json.JSONUtils;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
+import org.json.JSONObject;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JacksonSerializationConfiguration2Test extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class<?>[] {Resource.class};
+ }
+
+ @Override
+ protected Object[] getSingletons() {
+ JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
+ true);
+ jacksonProvider
+ .configure(org.codehaus.jackson.map.SerializationConfig.Feature.USE_STATIC_TYPING, true);
+ return new Object[] {jacksonProvider};
+ }
+
+ @Path("/resource")
+ public static class Resource {
+
+ @GET
+ @Path("empty")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Empty getEmptyPOJO() {
+ return new Empty();
+ }
+
+ @GET
+ @Path("group")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Group getMammalGroup(@QueryParam("num") int num) {
+ Group group = new Group();
+ Mammal leader;
+ if (num == 0) {
+ leader = new Dog();
+ leader.setName("Fido");
+ leader.setSpecies("Dog");
+ ((Dog)leader).setBreed("Laborador");
+ } else {
+ leader = new Whale();
+ leader.setName("Shamu");
+ leader.setSpecies("Whale");
+ ((Whale)leader).setToothed(true);
+
+ }
+ group.setLeader(leader);
+ return group;
+ }
+ }
+
+ public static class Empty {
+
+ }
+
+ public static class Group {
+ public Mammal leader;
+
+ public Mammal getLeader() {
+ return leader;
+ }
+
+ public void setLeader(Mammal leader) {
+ this.leader = leader;
+ }
+
+ }
+
+ public static class Mammal {
+ private String species;
+ private String name;
+
+ public String getSpecies() {
+ return species;
+ }
+
+ public void setSpecies(String species) {
+ this.species = species;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ }
+
+ public static class Dog extends Mammal {
+ public String breed;
+
+ public String getBreed() {
+ return breed;
+ }
+
+ public void setBreed(String breed) {
+ this.breed = breed;
+ }
+
+ }
+
+ public static class Whale extends Mammal {
+ public boolean isToothed;
+
+ public boolean isToothed() {
+ return isToothed;
+ }
+
+ public void setToothed(boolean isToothed) {
+ this.isToothed = isToothed;
+ }
+
+ }
+
+ public void testGETEmpty() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/resource/empty",
+ MediaType.APPLICATION_JSON);
+ try {
+ invoke(request);
+ fail("ServletException was not thrown for empty bean.");
+ } catch (ServletException e) {
+ }
+ }
+
+ public void testGETStaticTyping() throws Exception {
+ Random r = new Random();
+ int num = r.nextInt(2);
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/resource/group",
+ MediaType.APPLICATION_JSON);
+ request.setQueryString("num=" + num);
+ MockHttpServletResponse response = invoke(request);
+ assertEquals(200, response.getStatus());
+ if (num == 0)
+ assertTrue(JSONUtils
+ .equals(new JSONObject("{\"leader\":{\"species\":\"Dog\", \"name\":\"Fido\"}}"),
+ new JSONObject(response.getContentAsString())));
+ else
+ assertTrue(JSONUtils
+ .equals(new JSONObject("{\"leader\":{\"species\":\"Whale\", \"name\":\"Shamu\"}}"),
+ new JSONObject(response.getContentAsString())));
+ }
+}