|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.databind.*; |
| 5 | + |
| 6 | +public class ReadWriteOnlyProp935Test extends BaseMapTest |
| 7 | +{ |
| 8 | + // for [databind#935], verify read/write-only cases |
| 9 | + static class ReadXWriteY { |
| 10 | + @JsonProperty(access=JsonProperty.Access.READ_ONLY) |
| 11 | + public int x = 1; |
| 12 | + |
| 13 | + @JsonProperty(access=JsonProperty.Access.WRITE_ONLY) |
| 14 | + public int y = 2; |
| 15 | + |
| 16 | + public void setX(int x) { |
| 17 | + throw new Error("Should NOT set x"); |
| 18 | + } |
| 19 | + |
| 20 | + public int getY() { |
| 21 | + throw new Error("Should NOT get y"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public static class Pojo935 |
| 26 | + { |
| 27 | + private String firstName = "Foo"; |
| 28 | + private String lastName = "Bar"; |
| 29 | + |
| 30 | + @JsonProperty(access = JsonProperty.Access.READ_ONLY) |
| 31 | + public String getFullName() { |
| 32 | + return firstName + " " + lastName; |
| 33 | + } |
| 34 | + |
| 35 | + public String getFirstName() { |
| 36 | + return firstName; |
| 37 | + } |
| 38 | + |
| 39 | + public void setFirstName(String n) { |
| 40 | + firstName = n; |
| 41 | + } |
| 42 | + |
| 43 | + public String getLastName() { |
| 44 | + return lastName; |
| 45 | + } |
| 46 | + |
| 47 | + public void setLastName(String n) { |
| 48 | + lastName = n; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /* |
| 53 | + /********************************************************** |
| 54 | + /* Test methods |
| 55 | + /********************************************************** |
| 56 | + */ |
| 57 | + |
| 58 | + private final ObjectMapper MAPPER = new ObjectMapper(); |
| 59 | + |
| 60 | + // [databind#935] |
| 61 | + public void testReadOnlyAndWriteOnly() throws Exception |
| 62 | + { |
| 63 | + String json = MAPPER.writeValueAsString(new ReadXWriteY()); |
| 64 | + assertEquals("{\"x\":1}", json); |
| 65 | + |
| 66 | + ReadXWriteY result = MAPPER.readValue("{\"x\":5, \"y\":6}", ReadXWriteY.class); |
| 67 | + assertNotNull(result); |
| 68 | + assertEquals(0, result.x); |
| 69 | + assertEquals(6, result.y); |
| 70 | + } |
| 71 | + |
| 72 | + public void testReadOnl935() throws Exception |
| 73 | + { |
| 74 | + String json = MAPPER.writeValueAsString(new Pojo935()); |
| 75 | + Pojo935 result = MAPPER.readValue(json, Pojo935.class); |
| 76 | + assertNotNull(result); |
| 77 | + } |
| 78 | +} |
0 commit comments