private void verify(ListAppender<Integer> la, int atLeast) { assertFalse(la.isStarted()); assertTrue(atLeast+ " <= "+la.list.size(), atLeast <= la.list.size()); statusChecker.assertIsErrorFree(); statusChecker.assertContainsMatch("Worker thread will flush remaining events before exiting."); }
@Test public void properties() throws JoranException { String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "properties.xml"; assertNull(loggerContext.getProperty(CoreConstants.HOSTNAME_KEY)); assertNull(System.getProperty("sys")); configure(configFileAsStr); // Not supported in Android due to Issue #3 //assertNotNull(loggerContext.getProperty(CoreConstants.HOSTNAME_KEY)); assertNull(loggerContext.getProperty("transientKey1")); assertNull(loggerContext.getProperty("transientKey2")); assertEquals("node0", loggerContext.getProperty("nodeId")); assertEquals("tem", System.getProperty("sys")); assertNotNull(loggerContext.getProperty("path")); checker.assertIsErrorFree(); }
@Test public void basic() throws Exception { loadConfig(ClassicTestConstants.JORAN_INPUT_PREFIX + "unique.xml"); CachingDateFormatter sdf = new CachingDateFormatter("yyyyMMdd'T'HHmm"); String timestamp = sdf.format(System.currentTimeMillis()); sc.assertIsErrorFree(); Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME); root.info("hello"); File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "UNIK_" + timestamp + diffAsStr + "log.txt"); assertTrue(file.exists()); } }
private void verify(int expected) { StatusChecker checker = new StatusChecker(loggerContext); //StatusPrinter.print(loggerContext); checker.assertIsErrorFree(); int effectiveResets = checker .matchCount(CoreConstants.RESET_MSG_PREFIX); String failMsg = "effective=" + effectiveResets + ", expected=" + expected; // there might be more effective resets than the expected amount // since the harness may be sleeping while a reset occurs assertTrue(failMsg, expected <= effectiveResets && (expected + 2) >= effectiveResets); }
@Test public void collisionImpossibleForSingleAppender() { FileAppender<String> fileAppender = buildFileAppender("FA", "collisionImpossibleForSingleAppender"); fileAppender.start(); statusChecker.assertIsErrorFree(); }
@Test public void sysProps() throws JoranException { System.setProperty("k.lbcore254", ClassicTestConstants.ISSUES_PREFIX + "lbcore254"); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(ClassicTestConstants.ISSUES_PREFIX + "lbcore254.xml"); checker.assertIsErrorFree(); }
private void checkResetCount(int expected) { StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); int effectiveResets = checker.matchCount(CoreConstants.RESET_MSG_PREFIX); assertEquals(expected, effectiveResets); // String failMsg = "effective=" + effectiveResets + ", expected=" + expected; // // there might be more effective resets than the expected amount // since the harness may be sleeping while a reset occurs //assertTrue(failMsg, expected <= effectiveResets && (expected + 2) >= effectiveResets); }
@SuppressWarnings("deprecation") @Test public void encoderCharset() throws JoranException, IOException, InterruptedException { String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "encoderCharset.xml"; configure(configFileAsStr); ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CONSOLE"); assertNotNull(consoleAppender); LayoutWrappingEncoder<ILoggingEvent> encoder = (LayoutWrappingEncoder<ILoggingEvent>) consoleAppender.getEncoder(); assertEquals("UTF-8", encoder.getCharset().displayName()); StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); }
@Test public void fileUnderCatalinaBaseShouldBeFound() throws LifecycleException { System.setProperty(LogbackValve.CATALINA_BASE_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/"); final String fileName = "logback-access.xml"; setupValve(fileName); valve.start(); checker.assertContainsMatch("Found configuration file"); checker.assertContainsMatch("Done configuring"); checker.assertIsErrorFree(); }
@Test public void fileUnderCatalinaHomeShouldBeFound() throws LifecycleException { System.setProperty(LogbackValve.CATALINA_HOME_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/"); final String fileName = "logback-access.xml"; setupValve(fileName); valve.start(); checker.assertContainsMatch("Found configuration file"); checker.assertContainsMatch("Done configuring"); checker.assertIsErrorFree(); }
@Test public void levelChangePropagator0() throws JoranException, IOException, InterruptedException { String loggerName = "changePropagator0" + diff; java.util.logging.Logger.getLogger(loggerName).setLevel(java.util.logging.Level.INFO); String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator0.xml"; configure(configFileAsStr); StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); verifyJULLevel(loggerName, null); verifyJULLevel("a.b.c." + diff, Level.WARN); verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); }
@Test(timeout = 1000) public void concurrentReset() throws InterruptedException { LoggerContext loggerContext = new LoggerContext(); CyclicBarrier cyclicBarrier = new CyclicBarrier(CONCURRENT_RESET_THREAD_COUNT); StatusChecker statusChecker = new StatusChecker(loggerContext); int desiredResetCount = 100; RunnableWithCounterAndDone[] runnableArray = buildRunnableArray(loggerContext, cyclicBarrier); Harness harness = new Harness((Resetter) runnableArray[0], desiredResetCount); harness.execute(runnableArray); statusChecker.assertIsErrorFree(); }
@Test public void levelChangePropagator1() throws JoranException, IOException, InterruptedException { String loggerName = "changePropagator1" + diff; java.util.logging.Logger.getLogger(loggerName).setLevel(java.util.logging.Level.INFO); verifyJULLevel(loggerName, Level.INFO); String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "/jul/levelChangePropagator1.xml"; configure(configFileAsStr); StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); verifyJULLevel(loggerName, Level.INFO); verifyJULLevel("a.b.c." + diff, Level.WARN); verifyJULLevel(Logger.ROOT_LOGGER_NAME, Level.TRACE); }
@Test public void resourceShouldBeFound() throws LifecycleException { final String fileName = "logback-asResource.xml"; setupValve(fileName); valve.start(); checker.assertContainsMatch("Found ." + fileName + ". as a resource."); checker.assertContainsMatch("Done configuring"); checker.assertIsErrorFree(); }
@Test public void appenderStopShouldClearEntryInCollisionMap() { String key = "FA"; FileAppender<String> fileAppender = buildFileAppender(key, "collisionImpossibleForSingleAppender"); fileAppender.start(); assertCollisionMapHasEntry(FA_FILENAME_COLLISION_MAP, key); fileAppender.stop(); assertCollisionMapHasNoEntry(FA_FILENAME_COLLISION_MAP, key); statusChecker.assertIsErrorFree(); }
@Test public void timeoutPropertyShouldBeHonored() throws JoranException, InterruptedException { configure(SIFT_FOLDER_PREFIX + "timeout.xml"); long timeout = 30*1000; SiftingAppender sa = (SiftingAppender) root.getAppender("SIFT"); LoggingEvent event = new LoggingEvent("", logger, Level.DEBUG, "timeout", null, null); event.setTimeStamp(now); sa.doAppend(event); AppenderTracker<ILoggingEvent> tracker = sa.getAppenderTracker(); assertEquals(1, tracker.getComponentCount()); now += timeout+1; tracker.removeStaleComponents(now); assertEquals(0, tracker.getComponentCount()); statusChecker.assertIsErrorFree(); }
@Test(timeout = 3000L) public void fallbackToSafe_FollowedByRecovery() throws IOException, JoranException, InterruptedException { String path = CoreTestConstants.OUTPUT_DIR_PREFIX + "reconfigureOnChangeConfig_fallbackToSafe-" + diff + ".xml"; File topLevelFile = new File(path); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\"><root level=\"ERROR\"/></configuration> "); configure(topLevelFile); CountDownLatch changeDetectedLatch = waitForReconfigurationToBeDone(null); ReconfigureOnChangeTask oldRoct = getRegisteredReconfigureTask(); assertNotNull(oldRoct); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\">\n" + " <root></configuration>"); changeDetectedLatch.await(); statusChecker.assertContainsMatch(Status.WARN, FALLING_BACK_TO_SAFE_CONFIGURATION); statusChecker.assertContainsMatch(Status.INFO, RE_REGISTERING_PREVIOUS_SAFE_CONFIGURATION); loggerContext.getStatusManager().clear(); CountDownLatch secondDoneLatch = waitForReconfigurationToBeDone(oldRoct); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\"><root level=\"ERROR\"/></configuration> "); secondDoneLatch.await(); statusChecker.assertIsErrorFree(); statusChecker.containsMatch(DETECTED_CHANGE_IN_CONFIGURATION_FILES); }
@Test(timeout = 4000L) public void fallbackToSafeWithIncludedFile_FollowedByRecovery() throws IOException, JoranException, InterruptedException { String topLevelFileAsStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "reconfigureOnChangeConfig_top-" + diff + ".xml"; String innerFileAsStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "reconfigureOnChangeConfig_inner-" + diff + ".xml"; File topLevelFile = new File(topLevelFileAsStr); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\"><include file=\"" + innerFileAsStr + "\"/></configuration> "); File innerFile = new File(innerFileAsStr); writeToFile(innerFile, "<included><root level=\"ERROR\"/></included> "); configure(topLevelFile); CountDownLatch doneLatch = waitForReconfigurationToBeDone(null); ReconfigureOnChangeTask oldRoct = getRegisteredReconfigureTask(); assertNotNull(oldRoct); writeToFile(innerFile, "<included>\n<root>\n</included>"); doneLatch.await(); statusChecker.assertContainsMatch(Status.WARN, FALLING_BACK_TO_SAFE_CONFIGURATION); statusChecker.assertContainsMatch(Status.INFO, RE_REGISTERING_PREVIOUS_SAFE_CONFIGURATION); loggerContext.getStatusManager().clear(); CountDownLatch secondDoneLatch = waitForReconfigurationToBeDone(oldRoct); writeToFile(innerFile, "<included><root level=\"ERROR\"/></included> "); secondDoneLatch.await(); statusChecker.assertIsErrorFree(); statusChecker.containsMatch(DETECTED_CHANGE_IN_CONFIGURATION_FILES); }
@Test public void basic() throws Exception { String testId = "basic"; lc.putProperty("testId", testId); loadConfig(ClassicTestConstants.JORAN_INPUT_PREFIX + "rolling/" + testId + ".xml"); statusChecker.assertIsErrorFree(); Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME); expectedFilenameList.add(randomOutputDir + "z" + testId); RollingFileAppender<ILoggingEvent> rfa = (RollingFileAppender<ILoggingEvent>) root .getAppender("ROLLING"); TimeBasedRollingPolicy tprp = (TimeBasedRollingPolicy<ILoggingEvent>) rfa .getTriggeringPolicy(); TimeBasedFileNamingAndTriggeringPolicy tbnatp = tprp .getTimeBasedFileNamingAndTriggeringPolicy(); String prefix = "Hello---"; int runLength = 4; for (int i = 0; i < runLength; i++) { logger.debug(prefix + i); addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, false); incCurrentTime(500); tbnatp.setCurrentTime(currentTime); } existenceCheck(expectedFilenameList); sortedContentCheck(randomOutputDir, runLength, prefix); }
@Test(timeout = 2000) public void eventLossIfNeverBlock() { int bufferSize = 10; int loopLen = bufferSize * 2; delayingListAppender.setDelay(5000); // something greater than the test timeout asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.setQueueSize(bufferSize); asyncAppenderBase.setNeverBlock(true); asyncAppenderBase.start(); for (int i = 0; i < loopLen; i++) { asyncAppenderBase.doAppend(i); } asyncAppenderBase.stop(); // ListAppender size isn't a reliable test here, so just make sure we didn't // have any errors, and that we could complete the test in time. statusChecker.assertIsErrorFree(); }