001/* 002 * Copyright 2012-2016 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 * http://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.boot.test.util; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.springframework.context.ApplicationContext; 023import org.springframework.context.ConfigurableApplicationContext; 024import org.springframework.core.env.ConfigurableEnvironment; 025import org.springframework.core.env.Environment; 026import org.springframework.core.env.MapPropertySource; 027import org.springframework.core.env.MutablePropertySources; 028 029/** 030 * Test utilities for setting environment values. 031 * 032 * @author Dave Syer 033 * @author Stephane Nicoll 034 * @since 1.4.0 035 */ 036public abstract class EnvironmentTestUtils { 037 038 /** 039 * Add additional (high priority) values to an {@link Environment} owned by an 040 * {@link ApplicationContext}. Name-value pairs can be specified with colon (":") or 041 * equals ("=") separators. 042 * @param context the context with an environment to modify 043 * @param pairs the name:value pairs 044 */ 045 public static void addEnvironment(ConfigurableApplicationContext context, 046 String... pairs) { 047 addEnvironment(context.getEnvironment(), pairs); 048 } 049 050 /** 051 * Add additional (high priority) values to an {@link Environment}. Name-value pairs 052 * can be specified with colon (":") or equals ("=") separators. 053 * @param environment the environment to modify 054 * @param pairs the name:value pairs 055 */ 056 public static void addEnvironment(ConfigurableEnvironment environment, 057 String... pairs) { 058 addEnvironment("test", environment, pairs); 059 } 060 061 /** 062 * Add additional (high priority) values to an {@link Environment}. Name-value pairs 063 * can be specified with colon (":") or equals ("=") separators. 064 * @param environment the environment to modify 065 * @param name the property source name 066 * @param pairs the name:value pairs 067 */ 068 public static void addEnvironment(String name, ConfigurableEnvironment environment, 069 String... pairs) { 070 MutablePropertySources sources = environment.getPropertySources(); 071 Map<String, Object> map = getOrAdd(sources, name); 072 for (String pair : pairs) { 073 int index = getSeparatorIndex(pair); 074 String key = pair.substring(0, index > 0 ? index : pair.length()); 075 String value = index > 0 ? pair.substring(index + 1) : ""; 076 map.put(key.trim(), value.trim()); 077 } 078 } 079 080 @SuppressWarnings("unchecked") 081 private static Map<String, Object> getOrAdd(MutablePropertySources sources, 082 String name) { 083 if (sources.contains(name)) { 084 return (Map<String, Object>) sources.get(name).getSource(); 085 } 086 Map<String, Object> map = new HashMap<String, Object>(); 087 sources.addFirst(new MapPropertySource(name, map)); 088 return map; 089 } 090 091 private static int getSeparatorIndex(String pair) { 092 int colonIndex = pair.indexOf(":"); 093 int equalIndex = pair.indexOf("="); 094 if (colonIndex == -1) { 095 return equalIndex; 096 } 097 if (equalIndex == -1) { 098 return colonIndex; 099 } 100 return Math.min(colonIndex, equalIndex); 101 } 102 103}