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