001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind.test; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.oro.text.regex.Pattern; 019 import org.apache.oro.text.regex.Perl5Compiler; 020 import org.apache.oro.text.regex.Perl5Matcher; 021 022 /** 023 * A {@link org.apache.hivemind.test.ArgumentMatcher} implementation that treats the expected 024 * value (provided while training the mock object) as a Perl5 Regular expression, which is matched 025 * against the actual value. 026 * 027 * @author Howard Lewis Ship 028 * @since 1.1 029 */ 030 public class RegexpMatcher extends AbstractArgumentMatcher 031 { 032 private static Perl5Compiler _compiler = new Perl5Compiler(); 033 034 private static Perl5Matcher _matcher = new Perl5Matcher(); 035 036 public boolean compareArguments(Object expected, Object actual) 037 { 038 return matchRegexp((String) expected, (String) actual); 039 } 040 041 private boolean matchRegexp(String expectedRegexp, String actualString) 042 { 043 try 044 { 045 Pattern p = _compiler.compile(expectedRegexp); 046 047 return _matcher.matches(actualString, p); 048 } 049 catch (Exception ex) 050 { 051 throw new ApplicationRuntimeException(ex); 052 } 053 } 054 }