KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > TestUtils


1 /**
2  * $RCSfile: TestUtils.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2004/11/11 07:13:20 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  */

8
9 package org.jivesoftware.util;
10
11 import java.io.File JavaDoc;
12 import java.io.BufferedReader JavaDoc;
13 import java.io.FileReader JavaDoc;
14
15 /**
16  * A collection of utilities for test writers. <p>
17  *
18  * File methods:
19  *
20  * <ul><li>{@link #createTempFile()}</li>
21  * <li>{@link #createTempFile(String, String)}</li>
22  * <li>{@link #getAsString(java.io.File)}</li></ul>
23  */

24 public class TestUtils {
25
26     /**
27      * Creates a temp file.
28      * @see java.io.File#createTempFile(String, String)
29      */

30     public static File JavaDoc createTempFile() throws Exception JavaDoc {
31         return createTempFile("test", ".test");
32     }
33
34     /**
35      * Creates a temp file with the given filename suffix and prefix.
36      * @see java.io.File#createTempFile(String, String)
37      */

38     public static File JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix) throws Exception JavaDoc {
39         return File.createTempFile(prefix, suffix);
40     }
41
42     /**
43      * Returns the contents of the given file as a String.
44      */

45     public static String JavaDoc getAsString(File JavaDoc file) throws Exception JavaDoc {
46         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
47         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
48         String JavaDoc lineSeparator = System.getProperty("line.separator");
49         if (lineSeparator == null) {
50             lineSeparator = "\n";
51         }
52         String JavaDoc line = null;
53         while ((line=in.readLine()) != null) {
54             xml.append(line).append(lineSeparator);
55         }
56         in.close();
57         return xml.toString();
58     }
59
60     public static String JavaDoc prepareFilename(String JavaDoc filename) {
61         return filename.replace('/', File.separatorChar);
62     }
63 }
64
65
Popular Tags