1 5 package polyglot.pth; 6 7 import java.util.regex.Matcher ; 8 import java.util.regex.Pattern ; 9 10 import polyglot.util.ErrorInfo; 11 12 15 public class ExpectedFailure { 16 public ExpectedFailure(int kind) { 17 this(kind, null); 18 } 19 public ExpectedFailure(String errMsg) { 20 this(null, errMsg); 21 } 22 public ExpectedFailure(int kind, String errMsg) { 23 this(new Integer (kind), errMsg); 24 } 25 public ExpectedFailure(Integer kind, String errMsg) { 26 this.kind = kind; 27 this.errMsgRegExp = errMsg; 28 } 29 final Integer kind; 30 final String 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 m = Pattern.compile(errMsgRegExp).matcher(e.getMessage()); 38 return m.find(); 39 } 40 return true; 41 } 42 43 public boolean equals(Object 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 toString() { 58 StringBuffer sb = new StringBuffer (); 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 |