View Javadoc

1   /*
2    * Copyright 2007-2010 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  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              // if there is an issue initializing logging system, let's be sure to print the stack trace so we can debug!
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          // testing deferred resolution
72          assertEquals("2", config.getProperty("b"));
73  
74          // testing nested parameters
75          assertEquals("oracle-user", config.getProperty("username"));
76  
77          // testing multiple parameters in value
78          assertEquals("oracle-user+oracle", config.getProperty("multi"));
79  
80          // testing random integer generation
81          int x = Integer.valueOf(config.getProperty("randomInt"));
82          assertTrue(x >= 10);
83          assertTrue(x <= 20);
84  
85          // testing setting of system parameters
86          assertEquals("doIt", System.getProperty("some.derby.config"));
87  
88          // testing override setting
89          assertEquals("original", config.getProperty("foo"));
90  
91          // testing default to system param when not in file
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      * This method tests having a variable already set and then doing a circular reference.
146      * 
147      * @throws Exception
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         // The next line tests 2 things. 1. the inline replace, but with a system var
153         System.setProperty("environment", "test");
154         config.parseConfig();
155         
156         // run the test again, but this time use a standard config var.
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      * This method tests parsing a config file and then altering a variable that is used as a composite for other variables.
165      * 
166      * @throws Exception
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         // When you run the tests via maven the suite initially sets up the a rootConfig object.
189         // If this is the case then we should just Add our properties to the root config.
190         // If we call the init then we overwrite the existing config in the context, which breaks
191         // other tests in the suite.
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 }