From b5730496d3bfc3e6bc973dfa0cd4aae6716e2dc7 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 25 Feb 2010 19:57:54 +0000 Subject: [PATCH] Make createFile() static, so it can be used in @BeforeClass methods. --- .../vitro/testing/AbstractTestClass.java | 626 +++++++++--------- 1 file changed, 313 insertions(+), 313 deletions(-) diff --git a/webapp/test/edu/cornell/mannlib/vitro/testing/AbstractTestClass.java b/webapp/test/edu/cornell/mannlib/vitro/testing/AbstractTestClass.java index 846fad26c..33f00e674 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/testing/AbstractTestClass.java +++ b/webapp/test/edu/cornell/mannlib/vitro/testing/AbstractTestClass.java @@ -1,331 +1,331 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.testing; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.fail; - -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashSet; -import java.util.Properties; -import java.util.Set; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -/** - * A collection of useful routines to help when testing. - * - * - * @author jeb228 - */ -public abstract class AbstractTestClass { - +package edu.cornell.mannlib.vitro.testing; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * A collection of useful routines to help when testing. + * + * + * @author jeb228 + */ +public abstract class AbstractTestClass { + // ---------------------------------------------------------------------- // Control the level of logging output. // ---------------------------------------------------------------------- - - /** The layout we use for logging. */ - private static final PatternLayout patternLayout = new PatternLayout( - "%p %d{yyyy-MM-dd' 'HH:mm:ss.SSS} [%t] (%c{1}) %m%n"); - - /** - * Unless modified, all Logging will be done to the console at - * {@link Level#INFO}. - */ - @Before - @After - public void initializeLogging() { - LogManager.resetConfiguration(); - Logger.getRootLogger().addAppender(new ConsoleAppender(patternLayout)); - Logger.getRootLogger().setLevel(Level.INFO); - } - - /** - * Call this in a "@Before" or "@BeforeClass" method to change the logging - * level of a particular class. - */ - protected static void setLoggerLevel(Class clazz, Level level) { - Logger.getLogger(clazz).setLevel(level); - } - - /** - * Same thing, but for a logger that is not named directly after a class. - */ - protected static void setLoggerLevel(String category, Level level) { - Logger.getLogger(category).setLevel(level); - } - + + /** The layout we use for logging. */ + private static final PatternLayout patternLayout = new PatternLayout( + "%p %d{yyyy-MM-dd' 'HH:mm:ss.SSS} [%t] (%c{1}) %m%n"); + + /** + * Unless modified, all Logging will be done to the console at + * {@link Level#INFO}. + */ + @Before + @After + public void initializeLogging() { + LogManager.resetConfiguration(); + Logger.getRootLogger().addAppender(new ConsoleAppender(patternLayout)); + Logger.getRootLogger().setLevel(Level.INFO); + } + + /** + * Call this in a "@Before" or "@BeforeClass" method to change the logging + * level of a particular class. + */ + protected static void setLoggerLevel(Class clazz, Level level) { + Logger.getLogger(clazz).setLevel(level); + } + + /** + * Same thing, but for a logger that is not named directly after a class. + */ + protected static void setLoggerLevel(String category, Level level) { + Logger.getLogger(category).setLevel(level); + } + // ---------------------------------------------------------------------- // Control standard output or error output. // ---------------------------------------------------------------------- - - private static final PrintStream originalSysout = System.out; - private static final PrintStream originalSyserr = System.err; - private final ByteArrayOutputStream capturedSysout = new ByteArrayOutputStream(); - private final ByteArrayOutputStream capturedSyserr = new ByteArrayOutputStream(); - - @Before - @After - public void restoreOutputStreams() { - System.setOut(originalSysout); - System.setErr(originalSyserr); - capturedSysout.reset(); - capturedSyserr.reset(); - } - - protected void suppressSysout() { - System.setOut(new PrintStream(capturedSysout, true)); - } - - protected void suppressSyserr() { - System.setErr(new PrintStream(capturedSyserr, true)); - } - - protected String getSysoutForTest() { - return capturedSysout.toString(); - } - - protected String getSyserrForTest() { - return capturedSyserr.toString(); - } - + + private static final PrintStream originalSysout = System.out; + private static final PrintStream originalSyserr = System.err; + private final ByteArrayOutputStream capturedSysout = new ByteArrayOutputStream(); + private final ByteArrayOutputStream capturedSyserr = new ByteArrayOutputStream(); + + @Before + @After + public void restoreOutputStreams() { + System.setOut(originalSysout); + System.setErr(originalSyserr); + capturedSysout.reset(); + capturedSyserr.reset(); + } + + protected void suppressSysout() { + System.setOut(new PrintStream(capturedSysout, true)); + } + + protected void suppressSyserr() { + System.setErr(new PrintStream(capturedSyserr, true)); + } + + protected String getSysoutForTest() { + return capturedSysout.toString(); + } + + protected String getSyserrForTest() { + return capturedSyserr.toString(); + } + // ---------------------------------------------------------------------- // Set values on System properties for individual tests. // ---------------------------------------------------------------------- - - private static Properties originalSystemProperties = (Properties) System - .getProperties().clone(); - - @Before - @After - public void restoreSystemProperties() { - System.setProperties((Properties) originalSystemProperties.clone()); - } - + + private static Properties originalSystemProperties = (Properties) System + .getProperties().clone(); + + @Before + @After + public void restoreSystemProperties() { + System.setProperties((Properties) originalSystemProperties.clone()); + } + // ---------------------------------------------------------------------- // Manage temporary files. // ---------------------------------------------------------------------- - - /** - * Delete a file, either before or after the test. If it can't be deleted, - * complain. - */ - protected static void deleteFile(File file) { - if (file.exists()) { - if (!file.delete()) { - fail("Unable to delete file '" + file.getPath() + "'"); - } - } - } - - /** - * Delete all of the files in a directory, and the directory itself. Will - * not work if there are sub-directories. - */ - protected static void purgeDirectory(File directory) { - if (directory.exists()) { - File[] files = directory.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - fail("Directory '" + directory - + "' contains at least one nested directory."); - } - } - for (File file : files) { - deleteFile(file); - } - deleteFile(directory); - } - } - - /** - * Delete all of the files in a directory, any sub-directories, and the - * directory itself. - */ - protected static void purgeDirectoryRecursively(File directory) { - if (directory.exists()) { - File[] files = directory.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - purgeDirectoryRecursively(file); - } else { - deleteFile(file); - } - } - deleteFile(directory); - } - } - - /** - * Create a directory of a given name inside the Temp directory. If such a - * directory already exists, purge it and its contents and create it fresh. - */ - protected static File createTempDirectory(String name) throws IOException { - File tempDirectory = new File(System.getProperty("java.io.tmpdir"), - name); - + + /** + * Delete a file, either before or after the test. If it can't be deleted, + * complain. + */ + protected static void deleteFile(File file) { + if (file.exists()) { + if (!file.delete()) { + fail("Unable to delete file '" + file.getPath() + "'"); + } + } + } + + /** + * Delete all of the files in a directory, and the directory itself. Will + * not work if there are sub-directories. + */ + protected static void purgeDirectory(File directory) { + if (directory.exists()) { + File[] files = directory.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + fail("Directory '" + directory + + "' contains at least one nested directory."); + } + } + for (File file : files) { + deleteFile(file); + } + deleteFile(directory); + } + } + + /** + * Delete all of the files in a directory, any sub-directories, and the + * directory itself. + */ + protected static void purgeDirectoryRecursively(File directory) { + if (directory.exists()) { + File[] files = directory.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + purgeDirectoryRecursively(file); + } else { + deleteFile(file); + } + } + deleteFile(directory); + } + } + + /** + * Create a directory of a given name inside the Temp directory. If such a + * directory already exists, purge it and its contents and create it fresh. + */ + protected static File createTempDirectory(String name) throws IOException { + File tempDirectory = new File(System.getProperty("java.io.tmpdir"), + name); + // If it already exists, remove it, so we start clean. - if (tempDirectory.exists()) { - purgeDirectoryRecursively(tempDirectory); - } - - if (!tempDirectory.mkdir()) { - throw new IOException("failed to create temp directory '" - + tempDirectory.getPath() + "'"); - } - - return tempDirectory; - } - + if (tempDirectory.exists()) { + purgeDirectoryRecursively(tempDirectory); + } + + if (!tempDirectory.mkdir()) { + throw new IOException("failed to create temp directory '" + + tempDirectory.getPath() + "'"); + } + + return tempDirectory; + } + // ---------------------------------------------------------------------- // Other utilities. // ---------------------------------------------------------------------- - - /** - * Create a file and fill it with the contents provided. - */ - protected File createFile(File directory, String filename, String contents) - throws IOException { - Writer writer = null; - try { - File file = new File(directory, filename); - file.createNewFile(); - writer = new FileWriter(file); - writer.write(contents); - return file; - } finally { - writer.close(); - } - } - - /** - * Read the entire contents of a file, or throw an exception if it's not - * there. - */ - protected static String readFile(File file) throws IOException { - if (!file.exists()) { - throw new IOException("file '" + file.getPath() + "' ('" - + file.getAbsolutePath() + "') does not exist."); - } - FileReader fileReader = new FileReader(file); - String result = readAll(fileReader); - return result; - } - - /** - * Suck all the data from a {@link Reader} into a {@link String}. - */ - protected static String readAll(Reader reader) throws IOException { - StringBuilder result = new StringBuilder(); - BufferedReader buffered = new BufferedReader(reader); - String line; - while (null != (line = buffered.readLine())) { - result.append(line).append('\n'); - } - reader.close(); - return result.toString(); - } - - /** - * Suck all the data from a {@link InputStream} into a {@link String}. - */ - protected static String readAll(InputStream stream) throws IOException { - return readAll(new InputStreamReader(stream)); - } - - /** - * Convert a string to a URL without a checked exception. - */ - protected static URL url(String string) { - try { - return new URL(string); - } catch (MalformedURLException e) { - throw new IllegalArgumentException(e); - } - } - - /** - * Read each string into an XML document and then write it again. This - * should discard any differences that are not syntactically significant. - * Will they be identical? - */ - protected void assertEquivalentXmlDocs(String string1, String string2) { - String out1 = launderXmlDocument(string1); - String out2 = launderXmlDocument(string2); - if (!out1.equals(out2)) { - fail("XML documents are not equivalent: expected <" + string1 - + "> but was <" + string2 + ">"); - } - } - - /** - * Read a string of XML into a document and write it again. This should - * result in a canonical form that can be compared to other such strings. - */ - private String launderXmlDocument(String docString) { - StringWriter result = new StringWriter(); - try { - DocumentBuilderFactory bFactory = DocumentBuilderFactory - .newInstance(); - DocumentBuilder builder = bFactory.newDocumentBuilder(); - - TransformerFactory xFactory = TransformerFactory.newInstance(); - Transformer xformer = xFactory.newTransformer(); - - StringReader reader = new StringReader(docString); - Document doc = builder.parse(new InputSource(reader)); - xformer.transform(new DOMSource(doc), new StreamResult(result)); - } catch (ParserConfigurationException e) { - fail(e.toString()); - } catch (SAXException e) { - fail(e.toString()); - } catch (IOException e) { - fail(e.toString()); - } catch (TransformerException e) { - fail(e.toString()); - } - return result.toString().replaceAll(">\\s+<", "><"); - } - -} + + /** + * Create a file and fill it with the contents provided. + */ + protected static File createFile(File directory, String filename, + String contents) throws IOException { + Writer writer = null; + try { + File file = new File(directory, filename); + file.createNewFile(); + writer = new FileWriter(file); + writer.write(contents); + return file; + } finally { + writer.close(); + } + } + + /** + * Read the entire contents of a file, or throw an exception if it's not + * there. + */ + protected static String readFile(File file) throws IOException { + if (!file.exists()) { + throw new IOException("file '" + file.getPath() + "' ('" + + file.getAbsolutePath() + "') does not exist."); + } + FileReader fileReader = new FileReader(file); + String result = readAll(fileReader); + return result; + } + + /** + * Suck all the data from a {@link Reader} into a {@link String}. + */ + protected static String readAll(Reader reader) throws IOException { + StringBuilder result = new StringBuilder(); + BufferedReader buffered = new BufferedReader(reader); + String line; + while (null != (line = buffered.readLine())) { + result.append(line).append('\n'); + } + reader.close(); + return result.toString(); + } + + /** + * Suck all the data from a {@link InputStream} into a {@link String}. + */ + protected static String readAll(InputStream stream) throws IOException { + return readAll(new InputStreamReader(stream)); + } + + /** + * Convert a string to a URL without a checked exception. + */ + protected static URL url(String string) { + try { + return new URL(string); + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } + } + + /** + * Read each string into an XML document and then write it again. This + * should discard any differences that are not syntactically significant. + * Will they be identical? + */ + protected void assertEquivalentXmlDocs(String string1, String string2) { + String out1 = launderXmlDocument(string1); + String out2 = launderXmlDocument(string2); + if (!out1.equals(out2)) { + fail("XML documents are not equivalent: expected <" + string1 + + "> but was <" + string2 + ">"); + } + } + + /** + * Read a string of XML into a document and write it again. This should + * result in a canonical form that can be compared to other such strings. + */ + private String launderXmlDocument(String docString) { + StringWriter result = new StringWriter(); + try { + DocumentBuilderFactory bFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder builder = bFactory.newDocumentBuilder(); + + TransformerFactory xFactory = TransformerFactory.newInstance(); + Transformer xformer = xFactory.newTransformer(); + + StringReader reader = new StringReader(docString); + Document doc = builder.parse(new InputSource(reader)); + xformer.transform(new DOMSource(doc), new StreamResult(result)); + } catch (ParserConfigurationException e) { + fail(e.toString()); + } catch (SAXException e) { + fail(e.toString()); + } catch (IOException e) { + fail(e.toString()); + } catch (TransformerException e) { + fail(e.toString()); + } + return result.toString().replaceAll(">\\s+<", "><"); + } + +}