Fix test to run in just checkout
[smart-util.git] / smart-bean-spring-util / src / test / java / com / smartitengineering / util / bean / spring / PropertiesLocatorConfigurerTest.java
blob8c4a57229dfb45080408ada547e404ae375669fc
1 /*
2 * This is a utility project for wide range of applications
3 *
4 * Copyright (C) 8 Imran M Yousuf (imyousuf@smartitengineering.com)
5 *
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;
21 import java.io.File;
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;
32 /**
33 * Unit test for simple App.
35 public class PropertiesLocatorConfigurerTest
36 extends TestCase {
38 private ApplicationContext applicationContext;
39 private TestBean bean;
41 /**
42 * Create the test case
44 * @param testName name of the test case
46 public PropertiesLocatorConfigurerTest(String testName) {
47 super(testName);
50 /**
51 * @return the suite of tests being tested
53 public static Test suite() {
54 return new TestSuite(PropertiesLocatorConfigurerTest.class);
57 public void testBeanClasspath() {
58 getBean();
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");
70 try {
71 FileOutputStream fos = new FileOutputStream(file);
72 properties.store(fos, "");
73 fos.close();
75 catch (IOException ex) {
76 fail(ex.getMessage());
78 getBean();
79 assertEquals("default", bean.getPropertyDefault());
80 assertEquals("classpath", bean.getPropertyClassPath());
81 assertEquals("current dir", bean.getPropertyCurrentDir());
82 assertEquals("user home dir...sample", bean.getPropertyUserHome());
83 file.delete();
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");
91 try {
92 FileOutputStream fos = new FileOutputStream(file);
93 properties.store(fos, "");
94 fos.close();
96 catch (IOException ex) {
97 fail(ex.getMessage());
99 getBean();
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");
111 try {
112 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
113 properties.store(fos, "");
114 fos.close();
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");
124 try {
125 FileOutputStream fos = new FileOutputStream(fileUserHome);
126 properties.store(fos, "");
127 fos.close();
129 catch (IOException ex) {
130 fail(ex.getMessage());
132 getBean();
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() {
142 getBean2();
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() {
150 getBean3();
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() {
158 getBean3(true);
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();
167 getBean3();
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());
179 applicationContext =
180 new ClassPathXmlApplicationContext("test-app-context.xml");
181 bean =
182 (TestBean) applicationContext.getBean("testBean");
185 private void getBean2()
186 throws BeansException {
187 StringBuilder paths = new StringBuilder();
188 initPaths(paths);
189 applicationContext =
190 new ClassPathXmlApplicationContext("test-app-context.xml");
191 bean =
192 (TestBean) applicationContext.getBean("testBean2");
195 private void getBean3()
196 throws BeansException {
197 getBean3(false);
200 private void getBean3(boolean createBean3Rsrc) {
201 StringBuilder paths = new StringBuilder();
202 initPaths(paths, createBean3Rsrc);
203 applicationContext =
204 new ClassPathXmlApplicationContext("test-app-context.xml");
205 bean =
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);
217 if (!dir.exists()) {
218 dir.mkdirs();
220 Properties properties = new Properties();
221 properties.setProperty("testbean.default", "default 2");
222 File fileCurrentDir = new File(dir, "test-config-custom.properties");
223 try {
224 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
225 properties.store(fos, "");
226 fos.close();
228 catch (IOException ex) {
229 fail(ex.getMessage());
231 paths.append(path);
232 path = "./target/b/";
233 dir = new File(path);
234 if (!dir.exists()) {
235 dir.mkdirs();
237 properties = new Properties();
238 properties.setProperty("testbean.cp", "cp 2");
239 fileCurrentDir = new File(dir, "test-config-custom.properties");
240 try {
241 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
242 properties.store(fos, "");
243 fos.close();
245 catch (IOException ex) {
246 fail(ex.getMessage());
248 paths.append(',');
249 paths.append(path);
250 path = "./target/c/";
251 dir = new File(path);
252 if (!dir.exists()) {
253 dir.mkdirs();
255 properties = new Properties();
256 properties.setProperty("testbean.current_dir", "current dir");
257 fileCurrentDir = new File(dir, "test-config-custom.properties");
258 try {
259 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
260 properties.store(fos, "");
261 fos.close();
263 catch (IOException ex) {
264 fail(ex.getMessage());
266 paths.append(',');
267 paths.append(path);
268 path = "./target/d/";
269 dir = new File(path);
270 if (!dir.exists()) {
271 dir.mkdirs();
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");
277 try {
278 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
279 properties.store(fos, "");
280 fos.close();
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/");
290 dir.mkdirs();
291 fileCurrentDir = new File(dir, "test-config.properties");
292 try {
293 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
294 properties.store(fos, "");
295 fos.close();
297 catch (IOException ex) {
298 fail(ex.getMessage());
301 paths.append(',');
302 paths.append(path);