01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.db4ounit.util;
22:
23: import java.io.*;
24: import java.net.*;
25:
26: import com.db4o.foundation.io.*;
27:
28: import db4ounit.*;
29:
30: /**
31: * @sharpen.ignore
32: */
33: public class WorkspaceServices {
34:
35: public static String workspacePath(String fname) {
36: return Path4.combine(workspaceRoot(), fname);
37: }
38:
39: public static String workspaceTestFilePath(String fname) {
40: return Path4.combine(WorkspaceLocations.TEST_FOLDER, fname);
41: }
42:
43: /**
44: * @sharpen.property
45: */
46: public static String workspaceRoot() {
47: String property = System.getProperty("dir.workspace");
48: if (property != null) {
49: return property;
50: }
51: return findFolderWithChild(
52: pathToClass(WorkspaceServices.class), "db4oj.tests");
53: }
54:
55: static String pathToClass(Class clazz) {
56: final URL resource = clazz.getResource("/"
57: + clazz.getName().replace('.', '/') + ".class");
58: return new File(resource.getFile()).getParent();
59: }
60:
61: static String findFolderWithChild(String baseFolder,
62: String folderChild) {
63:
64: File test = new File(baseFolder, folderChild);
65: if (test.exists())
66: return test.getParent();
67:
68: if (getParentFile(test) == null)
69: return null;
70:
71: // we should test against root folder... :)
72: return findFolderWithChild(getParentFile(test).getParent(),
73: folderChild);
74: }
75:
76: private static File getParentFile(File file) {
77: String path = file.getParent();
78: if (path == null) {
79: return null;
80: }
81: return new File(path);
82: }
83:
84: public static File configurableWorkspacePath(
85: String configurableProperty, String defaultWorkspacePath) {
86: final String path = System.getProperty(configurableProperty,
87: workspacePath(defaultWorkspacePath));
88: final File file = new File(IOServices.safeCanonicalPath(path));
89: Assert.isTrue(file.exists(), path);
90: return file;
91: }
92:
93: }
|