Make the test case work and as comprehensive as possible
[smart-util.git] / smart-util / smart-spring-util / src / test / java / com / smartitengineering / util / spring / PropertiesLocatorConfigurerTest.java
blob010592fd763139b5a7a4630859ac3686ef7fe8bb
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.spring;
20 import java.io.File;
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;
31 /**
32 * Unit test for simple App.
34 public class PropertiesLocatorConfigurerTest
35 extends TestCase {
37 private ApplicationContext applicationContext;
38 private TestBean bean;
40 /**
41 * Create the test case
43 * @param testName name of the test case
45 public PropertiesLocatorConfigurerTest(String testName) {
46 super(testName);
49 /**
50 * @return the suite of tests being tested
52 public static Test suite() {
53 return new TestSuite(PropertiesLocatorConfigurerTest.class);
56 public void testBeanClasspath() {
57 getBean();
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");
69 try {
70 FileOutputStream fos = new FileOutputStream(file);
71 properties.store(fos, "");
72 fos.close();
74 catch (IOException ex) {
75 fail(ex.getMessage());
77 getBean();
78 assertEquals("default", bean.getPropertyDefault());
79 assertEquals("classpath", bean.getPropertyClassPath());
80 assertEquals("current dir", bean.getPropertyCurrentDir());
81 assertEquals("user home dir...sample", bean.getPropertyUserHome());
82 file.delete();
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");
90 try {
91 FileOutputStream fos = new FileOutputStream(file);
92 properties.store(fos, "");
93 fos.close();
95 catch (IOException ex) {
96 fail(ex.getMessage());
98 getBean();
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");
110 try {
111 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
112 properties.store(fos, "");
113 fos.close();
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");
123 try {
124 FileOutputStream fos = new FileOutputStream(fileUserHome);
125 properties.store(fos, "");
126 fos.close();
128 catch (IOException ex) {
129 fail(ex.getMessage());
131 getBean();
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() {
141 getBean2();
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 {
150 applicationContext =
151 new ClassPathXmlApplicationContext("test-app-context.xml");
152 bean =
153 (TestBean) applicationContext.getBean("testBean");
156 private void getBean2()
157 throws BeansException {
158 StringBuilder paths = new StringBuilder();
159 initPaths(paths);
160 applicationContext =
161 new ClassPathXmlApplicationContext("test-app-context.xml");
162 bean =
163 (TestBean) applicationContext.getBean("testBean2");
166 private void initPaths(StringBuilder paths) {
167 String path = "./target/a/";
168 File dir = new File(path);
169 if (!dir.exists()) {
170 dir.mkdirs();
172 Properties properties = new Properties();
173 properties.setProperty("testbean.default", "default 2");
174 File fileCurrentDir = new File(dir, "test-config-custom.properties");
175 try {
176 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
177 properties.store(fos, "");
178 fos.close();
180 catch (IOException ex) {
181 fail(ex.getMessage());
183 paths.append(path);
184 path = "./target/b/";
185 dir = new File(path);
186 if (!dir.exists()) {
187 dir.mkdirs();
189 properties = new Properties();
190 properties.setProperty("testbean.cp", "cp 2");
191 fileCurrentDir = new File(dir, "test-config-custom.properties");
192 try {
193 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
194 properties.store(fos, "");
195 fos.close();
197 catch (IOException ex) {
198 fail(ex.getMessage());
200 paths.append(',');
201 paths.append(path);
202 path = "./target/c/";
203 dir = new File(path);
204 if (!dir.exists()) {
205 dir.mkdirs();
207 properties = new Properties();
208 properties.setProperty("testbean.current_dir", "current dir");
209 fileCurrentDir = new File(dir, "test-config-custom.properties");
210 try {
211 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
212 properties.store(fos, "");
213 fos.close();
215 catch (IOException ex) {
216 fail(ex.getMessage());
218 paths.append(',');
219 paths.append(path);
220 path = "./target/d/";
221 dir = new File(path);
222 if (!dir.exists()) {
223 dir.mkdirs();
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");
229 try {
230 FileOutputStream fos = new FileOutputStream(fileCurrentDir);
231 properties.store(fos, "");
232 fos.close();
234 catch (IOException ex) {
235 fail(ex.getMessage());
237 paths.append(',');
238 paths.append(path);