Skip to content
Permalink
Browse files

Merge pull request #86 from dwnusbaum/loggerrule-enhancements

[JENKINS-47975] Add utility methods to LoggerRule for matching log entries
  • Loading branch information
jglick committed Nov 28, 2017
2 parents 366e912 + 30763c5 commit deed831b4630142dad6737df3994ba7f2207dd6f
@@ -36,6 +36,10 @@
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
@@ -169,4 +173,97 @@ protected void after() {
}
}

/**
* Creates a {@link Matcher} that matches if the {@link LoggerRule} has a {@link LogRecord} at
* the specified {@link Level}, with a message matching the specified matcher, and with a
* {@link Throwable} matching the specified matcher.
*
* @param level The {@link Level} of the {@link LoggerRule} to match. Pass {@code null} to match any {@link Level}.
* @param message the matcher to match against {@link LogRecord#getMessage}
* @param thrown the matcher to match against {@link LogRecord#getThrown()}
*/
public static Matcher<LoggerRule> recorded(@CheckForNull Level level, @Nonnull Matcher<String> message, @CheckForNull Matcher<Throwable> thrown) {
return new RecordedMatcher(level, message, thrown);
}

/**
* Creates a {@link Matcher} that matches if the {@link LoggerRule} has a {@link LogRecord} at
* the specified {@link Level} and with a message matching the specified matcher.
*
* @param level The {@link Level} of the {@link LoggerRule} to match. Pass {@code null} to match any {@link Level}.
* @param message The matcher to match against {@link LogRecord#getMessage}.
*/
public static Matcher<LoggerRule> recorded(@CheckForNull Level level, @Nonnull Matcher<String> message) {
return recorded(level, message, null);
}

/**
* Creates a {@link Matcher} that matches if the {@link LoggerRule} has a {@link LogRecord}
* with a message matching the specified matcher and with a {@link Throwable} matching the specified
* matcher.
*
* @param message the matcher to match against {@link LogRecord#getMessage}
* @param thrown the matcher to match against {@link LogRecord#getThrown()}
*/
public static Matcher<LoggerRule> recorded(@Nonnull Matcher<String> message, @CheckForNull Matcher<Throwable> thrown) {
return recorded(null, message, thrown);
}

/**
* Creates a {@link Matcher} that matches if the {@link LoggerRule} has a {@link LogRecord}
* with a message matching the specified matcher.
*
* @param message the matcher to match against {@link LogRecord#getMessage}
*/
public static Matcher<LoggerRule> recorded(@Nonnull Matcher<String> message) {
return recorded(null, message);
}

private static class RecordedMatcher extends TypeSafeMatcher<LoggerRule> {
@CheckForNull Level level;
@Nonnull Matcher<String> message;
@CheckForNull Matcher<Throwable> thrown;

public RecordedMatcher(@CheckForNull Level level, @Nonnull Matcher<String> message, @CheckForNull Matcher<Throwable> thrown) {
this.level = level;
this.message = message;
this.thrown = thrown;
}

@Override
protected boolean matchesSafely(LoggerRule item) {
synchronized (item) {
for (LogRecord record : item.getRecords()) {
if (level == null || record.getLevel() == level) {
if (message.matches(record.getMessage())) {
if (thrown != null) {
if (thrown.matches(record.getThrown())) {
return true;
}
} else {
return true;
}
}
}
}
}
return false;
}

@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("has LogRecord");
if (level != null) {
description.appendText(" with level ");
description.appendValue(level.getName());
}
description.appendText(" with a message matching ");
description.appendDescriptionOf(message);
if (thrown != null) {
description.appendText(" with an exception matching ");
description.appendDescriptionOf(thrown);
}
}
}

}
@@ -0,0 +1,86 @@
/*
* The MIT License
*
* Copyright 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.hudson.test;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Test;
import org.junit.Rule;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.jvnet.hudson.test.LoggerRule.recorded;

public class LoggerRuleTest {

@Rule
public LoggerRule logRule = new LoggerRule();

private static final Logger FOO_LOGGER = Logger.getLogger("Foo");
private static final Logger BAR_LOGGER = Logger.getLogger("Bar");

@Test
public void testRecordedSingleLogger() {
logRule.record("Foo", Level.INFO).capture(1);
FOO_LOGGER.log(Level.INFO, "Entry 1");
assertThat(logRule, recorded(equalTo("Entry 1")));
assertThat(logRule, recorded(Level.INFO, equalTo("Entry 1")));
assertThat(logRule, not(recorded(Level.WARNING, equalTo("Entry 1"))));
FOO_LOGGER.log(Level.INFO, "Entry 2");
assertThat(logRule, not(recorded(equalTo("Entry 1"))));
assertThat(logRule, recorded(equalTo("Entry 2")));
}

@Test
public void testRecordedMultipleLoggers() {
logRule.record("Foo", Level.INFO).record("Bar", Level.SEVERE).capture(2);
FOO_LOGGER.log(Level.INFO, "Foo Entry 1");
BAR_LOGGER.log(Level.SEVERE, "Bar Entry 1");
assertThat(logRule, recorded(equalTo("Foo Entry 1")));
assertThat(logRule, recorded(equalTo("Bar Entry 1")));
// All criteria must match a single LogRecord.
assertThat(logRule, not(recorded(Level.INFO, equalTo("Bar Entry 1"))));
}

@Test
public void testRecordedThrowable() {
logRule.record("Foo", Level.INFO).capture(1);
FOO_LOGGER.log(Level.INFO, "Foo Entry 1", new IllegalStateException());
assertThat(logRule, recorded(equalTo("Foo Entry 1"), instanceOf(IllegalStateException.class)));
assertThat(logRule, recorded(Level.INFO, equalTo("Foo Entry 1"), instanceOf(IllegalStateException.class)));
assertThat(logRule, not(recorded(Level.INFO, equalTo("Foo Entry 1"), instanceOf(IOException.class))));
}

@Test
public void testRecordedNoShortCircuit() {
logRule.record("Foo", Level.INFO).capture(2);
FOO_LOGGER.log(Level.INFO, "Foo Entry", new IllegalStateException());
FOO_LOGGER.log(Level.INFO, "Foo Entry", new IOException());
assertThat(logRule, recorded(Level.INFO, equalTo("Foo Entry"), instanceOf(IllegalStateException.class)));
assertThat(logRule, recorded(Level.INFO, equalTo("Foo Entry"), instanceOf(IOException.class)));
}
}

0 comments on commit deed831

Please sign in to comment.
You can’t perform that action at this time.