Skip to content

Commit 97dfabe

Browse files
committed
Polish SecurityNamespaceHandler Tests
Issue gh-8974
1 parent 944463e commit 97dfabe

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

config/src/test/java/org/springframework/security/config/SecurityNamespaceHandlerTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
2828
import org.springframework.messaging.Message;
2929
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
30+
import org.springframework.security.config.util.SpringSecurityVersions;
3031
import org.springframework.test.util.ReflectionTestUtils;
3132
import org.springframework.util.ClassUtils;
3233

@@ -147,4 +148,11 @@ public void websocketNotFoundExceptionNoMessageBlock() throws Exception {
147148
// should load just fine since no websocket block
148149
}
149150

151+
@Test
152+
public void configureWhenOldVersionThenErrorMessageContainsCorrectVersion() {
153+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
154+
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER, "3.0", null))
155+
.withMessageContaining(SpringSecurityVersions.getCurrentXsdVersionFromSpringSchemas());
156+
}
157+
150158
}

config/src/test/java/org/springframework/security/config/util/InMemoryXmlApplicationContext.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2020 the original author or authors.
2+
* Copyright 2009-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,10 @@
1616

1717
package org.springframework.security.config.util;
1818

19-
import java.io.IOException;
20-
import java.io.InputStream;
21-
import java.util.Properties;
22-
2319
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2420
import org.springframework.context.ApplicationContext;
2521
import org.springframework.context.support.AbstractXmlApplicationContext;
2622
import org.springframework.core.io.Resource;
27-
import org.springframework.security.core.SpringSecurityCoreVersion;
2823
import org.springframework.security.util.InMemoryResource;
2924

3025
/**
@@ -34,24 +29,6 @@
3429
*/
3530
public class InMemoryXmlApplicationContext extends AbstractXmlApplicationContext {
3631

37-
private static String getCurrentXSDVersionFromSpringSchemas() {
38-
Properties properties = new Properties();
39-
try (InputStream is = SpringSecurityCoreVersion.class.getClassLoader()
40-
.getResourceAsStream("META-INF/spring.schemas")) {
41-
properties.load(is);
42-
}
43-
catch (IOException ex) {
44-
throw new RuntimeException("Could not read 'META-INF/spring.schemas'", ex);
45-
}
46-
47-
String inPackageLocation = properties
48-
.getProperty("https://www.springframework.org/schema/security/spring-security.xsd");
49-
String[] parts = inPackageLocation.split("-");
50-
String currentXSD = parts[parts.length - 1];
51-
String currentVersion = currentXSD.replace(".xsd", "");
52-
return currentVersion;
53-
}
54-
5532
static final String BEANS_OPENING = "<b:beans xmlns='http://www.springframework.org/schema/security'\n"
5633
+ " xmlns:context='http://www.springframework.org/schema/context'\n"
5734
+ " xmlns:b='http://www.springframework.org/schema/beans'\n"
@@ -66,7 +43,7 @@ private static String getCurrentXSDVersionFromSpringSchemas() {
6643
+ "http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-2.5.xsd\n"
6744
+ "http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security-";
6845
static final String BEANS_CLOSE = "</b:beans>\n";
69-
static final String SPRING_SECURITY_VERSION = getCurrentXSDVersionFromSpringSchemas();
46+
static final String SPRING_SECURITY_VERSION = SpringSecurityVersions.getCurrentXsdVersionFromSpringSchemas();
7047

7148
Resource inMemoryXml;
7249

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.util;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.Properties;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
24+
25+
import org.springframework.security.core.SpringSecurityCoreVersion;
26+
27+
/**
28+
* For computing different Spring Security versions
29+
*/
30+
public final class SpringSecurityVersions {
31+
32+
static final Pattern SCHEMA_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+(\\.\\d+)?");
33+
34+
public static String getCurrentXsdVersionFromSpringSchemas() {
35+
Properties properties = new Properties();
36+
try (InputStream is = SpringSecurityCoreVersion.class.getClassLoader()
37+
.getResourceAsStream("META-INF/spring.schemas")) {
38+
properties.load(is);
39+
}
40+
catch (IOException ex) {
41+
throw new RuntimeException("Could not read 'META-INF/spring.schemas'", ex);
42+
}
43+
44+
String inPackageLocation = properties
45+
.getProperty("https://www.springframework.org/schema/security/spring-security.xsd");
46+
Matcher matcher = SCHEMA_VERSION_PATTERN.matcher(inPackageLocation);
47+
if (matcher.find()) {
48+
return matcher.group(0);
49+
}
50+
throw new IllegalStateException("Failed to find version");
51+
}
52+
53+
private SpringSecurityVersions() {
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)