1 /** 2 * Copyright (C) 2006 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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 */ 16 17 package com.google.inject.matcher; 18 19 import static com.google.inject.Asserts.assertEqualWhenReserialized; 20 import static com.google.inject.Asserts.assertEqualsBothWays; 21 import static com.google.inject.matcher.Matchers.annotatedWith; 22 import static com.google.inject.matcher.Matchers.any; 23 import static com.google.inject.matcher.Matchers.identicalTo; 24 import static com.google.inject.matcher.Matchers.inPackage; 25 import static com.google.inject.matcher.Matchers.inSubpackage; 26 import static com.google.inject.matcher.Matchers.not; 27 import static com.google.inject.matcher.Matchers.only; 28 import static com.google.inject.matcher.Matchers.returns; 29 import static com.google.inject.matcher.Matchers.subclassesOf; 30 31 import com.google.inject.name.Named; 32 import com.google.inject.name.Names; 33 34 import junit.framework.TestCase; 35 36 import java.io.IOException; 37 import java.lang.annotation.Retention; 38 import java.lang.annotation.RetentionPolicy; 39 import java.lang.reflect.Method; 40 import java.util.AbstractList; 41 42 /** 43 * @author crazybob (at) google.com (Bob Lee) 44 */ 45 46 public class MatcherTest extends TestCase { 47 48 public void testAny() { 49 assertTrue(any().matches(null)); 50 assertEquals("any()", any().toString()); 51 assertEqualsBothWays(any(), any()); 52 assertFalse(any().equals(not(any()))); 53 } 54 55 public void testNot() { 56 assertFalse(not(any()).matches(null)); 57 assertEquals("not(any())", not(any()).toString()); 58 assertEqualsBothWays(not(any()), not(any())); 59 assertFalse(not(any()).equals(any())); 60 } 61 62 public void testAnd() { 63 assertTrue(any().and(any()).matches(null)); 64 assertFalse(any().and(not(any())).matches(null)); 65 assertEquals("and(any(), any())", any().and(any()).toString()); 66 assertEqualsBothWays(any().and(any()), any().and(any())); 67 assertFalse(any().and(any()).equals(not(any()))); 68 } 69 70 public void testOr() { 71 assertTrue(any().or(not(any())).matches(null)); 72 assertFalse(not(any()).or(not(any())).matches(null)); 73 assertEquals("or(any(), any())", any().or(any()).toString()); 74 assertEqualsBothWays(any().or(any()), any().or(any())); 75 assertFalse(any().or(any()).equals(not(any()))); 76 } 77 78 public void testAnnotatedWith() { 79 assertTrue(annotatedWith(Foo.class).matches(Bar.class)); 80 assertFalse(annotatedWith(Foo.class).matches( 81 MatcherTest.class.getMethods()[0])); 82 assertEquals("annotatedWith(Foo.class)", annotatedWith(Foo.class).toString()); 83 assertEqualsBothWays(annotatedWith(Foo.class), annotatedWith(Foo.class)); 84 assertFalse(annotatedWith(Foo.class).equals(annotatedWith(Named.class))); 85 86 try { 87 annotatedWith(Baz.class); 88 fail(); 89 } catch (IllegalArgumentException expected) { 90 } 91 } 92 93 public void testSubclassesOf() { 94 assertTrue(subclassesOf(Runnable.class).matches(Runnable.class)); 95 assertTrue(subclassesOf(Runnable.class).matches(MyRunnable.class)); 96 assertFalse(subclassesOf(Runnable.class).matches(Object.class)); 97 assertEquals("subclassesOf(Runnable.class)", subclassesOf(Runnable.class).toString()); 98 assertEqualsBothWays(subclassesOf(Runnable.class), subclassesOf(Runnable.class)); 99 assertFalse(subclassesOf(Runnable.class).equals(subclassesOf(Object.class))); 100 } 101 102 public void testOnly() { 103 assertTrue(only(1000).matches(1000)); 104 assertFalse(only(1).matches(1000)); 105 assertEquals("only(1)", only(1).toString()); 106 assertEqualsBothWays(only(1), only(1)); 107 assertFalse(only(1).equals(only(2))); 108 } 109 110 @SuppressWarnings("UnnecessaryBoxing") 111 public void testIdenticalTo() { 112 Object o = new Object(); 113 assertEquals("identicalTo(1)", identicalTo(1).toString()); 114 assertTrue(identicalTo(o).matches(o)); 115 assertFalse(identicalTo(o).matches(new Object())); 116 assertEqualsBothWays(identicalTo(o), identicalTo(o)); 117 assertFalse(identicalTo(1).equals(identicalTo(new Integer(1)))); 118 } 119 120 public void testInPackage() { 121 Package matchersPackage = Matchers.class.getPackage(); 122 assertEquals("inPackage(com.google.inject.matcher)", inPackage(matchersPackage).toString()); 123 assertTrue(inPackage(matchersPackage).matches(MatcherTest.class)); 124 assertFalse(inPackage(matchersPackage).matches(Object.class)); 125 assertEqualsBothWays(inPackage(matchersPackage), inPackage(matchersPackage)); 126 assertFalse(inPackage(matchersPackage).equals(inPackage(Object.class.getPackage()))); 127 } 128 129 public void testInSubpackage() { 130 String stringPackageName = String.class.getPackage().getName(); 131 assertEquals("inSubpackage(java.lang)", inSubpackage(stringPackageName).toString()); 132 assertTrue(inSubpackage(stringPackageName).matches(Object.class)); 133 assertTrue(inSubpackage(stringPackageName).matches(Method.class)); 134 assertFalse(inSubpackage(stringPackageName).matches(Matchers.class)); 135 assertFalse(inSubpackage("jav").matches(Object.class)); 136 assertEqualsBothWays(inSubpackage(stringPackageName), inSubpackage(stringPackageName)); 137 assertFalse(inSubpackage(stringPackageName).equals(inSubpackage(Matchers.class.getPackage().getName()))); 138 } 139 140 public void testReturns() throws NoSuchMethodException { 141 Matcher<Method> predicate = returns(only(String.class)); 142 assertTrue(predicate.matches( 143 Object.class.getMethod("toString"))); 144 assertFalse(predicate.matches( 145 Object.class.getMethod("hashCode"))); 146 assertEquals("returns(only(class java.lang.String))", returns(only(String.class)).toString()); 147 assertEqualsBothWays(predicate, returns(only(String.class))); 148 assertFalse(predicate.equals(returns(only(Integer.class)))); 149 } 150 151 public void testSerialization() throws IOException { 152 assertEqualWhenReserialized(any()); 153 assertEqualWhenReserialized(not(any())); 154 assertEqualWhenReserialized(annotatedWith(Named.class)); 155 assertEqualWhenReserialized(annotatedWith(Names.named("foo"))); 156 assertEqualWhenReserialized(only("foo")); 157 assertEqualWhenReserialized(identicalTo(Object.class)); 158 assertEqualWhenReserialized(inPackage(String.class.getPackage())); 159 assertEqualWhenReserialized(inSubpackage(String.class.getPackage().getName())); 160 assertEqualWhenReserialized(returns(any())); 161 assertEqualWhenReserialized(subclassesOf(AbstractList.class)); 162 assertEqualWhenReserialized(only("a").or(only("b"))); 163 assertEqualWhenReserialized(only("a").and(only("b"))); 164 } 165 166 static abstract class MyRunnable implements Runnable {} 167 168 @Retention(RetentionPolicy.RUNTIME) 169 @interface Foo {} 170 171 @Foo 172 static class Bar {} 173 174 @interface Baz {} 175 176 @Baz 177 static class Car {} 178 } 179