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 org.springframework.core.env.AbstractEnvironment;
020import org.springframework.core.env.ConfigurableEnvironment;
021
022/**
023 * Simple {@link ConfigurableEnvironment} implementation exposing
024 * {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
025 * methods for testing purposes.
026 *
027 * @author Chris Beams
028 * @author Sam Brannen
029 * @since 3.2
030 * @see org.springframework.mock.env.MockPropertySource
031 */
032public class MockEnvironment extends AbstractEnvironment {
033
034        private MockPropertySource propertySource = new MockPropertySource();
035
036        /**
037         * Create a new {@code MockEnvironment} with a single {@link MockPropertySource}.
038         */
039        public MockEnvironment() {
040                getPropertySources().addLast(propertySource);
041        }
042
043        /**
044         * Set a property on the underlying {@link MockPropertySource} for this environment.
045         */
046        public void setProperty(String key, String value) {
047                propertySource.setProperty(key, value);
048        }
049
050        /**
051         * Convenient synonym for {@link #setProperty} that returns the current instance.
052         * Useful for method chaining and fluent-style use.
053         * @return this {@link MockEnvironment} instance
054         * @see MockPropertySource#withProperty
055         */
056        public MockEnvironment withProperty(String key, String value) {
057                this.setProperty(key, value);
058                return this;
059        }
060
061}