KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > samples > FirstCharMatcher


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.samples;
6
7 import org.easymock.AbstractMatcher;
8
9 public class FirstCharMatcher extends AbstractMatcher {
10     protected boolean argumentMatches(Object JavaDoc expected, Object JavaDoc actual) {
11         if (expected instanceof String JavaDoc) {
12             expected = firstCharacterIfString(expected);
13         }
14         if (actual instanceof String JavaDoc) {
15             actual = firstCharacterIfString(actual);
16         }
17         return super.argumentMatches(expected, actual);
18     }
19
20     protected String JavaDoc argumentToString(Object JavaDoc argument) {
21         if (argument instanceof String JavaDoc) {
22             argument = firstCharacterIfString(argument) + "...";
23         }
24         return super.argumentToString(argument);
25     }
26
27     private Object JavaDoc firstCharacterIfString(Object JavaDoc object) {
28         if (!(object instanceof String JavaDoc)) {
29             return object;
30         }
31         String JavaDoc string = (String JavaDoc) object;
32         return string.length() > 0 ? string.substring(0, 1) : string;
33     }
34 }
35
Popular Tags