@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 // Check the next log file name when <date> is specified: public void testLogFileWithDate() throws BasicIOException { // 1) When the directory is empty, the log file name has today's date: String logFileName = "log_t02_<date>.xls"; String realFileName = "log_t02_" + today.format(dateFormat) + ".xls"; LogFile logFile = new LogFile(_testLocation, logFileName, 1); assertEquals(UT.getNextLogFile(logFile), realFileName); // 2) getNextLogFile() returns sequencial file names: for (int i = 1; i < 99; i++) { UT.getNextLogFile(logFile); } realFileName = "log_t02_" + today.format(dateFormat) + "_100.xls"; assertEquals(UT.getNextLogFile(logFile), realFileName); realFileName = "log_t02_" + today.format(dateFormat) + "_101.xls"; assertEquals(UT.getNextLogFile(logFile), realFileName); // 3) If a file exists within the range of days, the existing file is used: today.setLenient(true); Date past = today.setDay(today.getDay() - 15); String pastFileName = "log_t02_" + past.format(dateFormat) + ".xls"; File file = _testLocation.createFile(pastFileName); logFile = new LogFile(_testLocation, logFileName, 15); assertEquals(UT.getNextLogFile(logFile), pastFileName); // 4) If a file exists but is already outside the range of days, a new file is created: file.delete(); past = today.setDay(today.getDay() - 16); logFile = new LogFile(_testLocation, logFileName, 15); realFileName = "log_t02_" + today.format(dateFormat) + ".xls"; assertEquals(UT.getNextLogFile(logFile), realFileName); // 5) If there is an existing log_t01_xx.xls file, getNextLogFile() returns it: realFileName = "log_t02_" + today.format(dateFormat) + "_49.xls"; _testLocation.createFile(realFileName); logFile = new LogFile(_testLocation, logFileName, 15); assertEquals(UT.getNextLogFile(logFile), realFileName); }