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