1
+ /*
2
+ * Copyright 2013-2017 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 .cloud .bootstrap .config ;
18
+
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertNull ;
22
+
23
+ import org .junit .Test ;
24
+ import org .springframework .boot .builder .SpringApplicationBuilder ;
25
+ import org .springframework .context .ConfigurableApplicationContext ;
26
+ import org .springframework .context .annotation .Bean ;
27
+ import org .springframework .context .annotation .Configuration ;
28
+
29
+ /**
30
+ * Integration tests for Bootstrap Listener's functionality of adding a bootstrap context
31
+ * as the root Application Context
32
+ *
33
+ * @author Biju Kunjummen
34
+ */
35
+ public class BootstrapListenerHierarchyIntegrationTests {
36
+
37
+ @ Test
38
+ public void shouldAddInABootstrapContext () {
39
+ ConfigurableApplicationContext context = new SpringApplicationBuilder ()
40
+ .sources (BasicConfiguration .class ).web (false ).run ();
41
+
42
+ assertNotNull (context .getParent ());
43
+ }
44
+
45
+ @ Test
46
+ public void shouldAddInOneBootstrapForABasicParentChildHierarchy () {
47
+ ConfigurableApplicationContext context = new SpringApplicationBuilder ()
48
+ .sources (RootConfiguration .class ).web (false )
49
+ .child (BasicConfiguration .class ).web (false ).run ();
50
+
51
+ // Should be RootConfiguration based context
52
+ ConfigurableApplicationContext parent = (ConfigurableApplicationContext ) context
53
+ .getParent ();
54
+ assertEquals ("rootBean" , parent .getBean ("rootBean" , String .class ));
55
+
56
+ // Parent should have the bootstrap context as parent
57
+ assertNotNull (parent .getParent ());
58
+
59
+ ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext ) parent
60
+ .getParent ();
61
+
62
+ // Bootstrap should be the root, there should be no other parent
63
+ assertNull (bootstrapContext .getParent ());
64
+ }
65
+
66
+ @ Test
67
+ public void shouldAddInOneBootstrapForSiblingsBasedHierarchy () {
68
+ ConfigurableApplicationContext context = new SpringApplicationBuilder ()
69
+ .sources (RootConfiguration .class ).web (false )
70
+ .child (BasicConfiguration .class ).web (false )
71
+ .sibling (BasicConfiguration .class ).web (false ).run ();
72
+
73
+ // Should be RootConfiguration based context
74
+ ConfigurableApplicationContext parent = (ConfigurableApplicationContext ) context
75
+ .getParent ();
76
+ assertEquals ("rootBean" , parent .getBean ("rootBean" , String .class ));
77
+
78
+ // Parent should have the bootstrap context as parent
79
+ assertNotNull (parent .getParent ());
80
+
81
+ ConfigurableApplicationContext bootstrapContext = (ConfigurableApplicationContext ) parent
82
+ .getParent ();
83
+
84
+ // Bootstrap should be the root, there should be no other parent
85
+ assertNull (bootstrapContext .getParent ());
86
+ }
87
+
88
+ @ Configuration
89
+ static class BasicConfiguration {
90
+ }
91
+
92
+ @ Configuration
93
+ static class RootConfiguration {
94
+
95
+ @ Bean
96
+ public String rootBean () {
97
+ return "rootBean" ;
98
+ }
99
+ }
100
+ }
0 commit comments