Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * This is free and unencumbered software released into the public domain.
   *
   * Please see https://github.com/binkley/binkley/blob/master/LICENSE.md.
   */
  
  package hm.binkley.util.logging.osi;
  
 import org.junit.Rule;
 import org.junit.Test;
 
 
 import static hm.binkley.util.logging.LoggerUtil.refreshLogback;
 import static hm.binkley.util.logging.osi.SupportLoggersIT.PatternMatcher.pattern;
 import static hm.binkley.util.logging.osi.OSI.SystemProperty.LOGBACK_CONFIGURATION_FILE;
 import static hm.binkley.util.logging.osi.OSI.SystemProperty.LOGBACK_INCLUDED_RESOURCE;
 import static hm.binkley.util.logging.osi.SupportLoggers.ALERT;
 import static hm.binkley.util.logging.osi.SupportLoggers.APPLICATION;
 import static hm.binkley.util.logging.osi.SupportLoggers.AUDIT;
 import static org.hamcrest.Matchers.isEmptyString;
 import static org.junit.Assert.assertThat;
 import static org.junit.contrib.java.lang.system.LogMode.LOG_ONLY;
 import static org.junit.rules.ExpectedException.none;

ITSupportLoggers integration tests SupportLoggers.

Author(s):
B. K. Oxley
 
 public final class SupportLoggersIT {
     @Rule
     public final ExpectedException thrown = none();
     @Rule
     public final StandardOutputStreamLog sout = new StandardOutputStreamLog();
     @Rule
     public final StandardErrorStreamLog serr = new StandardErrorStreamLog();
     @Rule
     public final ProvideSystemProperty sysprops = new ProvideSystemProperty();
 
     @Before
     public void setUp() {
         .setProperty(.key(), "osi-logback.xml");
         .setProperty(.key(), "osi-support-loggers-included.xml");
         refreshLogback();
     }
 
     @Test
     public void applicationShouldLogNormallyOnce() {
         .getLogger("test").error("Ignored");
 
         assertThat(.getLog(), containsOnce("Ignored"));
     }
 
     @Test
     public void alertShouldSayWarnOnStderrOnce() {
         .getLogger("alert").warn("Ignored");
 
         assertThat(.getLog(), containsOnce("ALERT/WARN"));
     }
 
     @Test
     public void alertShouldSayErrorOnStderrOnce() {
         .getLogger("alert").error("Ignored");
 
         assertThat(.getLog(), containsOnce("ALERT/ERROR"));
     }
 
     @Test
     public void alertShouldIncludeNonAlertLoggerName() {
         .getLogger("test").warn("Ignored");
 
         assertThat(.getLog(), containsOnce("ALERT/WARN"));
     }
 
     @Test
     public void alertShouldDuplicateOnStdoutOnce() {
         .getLogger("alert").warn("Ignored");
 
         assertThat(.getLog(), containsOnce("ALERT/WARN"));
     }
 
     @Test(expected = IllegalStateException.class)
     public void alertShouldComplainWithDebug() {
         .getLogger("test").debug("Ignored");
     }
 
     @Test(expected = IllegalStateException.class)
     public void alertShouldComplainWithInfo() {
         .getLogger("test").info("Ignored");
    }
    @Test
    public void auditShouldSayInfoOnStdoutTwice() {
        .getLogger("audit").info("Ignored");
        // TODO: How to get it only once when AUDIT goes to stdout?
        assertThat(.getLog(), containsTwice("AUDIT/INFO"));
    }
    @Test
    public void auditShouldSayWarnOnStdoutTwice() {
        .getLogger("audit").warn("Ignored");
        assertThat(.getLog(), containsTwice("AUDIT/WARN"));
    }
    @Test
    public void auditShouldSayErrorOnStdoutTwice() {
        .getLogger("audit").error("Ignored");
        assertThat(.getLog(), containsTwice("AUDIT/ERROR"));
    }
    @Test
        .getLogger("test").info("Ignored");
        assertThat(.getLog(), containsTwice("AUDIT/INFO"));
    }
    @Test
    public void auditShouldSayNothingOnStderr() {
        .getLogger("test").info("Ignored");
        assertThat(.getLog(), isEmptyString());
    }
    @Test(expected = IllegalStateException.class)
    public void auditShouldComplainWithDebug() {
        .getLogger("test").debug("Ignored");
    }
    private static Matcher<CharSequencecontainsOnce(final String s) {
        // TODO: Now you have two problems
        return pattern("(?s)([^\n]*" + s + "[^\n]*\n)(?!\\1)");
    }
    private static Matcher<CharSequencecontainsTwice(final String s) {
        // TODO: Now you have two problems
        return pattern("(?s)([^\n]*" + s + "[^\n]*\n)\\1");
    }

    
Tests if the argument is a java.lang.CharSequence that matches a regular expression.

Todo:
Copied from Hamcrest 1.4, yet to be released
    public static class PatternMatcher
            extends TypeSafeMatcher<CharSequence> {
        
Creates a matcher that matches if the examined java.lang.CharSequence matches the specified regular expression.

For example:

assertThat("myStringOfNote", pattern("[0-9]+"))

Parameters:
regex the regular expression that the returned matcher will use to match any examined java.lang.CharSequence
        @Factory
        public static Matcher<CharSequencepattern(final String regex) {
            return pattern(Pattern.compile(regex));
        }

        
Creates a matcher that matches if the examined java.lang.CharSequence matches the specified java.util.regex.Pattern.

For example:

assertThat("myStringOfNote", Pattern.compile("[0-9]+"))

Parameters:
pattern the pattern that the returned matcher will use to match any examined java.lang.CharSequence
        @Factory
        public static Matcher<CharSequencepattern(final Pattern pattern) {
            return new PatternMatcher(pattern);
        }
        private final Pattern pattern;
        public PatternMatcher(final Pattern pattern) {
            this. = pattern;
        }
        @Override
        public boolean matchesSafely(final CharSequence item) {
            return .matcher(item).matches();
        }
        @Override
        public void describeMismatchSafely(final CharSequence item,
                final Description mismatchDescription) {
            mismatchDescription.
                    appendText("was \"").
                    appendText(String.valueOf(item)).
                    appendText("\"");
        }
        @Override
        public void describeTo(final Description description) {
            description.
                    appendText("a string with pattern \"").
                    appendText(String.valueOf()).
                    appendText("\"");
        }
    }
New to GrepCode? Check out our FAQ X