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