2 * This is a utility project for wide range of applications
4 * Copyright (C) 8 Imran M Yousuf (imyousuf@smartitengineering.com)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 10-1 USA
18 package com
.smartitengineering
.util
.spring
;
21 import java
.io
.FileOutputStream
;
22 import java
.io
.IOException
;
23 import java
.util
.Properties
;
24 import junit
.framework
.Test
;
25 import junit
.framework
.TestCase
;
26 import junit
.framework
.TestSuite
;
27 import org
.springframework
.beans
.BeansException
;
28 import org
.springframework
.context
.ApplicationContext
;
29 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
32 * Unit test for simple App.
34 public class PropertiesLocatorConfigurerTest
37 private ApplicationContext applicationContext
;
38 private TestBean bean
;
41 * Create the test case
43 * @param testName name of the test case
45 public PropertiesLocatorConfigurerTest(String testName
) {
50 * @return the suite of tests being tested
52 public static Test
suite() {
53 return new TestSuite(PropertiesLocatorConfigurerTest
.class);
56 public void testBeanClasspath() {
58 assertEquals("default", bean
.getPropertyDefault());
59 assertEquals("classpath", bean
.getPropertyClassPath());
60 assertEquals("current dir...sample", bean
.getPropertyCurrentDir());
61 assertEquals("user home dir...sample", bean
.getPropertyUserHome());
64 public void testBeanCurrentDir() {
65 Properties properties
= new Properties();
66 properties
.setProperty("testbean.current_dir", "current dir");
67 File file
= new File(System
.getProperty("user.dir"),
68 "test-config.properties");
70 FileOutputStream fos
= new FileOutputStream(file
);
71 properties
.store(fos
, "");
74 catch (IOException ex
) {
75 fail(ex
.getMessage());
78 assertEquals("default", bean
.getPropertyDefault());
79 assertEquals("classpath", bean
.getPropertyClassPath());
80 assertEquals("current dir", bean
.getPropertyCurrentDir());
81 assertEquals("user home dir...sample", bean
.getPropertyUserHome());
85 public void testBeanHomeDir() {
86 Properties properties
= new Properties();
87 properties
.setProperty("testbean.user_home", "user home dir");
88 File file
= new File(System
.getProperty("user.home"),
89 "test-config.properties");
91 FileOutputStream fos
= new FileOutputStream(file
);
92 properties
.store(fos
, "");
95 catch (IOException ex
) {
96 fail(ex
.getMessage());
99 assertEquals("default", bean
.getPropertyDefault());
100 assertEquals("classpath", bean
.getPropertyClassPath());
101 assertEquals("current dir...sample", bean
.getPropertyCurrentDir());
102 assertEquals("user home dir", bean
.getPropertyUserHome());
105 public void testAll() {
106 Properties properties
= new Properties();
107 properties
.setProperty("testbean.current_dir", "current dir");
108 File fileCurrentDir
= new File(System
.getProperty("user.dir"),
109 "test-config.properties");
111 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
112 properties
.store(fos
, "");
115 catch (IOException ex
) {
116 fail(ex
.getMessage());
118 properties
= new Properties();
119 properties
.setProperty("testbean.current_dir", "current dir again");
120 properties
.setProperty("testbean.user_home", "user home dir");
121 File fileUserHome
= new File(System
.getProperty("user.home"),
122 "test-config.properties");
124 FileOutputStream fos
= new FileOutputStream(fileUserHome
);
125 properties
.store(fos
, "");
128 catch (IOException ex
) {
129 fail(ex
.getMessage());
132 assertEquals("default", bean
.getPropertyDefault());
133 assertEquals("classpath", bean
.getPropertyClassPath());
134 assertEquals("current dir again", bean
.getPropertyCurrentDir());
135 assertEquals("user home dir", bean
.getPropertyUserHome());
136 fileCurrentDir
.delete();
137 fileUserHome
.delete();
140 public void testBean2() {
142 assertTrue(bean
.getPropertyDefault().endsWith("2"));
143 assertTrue(bean
.getPropertyClassPath().endsWith("2"));
144 assertTrue(bean
.getPropertyCurrentDir().endsWith("2"));
145 assertTrue(bean
.getPropertyUserHome().endsWith("2"));
148 private void getBean()
149 throws BeansException
{
151 new ClassPathXmlApplicationContext("test-app-context.xml");
153 (TestBean
) applicationContext
.getBean("testBean");
156 private void getBean2()
157 throws BeansException
{
158 StringBuilder paths
= new StringBuilder();
161 new ClassPathXmlApplicationContext("test-app-context.xml");
163 (TestBean
) applicationContext
.getBean("testBean2");
166 private void initPaths(StringBuilder paths
) {
167 String path
= "./target/a/";
168 File dir
= new File(path
);
172 Properties properties
= new Properties();
173 properties
.setProperty("testbean.default", "default 2");
174 File fileCurrentDir
= new File(dir
, "test-config-custom.properties");
176 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
177 properties
.store(fos
, "");
180 catch (IOException ex
) {
181 fail(ex
.getMessage());
184 path
= "./target/b/";
185 dir
= new File(path
);
189 properties
= new Properties();
190 properties
.setProperty("testbean.cp", "cp 2");
191 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
193 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
194 properties
.store(fos
, "");
197 catch (IOException ex
) {
198 fail(ex
.getMessage());
202 path
= "./target/c/";
203 dir
= new File(path
);
207 properties
= new Properties();
208 properties
.setProperty("testbean.current_dir", "current dir");
209 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
211 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
212 properties
.store(fos
, "");
215 catch (IOException ex
) {
216 fail(ex
.getMessage());
220 path
= "./target/d/";
221 dir
= new File(path
);
225 properties
= new Properties();
226 properties
.setProperty("testbean.current_dir", "current dir 2");
227 properties
.setProperty("testbean.user_home", "user home dir 2");
228 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
230 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
231 properties
.store(fos
, "");
234 catch (IOException ex
) {
235 fail(ex
.getMessage());