1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.spf4j.test.matchers;
17
18 import org.hamcrest.Matcher;
19 import org.hamcrest.Matchers;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.spf4j.test.log.Attachments;
23 import org.spf4j.log.Level;
24 import org.spf4j.test.log.TestLogRecordImpl;
25
26
27
28
29
30 public class LogMatchersTest {
31
32
33 @Test(expected = AssertionError.class)
34 public void testMatching() {
35 TestLogRecordImpl rec = new TestLogRecordImpl("test", Level.ERROR, "la la {} bla", "a", "b");
36 rec.attach(Attachments.PRINTED);
37 rec.attach(Attachments.ASSERTED);
38 Assert.assertThat(rec, Matchers.allOf(LogMatchers.noAttachment(Attachments.ASSERTED),
39 LogMatchers.hasLevel(Level.ERROR)));
40
41 Assert.assertThat(rec, Matchers.allOf(LogMatchers.hasMessageWithPattern("la la a .*"),
42 LogMatchers.hasLevel(Level.ERROR)));
43 Assert.assertThat(rec, Matchers.allOf(LogMatchers.hasArgumentAt(0, "a"),
44 LogMatchers.hasLevel(Level.ERROR)));
45 Assert.assertThat(rec, Matchers.allOf(LogMatchers.hasExtraArgumentAt(0, "b"),
46 LogMatchers.hasLevel(Level.ERROR)));
47
48 }
49
50 @Test
51 @SuppressWarnings("unchecked")
52 public void testArgumentMatch() {
53 TestLogRecordImpl rec = new TestLogRecordImpl("test", Level.ERROR, "la la {} bla", "a", "b", 5L);
54 Assert.assertThat(rec, Matchers.allOf(LogMatchers.hasMatchingArguments(
55 Matchers.arrayContaining((Matcher) Matchers.equalTo("a"),
56 (Matcher) Matchers.equalTo("b"),
57 (Matcher) Matchers.allOf(Matchers.greaterThan(1L), Matchers.lessThan(7L)))
58 )));
59 }
60
61 @Test
62 public void testAssertThrowable() {
63 TestLogRecordImpl rec = new TestLogRecordImpl("test", Level.ERROR, "la la {} bla", "a", "b", 5L,
64 new IllegalStateException("My exception"));
65 Assert.assertThat(rec, Matchers.allOf(
66 LogMatchers.hasMatchingExtraThrowable(Matchers.hasProperty("message", Matchers.equalTo("My exception"))),
67 LogMatchers.hasMatchingExtraArguments(
68 Matchers.arrayContaining(
69 (Matcher) Matchers.equalTo("b"),
70 (Matcher) Matchers.allOf(Matchers.greaterThan(1L), Matchers.lessThan(7L))))
71 ));
72 }
73
74 }