1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.config;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Properties;
24
25 import org.apache.log4j.LogManager;
26 import org.apache.log4j.PropertyConfigurator;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.kuali.rice.core.config.Config;
31 import org.kuali.rice.core.config.ConfigContext;
32 import org.kuali.rice.core.config.ConfigurationException;
33 import org.kuali.rice.core.config.JAXBConfigImpl;
34 import org.kuali.rice.core.exception.RiceRuntimeException;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.context.support.ClassPathXmlApplicationContext;
37
38 public class JAXBConfigImplTest {
39
40 private static final String DEFAULT_LOG4J_CONFIG = "org/kuali/rice/core/logging/default-log4j.properties";
41
42 @BeforeClass
43 public static void setupClass() {
44 try {
45 Properties p = new Properties();
46 p.load(JAXBConfigImplTest.class.getClassLoader().getResourceAsStream(DEFAULT_LOG4J_CONFIG));
47 PropertyConfigurator.configure(p);
48 } catch (Exception e) {
49
50 e.printStackTrace();
51 throw new RiceRuntimeException(e);
52 }
53 }
54
55 @AfterClass
56 public static void destroyClass() {
57 LogManager.shutdown();
58 }
59
60 @Test
61 public void testBasicFunctionality() throws Exception {
62 System.setProperty("some.system.property", "sys-value");
63 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-config.xml");
64
65 config.parseConfig();
66
67 doBasicAssertions(config);
68 }
69
70 protected void doBasicAssertions(Config config) {
71
72 assertEquals("2", config.getProperty("b"));
73
74
75 assertEquals("oracle-user", config.getProperty("username"));
76
77
78 assertEquals("oracle-user+oracle", config.getProperty("multi"));
79
80
81 int x = Integer.valueOf(config.getProperty("randomInt"));
82 assertTrue(x >= 10);
83 assertTrue(x <= 20);
84
85
86 assertEquals("doIt", System.getProperty("some.derby.config"));
87
88
89 assertEquals("original", config.getProperty("foo"));
90
91
92 assertEquals("sys-value", config.getProperty("defaultToSystem"));
93 }
94
95 @Test
96 public void testSystemOverride() throws Exception {
97 System.setProperty("a", "3");
98
99 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-config.xml");
100
101 config.setSystemOverride(true);
102 config.parseConfig();
103 assertEquals("3", config.getProperty("b"));
104 }
105
106 @Test
107 public void testOverrideResolution() throws Exception {
108 List<String> files = new ArrayList<String>();
109 files.add("classpath:org/kuali/rice/core/config/jaxb-test-config.xml");
110 files.add("classpath:org/kuali/rice/core/config/jaxb-test-override.xml");
111
112 JAXBConfigImpl config = new JAXBConfigImpl(files);
113
114 config.parseConfig();
115 assertEquals("mysql-user", config.getProperty("username"));
116 }
117
118 @Test
119 public void testImportOverride() throws Exception {
120 List<String> files = new ArrayList<String>();
121 files.add("classpath:org/kuali/rice/core/config/jaxb-test-import-config.xml");
122
123 JAXBConfigImpl config = new JAXBConfigImpl(files);
124
125 config.parseConfig();
126 assertEquals("mysql-user", config.getProperty("username"));
127 }
128
129 @Test(expected = ConfigurationException.class)
130 public void testCircularReference() throws Exception {
131 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-circular.xml");
132
133 config.parseConfig();
134 }
135
136 @Test(expected = ConfigurationException.class)
137 public void testCircularReference2() throws Exception {
138 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-circular2.xml");
139
140 config.parseConfig();
141 }
142
143
144
145
146
147
148
149 @Test
150 public void testCircularReference3() throws Exception{
151 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-circular2.xml");
152
153 System.setProperty("environment", "test");
154 config.parseConfig();
155
156
157 config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-circular2.xml");
158 config.putProperty("environment", "test");
159 config.parseConfig();
160 }
161
162
163
164
165
166
167
168 @Test
169 public void testResolutionAfterParse() throws Exception{
170 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-config.xml");
171 config.parseConfig();
172
173 config.putProperty("db", "mysql");
174
175 assertEquals("mysql-user", config.getProperty("username"));
176 assertEquals("mysql-user+mysql", config.getProperty("multi"));
177 assertEquals("mysql", config.getProperty("db"));
178
179 }
180
181 @Test
182 public void testPropertiesParams() throws Exception {
183 Config rootConfig = ConfigContext.getCurrentContextConfig();
184
185 JAXBConfigImpl config = new JAXBConfigImpl("classpath:org/kuali/rice/core/config/jaxb-test-config.xml");
186 config.parseConfig();
187
188
189
190
191
192 if(rootConfig != null){
193 rootConfig.putProperties(config.getProperties());
194 }else{
195 ConfigContext.init(config);
196 }
197
198 ApplicationContext context = new ClassPathXmlApplicationContext("org/kuali/rice/core/config/jaxb-test-context.xml");
199 PropertyHolder holder = (PropertyHolder) context.getBean("jpaProps");
200
201 assertEquals(3, holder.getJpaProps().size());
202 assertEquals("1", holder.getJpaProps().getProperty("jpa1"));
203 assertEquals(null, holder.getJpaProps().getProperty("jpaFail"));
204
205 }
206
207 }