01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.boundary;
03:
04: import java.io.File;
05: import java.io.IOException;
06: import java.net.URL;
07:
08: import org.apache.commons.io.FileUtils;
09:
10: /**
11: * Helper class for working with files.
12: * @author Paul King
13: * @author Marc Guillemot
14: */
15: public final class FileBoundary {
16: private FileBoundary() {
17: }
18:
19: /**
20: * Helper method when creating files.
21: *
22: * @param filename the filename of the resource to get
23: * @param relativeClass the class whose package contains the resource
24: * @return the File if it exists
25: * @throws java.lang.IllegalStateException if the file could not be found
26: */
27: public static File getFile(final String filename,
28: final Class relativeClass) {
29: final URL url = relativeClass.getResource(filename);
30: if (url == null) {
31: // only used for getting resources within test code
32: throw new IllegalStateException(
33: "Could not find resource file '" + filename + "'");
34: }
35: return new File(url.getFile());
36: }
37:
38: /**
39: * Gets the bytes of the file
40: * @param file the file to read
41: * @return the file content
42: * @throws RuntimeException if the content can't be read
43: */
44: public static byte[] getBytes(final File file) {
45: try {
46: return FileUtils.readFileToByteArray(file);
47: } catch (final IOException e) {
48: throw new RuntimeException(e);
49: }
50: }
51: }
|