Skip to content

Commit 086056f

Browse files
committed
SEC-2289: Make compatible with Spring 4 as well
There are a few subtle changes in Spring 4 that this commit addresses
1 parent 26166ef commit 086056f

File tree

7 files changed

+46
-29
lines changed

7 files changed

+46
-29
lines changed

acl/src/test/java/org/springframework/security/acls/jdbc/BasicLookupStrategyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class BasicLookupStrategyTests {
5050
//~ Methods ========================================================================================================
5151
@BeforeClass
5252
public static void initCacheManaer() {
53-
cacheManager = new CacheManager();
53+
cacheManager = CacheManager.create();
5454
cacheManager.addCache(new Cache("basiclookuptestcache", 500, false, false, 30, 30));
5555
}
5656

acl/src/test/java/org/springframework/security/acls/jdbc/EhCacheBasedAclCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class EhCacheBasedAclCacheTests {
4040

4141
@BeforeClass
4242
public static void initCacheManaer() {
43-
cacheManager = new CacheManager();
43+
cacheManager = CacheManager.create();
4444
// Use disk caching immediately (to test for serialization issue reported in SEC-527)
4545
cacheManager.addCache(new Cache("ehcachebasedacltests", 0, true, false, 600, 300));
4646
}

cas/src/test/java/org/springframework/security/cas/authentication/EhCacheBasedTicketCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class EhCacheBasedTicketCacheTests extends AbstractStatelessTicketCacheTe
3939
//~ Methods ========================================================================================================
4040
@BeforeClass
4141
public static void initCacheManaer() {
42-
cacheManager = new CacheManager();
42+
cacheManager = CacheManager.create();
4343
cacheManager.addCache(new Cache("castickets", 500, false, false, 30, 30));
4444
}
4545

core/src/test/java/org/springframework/security/core/userdetails/cache/EhCacheBasedUserCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class EhCacheBasedUserCacheTests {
3939
//~ Methods ========================================================================================================
4040
@BeforeClass
4141
public static void initCacheManaer() {
42-
cacheManager = new CacheManager();
42+
cacheManager = CacheManager.create();
4343
cacheManager.addCache(new Cache("ehcacheusercachetests", 500, false, false, 30, 30));
4444
}
4545

openid/src/test/java/org/springframework/security/openid/OpenIDAuthenticationFilterTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class OpenIDAuthenticationFilterTests {
2525
private static final String REDIRECT_URL = "http://www.example.com/redirect";
2626
private static final String CLAIMED_IDENTITY_URL = "http://www.example.com/identity";
2727
private static final String REQUEST_PATH = "/j_spring_openid_security_check";
28-
private static final String FILTER_PROCESS_URL = "http://localhost:80" + REQUEST_PATH;
28+
private static final String FILTER_PROCESS_URL = "http://localhost:8080" + REQUEST_PATH;
2929
private static final String DEFAULT_TARGET_URL = FILTER_PROCESS_URL;
3030

3131
@Before
@@ -46,6 +46,7 @@ public Authentication authenticate(Authentication a) {
4646
@Test
4747
public void testFilterOperation() throws Exception {
4848
MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_PATH);
49+
req.setServerPort(8080);
4950
MockHttpServletResponse response = new MockHttpServletResponse();
5051

5152
req.setParameter("openid_identifier", " " + CLAIMED_IDENTITY_URL);
@@ -55,7 +56,7 @@ public void testFilterOperation() throws Exception {
5556
public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException {
5657
assertEquals(CLAIMED_IDENTITY_URL, claimedIdentity);
5758
assertEquals(DEFAULT_TARGET_URL, returnToUrl);
58-
assertEquals("http://localhost:80/", realm);
59+
assertEquals("http://localhost:8080/", realm);
5960
return REDIRECT_URL;
6061
}
6162
});
@@ -66,7 +67,7 @@ public String beginConsumption(HttpServletRequest req, String claimedIdentity, S
6667
// Filter chain shouldn't proceed
6768
verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
6869
}
69-
70+
7071
/**
7172
* Tests that the filter encodes any query parameters on the return_to URL.
7273
*/
@@ -78,13 +79,13 @@ public void encodesUrlParameters() throws Exception {
7879
MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_PATH);
7980
req.addParameter(paramName, paramValue);
8081
filter.setReturnToUrlParameters(Collections.singleton(paramName));
81-
82+
8283
URI returnTo = new URI(filter.buildReturnToUrl(req));
8384
String query = returnTo.getRawQuery();
8485
assertEquals(1, count(query, '='));
8586
assertEquals(0, count(query, '&'));
8687
}
87-
88+
8889
/**
8990
* Counts the number of occurrences of {@code c} in {@code s}.
9091
*/

web/src/test/java/org/springframework/security/web/util/AntPathRequestMatcherTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@
1616
import static org.junit.Assert.assertEquals;
1717
import static org.junit.Assert.assertFalse;
1818
import static org.junit.Assert.assertTrue;
19+
import static org.mockito.Mockito.when;
20+
21+
import javax.servlet.http.HttpServletRequest;
1922

2023
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.runners.MockitoJUnitRunner;
2127
import org.springframework.mock.web.MockHttpServletRequest;
2228

2329
/**
2430
* @author Luke Taylor
2531
* @author Rob Winch
2632
*/
33+
@RunWith(MockitoJUnitRunner.class)
2734
public class AntPathRequestMatcherTests {
35+
@Mock
36+
private HttpServletRequest request;
2837

2938
@Test
3039
public void singleWildcardMatchesAnyPath() {
@@ -65,17 +74,15 @@ public void trailingWildcardMatchesCorrectly() {
6574
@Test
6675
public void requestHasNullMethodMatches() {
6776
AntPathRequestMatcher matcher = new AntPathRequestMatcher("/something/*", "GET");
68-
MockHttpServletRequest request = createRequest("/something/here");
69-
request.setMethod(null);
77+
HttpServletRequest request = createRequestWithNullMethod("/something/here");
7078
assertTrue(matcher.matches(request));
7179
}
7280

7381
// SEC-2084
7482
@Test
7583
public void requestHasNullMethodNoMatch() {
7684
AntPathRequestMatcher matcher = new AntPathRequestMatcher("/something/*", "GET");
77-
MockHttpServletRequest request = createRequest("/nomatch");
78-
request.setMethod(null);
85+
HttpServletRequest request = createRequestWithNullMethod("/nomatch");
7986
assertFalse(matcher.matches(request));
8087
}
8188

@@ -142,6 +149,12 @@ public void toStringIsOk() throws Exception {
142149
new AntPathRequestMatcher("/blah", "GET").toString();
143150
}
144151

152+
private HttpServletRequest createRequestWithNullMethod(String path) {
153+
when(request.getQueryString()).thenReturn("doesntMatter");
154+
when(request.getServletPath()).thenReturn(path);
155+
return request;
156+
}
157+
145158
private MockHttpServletRequest createRequest(String path) {
146159
MockHttpServletRequest request = new MockHttpServletRequest();
147160
request.setQueryString("doesntMatter");

web/src/test/java/org/springframework/security/web/util/RegexRequestMatcherTests.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@
1212
*/
1313
package org.springframework.security.web.util;
1414

15-
import static org.junit.Assert.*;
15+
import static org.junit.Assert.assertFalse;
16+
import static org.junit.Assert.assertTrue;
17+
import static org.mockito.Mockito.when;
1618

17-
import org.junit.*;
19+
import javax.servlet.http.HttpServletRequest;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.mockito.runners.MockitoJUnitRunner;
1825
import org.springframework.mock.web.MockHttpServletRequest;
1926

2027
/**
2128
* @author Luke Taylor
2229
* @author Rob Winch
2330
*/
31+
@RunWith(MockitoJUnitRunner.class)
2432
public class RegexRequestMatcherTests {
33+
@Mock
34+
private HttpServletRequest request;
2535

2636
@Test
2737
public void doesntMatchIfHttpMethodIsDifferent() throws Exception {
@@ -57,42 +67,35 @@ public void queryStringIsMatcherCorrectly() throws Exception {
5767
@Test
5868
public void requestHasNullMethodMatches() {
5969
RegexRequestMatcher matcher = new RegexRequestMatcher("/something/.*", "GET");
60-
MockHttpServletRequest request = createRequest("/something/here");
61-
request.setMethod(null);
70+
HttpServletRequest request = createRequestWithNullMethod("/something/here");
6271
assertTrue(matcher.matches(request));
6372
}
6473

6574
// SEC-2084
6675
@Test
6776
public void requestHasNullMethodNoMatch() {
6877
RegexRequestMatcher matcher = new RegexRequestMatcher("/something/.*", "GET");
69-
MockHttpServletRequest request = createRequest("/nomatch");
70-
request.setMethod(null);
78+
HttpServletRequest request = createRequestWithNullMethod("/nomatch");
7179
assertFalse(matcher.matches(request));
7280
}
7381

7482
@Test
7583
public void requestHasNullMethodAndNullMatcherMatches() {
7684
RegexRequestMatcher matcher = new RegexRequestMatcher("/something/.*", null);
77-
MockHttpServletRequest request = createRequest("/something/here");
78-
request.setMethod(null);
85+
HttpServletRequest request = createRequestWithNullMethod("/something/here");
7986
assertTrue(matcher.matches(request));
8087
}
8188

8289
@Test
8390
public void requestHasNullMethodAndNullMatcherNoMatch() {
8491
RegexRequestMatcher matcher = new RegexRequestMatcher("/something/.*", null);
85-
MockHttpServletRequest request = createRequest("/nomatch");
86-
request.setMethod(null);
92+
HttpServletRequest request = createRequestWithNullMethod("/nomatch");
8793
assertFalse(matcher.matches(request));
8894
}
8995

90-
private MockHttpServletRequest createRequest(String path) {
91-
MockHttpServletRequest request = new MockHttpServletRequest();
92-
request.setQueryString("doesntMatter");
93-
request.setServletPath(path);
94-
request.setMethod("POST");
95-
96+
private HttpServletRequest createRequestWithNullMethod(String path) {
97+
when(request.getQueryString()).thenReturn("doesntMatter");
98+
when(request.getServletPath()).thenReturn(path);
9699
return request;
97100
}
98101
}

0 commit comments

Comments
 (0)