1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.context;
21
22 import org.apache.commons.lang3.tuple.Pair;
23 import org.grouplens.grapht.annotation.AnnotationBuilder;
24 import org.grouplens.grapht.reflect.InjectionPoint;
25 import org.grouplens.grapht.reflect.MockInjectionPoint;
26 import org.grouplens.grapht.reflect.QualifierMatcher;
27 import org.grouplens.grapht.reflect.Satisfaction;
28 import org.grouplens.grapht.reflect.internal.ClassSatisfaction;
29 import org.grouplens.grapht.reflect.Qualifiers;
30 import org.grouplens.grapht.reflect.internal.types.RoleA;
31 import org.grouplens.grapht.reflect.internal.types.RoleB;
32 import org.grouplens.grapht.reflect.internal.types.RoleD;
33 import org.junit.Assert;
34 import org.junit.Test;
35
36 import java.lang.annotation.Annotation;
37
38 import static org.hamcrest.Matchers.notNullValue;
39 import static org.hamcrest.Matchers.nullValue;
40
41 public class TypeElementMatcherTest {
42 @Test
43 public void testEquals() {
44 TypeElementMatcher m1 = new TypeElementMatcher(A.class);
45 TypeElementMatcher m2 = new TypeElementMatcher(B.class);
46 TypeElementMatcher m3 = new TypeElementMatcher(A.class, Qualifiers.match(RoleA.class));
47 TypeElementMatcher m4 = new TypeElementMatcher(A.class, Qualifiers.match(RoleB.class));
48
49 Assert.assertEquals(m1, new TypeElementMatcher(A.class));
50 Assert.assertEquals(m2, new TypeElementMatcher(B.class));
51 Assert.assertEquals(m3, new TypeElementMatcher(A.class, Qualifiers.match(RoleA.class)));
52 Assert.assertEquals(m4, new TypeElementMatcher(A.class, Qualifiers.match(RoleB.class)));
53
54 Assert.assertFalse(m1.equals(m2));
55 Assert.assertFalse(m2.equals(m3));
56 Assert.assertFalse(m3.equals(m4));
57 Assert.assertFalse(m4.equals(m1));
58 }
59
60 @Test
61 public void testExactClassNoRoleMatch() {
62 doTestMatch(A.class, null, A.class, null, true);
63 doTestMatch(C.class, null, C.class, null, true);
64 }
65
66 @Test
67 public void testExactClassExactRoleMatch() {
68 doTestMatch(A.class, RoleA.class, A.class, RoleA.class, true);
69 }
70
71 @Test
72 public void testSubclassNoRoleMatch() {
73 doTestMatch(A.class, null, B.class, null, true);
74 }
75
76 @Test
77 public void testNoClassInheritenceSubRoleNoMatch() {
78 doTestMatch(C.class, RoleA.class, A.class, RoleB.class, false);
79 doTestMatch(B.class, RoleA.class, A.class, RoleB.class, false);
80 }
81
82 @Test
83 public void testSubclassNoRoleInheritenceNoMatch() {
84 doTestMatch(A.class, RoleA.class, B.class, RoleD.class, false);
85 }
86
87 @Test
88 public void testNull() {
89 doTestMatch(null, null, null, null, true);
90 doTestMatch(null, null, A.class, null, false);
91 }
92
93 @SuppressWarnings({ "unchecked", "rawtypes" })
94 private void doTestMatch(Class<?> matcherType, Class<? extends Annotation> matcherRole,
95 Class<?> satisfactionType, Class<? extends Annotation> satisfactionRole,
96 boolean expected) {
97 QualifierMatcher mr = (matcherRole == null ? Qualifiers.matchDefault() : Qualifiers.match(matcherRole));
98 final Annotation sr = (satisfactionRole == null ? null : new AnnotationBuilder(satisfactionRole).build());
99 Satisfaction sat = null;
100 if (satisfactionType != null) {
101 sat = new ClassSatisfaction(satisfactionType);
102 }
103 InjectionPoint ip = new MockInjectionPoint(satisfactionType, sr, false);
104 Pair<Satisfaction, InjectionPoint> node = Pair.of(sat, ip);
105
106 TypeElementMatcher cm = new TypeElementMatcher(matcherType, mr);
107 if (expected) {
108 Assert.assertThat(cm.apply(node), notNullValue());
109 } else {
110 Assert.assertThat(cm.apply(node), nullValue());
111 }
112 }
113
114 public static class A { }
115
116 public static class B extends A { }
117
118 public static class C { }
119 }