MCPcopy Create free account
hub / github.com/google/guice / mapInjectorTest

Method mapInjectorTest

core/test/com/google/inject/internal/SpiUtils.java:136–336  ·  view source on GitHub ↗
(
      Key<T> mapKey,
      TypeLiteral<?> keyType,
      TypeLiteral<?> valueType,
      Iterable<? extends Module> modules,
      boolean allowDuplicates,
      int expectedMapBindings,
      MapResult<?, ?>... results)

Source from the content-addressed store, hash-verified

134 }
135
136 @SuppressWarnings("unchecked")
137 private static <T> void mapInjectorTest(
138 Key<T> mapKey,
139 TypeLiteral<?> keyType,
140 TypeLiteral<?> valueType,
141 Iterable<? extends Module> modules,
142 boolean allowDuplicates,
143 int expectedMapBindings,
144 MapResult<?, ?>... results) {
145 Injector injector = Guice.createInjector(modules);
146 Visitor<T> visitor = new Visitor<>();
147 Binding<T> mapBinding = injector.getBinding(mapKey);
148 MapBinderBinding<T> mapbinder = (MapBinderBinding<T>) mapBinding.acceptTargetVisitor(visitor);
149 assertNotNull(mapbinder);
150 assertEquals(mapKey, mapbinder.getMapKey());
151 assertEquals(keyType, mapbinder.getKeyTypeLiteral());
152 assertEquals(valueType, mapbinder.getValueTypeLiteral());
153 assertEquals(allowDuplicates, mapbinder.permitsDuplicates());
154 List<Map.Entry<?, Binding<?>>> entries = Lists.newArrayList(mapbinder.getEntries());
155 List<MapResult<?, ?>> mapResults = Lists.newArrayList(results);
156 assertEquals(
157 "wrong entries, expected: " + mapResults + ", but was: " + entries,
158 mapResults.size(),
159 entries.size());
160
161 for (MapResult<?, ?> result : mapResults) {
162 Map.Entry<?, Binding<?>> found = null;
163 for (Map.Entry<?, Binding<?>> entry : entries) {
164 Object key = entry.getKey();
165 Binding<?> value = entry.getValue();
166 if (key.equals(result.k) && matches(value, result.v)) {
167 found = entry;
168 break;
169 }
170 }
171 if (found == null) {
172 fail("Could not find entry: " + result + " in remaining entries: " + entries);
173 } else {
174 assertTrue(
175 "mapBinder doesn't contain: " + found.getValue(),
176 mapbinder.containsElement(found.getValue()));
177 entries.remove(found);
178 }
179 }
180
181 if (!entries.isEmpty()) {
182 fail("Found all entries of: " + mapResults + ", but more were left over: " + entries);
183 }
184
185 Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
186 Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
187 Key<?> mapOfCollectionOfProvider =
188 mapKey.ofType(mapOfCollectionOfProviderOf(keyType, valueType));
189 Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
190 Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
191 Key<?> collectionOfProvidersOfEntryOfProvider =
192 mapKey.ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
193 Key<?> setOfExtendsOfEntryOfProvider =

Callers 1

assertMapVisitorMethod · 0.95

Calls 15

createInjectorMethod · 0.95
getBindingMethod · 0.95
matchesMethod · 0.95
getMethod · 0.95
subtypeOfMethod · 0.95
getAllBindingsMethod · 0.95
isIndexableMethod · 0.95
ofTypeMethod · 0.80
mapOfProviderOfMethod · 0.80
mapOfSetOfProviderOfMethod · 0.80
entryOfProviderOfMethod · 0.80

Tested by

no test coverage detected