1 /* ***************************************************************************
2 * Copyright (c) 2012 Brabenetz Harald, Austria.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 *****************************************************************************/
17
18 package org.settings4j.test;
19
20 import java.io.File;
21 import java.net.URL;
22
23 import org.settings4j.Settings4j;
24 import org.settings4j.Settings4jRepository;
25 import org.settings4j.config.DOMConfigurator;
26 import org.settings4j.contentresolver.ClasspathContentResolver;
27 import org.settings4j.settings.DefaultSettingsRepository;
28
29
30 /**
31 * @author brabenetz
32 */
33 public final class TestUtils {
34
35 /** hide default constructor (utility pattern). */
36 private TestUtils() {
37 super();
38 }
39
40 /**
41 * reload org/settings4j/config/defaultsettings4j.xml.
42 */
43 public static void reconfigureSettings4jWithDefaultConfig() {
44 // settings4j will be configured with the default-fallback-config if no connector exists:
45 // org/settings4j/config/defaultsettings4j.xml
46 Settings4j.getSettingsRepository().getSettings().removeAllConnectors();
47 Settings4j.getString("something"); // reconfigure Settings4j with default values
48 }
49
50 /**
51 * Read and parse a Settings4j.xml Configuration from Classpath and return the SettingsRepository.
52 *
53 * @param classpathUrl The Classpath url where the settings4j.xml are placed.
54 * @return The SettingsRepository based on the given configuration found in the classpathUrl
55 */
56 public static Settings4jRepository getConfiguredSettingsRepository(final String classpathUrl) {
57
58 final URL url = ClasspathContentResolver.getResource(classpathUrl);
59 final Settings4jRepository settingsRepository = new DefaultSettingsRepository();
60 DOMConfigurator.configure(url, settingsRepository);
61 return settingsRepository;
62 }
63
64 /**
65 * The TempFolder from System Property "java.io.tmpdir" and the subfolder "Settings4jUnittest".<br />
66 * <br />
67 * On my windows maschine this is:<br />
68 * C:\Dokumente und Einstellungen\hbrabenetz\Lokale Einstellungen\Temp\Settings4jUnittest\<br />
69 *
70 * @return to tempfolder for unittests
71 */
72 public static File getTmpFolder() {
73 final String tmpdir = System.getProperty("java.io.tmpdir");
74 final File tmpFolder = new File(tmpdir + "/Settings4jUnittest");
75 return tmpFolder;
76 }
77
78 /**
79 * The test Folder for Unittest.<br />
80 * This is normaly a subfolder "target/test/Settings4jUnittest" of this current Project.
81 *
82 * @return the subfolder "target/test/Settings4jUnittest" of the current Project.
83 */
84 public static File getTestFolder() {
85 final File testFolder = new File("target/test/Settings4jUnittest");
86 return testFolder;
87 }
88
89 }