01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.test;
18:
19: import java.io.File;
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.net.URLClassLoader;
23:
24: /**
25: * Various test utilities are provided as static methods of this class.
26: *
27: * @author Alexey V. Varlamov
28: * @version $Revision$
29: */
30: public class TestResources {
31:
32: /**
33: * Name of system property specifying URL or filepath of
34: * isolated bundle with test resources.
35: *
36: * @see #getLoader()
37: */
38: public static final String RESOURCE_PATH = "test.resource.path";
39:
40: private static ClassLoader loader;
41:
42: /**
43: * Certain tests may require existence of isolated test resources -
44: * i.e. some resources not available via system (caller's) loader.
45: * This method is intended to support such resources.
46: * @see #RESOURCE_PATH
47: * @return a classloader which is aware of location of isolated resources
48: */
49: public static ClassLoader getLoader() {
50: if (loader == null) {
51: loader = createLoader();
52: }
53: return loader;
54: }
55:
56: public static ClassLoader createLoader() {
57: URL url = null;
58: try {
59: String path = System.getProperty(RESOURCE_PATH, ".");
60: File f = new File(path);
61: if (f.exists()) {
62: url = f.toURI().toURL();
63: } else {
64: url = new URL(path);
65: }
66: } catch (MalformedURLException e) {
67: throw new RuntimeException(
68: "Misconfigured path to test resources. "
69: + "Please set correct value of system property: "
70: + RESOURCE_PATH, e);
71: }
72: return URLClassLoader.newInstance(new URL[] { url });
73: }
74: }
|