ContextNamespaceHandlerTests.java 5.6 KB
Newer Older
1
/*
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.context.config;

19
import java.io.FileNotFoundException;
20
import java.util.Calendar;
21 22
import java.util.Date;

23
import org.junit.After;
24
import org.junit.Test;
25

26
import org.springframework.beans.FatalBeanException;
27 28
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
29 30 31
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.env.MockEnvironment;
32

33 34
import static org.junit.Assert.*;

35 36
/**
 * @author Arjen Poutsma
37
 * @author Dave Syer
38
 * @author Chris Beams
39
 * @author Juergen Hoeller
40 41
 * @since 2.5.6
 */
42
public class ContextNamespaceHandlerTests {
43

44 45 46 47 48
	@After
	public void tearDown() {
		System.getProperties().remove("foo");
	}

49

50
	@Test
51
	public void propertyPlaceholder() {
52 53
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"contextNamespaceHandlerTests-replace.xml", getClass());
54
		assertEquals("bar", applicationContext.getBean("string"));
55
		assertEquals("null", applicationContext.getBean("nullString"));
56 57 58
	}

	@Test
59
	public void propertyPlaceholderSystemProperties() {
60 61 62 63
		String value = System.setProperty("foo", "spam");
		try {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
					"contextNamespaceHandlerTests-system.xml", getClass());
64 65
			assertEquals("spam", applicationContext.getBean("string"));
			assertEquals("none", applicationContext.getBean("fallback"));
66 67
		}
		finally {
68
			if (value != null) {
69 70 71 72
				System.setProperty("foo", value);
			}
		}
	}
73

74
	@Test
75
	public void propertyPlaceholderEnvironmentProperties() {
76
		MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
77 78 79 80
		GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
		applicationContext.setEnvironment(env);
		applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
		applicationContext.refresh();
81 82
		assertEquals("spam", applicationContext.getBean("string"));
		assertEquals("none", applicationContext.getBean("fallback"));
83 84
	}

85
	@Test
86
	public void propertyPlaceholderLocation() {
87 88
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"contextNamespaceHandlerTests-location.xml", getClass());
89 90 91
		assertEquals("bar", applicationContext.getBean("foo"));
		assertEquals("foo", applicationContext.getBean("bar"));
		assertEquals("maps", applicationContext.getBean("spam"));
92 93
	}

94
	@Test
95
	public void propertyPlaceholderLocationWithSystemPropertyForOneLocation() {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		System.setProperty("properties",
				"classpath*:/org/springframework/context/config/test-*.properties");
		try {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
					"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
			assertEquals("bar", applicationContext.getBean("foo"));
			assertEquals("foo", applicationContext.getBean("bar"));
			assertEquals("maps", applicationContext.getBean("spam"));
		}
		finally {
			System.clearProperty("properties");
		}
	}

	@Test
111
	public void propertyPlaceholderLocationWithSystemPropertyForMultipleLocations() {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		System.setProperty("properties",
				"classpath*:/org/springframework/context/config/test-*.properties," +
				"classpath*:/org/springframework/context/config/empty-*.properties," +
				"classpath*:/org/springframework/context/config/missing-*.properties");
		try {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
					"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
			assertEquals("bar", applicationContext.getBean("foo"));
			assertEquals("foo", applicationContext.getBean("bar"));
			assertEquals("maps", applicationContext.getBean("spam"));
		}
		finally {
			System.clearProperty("properties");
		}
	}

	@Test
129
	public void propertyPlaceholderLocationWithSystemPropertyMissing() {
130 131 132 133 134 135 136 137 138 139 140 141
		try {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
					"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
			assertEquals("bar", applicationContext.getBean("foo"));
			assertEquals("foo", applicationContext.getBean("bar"));
			assertEquals("maps", applicationContext.getBean("spam"));
		}
		catch (FatalBeanException ex) {
			assertTrue(ex.getRootCause() instanceof FileNotFoundException);
		}
	}

142
	@Test
143
	public void propertyPlaceholderIgnored() {
144 145
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"contextNamespaceHandlerTests-replace-ignore.xml", getClass());
146
		assertEquals("${bar}", applicationContext.getBean("string"));
147
		assertEquals("null", applicationContext.getBean("nullString"));
148 149
	}

150
	@Test
151
	public void propertyOverride() {
152 153
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"contextNamespaceHandlerTests-override.xml", getClass());
154
		Date date = (Date) applicationContext.getBean("date");
155 156
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
157
		assertEquals(42, calendar.get(Calendar.MINUTE));
158
	}
159

160
}