|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 the authors of j2objc-common-libs-e2e-test |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.github.j2objccontrib.j2objcgradle; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +public class CoreJacksonDataBindMarshallingTest { |
| 29 | + |
| 30 | + private static String jsonValue = "{\n" + |
| 31 | + " \"addressList\": [\n" + |
| 32 | + " {\n" + |
| 33 | + " \"zipcode\": 12345,\n" + |
| 34 | + " \"street\": \"Stenhammer Drive\"\n" + |
| 35 | + " },\n" + |
| 36 | + " {\n" + |
| 37 | + " \"zipcode\": 7986,\n" + |
| 38 | + " \"street\": \"Market Street\"\n" + |
| 39 | + " }\n" + |
| 40 | + " ],\n" + |
| 41 | + " \"singleAddress\": {\n" + |
| 42 | + " \"zipcode\": 7986,\n" + |
| 43 | + " \"street\": \"Market Street\"\n" + |
| 44 | + " }\n" + |
| 45 | + "}" ; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testMarshalling() throws IOException { |
| 49 | + Address homeAddress = new Address(12345, "Stenhammer Drive"); |
| 50 | + Address workAddress = new Address(7986, "Market Street"); |
| 51 | + ArrayList<Address> addressList = new ArrayList<>(); |
| 52 | + addressList.add(homeAddress); |
| 53 | + addressList.add(workAddress); |
| 54 | + Person person = new Person(addressList); |
| 55 | + person.setSingleAddress(workAddress); |
| 56 | + |
| 57 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 58 | + String value = objectMapper.writeValueAsString(person); |
| 59 | + |
| 60 | + JsonNode expected = objectMapper.readTree(jsonValue); |
| 61 | + JsonNode actual = objectMapper.readTree(value); |
| 62 | + Assert.assertEquals(expected, actual); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testDemarshallingWithEmbeddedObject() throws IOException { |
| 68 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 69 | + Person personValue = objectMapper.readValue(jsonValue, Person.class); |
| 70 | + Assert.assertTrue(personValue.getSingleAddress() instanceof Address); |
| 71 | + Assert.assertEquals(7986, personValue.singleAddress.zipcode); |
| 72 | + Assert.assertEquals("Market Street", personValue.singleAddress.street); |
| 73 | + } |
| 74 | + |
| 75 | + // This test is expected to fail until j2objc is updated to a version where |
| 76 | + // this issue is fixed: https://github.com/google/j2objc/issues/639 |
| 77 | + @Test |
| 78 | + public void testDemarshallingListField() throws IOException { |
| 79 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 80 | + Person personValue = objectMapper.readValue(jsonValue, Person.class); |
| 81 | + |
| 82 | + List<Address> addresses = personValue.getAddressList(); |
| 83 | + Assert.assertEquals(2, addresses.size()); |
| 84 | + Address firstAddress = addresses.get(0); |
| 85 | + Assert.assertTrue(firstAddress instanceof Address); |
| 86 | + |
| 87 | + Assert.assertEquals(12345, firstAddress.zipcode); |
| 88 | + Assert.assertEquals("Stenhammer Drive", firstAddress.street); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + public static class Person { |
| 93 | + |
| 94 | + private ArrayList<Address> addressList; |
| 95 | + private Address singleAddress; |
| 96 | + |
| 97 | + public Person(ArrayList<Address> addressList) { |
| 98 | + this.addressList = addressList; |
| 99 | + } |
| 100 | + |
| 101 | + Person() { |
| 102 | + } |
| 103 | + |
| 104 | + public List<Address> getAddressList() { |
| 105 | + return addressList; |
| 106 | + } |
| 107 | + |
| 108 | + public void setAddressList(ArrayList<Address> addressList) { |
| 109 | + this.addressList = addressList; |
| 110 | + } |
| 111 | + |
| 112 | + public Address getSingleAddress() { |
| 113 | + return singleAddress; |
| 114 | + } |
| 115 | + |
| 116 | + public void setSingleAddress(Address singleAddress) { |
| 117 | + this.singleAddress = singleAddress; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static class Address { |
| 122 | + |
| 123 | + private int zipcode; |
| 124 | + private String street; |
| 125 | + |
| 126 | + Address() { |
| 127 | + } |
| 128 | + |
| 129 | + public Address(int zipcode, |
| 130 | + String street) { |
| 131 | + this.zipcode = zipcode; |
| 132 | + this.street = street; |
| 133 | + } |
| 134 | + |
| 135 | + public int getZipcode() { |
| 136 | + return zipcode; |
| 137 | + } |
| 138 | + |
| 139 | + public String getStreet() { |
| 140 | + return street; |
| 141 | + } |
| 142 | + |
| 143 | + public void setZipcode(int zipcode) { |
| 144 | + this.zipcode = zipcode; |
| 145 | + } |
| 146 | + |
| 147 | + public void setStreet(String street) { |
| 148 | + this.street = street; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments