|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.mock.web;
|
18 | 18 |
|
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | +import javax.servlet.http.HttpSessionBindingEvent; |
| 21 | +import javax.servlet.http.HttpSessionBindingListener; |
| 22 | + |
19 | 23 | import org.junit.Test;
|
20 | 24 |
|
21 | 25 | import static org.junit.Assert.*;
|
|
24 | 28 | * Unit tests for {@link MockHttpSession}.
|
25 | 29 | *
|
26 | 30 | * @author Sam Brannen
|
| 31 | + * @author Vedran Pavic |
27 | 32 | * @since 3.2
|
28 | 33 | */
|
29 | 34 | public class MockHttpSessionTests {
|
30 | 35 |
|
31 |
| - private final MockHttpSession session = new MockHttpSession(); |
| 36 | + private MockHttpSession session = new MockHttpSession(); |
32 | 37 |
|
33 | 38 |
|
34 | 39 | @Test
|
@@ -143,4 +148,70 @@ public void isNewOnInvalidatedSession() {
|
143 | 148 | session.isNew();
|
144 | 149 | }
|
145 | 150 |
|
| 151 | + @Test |
| 152 | + public void bindingListenerBindListener() { |
| 153 | + String bindingListenerName = "bindingListener"; |
| 154 | + CountingHttpSessionBindingListener bindingListener = new CountingHttpSessionBindingListener(); |
| 155 | + |
| 156 | + session.setAttribute(bindingListenerName, bindingListener); |
| 157 | + |
| 158 | + assertEquals(bindingListener.getCounter(), 1); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void bindingListenerBindListenerThenUnbind() { |
| 163 | + String bindingListenerName = "bindingListener"; |
| 164 | + CountingHttpSessionBindingListener bindingListener = new CountingHttpSessionBindingListener(); |
| 165 | + |
| 166 | + session.setAttribute(bindingListenerName, bindingListener); |
| 167 | + session.removeAttribute(bindingListenerName); |
| 168 | + |
| 169 | + assertEquals(bindingListener.getCounter(), 0); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void bindingListenerBindSameListenerTwice() { |
| 174 | + String bindingListenerName = "bindingListener"; |
| 175 | + CountingHttpSessionBindingListener bindingListener = new CountingHttpSessionBindingListener(); |
| 176 | + |
| 177 | + session.setAttribute(bindingListenerName, bindingListener); |
| 178 | + session.setAttribute(bindingListenerName, bindingListener); |
| 179 | + |
| 180 | + assertEquals(bindingListener.getCounter(), 1); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void bindingListenerBindListenerOverwrite() { |
| 185 | + String bindingListenerName = "bindingListener"; |
| 186 | + CountingHttpSessionBindingListener bindingListener1 = new CountingHttpSessionBindingListener(); |
| 187 | + CountingHttpSessionBindingListener bindingListener2 = new CountingHttpSessionBindingListener(); |
| 188 | + |
| 189 | + session.setAttribute(bindingListenerName, bindingListener1); |
| 190 | + session.setAttribute(bindingListenerName, bindingListener2); |
| 191 | + |
| 192 | + assertEquals(bindingListener1.getCounter(), 0); |
| 193 | + assertEquals(bindingListener2.getCounter(), 1); |
| 194 | + } |
| 195 | + |
| 196 | + private static class CountingHttpSessionBindingListener |
| 197 | + implements HttpSessionBindingListener { |
| 198 | + |
| 199 | + private final AtomicInteger counter = new AtomicInteger(0); |
| 200 | + |
| 201 | + @Override |
| 202 | + public void valueBound(HttpSessionBindingEvent event) { |
| 203 | + this.counter.incrementAndGet(); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void valueUnbound(HttpSessionBindingEvent event) { |
| 208 | + this.counter.decrementAndGet(); |
| 209 | + } |
| 210 | + |
| 211 | + int getCounter() { |
| 212 | + return this.counter.get(); |
| 213 | + } |
| 214 | + |
| 215 | + } |
| 216 | + |
146 | 217 | }
|
0 commit comments