1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.testtools.common;
17
18 import org.apache.commons.lang3.exception.ExceptionUtils;
19
20 import java.io.IOException;
21 import java.util.Iterator;
22 import java.util.Properties;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class JiraAwareFailureUtils {
47
48
49
50
51
52
53
54 public static final String JIRA_BROWSE_URL_PROPERTY = "jira.aware.browse.url";
55
56 private static String JIRA_BROWSE_URL = System.getProperty(JIRA_BROWSE_URL_PROPERTY,"https://jira.kuali.org/browse/");
57
58 private static String REGEX_DEFAULT_PROPS_LOCATION = "JiraAwareRegexFailures.properties";
59
60
61
62
63
64
65
66 public static final String REGEX_LOCATION_POPERTY = "jira.aware.regex.failures.location";
67
68 private static String REGEX_PROPS_LOCATION = System.getProperty(REGEX_LOCATION_POPERTY, REGEX_DEFAULT_PROPS_LOCATION);
69
70 private static String CONTAINS_DEFAULT_PROPS_LOCATION = "JiraAwareContainsFailures.properties";
71
72
73
74
75
76
77
78 public static final String CONTAINS_LOCATION_PROERTY = "jira.aware.contains.failures.location";
79
80 private static String CONTAINS_PROPS_LOCATION = System.getProperty(CONTAINS_LOCATION_PROERTY, CONTAINS_DEFAULT_PROPS_LOCATION);
81
82 static Properties regexJiraMatches;
83
84 static Properties jiraMatches;
85
86 static {
87 try {
88 regexJiraMatches = new PropertiesUtils().loadProperties(REGEX_PROPS_LOCATION, REGEX_DEFAULT_PROPS_LOCATION);
89 } catch (IOException e) {
90 e.printStackTrace();
91 }
92
93 try {
94 jiraMatches = new PropertiesUtils().loadProperties(CONTAINS_PROPS_LOCATION, CONTAINS_DEFAULT_PROPS_LOCATION);
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 }
99
100 private JiraAwareFailureUtils() {}
101
102
103
104
105
106
107
108
109
110 public static void fail(String message, JiraAwareFailable failable) {
111 failOnMatchedJira(message, failable);
112 failable.fail(message);
113 }
114
115
116
117
118
119
120
121
122
123
124 public static void fail(String contents, String message, JiraAwareFailable failable) {
125 failOnMatchedJira(contents, message, failable);
126 failable.fail(contents + " " + message);
127 }
128
129
130
131
132
133
134
135
136
137
138 public static void fail(String contents, String message, Throwable throwable, JiraAwareFailable failable) {
139 failOnMatchedJira(contents, message, failable);
140 if (throwable != null) {
141 failOnMatchedJira(ExceptionUtils.getStackTrace(throwable), throwable.getMessage(), failable);
142 failable.fail(contents + " " + message + " " + throwable.getMessage() + "\n\t" + ExceptionUtils.getStackTrace(throwable));
143 }
144 failable.fail(contents + " " + message + " " + contents);
145 }
146
147
148
149
150
151
152
153
154
155
156 public static void failOnMatchedJira(String contents, String message, JiraAwareFailable failable) {
157 if (message == null) {
158 message = "";
159 }
160
161 String match = findMatchedJiraContains(message);
162 if (match != null && !match.equals("")) {
163 failable.fail(match);
164 }
165
166 match = findMatchedJiraContains(contents);
167 if (match != null && !match.equals("")) {
168 failable.fail(match);
169 }
170
171 match = findMatchedJiraRegex(message);
172 if (match != null && !match.equals("")) {
173 failable.fail(match);
174 }
175
176 match = findMatchedJiraRegex(contents);
177 if (match != null && !match.equals("")) {
178 failable.fail(match);
179 }
180 }
181
182
183
184
185
186
187
188
189
190 public static void failOnMatchedJira(String contents, JiraAwareFailable failable) {
191 String match = findMatchedJira(contents);
192 if (match != null && !match.equals("")) {
193 failable.fail(match);
194 }
195 }
196
197
198
199
200
201
202
203
204
205 public static String findMatchedJira(String contents) {
206 String match = findMatchedJiraContains(contents);
207 if (match != null && !"".equals(match)) {
208 return match;
209 }
210
211 return findMatchedJiraRegex(contents);
212 }
213
214 protected static String findMatchedJiraContains(String contents) {
215 if (jiraMatches == null || jiraMatches.keySet() == null) {
216 System.out.println("WARNING JiraAwareFailureUtils contains properties empty, findMatchesJira contains not available.");
217 return "";
218 }
219 String key = null;
220
221 Iterator iter = jiraMatches.keySet().iterator();
222
223 while (iter.hasNext()) {
224 key = (String)iter.next();
225 if (contents.contains(key)) {
226 return("\n" + JIRA_BROWSE_URL + jiraMatches.get(key) + "\n\n" + contents);
227 }
228 }
229
230 return "";
231 }
232
233 protected static String findMatchedJiraRegex(String contents) {
234 if (regexJiraMatches == null || regexJiraMatches.keySet() == null) {
235 System.out.println("WARNING JiraAwareFailureUtils Regex properties empty, findMatchesJiraRegex not available.");
236 return "";
237 }
238 String key = null;
239 Pattern pattern = null;
240 Matcher matcher = null;
241
242 Iterator iter = regexJiraMatches.keySet().iterator();
243
244 while (iter.hasNext()) {
245 key = (String)iter.next();
246 pattern = Pattern.compile(key, Pattern.DOTALL);
247 matcher = pattern.matcher(contents);
248
249 if (matcher.find()) {
250 return("\n" + JIRA_BROWSE_URL + regexJiraMatches.get(key) + "\n\n" + contents);
251 }
252 }
253
254 return "";
255 }
256
257 }