1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.session;
20
21 import static org.apache.mina.session.AttributeKey.createKey;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.CoreMatchers.nullValue;
24 import static org.junit.Assert.assertThat;
25
26 import java.util.Set;
27
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32
33
34
35
36
37
38 public class AttributeContainerTest {
39
40
41 private static final int DEFAULT_VALUE = 0;
42
43
44 private AttributeContainer container;
45
46 @Rule
47 public ExpectedException exception = ExpectedException.none();
48
49 private static final AttributeKey<Integer> ATTRIBUTE_KEY = createKey(Integer.class, "myKey");
50
51 @Before
52 public void setUp() {
53 container = new DefaultAttributeContainer();
54 }
55
56
57
58
59
60
61
62 @Test
63 public void setAttributeWithoutKey() throws Exception {
64 exception.expect(IllegalArgumentException.class);
65 exception.expectMessage("Parameter >key< must not be null!");
66 container.setAttribute(null, DEFAULT_VALUE);
67 }
68
69
70
71
72
73
74
75
76
77
78 @Test
79 public void setAttributeWithUnsafeKey() throws Exception {
80 exception.expect(IllegalArgumentException.class);
81 exception.expectMessage("Invalid attribute value\r\n" + " expected type: java.lang.Integer\r\n"
82 + " actual type : java.lang.Double\r\n" + " actual value : 12.3");
83
84 AttributeKey<? extends Number> key = ATTRIBUTE_KEY;
85 Double value = 12.3;
86 container.setAttribute(key, value);
87 }
88
89
90
91
92
93
94
95 @Test
96 public void setAttributeForTheFirstTime() throws Exception {
97 Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 123);
98 assertThat(oldValue, is(nullValue()));
99 }
100
101
102
103
104
105
106 @Test
107 public void setAttributeForTheSecondTime() throws Exception {
108 container.setAttribute(ATTRIBUTE_KEY, 123);
109 Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 456);
110 assertThat(oldValue, is(123));
111 }
112
113
114
115
116
117
118
119 @Test
120 public void setAttributeValueToNull() throws Exception {
121 Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 456);
122
123 assertThat(oldValue, is(nullValue()));
124 }
125
126
127
128
129
130
131 @Test
132 public void setAttributeValueToNullIfPreviousValueIsAvailable() throws Exception {
133 container.setAttribute(ATTRIBUTE_KEY, 123);
134 Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, null);
135
136 assertThat(oldValue, is(123));
137 }
138
139
140
141
142
143
144
145 @Test
146 public void getAttributeWithoutKey() throws Exception {
147 exception.expect(IllegalArgumentException.class);
148 exception.expectMessage("Parameter >key< must not be null!");
149 container.getAttribute(null, DEFAULT_VALUE);
150 }
151
152
153
154
155
156
157 @Test
158 public void getAttributeThatIsNotPresent() throws Exception {
159 Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
160 assertThat(value, is(DEFAULT_VALUE));
161 }
162
163
164
165
166
167
168
169 @Test
170 public void getAttributeThatIsPresent() throws Exception {
171 container.setAttribute(ATTRIBUTE_KEY, 123);
172 Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
173 assertThat(value, is(123));
174 }
175
176
177
178
179
180
181
182
183 @Test
184 public void getAttributeKeysAndRemoveKey() throws Exception {
185 container.setAttribute(ATTRIBUTE_KEY, 123);
186 Set<AttributeKey<?>> set = container.getAttributeKeys();
187
188 try {
189 set.remove(ATTRIBUTE_KEY);
190 } catch (UnsupportedOperationException e) {
191 }
192
193 Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
194 assertThat(value, is(123));
195 }
196
197
198
199
200
201 @Test
202 public void removeAPresentAttribute() throws Exception {
203 container.setAttribute(ATTRIBUTE_KEY, 123);
204 Integer oldValue = container.removeAttribute(ATTRIBUTE_KEY);
205 assertThat(oldValue, is(123));
206 }
207
208
209
210
211
212 @Test
213 public void removeNonPresentAttribute() throws Exception {
214 Integer oldValue = container.removeAttribute(ATTRIBUTE_KEY);
215 assertThat(oldValue, is(nullValue()));
216 }
217
218
219
220
221
222 @Test
223 public void removeWithNullKey() throws Exception {
224 exception.expect(IllegalArgumentException.class);
225 exception.expectMessage("Parameter >key< must not be null!");
226 container.removeAttribute(null);
227 }
228 }