TestUtils.java 1.68 KB
Newer Older
Bill Lynch's avatar
Bill Lynch committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 */

package org.jivesoftware.util;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * A collection of utilities for test writers. <p>
 *
 * File methods:
 *
 *  <ul><li>{@link #createTempFile()}</li>
 *      <li>{@link #createTempFile(String, String)}</li>
 *      <li>{@link #getAsString(java.io.File)}</li></ul>
 */
public class TestUtils {

    /**
     * Creates a temp file.
     * @see java.io.File#createTempFile(String, String)
     */
    public static File createTempFile() throws Exception {
        return createTempFile("test", ".test");
    }

    /**
     * Creates a temp file with the given filename suffix and prefix.
     * @see java.io.File#createTempFile(String, String)
     */
    public static File createTempFile(String prefix, String suffix) throws Exception {
        return File.createTempFile(prefix, suffix);
    }

    /**
     * Returns the contents of the given file as a String.
     */
    public static String getAsString(File file) throws Exception {
        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuffer xml = new StringBuffer();
        String lineSeparator = System.getProperty("line.separator");
        if (lineSeparator == null) {
            lineSeparator = "\n";
        }
        String line = null;
        while ((line=in.readLine()) != null) {
            xml.append(line).append(lineSeparator);
        }
        in.close();
        return xml.toString();
    }

    public static String prepareFilename(String filename) {
        return filename.replace('/', File.separatorChar);
    }
}