1
+ package com .fasterxml .jackson .databind ;
2
+
3
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
4
+ import com .fasterxml .jackson .databind .node .TextNode ;
5
+ import org .junit .Before ;
6
+ import org .junit .Test ;
7
+ import org .mockito .Mock ;
8
+ import org .mockito .MockitoAnnotations ;
9
+
10
+ import java .io .*;
11
+ import java .net .URL ;
12
+ import java .util .Map ;
13
+
14
+ import static org .junit .Assert .*;
15
+ import static org .mockito .ArgumentMatchers .any ;
16
+ import static org .mockito .Mockito .verify ;
17
+ import static org .mockito .Mockito .when ;
18
+
19
+ public class ObjectReaderValueOfWithValueTypeTest {
20
+ final JsonMapper MAPPER = JsonMapper .builder ().build ();
21
+
22
+ static class POJO {
23
+ public Map <String , Object > name ;
24
+ }
25
+
26
+ private final POJO pojo = new POJO ();
27
+
28
+ @ Mock
29
+ ObjectReader objectReader ;
30
+
31
+
32
+ @ Before
33
+ public void before () {
34
+ MockitoAnnotations .initMocks (this );
35
+ }
36
+
37
+ @ Test
38
+ public void testValueOfStringWithValueType () throws IOException {
39
+ when (objectReader .readValue ((String ) any ())).thenReturn (pojo );
40
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
41
+ when (objectReader .readValue ((String ) any (), (Class ) any ())).thenCallRealMethod ();
42
+
43
+ String source = "" ;
44
+ POJO result = objectReader .readValue (source , POJO .class );
45
+
46
+ assertEquals (result , pojo );
47
+ verify (objectReader ).forType (POJO .class );
48
+ verify (objectReader ).readValue (source );
49
+ }
50
+
51
+
52
+ @ Test
53
+ public void testValueOfByteArrayWithValueType () throws IOException {
54
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
55
+ when (objectReader .readValue ((byte []) any ())).thenReturn (pojo );
56
+ when (objectReader .readValue ((byte []) any (), (Class ) any ())).thenCallRealMethod ();
57
+
58
+ byte [] source = "{}" .getBytes ();
59
+ POJO result = objectReader .readValue (source , POJO .class );
60
+
61
+ assertEquals (result , pojo );
62
+ verify (objectReader ).forType (POJO .class );
63
+ verify (objectReader ).readValue (source );
64
+ }
65
+
66
+ @ Test
67
+ public void testValueOfDataInputWithValueType () throws IOException {
68
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
69
+ when (objectReader .readValue ((DataInput ) any ())).thenReturn (pojo );
70
+ when (objectReader .readValue ((DataInput ) any (), (Class ) any ())).thenCallRealMethod ();
71
+
72
+ DataInput source = new DataInputStream (new ByteArrayInputStream ("{}" .getBytes ()));
73
+ POJO result = objectReader .readValue (source , POJO .class );
74
+
75
+ assertEquals (result , pojo );
76
+ verify (objectReader ).forType (POJO .class );
77
+ verify (objectReader ).readValue (source );
78
+ }
79
+
80
+ @ Test
81
+ public void testValueOfFileWithValueType () throws IOException {
82
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
83
+ when (objectReader .readValue ((File ) any ())).thenReturn (pojo );
84
+ when (objectReader .readValue ((File ) any (), (Class ) any ())).thenCallRealMethod ();
85
+
86
+ File source = new File ("unknownpath" );
87
+ POJO result = objectReader .readValue (source , POJO .class );
88
+
89
+ assertEquals (result , pojo );
90
+ verify (objectReader ).forType (POJO .class );
91
+ verify (objectReader ).readValue (source );
92
+ }
93
+
94
+ @ Test
95
+ public void testValueOfInputStreamWithValueType () throws IOException {
96
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
97
+ when (objectReader .readValue ((InputStream ) any ())).thenReturn (pojo );
98
+ when (objectReader .readValue ((InputStream ) any (), (Class ) any ())).thenCallRealMethod ();
99
+
100
+ InputStream source = new ByteArrayInputStream ("{}" .getBytes ());
101
+ POJO result = objectReader .readValue (source , POJO .class );
102
+
103
+ assertEquals (result , pojo );
104
+ verify (objectReader ).forType (POJO .class );
105
+ verify (objectReader ).readValue (source );
106
+ }
107
+
108
+ @ Test
109
+ public void testValueOfJsonNodeWithValueType () throws IOException {
110
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
111
+ when (objectReader .readValue ((JsonNode ) any ())).thenReturn (pojo );
112
+ when (objectReader .readValue ((JsonNode ) any (), (Class ) any ())).thenCallRealMethod ();
113
+
114
+ JsonNode source = new TextNode ("{}" );
115
+ POJO result = objectReader .readValue (source , POJO .class );
116
+
117
+ assertEquals (result , pojo );
118
+ verify (objectReader ).forType (POJO .class );
119
+ verify (objectReader ).readValue (source );
120
+ }
121
+
122
+ @ Test
123
+ public void testValueOfReaderWithValueType () throws IOException {
124
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
125
+ when (objectReader .readValue ((Reader ) any ())).thenReturn (pojo );
126
+ when (objectReader .readValue ((Reader ) any (), (Class ) any ())).thenCallRealMethod ();
127
+
128
+ Reader source = new StringReader ("{}" );
129
+ POJO result = objectReader .readValue (source , POJO .class );
130
+
131
+ assertEquals (result , pojo );
132
+ verify (objectReader ).forType (POJO .class );
133
+ verify (objectReader ).readValue (source );
134
+ }
135
+
136
+ @ Test
137
+ public void testValueOfURLWithValueType () throws IOException {
138
+ when (objectReader .forType ((Class ) any ())).thenReturn (objectReader );
139
+ when (objectReader .readValue ((URL ) any ())).thenReturn (pojo );
140
+ when (objectReader .readValue ((URL ) any (), (Class ) any ())).thenCallRealMethod ();
141
+
142
+ URL source = new URL ("http://www.test.com" );
143
+ POJO result = objectReader .readValue (source , POJO .class );
144
+
145
+ assertEquals (result , pojo );
146
+ verify (objectReader ).forType (POJO .class );
147
+ verify (objectReader ).readValue (source );
148
+ }
149
+ }
0 commit comments