KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > test > RegexpMatcher


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.test;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.oro.text.regex.Pattern;
19 import org.apache.oro.text.regex.Perl5Compiler;
20 import org.apache.oro.text.regex.Perl5Matcher;
21
22 /**
23  * A {@link org.apache.hivemind.test.ArgumentMatcher} implementation that treats the expected
24  * value (provided while training the mock object) as a Perl5 Regular expression, which is matched
25  * against the actual value.
26  *
27  * @author Howard Lewis Ship
28  * @since 1.1
29  */

30 public class RegexpMatcher extends AbstractArgumentMatcher
31 {
32     private static Perl5Compiler _compiler = new Perl5Compiler();
33
34     private static Perl5Matcher _matcher = new Perl5Matcher();
35
36     public boolean compareArguments(Object JavaDoc expected, Object JavaDoc actual)
37     {
38         return matchRegexp((String JavaDoc) expected, (String JavaDoc) actual);
39     }
40
41     private boolean matchRegexp(String JavaDoc expectedRegexp, String JavaDoc actualString)
42     {
43         try
44         {
45             Pattern p = _compiler.compile(expectedRegexp);
46
47             return _matcher.matches(actualString, p);
48         }
49         catch (Exception JavaDoc ex)
50         {
51             throw new ApplicationRuntimeException(ex);
52         }
53     }
54 }
Popular Tags