Skip to content

Commit d9a4fb4

Browse files
committed
Introduce "dummy" Environment implementation
For testing purposes in which an Environment implementation is required but a ConfigurableEnvironment is not desirable. All methods are no-ops and return null, therefore NPEs are likely.
1 parent 660458a commit d9a4fb4

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2013 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+
* http://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.core.env;
18+
19+
public class DummyEnvironment implements Environment {
20+
21+
public boolean containsProperty(String key) {
22+
return false;
23+
}
24+
25+
public String getProperty(String key) {
26+
return null;
27+
}
28+
29+
public String getProperty(String key, String defaultValue) {
30+
return null;
31+
}
32+
33+
public <T> T getProperty(String key, Class<T> targetType) {
34+
return null;
35+
}
36+
37+
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
38+
return null;
39+
}
40+
41+
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
42+
return null;
43+
}
44+
45+
public String getRequiredProperty(String key) throws IllegalStateException {
46+
return null;
47+
}
48+
49+
public <T> T getRequiredProperty(String key, Class<T> targetType)
50+
throws IllegalStateException {
51+
return null;
52+
}
53+
54+
public String resolvePlaceholders(String text) {
55+
return null;
56+
}
57+
58+
public String resolveRequiredPlaceholders(String text)
59+
throws IllegalArgumentException {
60+
return null;
61+
}
62+
63+
public String[] getActiveProfiles() {
64+
return null;
65+
}
66+
67+
public String[] getDefaultProfiles() {
68+
return null;
69+
}
70+
71+
public boolean acceptsProfiles(String... profiles) {
72+
return false;
73+
}
74+
75+
}

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616

1717
package org.springframework.web.servlet;
1818

19-
import static org.hamcrest.CoreMatchers.equalTo;
20-
import static org.hamcrest.CoreMatchers.instanceOf;
21-
import static org.hamcrest.CoreMatchers.notNullValue;
22-
import static org.hamcrest.CoreMatchers.sameInstance;
23-
import static org.junit.Assert.assertThat;
24-
2519
import java.io.IOException;
2620
import java.util.Locale;
2721

@@ -36,15 +30,15 @@
3630

3731
import org.springframework.beans.MutablePropertyValues;
3832
import org.springframework.beans.PropertyValue;
39-
import org.springframework.tests.sample.beans.TestBean;
4033
import org.springframework.context.ConfigurableApplicationContext;
4134
import org.springframework.context.support.DefaultMessageSourceResolvable;
4235
import org.springframework.core.env.ConfigurableEnvironment;
43-
import org.springframework.core.env.StandardEnvironment;
36+
import org.springframework.core.env.DummyEnvironment;
4437
import org.springframework.mock.web.test.MockHttpServletRequest;
4538
import org.springframework.mock.web.test.MockHttpServletResponse;
4639
import org.springframework.mock.web.test.MockServletConfig;
4740
import org.springframework.mock.web.test.MockServletContext;
41+
import org.springframework.tests.sample.beans.TestBean;
4842
import org.springframework.web.bind.EscapedErrors;
4943
import org.springframework.web.context.ConfigurableWebEnvironment;
5044
import org.springframework.web.context.ServletConfigAwareBean;
@@ -65,6 +59,9 @@
6559
import org.springframework.web.servlet.view.InternalResourceViewResolver;
6660
import org.springframework.web.util.WebUtils;
6761

62+
import static org.hamcrest.CoreMatchers.*;
63+
import static org.junit.Assert.*;
64+
6865
/**
6966
* @author Rod Johnson
7067
* @author Juergen Hoeller
@@ -842,8 +839,8 @@ public void testEnvironmentOperations() {
842839
servlet.setEnvironment(env1); // should succeed
843840
assertThat(servlet.getEnvironment(), sameInstance(env1));
844841
try {
845-
servlet.setEnvironment(new StandardEnvironment());
846-
fail("expected exception");
842+
servlet.setEnvironment(new DummyEnvironment());
843+
fail("expected IllegalArgumentException for non-configurable Environment");
847844
}
848845
catch (IllegalArgumentException ex) {
849846
}

0 commit comments

Comments
 (0)