|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* This program and the accompanying materials are made available under the |
| 4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| 5 |
* which accompanies this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Denise Smith - 2.6 - initial implementation |
| 12 |
******************************************************************************/ |
| 13 |
package org.eclipse.persistence.testing.jaxb.json.rootlevellist; |
| 14 |
|
| 15 |
import java.io.StringWriter; |
| 16 |
|
| 17 |
import javax.json.Json; |
| 18 |
import javax.json.JsonArray; |
| 19 |
import javax.json.JsonArrayBuilder; |
| 20 |
import javax.json.JsonWriter; |
| 21 |
import javax.xml.bind.JAXBContext; |
| 22 |
import javax.xml.bind.Marshaller; |
| 23 |
|
| 24 |
import org.eclipse.persistence.jaxb.JAXBContextFactory; |
| 25 |
import org.eclipse.persistence.jaxb.JAXBMarshaller; |
| 26 |
import org.eclipse.persistence.jaxb.MarshallerProperties; |
| 27 |
import org.eclipse.persistence.oxm.MediaType; |
| 28 |
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult; |
| 29 |
import org.eclipse.persistence.testing.oxm.OXTestCase; |
| 30 |
|
| 31 |
public class JsonObjectInArrayBuilderTestCases extends OXTestCase { |
| 32 |
|
| 33 |
private static final String CONTROL_JSON = "org/eclipse/persistence/testing/jaxb/json/rootlevellist/WithoutXmlRootElementSingle.json"; |
| 34 |
|
| 35 |
public JsonObjectInArrayBuilderTestCases(String name){ |
| 36 |
super(name); |
| 37 |
} |
| 38 |
|
| 39 |
public void testMarshalToArrayBuilderResult() throws Exception{ |
| 40 |
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithoutXmlRootElementRoot.class}, null); |
| 41 |
Marshaller jsonMarshaller = ctx.createMarshaller(); |
| 42 |
jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); |
| 43 |
jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false); |
| 44 |
|
| 45 |
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); |
| 46 |
JsonArrayBuilderResult result = new JsonArrayBuilderResult(jsonArrayBuilder); |
| 47 |
|
| 48 |
|
| 49 |
WithoutXmlRootElementRoot foo = new WithoutXmlRootElementRoot(); |
| 50 |
foo.setName("FOO"); |
| 51 |
|
| 52 |
jsonMarshaller.marshal(foo, result); |
| 53 |
|
| 54 |
WithoutXmlRootElementRoot foo2 = new WithoutXmlRootElementRoot(); |
| 55 |
foo.setName("FOO2"); |
| 56 |
|
| 57 |
jsonMarshaller.marshal(foo, result); |
| 58 |
|
| 59 |
JsonArray jsonArray = jsonArrayBuilder.build(); |
| 60 |
StringWriter sw = new StringWriter(); |
| 61 |
JsonWriter writer= Json.createWriter(sw); |
| 62 |
writer.writeArray(jsonArray); |
| 63 |
writer.close(); |
| 64 |
|
| 65 |
log(sw.toString()); |
| 66 |
String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]"; |
| 67 |
assertEquals(controlString, sw.toString()); |
| 68 |
} |
| 69 |
|
| 70 |
public void testMarshalToArrayBuilderResultWithRoot() throws Exception{ |
| 71 |
JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithXmlRootElementRoot.class}, null); |
| 72 |
Marshaller jsonMarshaller = ctx.createMarshaller(); |
| 73 |
jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); |
| 74 |
jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); |
| 75 |
|
| 76 |
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); |
| 77 |
JsonArrayBuilderResult result = new JsonArrayBuilderResult(jsonArrayBuilder); |
| 78 |
|
| 79 |
WithXmlRootElementRoot foo = new WithXmlRootElementRoot(); |
| 80 |
foo.setName("FOO"); |
| 81 |
|
| 82 |
jsonMarshaller.marshal(foo, result); |
| 83 |
|
| 84 |
WithXmlRootElementRoot foo2 = new WithXmlRootElementRoot(); |
| 85 |
foo2.setName("FOO2"); |
| 86 |
jsonMarshaller.marshal(foo2, result); |
| 87 |
|
| 88 |
JsonArray jsonArray = jsonArrayBuilder.build(); |
| 89 |
StringWriter sw = new StringWriter(); |
| 90 |
JsonWriter writer= Json.createWriter(sw); |
| 91 |
writer.writeArray(jsonArray); |
| 92 |
writer.close(); |
| 93 |
|
| 94 |
|
| 95 |
log(sw.toString()); |
| 96 |
String controlString = "[{\"root\":{\"name\":\"FOO\"}},{\"root\":{\"name\":\"FOO2\"}}]"; |
| 97 |
assertEquals(controlString, sw.toString()); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
} |