Browse Source

Introduce MockEnvironment in the spring-test module

For legacy reasons, a MockEnvironment implementation already exists in multiple places within Spring's test suite; however, it is not available to the general public.

This commit promotes MockEnvironment to a first-class citizen in the spring-test module, alongside the existing MockPropertySource.

In addition, the following house cleaning has been performed.

 - deleted MockPropertySource from the spring-expression module
 - deleted MockEnvironment from the "spring" integration testing module
 - updated test copies of MockPropertySource and MockEnvironment
 - documented MockEnvironment and MockPropertySource in the testing
   chapter of the reference manual

Issue: SPR-9492
pull/111/head
Sam Brannen 12 years ago
parent
commit
f49b22c78f
  1. 9
      spring-context/src/test/java/org/springframework/mock/env/MockEnvironment.java
  2. 7
      spring-context/src/test/java/org/springframework/mock/env/MockPropertySource.java
  3. 7
      spring-core/src/test/java/org/springframework/mock/env/MockPropertySource.java
  4. 102
      spring-expression/src/test/java/org/springframework/mock/env/MockPropertySource.java
  5. 9
      spring-test/src/main/java/org/springframework/mock/env/MockEnvironment.java
  6. 9
      spring-test/src/main/java/org/springframework/mock/env/MockPropertySource.java
  7. 10
      spring-test/src/main/java/org/springframework/mock/env/package-info.java
  8. 46
      src/dist/changelog.txt
  9. 16
      src/reference/docbook/testing.xml

