View Javadoc
1   /*
2    * Copyright 2002-2011 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.core.env;
18  
19  import java.util.Collections;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.junit.Test;
24  
25  import static org.hamcrest.CoreMatchers.*;
26  import static org.junit.Assert.*;
27  
28  /**
29   * Unit tests covering the extensibility of {@link AbstractEnvironment}.
30   *
31   * @author Chris Beams
32   * @since 3.1
33   */
34  public class CustomEnvironmentTests {
35  
36  	// -- tests relating to customizing reserved default profiles ----------------------
37  
38  	@Test
39  	public void control() {
40  		Environment env = new AbstractEnvironment() { };
41  		assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(true));
42  	}
43  
44  	@Test
45  	public void withNoReservedDefaultProfile() {
46  		class CustomEnvironment extends AbstractEnvironment {
47  			@Override
48  			protected Set<String> getReservedDefaultProfiles() {
49  				return Collections.emptySet();
50  			}
51  		}
52  
53  		Environment env = new CustomEnvironment();
54  		assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
55  	}
56  
57  	@Test
58  	public void withSingleCustomReservedDefaultProfile() {
59  		class CustomEnvironment extends AbstractEnvironment {
60  			@Override
61  			protected Set<String> getReservedDefaultProfiles() {
62  				return Collections.singleton("rd1");
63  			}
64  		}
65  
66  		Environment env = new CustomEnvironment();
67  		assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
68  		assertThat(env.acceptsProfiles("rd1"), is(true));
69  	}
70  
71  	@Test
72  	public void withMultiCustomReservedDefaultProfile() {
73  		class CustomEnvironment extends AbstractEnvironment {
74  			@Override
75  			@SuppressWarnings("serial")
76  			protected Set<String> getReservedDefaultProfiles() {
77  				return new HashSet<String>() {{ add("rd1"); add("rd2");  }};
78  			}
79  		}
80  
81  		ConfigurableEnvironment env = new CustomEnvironment();
82  		assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
83  		assertThat(env.acceptsProfiles("rd1", "rd2"), is(true));
84  
85  		// finally, issue additional assertions to cover all combinations of calling these
86  		// methods, however unlikely.
87  		env.setDefaultProfiles("d1");
88  		assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
89  		assertThat(env.acceptsProfiles("d1"), is(true));
90  
91  		env.setActiveProfiles("a1", "a2");
92  		assertThat(env.acceptsProfiles("d1"), is(false));
93  		assertThat(env.acceptsProfiles("a1", "a2"), is(true));
94  
95  		env.setActiveProfiles();
96  		assertThat(env.acceptsProfiles("d1"), is(true));
97  		assertThat(env.acceptsProfiles("a1", "a2"), is(false));
98  
99  		env.setDefaultProfiles();
100 		assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
101 		assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
102 		assertThat(env.acceptsProfiles("d1"), is(false));
103 		assertThat(env.acceptsProfiles("a1", "a2"), is(false));
104 	}
105 
106 
107 	// -- tests relating to customizing property sources -------------------------------
108 }