1 package wjhk.jupload2.testhelpers;
2
3 import java.awt.Cursor;
4 import java.awt.Frame;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Properties;
9 import java.util.Vector;
10
11 import javax.swing.JApplet;
12
13 import org.apache.log4j.Logger;
14
15 import wjhk.jupload2.context.JUploadContext;
16 import wjhk.jupload2.exception.JUploadException;
17 import wjhk.jupload2.gui.JUploadPanel;
18 import wjhk.jupload2.gui.JUploadTextArea;
19 import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
20 import wjhk.jupload2.policies.UploadPolicy;
21
22
23
24
25
26
27
28 public class JUploadContextTestHelper implements JUploadContext {
29
30
31 public static String TEST_CASE_MIME_TYPE = "X-jupload/noFileTypeIn_JUploadContextTestCase";
32
33
34 protected final Logger logger = Logger.getLogger(getClass());
35
36
37 public UploadPolicy uploadPolicy = null;
38
39
40 public JUploadPanel juploadPanel = null;
41
42
43 public JUploadTextArea logWindow = null;
44
45
46
47
48
49 public final static String TEST_ROOT_FOLDER = "tests/";
50
51
52
53
54 public final static String TEST_PROPERTIES_FOLDER = TEST_ROOT_FOLDER + "policies/";
55
56
57
58
59 public final static String TEST_FILES_FOLDER = TEST_ROOT_FOLDER + "files/";
60
61
62
63
64
65 public final static String TEST_DEFAULT_PROPERTIES_FILE = TEST_PROPERTIES_FOLDER
66 + "default_uploadPolicy.properties";
67
68
69
70
71 public Object lastRegisterUnloadObject = null;
72
73
74
75
76 public String lastRegisterUnloadMethod = null;
77
78
79
80
81
82 public JUploadContextTestHelper(UploadPolicy uploadPolicy, JUploadPanel juploadPanel) {
83 this.uploadPolicy = uploadPolicy;
84 this.juploadPanel = juploadPanel;
85 this.logWindow = new JUploadTextArea(100, 100, uploadPolicy);
86 }
87
88
89
90
91
92
93 public JUploadContextTestHelper(JUploadPanel juploadPanel) {
94 this.juploadPanel = juploadPanel;
95 this.logWindow = new JUploadTextArea(100, 100, uploadPolicy);
96 }
97
98
99
100
101
102
103
104
105 Properties loadPropertiesFromTestFile(String filename, Properties defaultProperties) {
106 Properties properties = new Properties(defaultProperties);
107 try {
108 InputStream isProperties = new FileInputStream(filename);
109 properties.load(isProperties);
110 isProperties.close();
111 } catch (IOException e1) {
112 System.out.println("Error while loading " + filename + " (" + e1.getClass().getName() + ")");
113 e1.printStackTrace();
114 }
115
116 return properties;
117 }
118
119 boolean getBooleanProperty(String property, boolean def) {
120 if (property.startsWith("xxx")) {
121
122
123 throw new UnsupportedOperationException(this.getClass() + ".getBooleanProperty() unknown property: '"
124 + property + "'");
125 } else {
126 return parseBoolean(property, def);
127 }
128 }
129
130
131
132
133
134
135
136
137 public void displayURL(String url, boolean success) {
138 throw new UnsupportedOperationException(this.getClass() + ".displayURL() is not implemented in tests cases");
139 }
140
141
142
143
144 public JApplet getApplet() {
145 throw new UnsupportedOperationException(this.getClass() + ".getApplet() is not implemented in tests cases");
146 }
147
148
149
150
151 public String getBuildDate() {
152 return "no build date for TestCase";
153 }
154
155
156
157
158 public int getBuildNumber() {
159 throw new UnsupportedOperationException(this.getClass() + ".getBuildNumber() is not implemented in tests cases");
160 }
161
162
163
164
165 public Cursor getCursor() {
166 throw new UnsupportedOperationException(this.getClass() + ".getCursor() is not implemented in tests cases");
167 }
168
169
170
171
172 public String getLastModified() {
173 throw new UnsupportedOperationException(this.getClass()
174 + ".getLastModified() is not implemented in tests cases");
175 }
176
177
178
179
180 public JUploadTextArea getLogWindow() {
181 return this.logWindow;
182 }
183
184
185
186
187 public String getMimeType(final String fileExtension) {
188 return TEST_CASE_MIME_TYPE;
189 }
190
191
192
193
194
195
196
197
198
199
200 public String getParameter(String key, String def) {
201 String paramStr = (System.getProperty(key) != null ? System.getProperty(key) : def);
202 return paramStr;
203 }
204
205
206
207
208
209
210 public int getParameter(String key, int def) {
211 String paramDef = Integer.toString(def);
212 String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
213 return parseInt(paramStr, def);
214 }
215
216
217
218
219
220
221 public float getParameter(String key, float def) {
222 String paramDef = Float.toString(def);
223 String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
224 return parseFloat(paramStr, def);
225 }
226
227
228
229
230
231
232 public long getParameter(String key, long def) {
233 String paramDef = Long.toString(def);
234 String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
235 return parseLong(paramStr, def);
236 }
237
238
239
240
241
242
243
244 public boolean getParameter(String key, boolean def) {
245 String paramDef = (def ? "true" : "false");
246 String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
247 return parseBoolean(paramStr, def);
248 }
249
250
251 public FileListViewMode getParameter(String key, FileListViewMode def) {
252 String paramDef = def.toString();
253 String paramStr = System.getProperty(key) != null ? System.getProperty(key) : paramDef;
254 return parseFileListViewMode(paramStr, def);
255 }
256
257
258
259
260 public JUploadPanel getUploadPanel() {
261 return this.juploadPanel;
262 }
263
264
265
266
267 public UploadPolicy getUploadPolicy() {
268 return this.uploadPolicy;
269 }
270
271
272
273
274 public String getDetailedVersionMessage() {
275 return "TestCase (detailed message)";
276 }
277
278
279
280
281 public String getVersion() {
282 return "TestCase version";
283 }
284
285
286
287
288 public String getSvnRevision() {
289 return "TestCase svn revision";
290 }
291
292
293
294
295 public String normalizeURL(String url) throws JUploadException {
296 return url;
297 }
298
299
300 public int parseInt(String value, int def) {
301 int ret = def;
302
303 try {
304 ret = Integer.parseInt(value);
305 } catch (NumberFormatException e) {
306 ret = def;
307 this.logger.warn("Invalid int value: " + value + ", using default value: " + def);
308
309 }
310 return ret;
311 }
312
313
314 public float parseFloat(String value, float def) {
315 float ret = def;
316
317 try {
318 ret = Float.parseFloat(value);
319 } catch (NumberFormatException e) {
320 ret = def;
321 this.logger.warn("Invalid float value: " + value + ", using default value: " + def);
322 }
323 return ret;
324 }
325
326
327 public long parseLong(String value, long def) {
328 long ret = def;
329
330 try {
331 ret = Long.parseLong(value);
332 } catch (NumberFormatException e) {
333 ret = def;
334 this.logger.warn("Invalid long value: " + value + ", using default value: " + def);
335 }
336 return ret;
337 }
338
339
340 public boolean parseBoolean(String value, boolean def) {
341
342 if (value.toUpperCase().equals("FALSE")) {
343 return false;
344 } else if (value.toUpperCase().equals("TRUE")) {
345 return true;
346 } else {
347 this.logger.warn("Invalid boolean value: " + value + ", using default value: " + def);
348 }
349 return def;
350 }
351
352
353 public FileListViewMode parseFileListViewMode(String value, FileListViewMode def) {
354 FileListViewMode ret = def;
355 try {
356 ret = FileListViewMode.valueOf(value);
357 } catch (IllegalArgumentException e) {
358 this.logger.warn("Invalid FileListViewMode value: " + value + ", using default value: " + def);
359 }
360 return ret;
361 }
362
363
364
365
366 public void readCookieFromNavigator(Vector<String> headers) {
367 this.logger.warn(this.getClass() + ".readCookieFromNavigator() is not implemented in tests cases");
368 }
369
370
371
372
373 public void readUserAgentFromNavigator(Vector<String> headers) {
374 this.logger.warn(this.getClass() + ".readUserAgentFromNavigator() is not implemented in tests cases");
375 }
376
377
378
379
380 public void registerUnload(Object object, String method) {
381 this.lastRegisterUnloadObject = object;
382 this.lastRegisterUnloadMethod = method;
383 this.logger.warn(this.getClass() + ".registerUnload() is not implemented in tests cases");
384 }
385
386
387
388
389 public void runUnload() {
390 throw new UnsupportedOperationException(this.getClass() + ".runUnload() is not implemented in tests cases");
391 }
392
393
394
395
396 public Cursor setCursor(Cursor cursor) {
397 throw new UnsupportedOperationException(this.getClass() + ".setCursor() is not implemented in tests cases");
398 }
399
400
401
402
403 public void setProperty(String prop, String value) {
404 throw new UnsupportedOperationException(this.getClass() + ".setProperty() is not implemented in tests cases");
405 }
406
407
408
409
410 public Cursor setWaitCursor() {
411 throw new UnsupportedOperationException(this.getClass() + ".setWaitCursor() is not implemented in tests cases");
412 }
413
414
415
416
417 public void showStatus(String status) {
418 this.logger.warn(this.getClass() + ".showStatus() is not implemented in tests cases");
419 }
420
421
422
423
424 public String startUpload() {
425 throw new UnsupportedOperationException(this.getClass() + ".startUpload() is not implemented in tests cases");
426 }
427
428
429 public Frame getFrame() {
430 throw new UnsupportedOperationException(this.getClass() + ".getFrame() is not implemented in tests cases");
431 }
432
433 }