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
.bean
.spring
;
20 import com
.smartitengineering
.util
.bean
.BeanFactoryRegistrar
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.util
.Properties
;
25 import junit
.framework
.Test
;
26 import junit
.framework
.TestCase
;
27 import junit
.framework
.TestSuite
;
28 import org
.springframework
.beans
.BeansException
;
29 import org
.springframework
.context
.ApplicationContext
;
30 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
33 * Unit test for simple App.
35 public class PropertiesLocatorConfigurerTest
38 private ApplicationContext applicationContext
;
39 private TestBean bean
;
42 * Create the test case
44 * @param testName name of the test case
46 public PropertiesLocatorConfigurerTest(String testName
) {
51 * @return the suite of tests being tested
53 public static Test
suite() {
54 return new TestSuite(PropertiesLocatorConfigurerTest
.class);
57 public void testBeanClasspath() {
59 assertEquals("default", bean
.getPropertyDefault());
60 assertEquals("classpath", bean
.getPropertyClassPath());
61 assertEquals("current dir...sample", bean
.getPropertyCurrentDir());
62 assertEquals("user home dir...sample", bean
.getPropertyUserHome());
65 public void testBeanCurrentDir() {
66 Properties properties
= new Properties();
67 properties
.setProperty("testbean.current_dir", "current dir");
68 File file
= new File(System
.getProperty("user.dir"),
69 "test-config.properties");
71 FileOutputStream fos
= new FileOutputStream(file
);
72 properties
.store(fos
, "");
75 catch (IOException ex
) {
76 fail(ex
.getMessage());
79 assertEquals("default", bean
.getPropertyDefault());
80 assertEquals("classpath", bean
.getPropertyClassPath());
81 assertEquals("current dir", bean
.getPropertyCurrentDir());
82 assertEquals("user home dir...sample", bean
.getPropertyUserHome());
86 public void testBeanHomeDir() {
87 Properties properties
= new Properties();
88 properties
.setProperty("testbean.user_home", "user home dir");
89 File file
= new File(System
.getProperty("user.home"),
90 "test-config.properties");
92 FileOutputStream fos
= new FileOutputStream(file
);
93 properties
.store(fos
, "");
96 catch (IOException ex
) {
97 fail(ex
.getMessage());
100 assertEquals("default", bean
.getPropertyDefault());
101 assertEquals("classpath", bean
.getPropertyClassPath());
102 assertEquals("current dir...sample", bean
.getPropertyCurrentDir());
103 assertEquals("user home dir", bean
.getPropertyUserHome());
106 public void testAll() {
107 Properties properties
= new Properties();
108 properties
.setProperty("testbean.current_dir", "current dir");
109 File fileCurrentDir
= new File(System
.getProperty("user.dir"),
110 "test-config.properties");
112 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
113 properties
.store(fos
, "");
116 catch (IOException ex
) {
117 fail(ex
.getMessage());
119 properties
= new Properties();
120 properties
.setProperty("testbean.current_dir", "current dir again");
121 properties
.setProperty("testbean.user_home", "user home dir");
122 File fileUserHome
= new File(System
.getProperty("user.home"),
123 "test-config.properties");
125 FileOutputStream fos
= new FileOutputStream(fileUserHome
);
126 properties
.store(fos
, "");
129 catch (IOException ex
) {
130 fail(ex
.getMessage());
133 assertEquals("default", bean
.getPropertyDefault());
134 assertEquals("classpath", bean
.getPropertyClassPath());
135 assertEquals("current dir again", bean
.getPropertyCurrentDir());
136 assertEquals("user home dir", bean
.getPropertyUserHome());
137 fileCurrentDir
.delete();
138 fileUserHome
.delete();
141 public void testBean2() {
143 assertTrue(bean
.getPropertyDefault().endsWith("2"));
144 assertTrue(bean
.getPropertyClassPath().endsWith("2"));
145 assertTrue(bean
.getPropertyCurrentDir().endsWith("2"));
146 assertTrue(bean
.getPropertyUserHome().endsWith("2"));
149 public void testDefaultResourceSuffix() {
151 assertEquals("default", bean
.getPropertyDefault());
152 assertEquals("classpath", bean
.getPropertyClassPath());
153 assertEquals("current dir...sample", bean
.getPropertyCurrentDir());
154 assertEquals("user home dir...sample", bean
.getPropertyUserHome());
157 public void testContextAndPath() {
159 assertEquals("default", bean
.getPropertyDefault());
160 assertEquals("classpath", bean
.getPropertyClassPath());
161 assertEquals("current dir 3", bean
.getPropertyCurrentDir());
162 assertEquals("user home dir 3", bean
.getPropertyUserHome());
165 public void testAggregator() {
166 TestBeanDummyAggregatorLevel1 aggregator
= new TestBeanDummyAggregatorLevel1();
168 BeanFactoryRegistrar
.aggregate(aggregator
);
169 assertNull(aggregator
.getTestBeanN());
170 assertNull(aggregator
.getTestBeanNull());
171 assertEquals(applicationContext
.getBean("testBean"), aggregator
.getTestBean1());
172 assertEquals(applicationContext
.getBean("testBean2"), aggregator
.getTestBean2());
173 assertEquals(applicationContext
.getBean("testBean3"), aggregator
.getTestBean3());
176 private void getBean()
177 throws BeansException
{
178 initPaths(new StringBuilder());
180 new ClassPathXmlApplicationContext("test-app-context.xml");
182 (TestBean
) applicationContext
.getBean("testBean");
185 private void getBean2()
186 throws BeansException
{
187 StringBuilder paths
= new StringBuilder();
190 new ClassPathXmlApplicationContext("test-app-context.xml");
192 (TestBean
) applicationContext
.getBean("testBean2");
195 private void getBean3()
196 throws BeansException
{
200 private void getBean3(boolean createBean3Rsrc
) {
201 StringBuilder paths
= new StringBuilder();
202 initPaths(paths
, createBean3Rsrc
);
204 new ClassPathXmlApplicationContext("test-app-context.xml");
206 (TestBean
) applicationContext
.getBean("testBean3");
209 private void initPaths(StringBuilder paths
) {
210 initPaths(paths
, false);
213 private void initPaths(StringBuilder paths
,
214 boolean createBean3Rsrc
) {
215 String path
= "./target/a/";
216 File dir
= new File(path
);
220 Properties properties
= new Properties();
221 properties
.setProperty("testbean.default", "default 2");
222 File fileCurrentDir
= new File(dir
, "test-config-custom.properties");
224 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
225 properties
.store(fos
, "");
228 catch (IOException ex
) {
229 fail(ex
.getMessage());
232 path
= "./target/b/";
233 dir
= new File(path
);
237 properties
= new Properties();
238 properties
.setProperty("testbean.cp", "cp 2");
239 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
241 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
242 properties
.store(fos
, "");
245 catch (IOException ex
) {
246 fail(ex
.getMessage());
250 path
= "./target/c/";
251 dir
= new File(path
);
255 properties
= new Properties();
256 properties
.setProperty("testbean.current_dir", "current dir");
257 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
259 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
260 properties
.store(fos
, "");
263 catch (IOException ex
) {
264 fail(ex
.getMessage());
268 path
= "./target/d/";
269 dir
= new File(path
);
273 properties
= new Properties();
274 properties
.setProperty("testbean.current_dir", "current dir 2");
275 properties
.setProperty("testbean.user_home", "user home dir 2");
276 fileCurrentDir
= new File(dir
, "test-config-custom.properties");
278 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
279 properties
.store(fos
, "");
282 catch (IOException ex
) {
283 fail(ex
.getMessage());
285 if (createBean3Rsrc
) {
286 properties
= new Properties();
287 properties
.setProperty("testbean.current_dir", "current dir 3");
288 properties
.setProperty("testbean.user_home", "user home dir 3");
289 dir
= new File(dir
, "custom-context/custom-path/");
291 fileCurrentDir
= new File(dir
, "test-config.properties");
293 FileOutputStream fos
= new FileOutputStream(fileCurrentDir
);
294 properties
.store(fos
, "");
297 catch (IOException ex
) {
298 fail(ex
.getMessage());