01: /**
02: * $RCSfile$
03: * $Revision: 317 $
04: * $Date: 2004-11-10 23:13:20 -0800 (Wed, 10 Nov 2004) $
05: *
06: * Copyright (C) 2004 Jive Software. All rights reserved.
07: */package org.jivesoftware.util;
08:
09: import java.io.File;
10: import java.io.BufferedReader;
11: import java.io.FileReader;
12:
13: /**
14: * A collection of utilities for test writers. <p>
15: *
16: * File methods:
17: *
18: * <ul><li>{@link #createTempFile()}</li>
19: * <li>{@link #createTempFile(String, String)}</li>
20: * <li>{@link #getAsString(java.io.File)}</li></ul>
21: */
22: public class TestUtils {
23:
24: /**
25: * Creates a temp file.
26: * @see java.io.File#createTempFile(String, String)
27: */
28: public static File createTempFile() throws Exception {
29: return createTempFile("test", ".test");
30: }
31:
32: /**
33: * Creates a temp file with the given filename suffix and prefix.
34: * @see java.io.File#createTempFile(String, String)
35: */
36: public static File createTempFile(String prefix, String suffix)
37: throws Exception {
38: return File.createTempFile(prefix, suffix);
39: }
40:
41: /**
42: * Returns the contents of the given file as a String.
43: */
44: public static String getAsString(File file) throws Exception {
45: BufferedReader in = new BufferedReader(new FileReader(file));
46: StringBuffer xml = new StringBuffer();
47: String lineSeparator = System.getProperty("line.separator");
48: if (lineSeparator == null) {
49: lineSeparator = "\n";
50: }
51: String line = null;
52: while ((line = in.readLine()) != null) {
53: xml.append(line).append(lineSeparator);
54: }
55: in.close();
56: return xml.toString();
57: }
58:
59: public static String prepareFilename(String filename) {
60: return filename.replace('/', File.separatorChar);
61: }
62: }
|