View Javadoc
1   /*
2    * Copyright 2016 The Apache Software Foundation.
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.apache.commons.jexl3;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import org.apache.commons.logging.Log;
21  
22  /**
23   * A log implementation to help control tests results.
24   */
25  public class CaptureLog implements Log {
26      private final List<Object[]> captured = new ArrayList<>();
27  
28      static Object caller() {
29          final StackTraceElement[] stack = new Exception().fillInStackTrace().getStackTrace();
30          return stack[2];
31      }
32  
33      public CaptureLog() {
34          this("org.apache.commons.jexl3");
35      }
36  
37      public CaptureLog(final String name) {
38          //super(name);
39      }
40  
41      public boolean isEmpty() {
42          return captured.isEmpty();
43      }
44  
45      public int count(final String type) {
46          int count = 0;
47          for (final Object[] l : captured) {
48              if (type.equals(l[0].toString())) {
49                  count += 1;
50              }
51          }
52          return count;
53      }
54  
55      //@Override
56      public boolean isEnabledFor(final int /*Priority*/ p) {
57          return true;
58      }
59  
60      @Override
61      public void debug(final Object o) {
62          captured.add(new Object[]{"debug", caller(), o});
63      }
64  
65      @Override
66      public void debug(final Object o, final Throwable thrwbl) {
67          captured.add(new Object[]{"debug", caller(), o, thrwbl});
68      }
69  
70      @Override
71      public void error(final Object o) {
72          captured.add(new Object[]{"error", caller(), o});
73      }
74  
75      @Override
76      public void error(final Object o, final Throwable thrwbl) {
77          captured.add(new Object[]{"error", caller(), o, thrwbl});
78      }
79  
80      @Override
81      public void fatal(final Object o) {
82          captured.add(new Object[]{"fatal", caller(), o});
83      }
84  
85      @Override
86      public void fatal(final Object o, final Throwable thrwbl) {
87          captured.add(new Object[]{"fatal", caller(), o, thrwbl});
88      }
89  
90      @Override
91      public void info(final Object o) {
92          captured.add(new Object[]{"info", caller(), o});
93      }
94  
95      @Override
96      public void info(final Object o, final Throwable thrwbl) {
97          captured.add(new Object[]{"info", caller(), o, thrwbl});
98      }
99  
100     @Override
101     public boolean isDebugEnabled() {
102         return true;
103     }
104 
105     @Override
106     public boolean isErrorEnabled() {
107         return true;
108     }
109 
110     @Override
111     public boolean isFatalEnabled() {
112         return true;
113     }
114 
115     @Override
116     public boolean isInfoEnabled() {
117         return true;
118     }
119 
120     @Override
121     public boolean isTraceEnabled() {
122         return true;
123     }
124 
125     @Override
126     public boolean isWarnEnabled() {
127         return true;
128     }
129 
130     @Override
131     public void trace(final Object o) {
132         captured.add(new Object[]{"trace", caller(), o});
133     }
134 
135     @Override
136     public void trace(final Object o, final Throwable thrwbl) {
137         captured.add(new Object[]{"trace", caller(), o, thrwbl});
138     }
139 
140     @Override
141     public void warn(final Object o) {
142         captured.add(new Object[]{"warn", caller(), o});
143     }
144 
145     @Override
146     public void warn(final Object o, final Throwable thrwbl) {
147         captured.add(new Object[]{"warn", caller(), o, thrwbl});
148     }
149 
150 }