1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.spf4j.test.matchers;
17
18 import java.util.Objects;
19 import java.util.Set;
20 import java.util.function.Predicate;
21 import java.util.stream.Collectors;
22 import org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.hamcrest.Matcher;
25 import org.hamcrest.Matchers;
26 import org.slf4j.Marker;
27 import org.spf4j.log.Level;
28 import org.spf4j.test.log.TestLogRecord;
29
30
31
32
33
34 public final class LogMatchers {
35
36 private LogMatchers() { }
37
38 public static Matcher<TestLogRecord> hasMatchingFormat(final Matcher<String> tMatcher) {
39 return Matchers.hasProperty("messageFormat", tMatcher);
40 }
41
42 public static Matcher<TestLogRecord> hasFormat(final String format) {
43 return Matchers.hasProperty("messageFormat", Matchers.equalTo(format));
44 }
45
46 public static Matcher<TestLogRecord> hasFormatWithPattern(final String formatPattern) {
47 return Matchers.hasProperty("messageFormat", PatternMatcher.matchesPattern(formatPattern));
48 }
49
50 public static Matcher<TestLogRecord> hasMatchingMarker(final Matcher<Marker> tMatcher) {
51 return Matchers.hasProperty("marker", tMatcher);
52 }
53
54 public static Matcher<TestLogRecord> hasMarker(final Marker marker) {
55 return Matchers.hasProperty("marker", Matchers.equalTo(marker));
56 }
57
58 public static Matcher<TestLogRecord> hasMatchingMessage(final Matcher<String> tMatcher) {
59 return Matchers.hasProperty("message", tMatcher);
60 }
61
62 public static Matcher<TestLogRecord> hasMessage(final String message) {
63 return Matchers.hasProperty("message", Matchers.equalTo(message));
64 }
65
66 public static Matcher<TestLogRecord> hasMessageWithPattern(final String messagePattern) {
67 return Matchers.hasProperty("message", PatternMatcher.matchesPattern(messagePattern));
68 }
69
70 public static Matcher<TestLogRecord> hasLevel(final Level level) {
71 return Matchers.hasProperty("level", Matchers.equalTo(level));
72 }
73
74 public static Matcher<TestLogRecord> hasNotLoggers(final Set<String> loggers) {
75 Iterable<Matcher<String>> matchers = loggers.stream()
76 .map((x) -> Matchers.startsWith(x)).collect(Collectors.toList());
77 return Matchers.not(Matchers.hasProperty("loggerName", Matchers.anyOf((Iterable) matchers)));
78 }
79
80 public static Matcher<TestLogRecord> hasNotLogger(final Predicate<String> pred) {
81 return Matchers.not(Matchers.hasProperty("loggerName", PredicateMatcher.matchesPredicate(pred)));
82 }
83
84 public static Matcher<TestLogRecord> hasMatchingArguments(final Matcher<Object[]> matcher) {
85 return Matchers.hasProperty("arguments", matcher);
86 }
87
88 public static Matcher<TestLogRecord> hasArguments(final Object... objects) {
89 return Matchers.hasProperty("arguments", Matchers.arrayContaining(objects));
90 }
91
92 public static Matcher<TestLogRecord> hasArgumentAt(final int idx, final Object object) {
93 return new BaseMatcher<TestLogRecord>() {
94 @Override
95 public boolean matches(final Object item) {
96 if (item instanceof TestLogRecord) {
97 TestLogRecord lr = (TestLogRecord) item;
98 Object[] arguments = lr.getArguments();
99 return idx < arguments.length && Objects.equals(arguments[idx], object);
100 } else {
101 return false;
102 }
103 }
104
105 @Override
106 public void describeTo(final Description description) {
107 description.appendText("Message argument [").appendValue(idx).appendText("] is ").appendValue(object);
108 }
109 };
110 }
111
112 public static Matcher<TestLogRecord> hasMatchingArgumentAt(final int idx, final Matcher<Object> matcher) {
113 return new BaseMatcher<TestLogRecord>() {
114 @Override
115 public boolean matches(final Object item) {
116 if (item instanceof TestLogRecord) {
117 TestLogRecord lr = (TestLogRecord) item;
118 Object[] arguments = lr.getArguments();
119 return idx < arguments.length && matcher.matches(arguments[idx]);
120 } else {
121 return false;
122 }
123 }
124
125 @Override
126 public void describeTo(final Description description) {
127 description.appendText("Message argument [").appendValue(idx).appendText("] matches ");
128 matcher.describeTo(description);
129 }
130 };
131 }
132
133 public static Matcher<TestLogRecord> hasAttachment(final String attachment) {
134 return Matchers.hasProperty("attachments", Matchers.hasItem(attachment));
135 }
136
137 public static Matcher<TestLogRecord> noAttachment(final String attachment) {
138 return Matchers.not(Matchers.hasProperty("attachments", Matchers.hasItem(attachment)));
139 }
140
141 public static Matcher<TestLogRecord> hasExtraArguments(final Object... objects) {
142 return Matchers.hasProperty("extraArguments", Matchers.arrayContaining(objects));
143 }
144
145 public static Matcher<TestLogRecord> hasExtraArgumentAt(final int idx, final Object object) {
146 return new BaseMatcher<TestLogRecord>() {
147 @Override
148 public boolean matches(final Object item) {
149 if (item instanceof TestLogRecord) {
150 TestLogRecord lr = (TestLogRecord) item;
151 Object[] arguments = lr.getExtraArguments();
152 return idx < arguments.length && Objects.equals(arguments[idx], object);
153 } else {
154 return false;
155 }
156 }
157
158 @Override
159 public void describeTo(final Description description) {
160 description.appendText("Log extra argument [").appendValue(idx).appendText("] is ").appendValue(object);
161 }
162 };
163 }
164
165 public static Matcher<TestLogRecord> hasExtraArgument(final Object object) {
166 return new BaseMatcher<TestLogRecord>() {
167 @Override
168 public boolean matches(final Object item) {
169 if (item instanceof TestLogRecord) {
170 TestLogRecord lr = (TestLogRecord) item;
171 Object[] arguments = lr.getExtraArguments();
172 for (Object arg : arguments) {
173 if (Objects.equals(arg, object)) {
174 return true;
175 }
176 }
177 }
178 return false;
179 }
180
181 @Override
182 public void describeTo(final Description description) {
183 description.appendText("Log extra argument is ").appendValue(object);
184 }
185 };
186 }
187
188
189 public static Matcher<TestLogRecord> hasMatchingExtraArgumentsContaining(final Matcher<Object>... matcher) {
190 return Matchers.hasProperty("extraArguments", Matchers.hasItems(matcher));
191 }
192
193 public static Matcher<TestLogRecord> hasMatchingExtraArguments(final Matcher<Object[]> matcher) {
194 return Matchers.hasProperty("extraArguments", matcher);
195 }
196
197 public static Matcher<TestLogRecord> hasMatchingExtraThrowable(final Matcher<Throwable> matcher) {
198 return Matchers.hasProperty("extraThrowable", matcher);
199 }
200
201 public static Matcher<TestLogRecord> hasMatchingExtraThrowableChain(final Matcher<Iterable<Throwable>> matcher) {
202 return Matchers.hasProperty("extraThrowableChain", matcher);
203 }
204
205 }