forked from apache/johnzon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapperAdapterExceptionsTest.java
193 lines (148 loc) · 5.38 KB
/
MapperAdapterExceptionsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* 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.johnzon.mapper;
import org.junit.Test;
import java.util.concurrent.Callable;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class MapperAdapterExceptionsTest {
private static final RuntimeException FROM_EXCEPTION = new RuntimeException("I am user, hear me roar");
private static final RuntimeException TO_EXCEPTION = new RuntimeException("I am user, hear me roar");
@Test
public void adapterFromRuntimeException() {
final Runnable adapterFrom = () -> {
try (final Mapper mapper = new MapperBuilder().addAdapter(new FailingAdapter()).setSnippetMaxLength(20).build()) {
mapper.writeObjectAsString(new Widget(new Color("red")));
}
};
ExceptionAsserts.from(adapterFrom)
// TODO: not consistent with how getter user exceptions are handled
.assertSame(FROM_EXCEPTION);
}
@Test
public void adapterToRuntimeException() {
final Runnable adapterTo = () -> {
try (final Mapper mapper = new MapperBuilder().addAdapter(new FailingAdapter()).setSnippetMaxLength(20).build()) {
mapper.readObject("{\"color\":{\"red\":2550,\"green\":0,\"blue\":0}}", Widget.class);
}
};
ExceptionAsserts.from(adapterTo)
.assertCauseChain(TO_EXCEPTION)
.assertInstanceOf(MapperException.class)
.assertMessage("Widget property 'color' of type Color cannot be mapped to json object value: {\"red\":2550,\"green\":...\n" +
"I am user, hear me roar");
}
@Test
public void adapterFromNulValue() {
final Runnable adapterFrom = () -> {
try (final Mapper mapper = new MapperBuilder().addAdapter(new ReturnNull()).setSnippetMaxLength(20).build()) {
mapper.writeObjectAsString(new Widget(new Color("red")));
}
};
ExceptionAsserts.from(adapterFrom)
// TODO Review: to() can return null, but from() cannot
.assertInstanceOf(NullPointerException.class);
}
@Test
public void adapterToNullValue() throws Exception {
final Callable<Widget> adapterTo = () -> {
try (final Mapper mapper = new MapperBuilder().addAdapter(new ReturnNull()).setSnippetMaxLength(20).build()) {
return mapper.readObject("{\"color\":{\"red\":2550,\"green\":0,\"blue\":0}}", Widget.class);
}
};
final Widget widget = adapterTo.call();
assertNotNull(widget);
assertNull(widget.getColor());
}
public static class FailingAdapter implements Adapter<Color, RGB> {
// writeObject
@Override
public RGB from(final Color color) {
throw FROM_EXCEPTION;
}
// readObject
@Override
public Color to(final RGB rgb) {
throw TO_EXCEPTION;
}
}
public static class ReturnNull implements Adapter<Color, RGB> {
// writeObject
@Override
public RGB from(final Color color) {
return null;
}
// readObject
@Override
public Color to(final RGB rgb) {
return null;
}
}
public static class Widget {
private Color color;
public Widget() {
}
public Widget(final Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(final Color color) {
this.color = color;
}
}
public static class Color {
private final String name;
public Color(final String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static class RGB {
private int red;
private int green;
private int blue;
public RGB() {
}
public RGB(final int red, final int green, final int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return red;
}
public void setRed(final int red) {
this.red = red;
}
public int getGreen() {
return green;
}
public void setGreen(final int green) {
this.green = green;
}
public int getBlue() {
return blue;
}
public void setBlue(final int blue) {
this.blue = blue;
}
}
}