View Javadoc

1   /**
2    * Copyright 2010-2012 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.common.util.service;
17  
18  import java.io.File;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.junit.Test;
23  import org.kuali.common.util.LocationUtils;
24  import org.springframework.context.support.ClassPathXmlApplicationContext;
25  
26  public class SpringContextLoadingTest {
27  
28  	@Test
29  	public void test() {
30  		try {
31  
32  			Properties properties = new Properties();
33  			properties.setProperty("spring.message", "Good bye!");
34  
35  			ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext();
36  			parent.refresh();
37  			parent.getBeanFactory().registerSingleton("properties", properties);
38  
39  			String location1 = "classpath:org/kuali/common/util/child-context.xml";
40  			String location2 = "/Users/jeffcaddel/ws/kuali-util/src/test/resources/org/kuali/common/util/child-context.xml";
41  			String[] locations = getLocations(new String[] { location1, location2 });
42  
43  			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(locations, false, parent);
44  			context.refresh();
45  			context.close();
46  		} catch (Exception e) {
47  			e.printStackTrace();
48  		}
49  	}
50  
51  	protected String[] getLocations(List<String> locations) {
52  		return getLocations(locations.toArray(new String[locations.size()]));
53  	}
54  
55  	protected String[] getLocations(String[] locations) {
56  		for (int i = 0; i < locations.length; i++) {
57  			String location = locations[i];
58  			if (!LocationUtils.exists(location)) {
59  				throw new IllegalArgumentException("Location [" + location + "] does not exist");
60  			}
61  			if (LocationUtils.isExistingFile(location)) {
62  				File file = new File(location);
63  				locations[i] = LocationUtils.getURLString(file);
64  			}
65  		}
66  		return locations;
67  	}
68  }