9
spring-context/src/test/java/org/springframework/mock/env/MockEnvironment.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,13 +20,14 @@ import org.springframework.core.env.AbstractEnvironment; @@ -20,13 +20,14 @@ import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Simple {@link ConfigurableEnvironment} implementation exposing a
* Simple {@link ConfigurableEnvironment} implementation exposing
* {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
* methods for testing purposes.
*
* @author Chris Beams
* @since 3.1
* @see MockPropertySource
* @author Sam Brannen
* @since 3.2
* @see org.springframework.mock.env.MockPropertySource
*/
public class MockEnvironment extends AbstractEnvironment {

7
spring-context/src/test/java/org/springframework/mock/env/MockPropertySource.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource; @@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource;
*
* @author Chris Beams
* @since 3.1
* @see MockEnvironment
* @see org.springframework.mock.env.MockEnvironment
*/
public class MockPropertySource extends PropertiesPropertySource {
@ -75,7 +75,7 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -75,7 +75,7 @@ public class MockPropertySource extends PropertiesPropertySource {
}
/**
* Create a new {@code MockPropertySource} with with the given name and backed by the given
* Create a new {@code MockPropertySource} with the given name and backed by the given
* {@link Properties} object
* @param name the {@linkplain #getName() name} of the property source
* @param properties the properties to use
@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource {
this.setProperty(name, value);
return this;
}
}

7
spring-core/src/test/java/org/springframework/mock/env/MockPropertySource.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource; @@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource;
*
* @author Chris Beams
* @since 3.1
* @see MockEnvironment
* @see org.springframework.mock.env.MockEnvironment
*/
public class MockPropertySource extends PropertiesPropertySource {
@ -75,7 +75,7 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -75,7 +75,7 @@ public class MockPropertySource extends PropertiesPropertySource {
}
/**
* Create a new {@code MockPropertySource} with with the given name and backed by the given
* Create a new {@code MockPropertySource} with the given name and backed by the given
* {@link Properties} object
* @param name the {@linkplain #getName() name} of the property source
* @param properties the properties to use
@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource {
this.setProperty(name, value);
return this;
}
}

102
spring-expression/src/test/java/org/springframework/mock/env/MockPropertySource.java vendored

@ -1,102 +0,0 @@ @@ -1,102 +0,0 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mock.env;
import java.util.Properties;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
/**
* Simple {@link PropertySource} implementation for use in testing. Accepts
* a user-provided {@link Properties} object, or if omitted during construction,
* the implementation will initialize its own.
*
* The {@link #setProperty} and {@link #withProperty} methods are exposed for
* convenience, for example:
* <pre>
* {@code
* PropertySource<?> source = new MockPropertySource().withProperty("foo", "bar");
* }
* </pre>
*
* @author Chris Beams
* @since 3.1
*/
public class MockPropertySource extends PropertiesPropertySource {
/**
* {@value} is the default name for {@link MockPropertySource} instances not
* otherwise given an explicit name.
* @see #MockPropertySource()
* @see #MockPropertySource(String)
*/
public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties";
/**
* Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
* that will maintain its own internal {@link Properties} instance.
*/
public MockPropertySource() {
this(new Properties());
}
/**
* Create a new {@code MockPropertySource} with the given name that will
* maintain its own internal {@link Properties} instance.
* @param name the {@linkplain #getName() name} of the property source
*/
public MockPropertySource(String name) {
this(name, new Properties());
}
/**
* Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
* and backed by the given {@link Properties} object.
* @param properties the properties to use
*/
public MockPropertySource(Properties properties) {
this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties);
}
/**
* Create a new {@code MockPropertySource} with with the given name and backed by the given
* {@link Properties} object
* @param name the {@linkplain #getName() name} of the property source
* @param properties the properties to use
*/
public MockPropertySource(String name, Properties properties) {
super(name, properties);
}
/**
* Set the given property on the underlying {@link Properties} object.
*/
public void setProperty(String name, Object value) {
this.source.put(name, value);
}
/**
* Convenient synonym for {@link #setProperty} that returns the current instance.
* Useful for method chaining and fluent-style use.
* @return this {@link MockPropertySource} instance
*/
public MockPropertySource withProperty(String name, Object value) {
this.setProperty(name, value);
return this;
}
}

9
src/test/java/org/springframework/mock/env/MockEnvironment.java → spring-test/src/main/java/org/springframework/mock/env/MockEnvironment.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,13 +20,14 @@ import org.springframework.core.env.AbstractEnvironment; @@ -20,13 +20,14 @@ import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Simple {@link ConfigurableEnvironment} implementation exposing a
* Simple {@link ConfigurableEnvironment} implementation exposing
* {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
* methods for testing purposes.
*
* @author Chris Beams
* @since 3.1
* @see MockPropertySource
* @author Sam Brannen
* @since 3.2
* @see org.springframework.mock.env.MockPropertySource
*/
public class MockEnvironment extends AbstractEnvironment {

9
spring-test/src/main/java/org/springframework/mock/env/MockPropertySource.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource; @@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource;
*
* @author Chris Beams
* @since 3.1
* @see MockEnvironment
* @see org.springframework.mock.env.MockEnvironment
*/
public class MockPropertySource extends PropertiesPropertySource {
@ -75,8 +75,8 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -75,8 +75,8 @@ public class MockPropertySource extends PropertiesPropertySource {
}
/**
* Create a new {@code MockPropertySource} with with the given name and backed by the given
* {@link Properties} object
* Create a new {@code MockPropertySource} with the given name and backed by the given
* {@link Properties} object.
* @param name the {@linkplain #getName() name} of the property source
* @param properties the properties to use
*/
@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource { @@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource {
this.setProperty(name, value);
return this;
}
}

10
spring-test/src/main/java/org/springframework/mock/env/package-info.java vendored

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
/**
* This package contains mock implementations of the
* {@link org.springframework.core.env.Environment Environment} and
* {@link org.springframework.core.env.PropertySource PropertySource}
* abstractions introduced in Spring 3.1.
*
* <p>These <em>mocks</em> are useful for developing <em>out-of-container</em>
* unit tests for code that depends on environment-specific properties.
*/
package org.springframework.mock.env;

46
src/dist/changelog.txt vendored

@ -7,29 +7,29 @@ Changes in version 3.2 M2 (2012-08-xx) @@ -7,29 +7,29 @@ Changes in version 3.2 M2 (2012-08-xx)
--------------------------------------
* spring-test module now depends on junit:junit-dep (SPR-6966)
* infer return type of parameterized factory methods (SPR-9493)
* used BufferedInputStream in SimpleMetaDataReader to double performance (SPR-9528)
* added "repeatCount" bean property to Quartz SimpleTriggerFactoryBean (SPR-9521)
* added "jtaTransactionManager" property to Hibernate 4 LocalSessionFactoryBean/Builder (SPR-9480)
* raise RestClientException instead of IllegalArgumentException for unknown status codes
* added "defaultCharset" property to StringHttpMessageConverter (SPR-9487)
* added JacksonObjectMapperFactoryBean for configuring a Jackson ObjectMapper in XML
* added ContentNegotiationManager/ContentNegotiationStrategy to resolve requested media types
* added support for the HTTP PATCH method to Spring MVC and to RestTemplate (SPR-7985)
* enable smart suffix pattern match in @RequestMapping methods (SPR-7632)
* DispatcherPortlet does not forward event exceptions to the render phase by default (SPR-9287)
* add defaultCharset property to StringHttpMessageConverter
* add @ExceptionResolver annotation to detect classes with @ExceptionHandler methods
* move RSS/Atom message converter registration ahead of jackson/jaxb2
* handle BindException in DefaultHandlerExceptionResolver
* parameterize DeferredResult type
* use reflection to instantiate StandardServletAsyncWebRequest
* fix issue with forward before async request processing
* add exclude patterns for mapped interceptors in MVC namespace and MVC Java config
* support content negotiation options in MVC namespace and MVC Java config
* support named dispatchers in MockServletContext (SPR-9587)
* support single, unqualified tx manager in the TestContext framework (SPR-9645)
* support TransactionManagementConfigurer in the TestContext framework (SPR-9604)
* now inferring return type of parameterized factory methods (SPR-9493)
* now using BufferedInputStream in SimpleMetaDataReader to double performance (SPR-9528)
* introduced "repeatCount" property in Quartz SimpleTriggerFactoryBean (SPR-9521)
* introduced "jtaTransactionManager" property in Hibernate 4 LocalSessionFactoryBean/Builder (SPR-9480)
* now raising RestClientException instead of IllegalArgumentException for unknown status codes
* introduced JacksonObjectMapperFactoryBean for configuring a Jackson ObjectMapper in XML
* introduced ContentNegotiationManager/ContentNegotiationStrategy for resolving requested media types
* introduced support for the HTTP PATCH method in Spring MVC and RestTemplate (SPR-7985)
* enabled smart suffix pattern matching in @RequestMapping methods (SPR-7632)
* DispatcherPortlet no longer forwards event exceptions to the render phase by default (SPR-9287)
* introduced "defaultCharset" property in StringHttpMessageConverter (SPR-9487)
* introduced @ExceptionResolver annotation for detecting classes with @ExceptionHandler methods
* moved RSS/Atom message converter registration ahead of jackson/jaxb2
* now handling BindException in DefaultHandlerExceptionResolver
* DeferredResult type is now parameterized
* now using reflection to instantiate StandardServletAsyncWebRequest
* fixed issue with forward before async request processing
* introduced exclude patterns for mapped interceptors in MVC namespace and MVC Java config
* introduced support for content negotiation options in MVC namespace and MVC Java config
* introduced support for named dispatchers in MockServletContext (SPR-9587)
* introduced support for single, unqualified tx manager in the TestContext framework (SPR-9645)
* introduced support for TransactionManagementConfigurer in the TestContext framework (SPR-9604)
* introduced MockEnvironment in the spring-test module (SPR-9492)
Changes in version 3.2 M1 (2012-05-28)

16
src/reference/docbook/testing.xml

@ -47,6 +47,22 @@ @@ -47,6 +47,22 @@
<section xml:id="mock-objects">
<title>Mock Objects</title>
<section xml:id="mock-objects-env">
<title>Environment</title>
<para>The <literal>org.springframework.mock.env</literal> package
contains mock implementations of the
<interfacename>Environment</interfacename> and
<interfacename>PropertySource</interfacename> abstractions introduced
in Spring 3.1 (see <xref
linkend="new-in-3.1-environment-abstraction" /> and <xref
linkend="new-in-3.1-property-source-abstraction" />).
<classname>MockEnvironment</classname> and
<classname>MockPropertySource</classname> are useful for developing
<emphasis>out-of-container</emphasis> unit tests for code that depends
on environment-specific properties.</para>
</section>
<section xml:id="mock-objects-jndi">
<title>JNDI</title>

Loading…
Cancel
Save