/
VRaptorConvertersAdapterTest.java
141 lines (114 loc) · 3.96 KB
/
VRaptorConvertersAdapterTest.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
/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
* All rights reserved.
*
* Licensed 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 br.com.caelum.vraptor.http.ognl;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import ognl.Ognl;
import ognl.OgnlException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.Converter;
import br.com.caelum.vraptor.core.Converters;
public class VRaptorConvertersAdapterTest {
private @Mock Converters converters;
private @Mock Converter converter;
private VRaptorConvertersAdapter adapter;
private Cat myCat;
private ResourceBundle bundle;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
bundle = ResourceBundle.getBundle("messages");
adapter = new VRaptorConvertersAdapter(converters, bundle);
myCat = new Cat();
}
public static class Cat {
private int length;
private Tail tail;
private List<Leg> legs = new ArrayList<Leg>();
public void setLength(int length) {
this.length = length;
}
public int getLength() {
return length;
}
public void setTail(Tail tail) {
this.tail = tail;
}
public Tail getTail() {
return tail;
}
public void setLegs(List<Leg> legs) {
this.legs = legs;
}
public List<Leg> getLegs() {
return legs;
}
}
public static class Leg {
@SuppressWarnings("unused")
private final int length;
public Leg(int length) {
this.length = length;
}
}
public static class Tail {
public Tail(int l) {
length = l;
}
private final int length;
}
@Test
@SuppressWarnings("unchecked")
public void shouldInvokePrimitiveConverter() throws OgnlException {
when(converters.to(int.class)).thenReturn(converter);
when(converter.convert("2", int.class, bundle)).thenReturn(2);
Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
Ognl.setValue("length", context, myCat, "2");
assertThat(myCat.length, is(equalTo(2)));
}
@SuppressWarnings("unchecked")
@Test
public void shouldInvokeCustomTypeConverter() throws OgnlException {
when(converters.to(Tail.class)).thenReturn(converter);
when(converter.convert("15", Tail.class, bundle)).thenReturn(new Tail(15));
Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
Ognl.setValue("tail", context, myCat, "15");
assertThat(myCat.tail.length, is(equalTo(15)));
}
@Test(expected=IllegalArgumentException.class)
public void shouldThrowExceptionIfNoConverterIsFound() throws Throwable {
when(converters.to(Tail.class)).thenReturn(null);
Map<?,?> context = Ognl.createDefaultContext(myCat);
Ognl.setTypeConverter(context, adapter);
try {
Ognl.setValue("tail", context, myCat, "15");
} catch (OgnlException e) {
throw e.getCause();
}
}
}