01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package com.google.inject.matcher;
16:
17: import static com.google.inject.matcher.Matchers.*;
18:
19: import junit.framework.TestCase;
20:
21: import java.lang.annotation.Retention;
22: import java.lang.annotation.RetentionPolicy;
23: import java.lang.reflect.Method;
24:
25: /**
26: * @author crazybob@google.com (Bob Lee)
27: */
28: public class MatcherTest extends TestCase {
29:
30: public void testAny() {
31: assertTrue(any().matches(null));
32: }
33:
34: public void testNot() {
35: assertFalse(not(any()).matches(null));
36: }
37:
38: public void testAnd() {
39: assertTrue(any().and(any()).matches(null));
40: assertFalse(any().and(not(any())).matches(null));
41: }
42:
43: public void testAnnotatedWith() {
44: assertTrue(annotatedWith(Foo.class).matches(Bar.class));
45: assertFalse(annotatedWith(Foo.class).matches(
46: MatcherTest.class.getMethods()[0]));
47: }
48:
49: public void testSubclassesOf() {
50: assertTrue(subclassesOf(Runnable.class).matches(Runnable.class));
51: assertTrue(subclassesOf(Runnable.class).matches(
52: MyRunnable.class));
53: assertFalse(subclassesOf(Runnable.class).matches(Object.class));
54: }
55:
56: public void testOnly() {
57: assertTrue(only(1000).matches(new Integer(1000)));
58: assertFalse(only(1).matches(new Integer(1000)));
59: }
60:
61: public void testSameAs() {
62: Object o = new Object();
63: assertTrue(only(o).matches(o));
64: assertFalse(only(o).matches(new Object()));
65: }
66:
67: public void testInPackage() {
68: assertTrue(inPackage(Matchers.class.getPackage()).matches(
69: MatcherTest.class));
70: assertFalse(inPackage(Matchers.class.getPackage()).matches(
71: Object.class));
72: }
73:
74: public void testReturns() throws NoSuchMethodException {
75: Matcher<Method> predicate = returns(only(String.class));
76: assertTrue(predicate
77: .matches(Object.class.getMethod("toString")));
78: assertFalse(predicate.matches(Object.class
79: .getMethod("hashCode")));
80: }
81:
82: static abstract class MyRunnable implements Runnable {
83: }
84:
85: @Retention(RetentionPolicy.RUNTIME)
86: @interface Foo {
87: }
88:
89: @Foo
90: static class Bar {
91: }
92: }
|