Repository: cxf Updated Branches: refs/heads/master cfda95f21 -> 8ee19a58d
CXF-5413: Support Java API for JSON Processing. Minor classes/packages renaming Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8ee19a58 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8ee19a58 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8ee19a58 Branch: refs/heads/master Commit: 8ee19a58da2195ed990b5f2b07f6af3f27a15d8e Parents: cfda95f Author: reta <[email protected]> Authored: Wed Apr 30 16:44:54 2014 -0400 Committer: reta <[email protected]> Committed: Wed Apr 30 16:44:54 2014 -0400 ---------------------------------------------------------------------- .../provider/jsr353/JsonJsr353Provider.java | 111 ----------- .../provider/jsrjsonp/JsrJsonpProvider.java | 111 +++++++++++ .../provider/jsr353/JsonJsr353ProviderTest.java | 189 ------------------ .../provider/jsrjsonp/JsrJsonpProviderTest.java | 189 ++++++++++++++++++ .../jaxrs/provider/JsonJsr353ProviderTest.java | 192 ------------------- .../jaxrs/provider/JsrJsonpProviderTest.java | 192 +++++++++++++++++++ 6 files changed, 492 insertions(+), 492 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353Provider.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353Provider.java b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353Provider.java deleted file mode 100644 index 26ef3ed..0000000 --- a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353Provider.java +++ /dev/null @@ -1,111 +0,0 @@ -/** - * 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.cxf.jaxrs.provider.jsr353; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonException; -import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.json.JsonStructure; -import javax.json.JsonWriter; -import javax.ws.rs.Consumes; -import javax.ws.rs.Produces; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyReader; -import javax.ws.rs.ext.MessageBodyWriter; -import javax.ws.rs.ext.Provider; - -import org.apache.cxf.jaxrs.utils.ExceptionUtils; - -@Produces({"application/json", "application/*+json" }) -@Consumes({"application/json", "application/*+json" }) -@Provider -public class JsonJsr353Provider implements MessageBodyReader<JsonStructure>, MessageBodyWriter<JsonStructure> { - @Override - public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { - return JsonStructure.class.isAssignableFrom(type) - || JsonObject.class.isAssignableFrom(type) - || JsonArray.class.isAssignableFrom(type); - } - - @Override - public long getSize(JsonStructure t, Class<?> type, Type genericType, Annotation[] annotations, - MediaType mediaType) { - - return -1; - } - - @Override - public void writeTo(JsonStructure t, Class<?> type, Type genericType, Annotation[] annotations, - MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) - throws IOException, WebApplicationException { - - if (entityStream == null) { - throw new IOException("Initialized OutputStream should be provided"); - } - - JsonWriter writer = null; - try { - writer = Json.createWriter(entityStream); - writer.write(t); - } finally { - if (writer != null) { - writer.close(); - } - } - } - - @Override - public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { - return JsonStructure.class.isAssignableFrom(type) - || JsonObject.class.isAssignableFrom(type) - || JsonArray.class.isAssignableFrom(type); - } - - @Override - public JsonStructure readFrom(Class<JsonStructure> type, Type genericType, Annotation[] annotations, - MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) - throws IOException, WebApplicationException { - - if (entityStream == null) { - throw new IOException("Initialized InputStream should be provided"); - } - - JsonReader reader = null; - try { - reader = Json.createReader(entityStream); - return reader.read(); - } catch (JsonException ex) { - throw ExceptionUtils.toBadRequestException(ex, null); - } finally { - if (reader != null) { - reader.close(); - } - } - } -} http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProvider.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProvider.java b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProvider.java new file mode 100644 index 0000000..0c6b047 --- /dev/null +++ b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProvider.java @@ -0,0 +1,111 @@ +/** + * 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.cxf.jaxrs.provider.jsrjsonp; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonException; +import javax.json.JsonObject; +import javax.json.JsonReader; +import javax.json.JsonStructure; +import javax.json.JsonWriter; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import org.apache.cxf.jaxrs.utils.ExceptionUtils; + +@Produces({"application/json", "application/*+json" }) +@Consumes({"application/json", "application/*+json" }) +@Provider +public class JsrJsonpProvider implements MessageBodyReader<JsonStructure>, MessageBodyWriter<JsonStructure> { + @Override + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return JsonStructure.class.isAssignableFrom(type) + || JsonObject.class.isAssignableFrom(type) + || JsonArray.class.isAssignableFrom(type); + } + + @Override + public long getSize(JsonStructure t, Class<?> type, Type genericType, Annotation[] annotations, + MediaType mediaType) { + + return -1; + } + + @Override + public void writeTo(JsonStructure t, Class<?> type, Type genericType, Annotation[] annotations, + MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) + throws IOException, WebApplicationException { + + if (entityStream == null) { + throw new IOException("Initialized OutputStream should be provided"); + } + + JsonWriter writer = null; + try { + writer = Json.createWriter(entityStream); + writer.write(t); + } finally { + if (writer != null) { + writer.close(); + } + } + } + + @Override + public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return JsonStructure.class.isAssignableFrom(type) + || JsonObject.class.isAssignableFrom(type) + || JsonArray.class.isAssignableFrom(type); + } + + @Override + public JsonStructure readFrom(Class<JsonStructure> type, Type genericType, Annotation[] annotations, + MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + + if (entityStream == null) { + throw new IOException("Initialized InputStream should be provided"); + } + + JsonReader reader = null; + try { + reader = Json.createReader(entityStream); + return reader.read(); + } catch (JsonException ex) { + throw ExceptionUtils.toBadRequestException(ex, null); + } finally { + if (reader != null) { + reader.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353ProviderTest.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353ProviderTest.java b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353ProviderTest.java deleted file mode 100644 index 355e33b..0000000 --- a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsr353/JsonJsr353ProviderTest.java +++ /dev/null @@ -1,189 +0,0 @@ -/** - * 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.cxf.jaxrs.provider.jsr353; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.StringWriter; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonObject; -import javax.json.JsonStructure; -import javax.json.JsonValue; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.instanceOf; - -public class JsonJsr353ProviderTest extends Assert { - private JsonJsr353Provider provider; - - @Before - public void setUp() { - provider = new JsonJsr353Provider(); - } - - @Test(expected = IOException.class) - public void testReadWithNullStream() throws Exception { - provider.readFrom(JsonStructure.class, null, null, null, null, null); - } - - @Test - public void testGetSizeReturnsMinusOne() throws Exception { - assertThat(provider.getSize(null, JsonStructure.class, null, null, null), - equalTo(-1L)); - } - - @Test - public void testReadableTypes() throws Exception { - assertThat(provider.isReadable(JsonArray.class, null, null, null), - equalTo(true)); - assertThat(provider.isReadable(JsonStructure.class, null, null, null), - equalTo(true)); - assertThat(provider.isReadable(JsonObject.class, null, null, null), - equalTo(true)); - assertThat(provider.isReadable(JsonValue.class, null, null, null), - equalTo(false)); - } - - @Test - public void testWritableTypes() throws Exception { - assertThat(provider.isWriteable(JsonArray.class, null, null, null), - equalTo(true)); - assertThat(provider.isWriteable(JsonStructure.class, null, null, null), - equalTo(true)); - assertThat(provider.isWriteable(JsonObject.class, null, null, null), - equalTo(true)); - assertThat(provider.isWriteable(JsonValue.class, null, null, null), - equalTo(false)); - } - - @Test(expected = IOException.class) - public void testWriteWithNullStream() throws Exception { - final JsonObject obj = Json.createObjectBuilder() - .add("firstName", "Tom") - .add("lastName", "Tommyknocker") - .build(); - - provider.writeTo(obj, JsonObject.class, null, null, null, null, null); - } - - @Test - public void testReadMalformedJson() throws Exception { - byte[] bytes = "junk".getBytes(); - - try { - provider.readFrom(JsonStructure.class, null, null, null, null, - new ByteArrayInputStream(bytes)); - fail("400 BAD REQUEST is expected"); - } catch (WebApplicationException ex) { - assertThat(ex.getResponse().getStatus(), - equalTo(Response.Status.BAD_REQUEST.getStatusCode())); - } - } - - @Test - public void testReadJsonObject() throws Exception { - final StringWriter writer = new StringWriter(); - - Json.createGenerator(writer) - .writeStartObject() - .write("firstName", "Tom") - .write("lastName", "Tommyknocker") - .writeEnd() - .close(); - - final String str = writer.toString(); - writer.close(); - - final JsonStructure obj = provider.readFrom(JsonStructure.class, null, null, null, null, - new ByteArrayInputStream(str.getBytes())); - - assertThat(obj, instanceOf(JsonObject.class)); - assertThat(((JsonObject)obj).getString("firstName"), equalTo("Tom")); - assertThat(((JsonObject)obj).getString("lastName"), equalTo("Tommyknocker")); - } - - @Test - public void testReadJsonArray() throws Exception { - final StringWriter writer = new StringWriter(); - - Json.createGenerator(writer) - .writeStartArray() - .write("Tom") - .write("Tommyknocker") - .writeEnd() - .close(); - - final JsonStructure obj = provider.readFrom(JsonStructure.class, null, null, null, null, - new ByteArrayInputStream(writer.toString().getBytes())); - - assertThat(obj, instanceOf(JsonArray.class)); - assertThat(((JsonArray)obj).getString(0), equalTo("Tom")); - assertThat(((JsonArray)obj).getString(1), equalTo("Tommyknocker")); - assertThat(((JsonArray)obj).size(), equalTo(2)); - } - - @Test - public void testWriteJsonObject() throws Exception { - final JsonObject obj = Json.createObjectBuilder() - .add("firstName", "Tom") - .add("lastName", "Tommyknocker") - .build(); - - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - provider.writeTo(obj, JsonStructure.class, null, null, null, null, out); - out.close(); - - assertThat(new String(out.toByteArray()), - equalTo("{\"firstName\":\"Tom\",\"lastName\":\"Tommyknocker\"}")); - } - - @Test - public void testWriteJsonArray() throws Exception { - final JsonArray obj = Json.createArrayBuilder() - .add( - Json.createObjectBuilder() - .add("firstName", "Tom") - .add("lastName", "Tommyknocker") - ) - .add( - Json.createObjectBuilder() - .add("firstName", "Bob") - .add("lastName", "Bobbyknocker") - ) - .build(); - - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - provider.writeTo(obj, JsonStructure.class, null, null, null, null, out); - out.close(); - - assertThat(new String(out.toByteArray()), - equalTo("[{\"firstName\":\"Tom\",\"lastName\":\"Tommyknocker\"}," - + "{\"firstName\":\"Bob\",\"lastName\":\"Bobbyknocker\"}]")); - } - -} http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProviderTest.java ---------------------------------------------------------------------- diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProviderTest.java b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProviderTest.java new file mode 100644 index 0000000..a2002d4 --- /dev/null +++ b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/jsrjsonp/JsrJsonpProviderTest.java @@ -0,0 +1,189 @@ +/** + * 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.cxf.jaxrs.provider.jsrjsonp; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringWriter; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; +import javax.json.JsonStructure; +import javax.json.JsonValue; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; + +public class JsrJsonpProviderTest extends Assert { + private JsrJsonpProvider provider; + + @Before + public void setUp() { + provider = new JsrJsonpProvider(); + } + + @Test(expected = IOException.class) + public void testReadWithNullStream() throws Exception { + provider.readFrom(JsonStructure.class, null, null, null, null, null); + } + + @Test + public void testGetSizeReturnsMinusOne() throws Exception { + assertThat(provider.getSize(null, JsonStructure.class, null, null, null), + equalTo(-1L)); + } + + @Test + public void testReadableTypes() throws Exception { + assertThat(provider.isReadable(JsonArray.class, null, null, null), + equalTo(true)); + assertThat(provider.isReadable(JsonStructure.class, null, null, null), + equalTo(true)); + assertThat(provider.isReadable(JsonObject.class, null, null, null), + equalTo(true)); + assertThat(provider.isReadable(JsonValue.class, null, null, null), + equalTo(false)); + } + + @Test + public void testWritableTypes() throws Exception { + assertThat(provider.isWriteable(JsonArray.class, null, null, null), + equalTo(true)); + assertThat(provider.isWriteable(JsonStructure.class, null, null, null), + equalTo(true)); + assertThat(provider.isWriteable(JsonObject.class, null, null, null), + equalTo(true)); + assertThat(provider.isWriteable(JsonValue.class, null, null, null), + equalTo(false)); + } + + @Test(expected = IOException.class) + public void testWriteWithNullStream() throws Exception { + final JsonObject obj = Json.createObjectBuilder() + .add("firstName", "Tom") + .add("lastName", "Tommyknocker") + .build(); + + provider.writeTo(obj, JsonObject.class, null, null, null, null, null); + } + + @Test + public void testReadMalformedJson() throws Exception { + byte[] bytes = "junk".getBytes(); + + try { + provider.readFrom(JsonStructure.class, null, null, null, null, + new ByteArrayInputStream(bytes)); + fail("400 BAD REQUEST is expected"); + } catch (WebApplicationException ex) { + assertThat(ex.getResponse().getStatus(), + equalTo(Response.Status.BAD_REQUEST.getStatusCode())); + } + } + + @Test + public void testReadJsonObject() throws Exception { + final StringWriter writer = new StringWriter(); + + Json.createGenerator(writer) + .writeStartObject() + .write("firstName", "Tom") + .write("lastName", "Tommyknocker") + .writeEnd() + .close(); + + final String str = writer.toString(); + writer.close(); + + final JsonStructure obj = provider.readFrom(JsonStructure.class, null, null, null, null, + new ByteArrayInputStream(str.getBytes())); + + assertThat(obj, instanceOf(JsonObject.class)); + assertThat(((JsonObject)obj).getString("firstName"), equalTo("Tom")); + assertThat(((JsonObject)obj).getString("lastName"), equalTo("Tommyknocker")); + } + + @Test + public void testReadJsonArray() throws Exception { + final StringWriter writer = new StringWriter(); + + Json.createGenerator(writer) + .writeStartArray() + .write("Tom") + .write("Tommyknocker") + .writeEnd() + .close(); + + final JsonStructure obj = provider.readFrom(JsonStructure.class, null, null, null, null, + new ByteArrayInputStream(writer.toString().getBytes())); + + assertThat(obj, instanceOf(JsonArray.class)); + assertThat(((JsonArray)obj).getString(0), equalTo("Tom")); + assertThat(((JsonArray)obj).getString(1), equalTo("Tommyknocker")); + assertThat(((JsonArray)obj).size(), equalTo(2)); + } + + @Test + public void testWriteJsonObject() throws Exception { + final JsonObject obj = Json.createObjectBuilder() + .add("firstName", "Tom") + .add("lastName", "Tommyknocker") + .build(); + + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + provider.writeTo(obj, JsonStructure.class, null, null, null, null, out); + out.close(); + + assertThat(new String(out.toByteArray()), + equalTo("{\"firstName\":\"Tom\",\"lastName\":\"Tommyknocker\"}")); + } + + @Test + public void testWriteJsonArray() throws Exception { + final JsonArray obj = Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add("firstName", "Tom") + .add("lastName", "Tommyknocker") + ) + .add( + Json.createObjectBuilder() + .add("firstName", "Bob") + .add("lastName", "Bobbyknocker") + ) + .build(); + + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + provider.writeTo(obj, JsonStructure.class, null, null, null, null, out); + out.close(); + + assertThat(new String(out.toByteArray()), + equalTo("[{\"firstName\":\"Tom\",\"lastName\":\"Tommyknocker\"}," + + "{\"firstName\":\"Bob\",\"lastName\":\"Bobbyknocker\"}]")); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsonJsr353ProviderTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsonJsr353ProviderTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsonJsr353ProviderTest.java deleted file mode 100644 index d0e7d1a..0000000 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsonJsr353ProviderTest.java +++ /dev/null @@ -1,192 +0,0 @@ -/** - * 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.cxf.systest.jaxrs.provider; - -import java.util.Arrays; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonObject; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; - -import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; -import org.apache.cxf.jaxrs.client.WebClient; -import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; -import org.apache.cxf.jaxrs.model.AbstractResourceInfo; -import org.apache.cxf.jaxrs.provider.jsr353.JsonJsr353Provider; -import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; -import org.apache.cxf.testutil.common.AbstractBusTestServerBase; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.core.IsNull.nullValue; - - -public class JsonJsr353ProviderTest extends AbstractBusClientServerTestBase { - public static final String PORT = allocatePort(JsonJsr353ProviderTest.class); - - @Ignore - public static class Server extends AbstractBusTestServerBase { - protected void run() { - final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); - sf.setResourceClasses(BookJsonStore.class); - sf.setResourceProvider(BookJsonStore.class, - new SingletonResourceProvider(new BookJsonStore())); - sf.setProvider(new JsonJsr353Provider()); - sf.setAddress("http://localhost:" + PORT + "/"); - sf.create(); - } - - public static void main(String[] args) { - try { - Server s = new Server(); - s.start(); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(-1); - } finally { - System.out.println("done!"); - } - } - } - - @BeforeClass - public static void startServers() throws Exception { - AbstractResourceInfo.clearAllMaps(); - //keep out of process due to stack traces testing failures - assertTrue("server did not launch correctly", launchServer(Server.class, true)); - createStaticBus(); - } - - @Before - public void setUp() { - final Response r = createWebClient("/bookstore/books").delete(); - assertEquals(Status.OK.getStatusCode(), r.getStatus()); - } - - @Test - public void testNoResultsAreReturned() throws Exception { - final Response r = createWebClient("/bookstore/books/155").get(); - assertEquals(Status.NO_CONTENT.getStatusCode(), r.getStatus()); - } - - @Test - public void testPostSimpleJsonObject() { - final Response r = createWebClient("/bookstore/books") - .header("Content-Type", MediaType.APPLICATION_JSON) - .post( - Json - .createObjectBuilder() - .add("id", 1) - .add("name", "Book 1") - .build() - ); - assertEquals(Status.CREATED.getStatusCode(), r.getStatus()); - } - - @Test - public void testPostComplexJsonObject() { - final Response r = createWebClient("/bookstore/books") - .header("Content-Type", MediaType.APPLICATION_JSON) - .post( - Json - .createObjectBuilder() - .add("id", 1) - .add("name", "Book 1") - .add("chapters", - Json.createArrayBuilder() - .add( - Json.createObjectBuilder() - .add("id", 1) - .add("title", "Chapter 1") - ) - .add( - Json.createObjectBuilder() - .add("id", 2) - .add("title", "Chapter 2") - ) - ) - .build() - ); - assertEquals(Status.CREATED.getStatusCode(), r.getStatus()); - } - - @Test - public void testPostAndGetSimpleJsonObject() { - testPostSimpleJsonObject(); - - final Response r = createWebClient("/bookstore/books/1").get(); - assertEquals(Status.OK.getStatusCode(), r.getStatus()); - - JsonObject obj = r.readEntity(JsonObject.class); - assertThat(obj.getInt("id"), equalTo(1)); - assertThat(obj.getString("name"), equalTo("Book 1")); - assertThat(obj.get("chapters"), nullValue()); - } - - @Test - public void testPostAndGetComplexJsonObject() { - testPostComplexJsonObject(); - - final Response r = createWebClient("/bookstore/books/1").get(); - assertEquals(Status.OK.getStatusCode(), r.getStatus()); - - JsonObject obj = r.readEntity(JsonObject.class); - assertThat(obj.getInt("id"), equalTo(1)); - assertThat(obj.getString("name"), equalTo("Book 1")); - assertThat(obj.get("chapters"), instanceOf(JsonArray.class)); - - final JsonArray chapters = (JsonArray)obj.get("chapters"); - assertThat(chapters.size(), equalTo(2)); - assertThat(((JsonObject)chapters.get(0)).getInt("id"), equalTo(1)); - assertThat(((JsonObject)chapters.get(0)).getString("title"), equalTo("Chapter 1")); - assertThat(((JsonObject)chapters.get(1)).getInt("id"), equalTo(2)); - assertThat(((JsonObject)chapters.get(1)).getString("title"), equalTo("Chapter 2")); - } - - @Test - public void testPostAndGetBooks() { - testPostSimpleJsonObject(); - - final Response r = createWebClient("/bookstore/books").get(); - assertEquals(Status.OK.getStatusCode(), r.getStatus()); - - final JsonArray obj = r.readEntity(JsonArray.class); - assertThat(obj.size(), equalTo(1)); - assertThat(obj.get(0), instanceOf(JsonObject.class)); - - assertThat(((JsonObject)obj.get(0)).getInt("id"), equalTo(1)); - assertThat(((JsonObject)obj.get(0)).getString("name"), equalTo("Book 1")); - } - - private static WebClient createWebClient(final String url) { - return WebClient - .create("http://localhost:" + PORT + url, - Arrays.< Object >asList(new JsonJsr353Provider())) - .accept(MediaType.APPLICATION_JSON); - } - -} http://git-wip-us.apache.org/repos/asf/cxf/blob/8ee19a58/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsrJsonpProviderTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsrJsonpProviderTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsrJsonpProviderTest.java new file mode 100644 index 0000000..e8ba151 --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/provider/JsrJsonpProviderTest.java @@ -0,0 +1,192 @@ +/** + * 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.cxf.systest.jaxrs.provider; + +import java.util.Arrays; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider; +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.core.IsNull.nullValue; + + +public class JsrJsonpProviderTest extends AbstractBusClientServerTestBase { + public static final String PORT = allocatePort(JsrJsonpProviderTest.class); + + @Ignore + public static class Server extends AbstractBusTestServerBase { + protected void run() { + final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); + sf.setResourceClasses(BookJsonStore.class); + sf.setResourceProvider(BookJsonStore.class, + new SingletonResourceProvider(new BookJsonStore())); + sf.setProvider(new JsrJsonpProvider()); + sf.setAddress("http://localhost:" + PORT + "/"); + sf.create(); + } + + public static void main(String[] args) { + try { + Server s = new Server(); + s.start(); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(-1); + } finally { + System.out.println("done!"); + } + } + } + + @BeforeClass + public static void startServers() throws Exception { + AbstractResourceInfo.clearAllMaps(); + //keep out of process due to stack traces testing failures + assertTrue("server did not launch correctly", launchServer(Server.class, true)); + createStaticBus(); + } + + @Before + public void setUp() { + final Response r = createWebClient("/bookstore/books").delete(); + assertEquals(Status.OK.getStatusCode(), r.getStatus()); + } + + @Test + public void testNoResultsAreReturned() throws Exception { + final Response r = createWebClient("/bookstore/books/155").get(); + assertEquals(Status.NO_CONTENT.getStatusCode(), r.getStatus()); + } + + @Test + public void testPostSimpleJsonObject() { + final Response r = createWebClient("/bookstore/books") + .header("Content-Type", MediaType.APPLICATION_JSON) + .post( + Json + .createObjectBuilder() + .add("id", 1) + .add("name", "Book 1") + .build() + ); + assertEquals(Status.CREATED.getStatusCode(), r.getStatus()); + } + + @Test + public void testPostComplexJsonObject() { + final Response r = createWebClient("/bookstore/books") + .header("Content-Type", MediaType.APPLICATION_JSON) + .post( + Json + .createObjectBuilder() + .add("id", 1) + .add("name", "Book 1") + .add("chapters", + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add("id", 1) + .add("title", "Chapter 1") + ) + .add( + Json.createObjectBuilder() + .add("id", 2) + .add("title", "Chapter 2") + ) + ) + .build() + ); + assertEquals(Status.CREATED.getStatusCode(), r.getStatus()); + } + + @Test + public void testPostAndGetSimpleJsonObject() { + testPostSimpleJsonObject(); + + final Response r = createWebClient("/bookstore/books/1").get(); + assertEquals(Status.OK.getStatusCode(), r.getStatus()); + + JsonObject obj = r.readEntity(JsonObject.class); + assertThat(obj.getInt("id"), equalTo(1)); + assertThat(obj.getString("name"), equalTo("Book 1")); + assertThat(obj.get("chapters"), nullValue()); + } + + @Test + public void testPostAndGetComplexJsonObject() { + testPostComplexJsonObject(); + + final Response r = createWebClient("/bookstore/books/1").get(); + assertEquals(Status.OK.getStatusCode(), r.getStatus()); + + JsonObject obj = r.readEntity(JsonObject.class); + assertThat(obj.getInt("id"), equalTo(1)); + assertThat(obj.getString("name"), equalTo("Book 1")); + assertThat(obj.get("chapters"), instanceOf(JsonArray.class)); + + final JsonArray chapters = (JsonArray)obj.get("chapters"); + assertThat(chapters.size(), equalTo(2)); + assertThat(((JsonObject)chapters.get(0)).getInt("id"), equalTo(1)); + assertThat(((JsonObject)chapters.get(0)).getString("title"), equalTo("Chapter 1")); + assertThat(((JsonObject)chapters.get(1)).getInt("id"), equalTo(2)); + assertThat(((JsonObject)chapters.get(1)).getString("title"), equalTo("Chapter 2")); + } + + @Test + public void testPostAndGetBooks() { + testPostSimpleJsonObject(); + + final Response r = createWebClient("/bookstore/books").get(); + assertEquals(Status.OK.getStatusCode(), r.getStatus()); + + final JsonArray obj = r.readEntity(JsonArray.class); + assertThat(obj.size(), equalTo(1)); + assertThat(obj.get(0), instanceOf(JsonObject.class)); + + assertThat(((JsonObject)obj.get(0)).getInt("id"), equalTo(1)); + assertThat(((JsonObject)obj.get(0)).getString("name"), equalTo("Book 1")); + } + + private static WebClient createWebClient(final String url) { + return WebClient + .create("http://localhost:" + PORT + url, + Arrays.< Object >asList(new JsrJsonpProvider())) + .accept(MediaType.APPLICATION_JSON); + } + +}
