|
| 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.failing; |
| 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 GenericsTest { |
| 29 | + public static class Person { |
| 30 | + |
| 31 | + private String name; |
| 32 | + private String lastName; |
| 33 | + private int age; |
| 34 | + private ArrayList<Address> addressList; |
| 35 | + private Address singleAddress; |
| 36 | + |
| 37 | + public Person(String name, |
| 38 | + String lastName, |
| 39 | + int age, |
| 40 | + ArrayList<Address> addressList) { |
| 41 | + this.name = name; |
| 42 | + this.lastName = lastName; |
| 43 | + this.age = age; |
| 44 | + this.addressList = addressList; |
| 45 | + } |
| 46 | + |
| 47 | + Person() { |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String toString() { |
| 52 | + StringBuilder stringBuilder = new StringBuilder(); |
| 53 | + stringBuilder.append("name: ") |
| 54 | + .append(this.name).append("\n") |
| 55 | + .append("lastName: ") |
| 56 | + .append(this.lastName).append("\n") |
| 57 | + .append("age: ") |
| 58 | + .append(this.age).append("\n"); |
| 59 | + |
| 60 | + for (Address address : this.addressList) { |
| 61 | + stringBuilder.append(address.toString()); |
| 62 | + } |
| 63 | + |
| 64 | + return stringBuilder.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + public String getName() { |
| 68 | + return name; |
| 69 | + } |
| 70 | + |
| 71 | + public String getLastName() { |
| 72 | + return lastName; |
| 73 | + } |
| 74 | + |
| 75 | + public int getAge() { |
| 76 | + return age; |
| 77 | + } |
| 78 | + |
| 79 | + public List<Address> getAddressList() { |
| 80 | + return addressList; |
| 81 | + } |
| 82 | + |
| 83 | + public void setName(String name) { |
| 84 | + this.name = name; |
| 85 | + } |
| 86 | + |
| 87 | + public void setLastName(String lastName) { |
| 88 | + this.lastName = lastName; |
| 89 | + } |
| 90 | + |
| 91 | + public void setAge(int age) { |
| 92 | + this.age = age; |
| 93 | + } |
| 94 | + |
| 95 | + public void setAddressList(ArrayList<Address> addressList) { |
| 96 | + this.addressList = addressList; |
| 97 | + } |
| 98 | + |
| 99 | + public Address getSingleAddress() { |
| 100 | + return singleAddress; |
| 101 | + } |
| 102 | + |
| 103 | + public void setSingleAddress(Address singleAddress) { |
| 104 | + this.singleAddress = singleAddress; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static class Address { |
| 109 | + |
| 110 | + private int zipcode; |
| 111 | + private String street; |
| 112 | + |
| 113 | + Address() { |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + public Address(int zipcode, |
| 118 | + String street) { |
| 119 | + this.zipcode = zipcode; |
| 120 | + this.street = street; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + StringBuilder stringBuilder = new StringBuilder(); |
| 126 | + stringBuilder.append("zipcode: ") |
| 127 | + .append(this.zipcode).append("\n") |
| 128 | + .append("street: ") |
| 129 | + .append(this.street).append("\n"); |
| 130 | + |
| 131 | + return stringBuilder.toString(); |
| 132 | + } |
| 133 | + |
| 134 | + public int getZipcode() { |
| 135 | + return zipcode; |
| 136 | + } |
| 137 | + |
| 138 | + public String getStreet() { |
| 139 | + return street; |
| 140 | + } |
| 141 | + |
| 142 | + public void setZipcode(int zipcode) { |
| 143 | + this.zipcode = zipcode; |
| 144 | + } |
| 145 | + |
| 146 | + public void setStreet(String street) { |
| 147 | + this.street = street; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testMarshalling() throws IOException { |
| 153 | + Address homeAddress = new Address(12345, "Stenhammer Drive"); |
| 154 | + Address workAddress = new Address(7986, "Market Street"); |
| 155 | + ArrayList<Address> addressList = new ArrayList<>(); |
| 156 | + addressList.add(homeAddress); |
| 157 | + addressList.add(workAddress); |
| 158 | + Person person = new Person("Sawyer", "Bootstrapper", 23, addressList); |
| 159 | + person.setSingleAddress(workAddress); |
| 160 | + |
| 161 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 162 | + String value = objectMapper.writeValueAsString(person); |
| 163 | + |
| 164 | + JsonNode expected = objectMapper.readTree(jsonValue); |
| 165 | + JsonNode actual = objectMapper.readTree(value); |
| 166 | + Assert.assertEquals(expected, actual); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + private static String jsonValue = "{\"name\":\"Sawyer\",\"lastName\":\"Bootstrapper\",\"age\":23,\"addressList\":[{\"zipcode\":12345,\"street\":" + |
| 171 | + "\"Stenhammer Drive\"},{\"zipcode\":7986,\"street\":\"Market Street\"}],\"singleAddress\":{\"zipcode\":7986,\"street\":\"Market Street\"}}"; |
| 172 | + |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testDemarshallingWithEmbeddedObject() throws IOException { |
| 176 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 177 | + Person personValue = objectMapper.readValue(jsonValue, Person.class); |
| 178 | + Assert.assertTrue(personValue.getSingleAddress() instanceof Address); |
| 179 | + Assert.assertEquals(7986, personValue.singleAddress.zipcode); |
| 180 | + Assert.assertEquals("Market Street", personValue.singleAddress.street); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testDemarshallingSimpleFields() throws IOException { |
| 185 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 186 | + Person personValue = objectMapper.readValue(jsonValue, Person.class); |
| 187 | + |
| 188 | + Assert.assertEquals("Sawyer", personValue.name); |
| 189 | + Assert.assertEquals("Bootstrapper", personValue.lastName); |
| 190 | + Assert.assertEquals(23, personValue.age); |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testDemarshallingListField() throws IOException { |
| 196 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 197 | + Person personValue = objectMapper.readValue(jsonValue, Person.class); |
| 198 | + |
| 199 | + List<Address> addresses = personValue.addressList; |
| 200 | + Assert.assertEquals(2, addresses.size()); |
| 201 | + Address firstAddress = addresses.get(0); |
| 202 | + Assert.assertTrue(firstAddress instanceof Address); |
| 203 | + |
| 204 | + Assert.assertEquals(12345, firstAddress.zipcode); |
| 205 | + Assert.assertEquals("Stenhammer Drive", firstAddress.street); |
| 206 | + System.out.println("demarhsalling works"); |
| 207 | + |
| 208 | + } |
| 209 | +} |
0 commit comments