@Test public void t04_TestTxtLogger() throws BasicIOException { final String filename1 = "test01.log"; final String filename2 = "test01.txt"; final LogFile logFile = new LogFile(_testLocation, filename1); Logger logger = new Logger(logFile); // The file should be created immediately: File file = _testLocation.getFile(filename1); if (file == null) { fail(); } // It should be locked: try { file.getOutputStream(); fail(); } catch (FileLockedException fle) { println(fle); } // The .txt file should NOT have been created: if (_testLocation.exists(filename2)) { fail(); } // Change output, should delete the file: logger.setOutput(System.out); if (_testLocation.exists(filename1)) { fail(); } // Again open the same file: logger.setOutput(logFile); assertEquals(file.getSize(), 0L); logger.info("first message"); long size = file.getSize(); assertTrue(size > 0); // Change to stdout and check the file is still there: logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } // We can now open it: ExtendedReader in = new ExtendedReader(file.getInputStream()); assertTrue(in.readLine().startsWith("INFO")); in.close(); // Ensure that if we disable the logger it does not write to the file: logger.setOutput(logFile); logger.setEnabled(false); logger.info("second message"); logger.setOutput(System.out); assertEquals(file.getSize(), size); file.delete(); // Print an exception to stdout: logger.setEnabled(true); logger.error("Oops!", new Exception()); // Print an exception to TXT: logger.setOutput(logFile); logger.error("Oops!", new Exception()); logger.setOutput(System.out); file.delete(); }
@Test // TODO can we add assertions here? public void t03_TestStandardLogger() throws FileSystemException, ReadWriteException { Logger logger = new Logger(System.out); logger.info("this is a trace message"); logger.warn("this is a warning"); logger.warn("this is a warning", getException(1)); logger.error("this is an error", getException(1)); logger.error(getException(1)); }
@Test public void t06_TestXlsLogger() throws BasicIOException { final String filename1 = "test01.xls"; final String filename2 = "test01.txt"; final LogFile logFile = new LogFile(_testLocation, filename1); Logger logger = new Logger(logFile); // The file should be created immediately: File file = _testLocation.getFile(filename1); if (file == null) { fail(); } // It should be locked: try { file.getOutputStream(); fail(); } catch (FileLockedException fle) { println(fle); } // Get the initial file size (it has the header): long initialSize = file.getSize(); // The .txt file should also have been created: if (!_testLocation.exists(filename2)) { fail(); } // Change output, should delete both files: logger.setOutput(System.out); if (_testLocation.exists(filename1)) { fail(); } if (_testLocation.exists(filename2)) { fail(); } // Again open the same file: logger.setOutput(logFile); assertEquals(file.getSize(), initialSize); logger.info("first message"); // On XLS, this doesn't write to disk straight away: assertEquals(file.getSize(), initialSize); // So lets force it to write: LogFile.setXlsBufferSize(10); for (int i = 0; i < 9; i++) { logger.info("message " + (i + 2)); } assertEquals(file.getSize(), initialSize); logger.info("11th message"); long size = file.getSize(); assertTrue(size > initialSize); // Change to stdout and check the file is still there: logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } // ... and the TXT file is not there: if (_testLocation.exists(filename2)) { fail(); } // We can now open it: ExtendedInputStream in = file.getInputStream(); in.close(); // Ensure that if we disable the logger it does not write to the file: logger.setOutput(new LogFile(_testLocation, filename1)); logger.setEnabled(false); logger.info("12th message"); // We can only check the file's size is the same once we close the XLS logger: logger.setOutput(System.out); assertTrue(file.getSize() == size); // Write an error and ensure the ex file remains there: logger.setOutput(logFile); logger.setEnabled(true); logger.error("an error", getException(5)); logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } if (!_testLocation.exists(filename2)) { fail(); } _testLocation.getFile(filename1).delete(); _testLocation.getFile(filename2).delete(); }
@Test public void t05_TestCsvLogger() throws BasicIOException { final String filename1 = "test01.csv"; final String filename2 = "test01.txt"; final LogFile logFile = new LogFile(_testLocation, filename1); Logger logger = new Logger(logFile); // The file should be created immediately: File file = _testLocation.getFile(filename1); if (file == null) { fail(); } // It should be locked: try { file.getOutputStream(); fail(); } catch (FileLockedException fle) { println(fle); } // Get the initial file size (it has the header): long initialSize = file.getSize(); // The .txt file should also have been created: if (!_testLocation.exists(filename2)) { fail(); } // Change output, should delete both files: logger.setOutput(System.out); if (_testLocation.exists(filename1)) { fail(); } if (_testLocation.exists(filename2)) { fail(); } // Again open the same file: logger.setOutput(logFile); assertEquals(file.getSize(), initialSize); logger.info("first message"); long size = file.getSize(); assertTrue(size > initialSize); // Change to stdout and check the file is still there: logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } // ... and the TXT file is not there: if (_testLocation.exists(filename2)) { fail(); } // We can now open it: ExtendedReader in = new ExtendedReader(file.getInputStream()); assertTrue(in.readLine().startsWith("Timestamp")); String s = in.readLine(); s = s.substring(s.indexOf(",") + 1); assertTrue(s.startsWith("INFO")); assertTrue(s.endsWith(",")); in.close(); // Ensure that if we disable the logger it does not write to the file: logger.setOutput(new LogFile(_testLocation, filename1)); logger.setEnabled(false); logger.info("second message"); logger.setOutput(System.out); assertEquals(file.getSize(), size); // Write an error and ensure the ex file remains there: logger.setOutput(logFile); logger.setEnabled(true); logger.error("an error", getException(5)); logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } if (!_testLocation.exists(filename2)) { fail(); } _testLocation.getFile(filename1).delete(); _testLocation.getFile(filename2).delete(); }