1 /*
2 * Copyright 2002-2012 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.mock.env;
18
19 import java.util.Properties;
20
21 import org.springframework.core.env.PropertiesPropertySource;
22 import org.springframework.core.env.PropertySource;
23
24 /**
25 * Simple {@link PropertySource} implementation for use in testing. Accepts
26 * a user-provided {@link Properties} object, or if omitted during construction,
27 * the implementation will initialize its own.
28 *
29 * The {@link #setProperty} and {@link #withProperty} methods are exposed for
30 * convenience, for example:
31 * <pre>
32 * {@code
33 * PropertySource<?> source = new MockPropertySource().withProperty("foo", "bar");
34 * }
35 * </pre>
36 *
37 * @author Chris Beams
38 * @since 3.1
39 * @see org.springframework.mock.env.MockEnvironment
40 */
41 public class MockPropertySource extends PropertiesPropertySource {
42
43 /**
44 * {@value} is the default name for {@link MockPropertySource} instances not
45 * otherwise given an explicit name.
46 * @see #MockPropertySource()
47 * @see #MockPropertySource(String)
48 */
49 public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties";
50
51 /**
52 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
53 * that will maintain its own internal {@link Properties} instance.
54 */
55 public MockPropertySource() {
56 this(new Properties());
57 }
58
59 /**
60 * Create a new {@code MockPropertySource} with the given name that will
61 * maintain its own internal {@link Properties} instance.
62 * @param name the {@linkplain #getName() name} of the property source
63 */
64 public MockPropertySource(String name) {
65 this(name, new Properties());
66 }
67
68 /**
69 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
70 * and backed by the given {@link Properties} object.
71 * @param properties the properties to use
72 */
73 public MockPropertySource(Properties properties) {
74 this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties);
75 }
76
77 /**
78 * Create a new {@code MockPropertySource} with the given name and backed by the given
79 * {@link Properties} object
80 * @param name the {@linkplain #getName() name} of the property source
81 * @param properties the properties to use
82 */
83 public MockPropertySource(String name, Properties properties) {
84 super(name, properties);
85 }
86
87 /**
88 * Set the given property on the underlying {@link Properties} object.
89 */
90 public void setProperty(String name, Object value) {
91 this.source.put(name, value);
92 }
93
94 /**
95 * Convenient synonym for {@link #setProperty} that returns the current instance.
96 * Useful for method chaining and fluent-style use.
97 * @return this {@link MockPropertySource} instance
98 */
99 public MockPropertySource withProperty(String name, Object value) {
100 this.setProperty(name, value);
101 return this;
102 }
103
104 }