1 /*
2 * Copyright (c) 2001 by
3 * Siegfried GOESCHL <mailto:siegfried.goeschl@itserv.at>
4 * and Dima STADNIK <mailto:5d5@mail.ru>
5 *
6 * This program is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the GNU
9 * General Public License as published by the Free Software Foundation.
10 * Version 2 of the license should be included with this distribution in
11 * the file LICENSE, as well as License.html. If the license is not
12 * included with this distribution, you may find a copy at the FSF web
13 * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
14 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
15 *
16 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
17 * NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
18 * OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
19 * CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
20 * REDISTRIBUTION OF THIS SOFTWARE.
21 */
22
23 package junit.extensions.conf;
24
25
26 import java.io.*;
27 import java.util.ArrayList;
28
29
30 /***
31 * Factory for creating configurations.
32 *
33 * @author Siegfried GOESCHL
34 * @author Dima STADNIK
35 */
36 public class ConfigurationFactory {
37
38 /*** Defaults for finding the configuration files */
39 public static final String[] DEFAULT_PATH_LIST = {
40 "src",
41 "src/java",
42 "src/test",
43 "src/test/java"
44 };
45
46 /*** Property key that defines loadable configuration implementation class name. */
47 public static final String LOADER = "junit.conf.class";
48
49 /*** Property key that defines configuration file or path to configuration files. */
50 public static final String HINT = "junit.conf";
51
52 /*** Extension of configuration files. */
53 public static final String EXTENSION = "ini";
54
55 /*** The only instance of configuration factory. */
56 protected static ConfigurationFactory factory = null;
57
58 /*** Configuration created by factory. */
59 protected LoadableConfiguration conf = null;
60
61 /***
62 * We will create instances by ourselves.
63 */
64 private ConfigurationFactory() {}
65
66 /***
67 * Returns factory instance.
68 *
69 * @return Factory instance.
70 */
71 public static ConfigurationFactory getFactory() {
72 return factory;
73 }
74
75 /***
76 * Returns current configuration.
77 *
78 * @return Configuration instance.
79 */
80 public Configuration getConfiguration() {
81 return conf;
82 }
83
84 /***
85 * Explicit initialization of factory.
86 *
87 * @param base Class that is used as base to find configuration.
88 */
89 public static synchronized void init(Class base) {
90 if (factory == null) {
91 factory = new ConfigurationFactory();
92 try {
93 factory.load(base);
94 } catch (Exception e) {
95 System.err.println("Unable to initialize configuration factory: " + e.getMessage());
96 throw new RuntimeException(
97 "Unable to initialize test data repository due to : "
98 + e.getMessage()
99 );
100 }
101 }
102 }
103
104 /***
105 * Loads configuration into factory.
106 *
107 * @param base Class that is used as base to find configuration.
108 */
109 protected void load(Class base) throws IOException {
110
111 String hint = System.getProperty(HINT);
112
113 // If an explicit hints exists then take that one - otherwise
114 // use the classpath as a list of directories to find the
115 // configuration file
116
117 if (hint == null || hint.length() == 0) {
118 String[] pathList = createPathList();
119
120 conf = getConfiguration(pathList, getFileName(base));
121 } else {
122 File file = new File(hint);
123
124 if (!file.exists()) {
125 System.out.println("Unable to initialize configuration factory using " + hint);
126 throw new RuntimeException(
127 "Unable to initialize test data repository"
128 );
129 }
130 if (file.isDirectory()) {
131 String[] pathList = { hint };
132
133 conf = getConfiguration(pathList, getFileName(base));
134 } else {
135 conf = getConfiguration(null, file.getAbsolutePath());
136 }
137 }
138 }
139
140 /***
141 * Generates configuration file name based on class name.
142 *
143 * @param base Reference class.
144 * @return File name.
145 */
146 protected String getFileName(Class base) {
147 return base.getName().replace('.', '/') + '.' + EXTENSION;
148 }
149
150 /***
151 * Factory method for instantiating and populating configuration.
152 *
153 * @param path Search path for configuration files.
154 * @param fileName Name of configuration file.
155 */
156 protected LoadableConfiguration getConfiguration(String[] path, String fileName) throws IOException {
157 LoadableConfiguration conf = null;
158 String confClassName = System.getProperty(LOADER, FileConfiguration.class.getName());
159
160 System.out.println("Using configuration " + confClassName);
161 try {
162 conf = (LoadableConfiguration) Class.forName(confClassName).newInstance();
163 } catch (Exception e) {
164 System.err.println("Unable to instantiate configuration: " + e.getMessage());
165 throw new RuntimeException(
166 "Unable to initialize test data repository due to : "
167 + e.getMessage()
168 );
169 }
170 try {
171 conf.load(path, fileName);
172 } catch (Exception e) {
173 System.err.println("Unable to populate configuration: " + e.getMessage());
174 throw new RuntimeException(
175 "Unable to initialize test data repository due to : "
176 + e.getMessage()
177 );
178 }
179 return conf;
180 }
181
182 /***
183 * Create a list of paths where the configuration files could be found
184 */
185
186 private String[] createPathList() {
187 File currDir = null;
188 ArrayList result = new ArrayList();
189
190 // add the current directory
191 result.add(".");
192
193 // add a couple of default directories
194 for (int i = 0; i < DEFAULT_PATH_LIST.length; i++) {
195 currDir = new File(DEFAULT_PATH_LIST[i]);
196 if (currDir.exists() && currDir.isDirectory()) {
197 result.add(currDir.getAbsolutePath());
198 }
199 }
200
201 return (String[]) result.toArray(new String[result.size()]);
202 }
203 }
This page was automatically generated by Maven