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.reflect;
21  
22  import org.grouplens.grapht.*;
23  
24  import javax.inject.Provider;
25  import java.lang.reflect.Type;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  
30  
31  /**
32   * MockSatisfaction is a simple implementation of Satisfactions for certain
33   * types of test cases. It can be configured by its constructors, although
34   * {@link Satisfaction#makeInstantiator(Map, LifecycleManager)} always returns the same provider.
35   * 
36   * @author <a href="http://grouplens.org">GroupLens Research</a>
37   */
38  public class MockSatisfaction implements Satisfaction {
39      private static final long serialVersionUID = 1L;
40  
41      private final Provider<?> provider;
42      private final Class<?> type;
43      private final List<Desire> dependencies;
44      
45      public MockSatisfaction() {
46          this(Object.class, new Object(), new ArrayList<Desire>());
47      }
48      
49      public MockSatisfaction(Class<?> type) {
50          this(type, new ArrayList<Desire>());
51      }
52      
53      public MockSatisfaction(Class<?> type, List<Desire> dependencies) {
54          this(type, new NullProvider(), dependencies);
55      }
56      
57      public MockSatisfaction(Class<?> type, Object instance, List<Desire> dependencies) {
58          this(type, new InstanceProvider(instance), dependencies);
59      }
60      
61      public MockSatisfaction(Class<?> type, Provider<?> provider, List<Desire> dependencies) {
62          this.type = type;
63          this.provider = provider;
64          this.dependencies = dependencies;
65      }
66      
67      @Override
68      public CachePolicy getDefaultCachePolicy() {
69          return CachePolicy.NO_PREFERENCE;
70      }
71      
72      @Override
73      public List<Desire> getDependencies() {
74          return dependencies;
75      }
76  
77      @Override
78      public Type getType() {
79          return type;
80      }
81  
82      @Override
83      public Class<?> getErasedType() {
84          return type;
85      }
86  
87      @Override
88      public boolean hasInstance() {
89          return true;
90      }
91  
92      @Override
93      public <T> T visit(SatisfactionVisitor<T> visitor) {
94          throw new UnsupportedOperationException("cannot visit the mock satisfaction");
95      }
96  
97      @Override
98      public Instantiator makeInstantiator(Map<Desire,Instantiator> dependencies,
99                                           LifecycleManager lm) {
100         return Instantiators.ofProvider(provider);
101     }
102 
103     @Override
104     public String toString() {
105         StringBuilder sb = new StringBuilder();
106         return sb.append("mock satisfaction of ")
107                  .append(type)
108                  .append(" with provider ")
109                  .append(provider)
110                  .append(" (")
111                  .append(dependencies.size())
112                  .append(" dependencies)")
113                  .toString();
114     }
115 
116     @SuppressWarnings("rawtypes")
117     private static class NullProvider implements Provider {
118         @Override
119         public Object get() {
120             return null;
121         }
122 
123         @Override
124         public String toString() {
125             return "return null";
126         }
127     }
128     
129     @SuppressWarnings("rawtypes")
130     private static class InstanceProvider implements Provider {
131         private final Object instance;
132         
133         public InstanceProvider(Object instance) {
134             this.instance = instance;
135         }
136         
137         @Override
138         public Object get() {
139             return instance;
140         }
141 
142         @Override
143         public String toString() {
144             return "return " + instance;
145         }
146     }
147 }