diff options
author | Martin Ritchie <ritchiem@apache.org> | 2009-07-22 09:52:02 +0000 |
---|---|---|
committer | Martin Ritchie <ritchiem@apache.org> | 2009-07-22 09:52:02 +0000 |
commit | ba01534206bc194dab376f25fcc3fa3687d0dc2c (patch) | |
tree | 6f7f204aa120473340f4fbc2c10889e463d475b9 /qpid/java/common | |
parent | 33ee5b9247bd4d1e6b7eb88869286ed77c2baf17 (diff) | |
download | qpid-python-ba01534206bc194dab376f25fcc3fa3687d0dc2c.tar.gz |
QPID-1992 : Addition of new Broker Logging Framework
Provided static CurrentActor for accessing ThreadLocal.
Included Test to validate setting of ThreadLocals.
Added Test for AMQPActor
Added getRootMessageLogger() to IApplicationRegistry
Adjusted *ProtocolSessions to start counting at 0.
Allowed Setting of Vhost on the MockProtocolSession
Created a fixed Principle in MockProtocolSession
Changes to MockProtocolSession, prevent NPEs when the AMQPActor creates its log string.
Converted CurrentActor to use a Stack allowing a variety of actors to take their turn on a thread.
Improved package structure
Added testing for Actors
Moved FileMonitorTools functionality to FileUtils and provided a Test
Converted Log4jMessageLoggerTest to a proper UnitTest
Moved Test cases to test package
Updated other broker tests to set the authenticated user before setting the virtualhost,
Whilst the logging could output null as the username it would be better if the tests correctly set the authorizedID.
Update to include tests for disabled logging
Fully tested LogSubjects
Updated MockAMQQueue to be able to take a Virtualhost as per a normal Queue.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@796650 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/common')
-rw-r--r-- | qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java | 182 | ||||
-rw-r--r-- | qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java | 80 |
2 files changed, 184 insertions, 78 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java index 63222b50db..515c849290 100644 --- a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java +++ b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java @@ -20,7 +20,18 @@ */ package org.apache.qpid.util; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.LinkedList; +import java.util.List; /** * FileUtils provides some simple helper methods for working with files. It follows the convention of wrapping all @@ -46,7 +57,8 @@ public class FileUtils { BufferedInputStream is = null; - try{ + try + { try { is = new BufferedInputStream(new FileInputStream(filename)); @@ -57,7 +69,9 @@ public class FileUtils } return readStreamAsString(is); - }finally { + } + finally + { if (is != null) { try @@ -210,68 +224,69 @@ public class FileUtils /* * Deletes a given file */ - public static boolean deleteFile(String filePath) - { - return delete(new File(filePath), false); - } + public static boolean deleteFile(String filePath) + { + return delete(new File(filePath), false); + } /* * Deletes a given empty directory */ - public static boolean deleteDirectory(String directoryPath) - { - File directory = new File(directoryPath); - - if (directory.isDirectory()) - { - if (directory.listFiles().length == 0) - { - return delete(directory, true); - } - } - - return false; - } - - /** - * Delete a given file/directory, - * A directory will always require the recursive flag to be set. - * if a directory is specified and recursive set then delete the whole tree - * @param file the File object to start at - * @param recursive boolean to recurse if a directory is specified. - * @return <code>true</code> if and only if the file or directory is - * successfully deleted; <code>false</code> otherwise - */ - public static boolean delete(File file, boolean recursive) - { - boolean success = true; - - if (file.isDirectory()) - { - if (recursive) - { - File[] files = file.listFiles(); - - // This can occur if the file is deleted outside the JVM - if (files == null) - { - return false; - } - - for (int i = 0; i < files.length; i++) - { - success = delete(files[i], true) && success; - } - - return success && file.delete(); - } - - return false; - } - - return file.delete(); - } + public static boolean deleteDirectory(String directoryPath) + { + File directory = new File(directoryPath); + if (directory.isDirectory()) + { + if (directory.listFiles().length == 0) + { + return delete(directory, true); + } + } + + return false; + } + + /** + * Delete a given file/directory, + * A directory will always require the recursive flag to be set. + * if a directory is specified and recursive set then delete the whole tree + * + * @param file the File object to start at + * @param recursive boolean to recurse if a directory is specified. + * + * @return <code>true</code> if and only if the file or directory is + * successfully deleted; <code>false</code> otherwise + */ + public static boolean delete(File file, boolean recursive) + { + boolean success = true; + + if (file.isDirectory()) + { + if (recursive) + { + File[] files = file.listFiles(); + + // This can occur if the file is deleted outside the JVM + if (files == null) + { + return false; + } + + for (int i = 0; i < files.length; i++) + { + success = delete(files[i], true) && success; + } + + return success && file.delete(); + } + + return false; + } + + return file.delete(); + } public static class UnableToCopyException extends Exception { @@ -294,7 +309,6 @@ public class FileUtils throw new IllegalArgumentException("Unable to copy '" + source.toString() + "' to '" + dst + "' a file with same name exists."); } - if (source.isFile()) { copy(source, dst); @@ -303,22 +317,48 @@ public class FileUtils //else we have a source directory if (!dst.isDirectory() && !dst.mkdir()) { - throw new UnableToCopyException("Unable to create destination directory"); + throw new UnableToCopyException("Unable to create destination directory"); } - for (File file : source.listFiles()) { - if (file.isFile()) - { - copy(file, new File(dst.toString() + File.separator + file.getName())); - } - else - { - copyRecursive(file, new File(dst + File.separator + file.getName())); - } + if (file.isFile()) + { + copy(file, new File(dst.toString() + File.separator + file.getName())); + } + else + { + copyRecursive(file, new File(dst + File.separator + file.getName())); + } } + } + + /** + * Checks the specified file for instances of the search string. + * + * @param file the file to search + * @param search the search String + * + * @throws java.io.IOException + * @return the list of matching entries + */ + public static List<String> searchFile(File file, String search) + throws IOException + { + + List<String> results = new LinkedList<String>(); + + BufferedReader reader = new BufferedReader(new FileReader(file)); + while (reader.ready()) + { + String line = reader.readLine(); + if (line.contains(search)) + { + results.add(line); + } + } + return results; } } diff --git a/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java b/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java index 94e7e20a86..7eba5f092e 100644 --- a/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java +++ b/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java @@ -22,11 +22,12 @@ package org.apache.qpid.util; import junit.framework.TestCase; -import java.io.File; -import java.io.IOException; import java.io.BufferedWriter; -import java.io.FileWriter; +import java.io.File; import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; public class FileUtilsTest extends TestCase { @@ -47,7 +48,7 @@ public class FileUtilsTest extends TestCase //Create initial file File test = createTestFile(fileName, TEST_DATA); - + try { //Check number of files before copy @@ -137,7 +138,6 @@ public class FileUtilsTest extends TestCase testSubDir.deleteOnExit(); testDir.deleteOnExit(); - //Perform Copy File copyDir = new File(testDir.toString() + COPY); try @@ -282,7 +282,7 @@ public class FileUtilsTest extends TestCase public void testDeleteNonExistentFile() { - File test = new File("FileUtilsTest-testDelete-"+System.currentTimeMillis()); + File test = new File("FileUtilsTest-testDelete-" + System.currentTimeMillis()); assertTrue("File exists", !test.exists()); assertFalse("File is a directory", test.isDirectory()); @@ -303,7 +303,6 @@ public class FileUtilsTest extends TestCase } } - /** * Given two lists of File arrays ensure they are the same length and all entries in Before are in After * @@ -543,4 +542,71 @@ public class FileUtilsTest extends TestCase } } + public static final String SEARCH_STRING = "testSearch"; + + /** + * Test searchFile(File file, String search) will find a match when it + * exists. + * + * @throws java.io.IOException if unable to perform test setup + */ + public void testSearchSucceed() throws IOException + { + File _logfile = File.createTempFile("FileUtilsTest-testSearchSucceed", ".out"); + + prepareFileForSearchTest(_logfile); + + List<String> results = FileUtils.searchFile(_logfile, SEARCH_STRING); + + assertNotNull("Null result set returned", results); + + assertEquals("Results do not contain expected count", 1, results.size()); + } + + /** + * Test searchFile(File file, String search) will not find a match when the + * test string does not exist. + * + * @throws java.io.IOException if unable to perform test setup + */ + public void testSearchFail() throws IOException + { + File _logfile = File.createTempFile("FileUtilsTest-testSearchFail", ".out"); + + prepareFileForSearchTest(_logfile); + + List<String> results = FileUtils.searchFile(_logfile, "Hello"); + + assertNotNull("Null result set returned", results); + + //Validate we only got one message + if (results.size() > 0) + { + System.err.println("Unexpected messages"); + + for (String msg : results) + { + System.err.println(msg); + } + } + + assertEquals("Results contains data when it was not expected", + 0, results.size()); + } + + /** + * Write the SEARCH_STRING in to the given file. + * + * @param logfile The file to write the SEARCH_STRING into + * + * @throws IOException if an error occurs + */ + private void prepareFileForSearchTest(File logfile) throws IOException + { + BufferedWriter writer = new BufferedWriter(new FileWriter(logfile)); + writer.append(SEARCH_STRING); + writer.flush(); + writer.close(); + } + } |