1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.apache.commons.vfs2.util.Messages;
27
28 import junit.framework.TestCase;
29
30
31
32
33 public abstract class AbstractVfsTestCase extends TestCase {
34
35 private static File baseDir;
36
37
38 private static final Pattern URL_PATTERN = Pattern.compile("[a-z]+://.*");
39
40
41 private static final Pattern PASSWORD_PATTERN = Pattern.compile(":(?:[^/]+)@");
42
43
44
45
46 public static void assertSameMessage(final String code, final Object param, final Throwable throwable) {
47 assertSameMessage(code, new Object[] { param }, throwable);
48 }
49
50
51
52
53 private static void assertSameMessage(final String code, final Object[] params, final Throwable throwable) {
54 Object[] parmArray = params;
55 if (throwable instanceof FileSystemException) {
56 final FileSystemException fse = (FileSystemException) throwable;
57
58
59 assertEquals(code, fse.getCode());
60 assertEquals(params.length, fse.getInfo().length);
61 parmArray = new Object[params.length];
62 for (int i = 0; i < params.length; i++) {
63 String value = String.valueOf(params[i]);
64
65 final Matcher urlMatcher = URL_PATTERN.matcher(value);
66 if (urlMatcher.find()) {
67 final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(value);
68 value = pwdMatcher.replaceFirst(":***@");
69 }
70 assertEquals(value, fse.getInfo()[i]);
71 parmArray[i] = value;
72 }
73 }
74
75
76 final String message = Messages.getString(code, parmArray);
77 assertEquals(message, throwable.getMessage());
78 }
79
80
81
82
83 public static boolean equals(final Object o1, final Object o2) {
84 if (o1 == null && o2 == null) {
85 return true;
86 }
87 if (o1 == null || o2 == null) {
88 return false;
89 }
90 return o1.equals(o2);
91 }
92
93
94
95
96 public static File getCanonicalFile(final File file) {
97 try {
98 return file.getCanonicalFile();
99 } catch (final IOException e) {
100 return file.getAbsoluteFile();
101 }
102 }
103
104 public static String getResourceTestDirectory() {
105 return System.getProperty("test.basedir.res", "test-data");
106 }
107
108
109
110
111
112
113
114
115 public static String getTestDirectory() {
116 return System.getProperty("test.basedir", "target/test-classes/test-data");
117 }
118
119
120
121
122
123
124 public static File getTestDirectory(final String name) {
125 File file = new File(getTestDirectoryFile(), name);
126 file = getCanonicalFile(file);
127 assertTrue("Test directory \"" + file + "\" does not exist or is not a directory.",
128 file.isDirectory() || file.mkdirs());
129 return file;
130 }
131
132
133
134
135 public static File getTestDirectoryFile() {
136 if (baseDir == null) {
137 final String baseDirProp = getTestDirectory();
138
139 if (baseDirProp.startsWith("file://")) {
140 try {
141 baseDir = getCanonicalFile(new File(new URI(baseDirProp)));
142 } catch (final URISyntaxException e) {
143 baseDir = getCanonicalFile(new File(baseDirProp));
144 }
145 } else {
146 baseDir = getCanonicalFile(new File(baseDirProp));
147 }
148 }
149 return baseDir;
150 }
151
152
153
154
155
156
157 public static File getTestResource(final String name) {
158 return getTestResource(name, true);
159 }
160
161
162
163
164
165
166 public static File getTestResource(final String name, final boolean mustExist) {
167 File file = new File(getTestDirectoryFile(), name);
168 file = getCanonicalFile(file);
169 if (mustExist) {
170 assertTrue("Test file \"" + file + "\" does not exist.", file.exists());
171 } else {
172 assertFalse("Test file \"" + file + "\" should not exist.", file.exists());
173 }
174
175 return file;
176 }
177 }