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.log.annotations;
17
18 import java.lang.annotation.ElementType;
19 import java.lang.annotation.Repeatable;
20 import java.lang.annotation.Retention;
21 import java.lang.annotation.RetentionPolicy;
22 import java.lang.annotation.Target;
23 import java.util.concurrent.TimeUnit;
24 import org.spf4j.log.Level;
25
26 /**
27 * Annotation to assert log behavior.
28 * This annotation will describe a expected log event. If this expected log event will not happen during the
29 * unit test execution, the unit test will fail.
30 * @author Zoltan Farkas
31 * @see org.spf4j.test.log.annotations
32 */
33 @Retention(RetentionPolicy.RUNTIME)
34 @Target(ElementType.METHOD)
35 @Repeatable(ExpectLogs.class)
36 public @interface ExpectLog {
37 /**
38 * @return the log category to expect. ("" is the root category).
39 */
40 String category() default "";
41
42 /**
43 * @return the log category to expect. (Void.class is the root category and default).
44 */
45 Class<?> categoryClass() default Void.class;
46
47 /**
48 * @return expected log level.
49 */
50 Level level() default Level.ERROR;
51 /**
52 * @return regexp of expected log message.
53 */
54 String messageRegexp() default ".*";
55
56 /**
57 * @return nr of times the expectation to be asserted.
58 */
59 int nrTimes() default 1;
60
61 /**
62 * @return the number of TU to wait for a log message after the unit test execution is finished.
63 */
64 long expectationTimeout() default 0;
65
66 /**
67 * @return expectation timeout unit.
68 */
69 TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
70
71 final class Util {
72
73 private Util() { }
74
75 public static String effectiveCategory(final ExpectLog ann) {
76 Class<?> categoryClass = ann.categoryClass();
77 if (categoryClass == Void.class) {
78 return ann.category();
79 } else {
80 return categoryClass.getName();
81 }
82 }
83 }
84
85 }