View Javadoc
1   /*
2    * Copyright 2018 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Zoltan Farkas
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  }