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