001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.mock.env; 018 019import java.util.Properties; 020 021import org.springframework.core.env.PropertiesPropertySource; 022import org.springframework.core.env.PropertySource; 023 024/** 025 * Simple {@link PropertySource} implementation for use in testing. Accepts 026 * a user-provided {@link Properties} object, or if omitted during construction, 027 * the implementation will initialize its own. 028 * 029 * The {@link #setProperty} and {@link #withProperty} methods are exposed for 030 * convenience, for example: 031 * <pre class="code"> 032 * {@code 033 * PropertySource<?> source = new MockPropertySource().withProperty("foo", "bar"); 034 * } 035 * </pre> 036 * 037 * @author Chris Beams 038 * @since 3.1 039 * @see org.springframework.mock.env.MockEnvironment 040 */ 041public class MockPropertySource extends PropertiesPropertySource { 042 043 /** 044 * {@value} is the default name for {@link MockPropertySource} instances not 045 * otherwise given an explicit name. 046 * @see #MockPropertySource() 047 * @see #MockPropertySource(String) 048 */ 049 public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties"; 050 051 /** 052 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME} 053 * that will maintain its own internal {@link Properties} instance. 054 */ 055 public MockPropertySource() { 056 this(new Properties()); 057 } 058 059 /** 060 * Create a new {@code MockPropertySource} with the given name that will 061 * maintain its own internal {@link Properties} instance. 062 * @param name the {@linkplain #getName() name} of the property source 063 */ 064 public MockPropertySource(String name) { 065 this(name, new Properties()); 066 } 067 068 /** 069 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME} 070 * and backed by the given {@link Properties} object. 071 * @param properties the properties to use 072 */ 073 public MockPropertySource(Properties properties) { 074 this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties); 075 } 076 077 /** 078 * Create a new {@code MockPropertySource} with the given name and backed by the given 079 * {@link Properties} object. 080 * @param name the {@linkplain #getName() name} of the property source 081 * @param properties the properties to use 082 */ 083 public MockPropertySource(String name, Properties properties) { 084 super(name, properties); 085 } 086 087 /** 088 * Set the given property on the underlying {@link Properties} object. 089 */ 090 public void setProperty(String name, Object value) { 091 this.source.put(name, value); 092 } 093 094 /** 095 * Convenient synonym for {@link #setProperty} that returns the current instance. 096 * Useful for method chaining and fluent-style use. 097 * @return this {@link MockPropertySource} instance 098 */ 099 public MockPropertySource withProperty(String name, Object value) { 100 this.setProperty(name, value); 101 return this; 102 } 103 104}