View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 }