KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > pth > ExpectedFailure


1 /*
2  * Author : Stephen Chong
3  * Created: Nov 24, 2003
4  */

5 package polyglot.pth;
6
7 import java.util.regex.Matcher JavaDoc;
8 import java.util.regex.Pattern JavaDoc;
9
10 import polyglot.util.ErrorInfo;
11
12 /**
13  *
14  */

15 public class ExpectedFailure {
16     public ExpectedFailure(int kind) {
17         this(kind, null);
18     }
19     public ExpectedFailure(String JavaDoc errMsg) {
20         this(null, errMsg);
21     }
22     public ExpectedFailure(int kind, String JavaDoc errMsg) {
23         this(new Integer JavaDoc(kind), errMsg);
24     }
25     public ExpectedFailure(Integer JavaDoc kind, String JavaDoc errMsg) {
26         this.kind = kind;
27         this.errMsgRegExp = errMsg;
28     }
29     final Integer JavaDoc kind;
30     final String JavaDoc errMsgRegExp;
31     
32     public boolean matches(ErrorInfo e) {
33         if (kind != null && kind.intValue() != e.getErrorKind()) {
34             return false;
35         }
36         if (errMsgRegExp != null) {
37             Matcher JavaDoc m = Pattern.compile(errMsgRegExp).matcher(e.getMessage());
38             return m.find();
39         }
40         return true;
41     }
42     
43     public boolean equals(Object JavaDoc o) {
44         if (o instanceof ExpectedFailure) {
45             ExpectedFailure that = (ExpectedFailure)o;
46             return (that.kind == this.kind || (that.kind != null && that.kind.equals(this.kind))) &&
47                 (that.errMsgRegExp == this.errMsgRegExp ||
48             (that.errMsgRegExp != null && that.errMsgRegExp.equals(this.errMsgRegExp)));
49         }
50         return false;
51     }
52     public int hashCode() {
53         return (errMsgRegExp==null ? -323
54                                    : errMsgRegExp.hashCode())
55               + (kind==null ? 41 : kind.hashCode());
56     }
57     public String JavaDoc toString() {
58         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
59         if (kind != null) {
60             sb.append(ErrorInfo.getErrorString(kind.intValue()));
61         }
62         else {
63             sb.append("error");
64         }
65         if (errMsgRegExp != null) {
66             sb.append(" matching the regular expression '");
67             sb.append(errMsgRegExp);
68             sb.append('\'');
69         }
70         return sb.toString();
71     }
72 }
73
Popular Tags