use of com.google.inject.Binding in project gerrit by GerritCodeReview.
the class PrivateInternals_DynamicTypes method dynamicMapsOf.
public static Map<TypeLiteral<?>, DynamicMap<?>> dynamicMapsOf(Injector src) {
Map<TypeLiteral<?>, DynamicMap<?>> m = new HashMap<>();
for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
TypeLiteral<?> type = e.getKey().getTypeLiteral();
if (type.getRawType() == DynamicMap.class) {
ParameterizedType p = (ParameterizedType) type.getType();
m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicMap<?>) e.getValue().getProvider().get());
}
}
if (m.isEmpty()) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(m);
}
use of com.google.inject.Binding in project gerrit by GerritCodeReview.
the class PrivateInternals_DynamicTypes method dynamicSetsOf.
public static Map<TypeLiteral<?>, DynamicSet<?>> dynamicSetsOf(Injector src) {
Map<TypeLiteral<?>, DynamicSet<?>> m = new HashMap<>();
for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
TypeLiteral<?> type = e.getKey().getTypeLiteral();
if (type.getRawType() == DynamicSet.class) {
ParameterizedType p = (ParameterizedType) type.getType();
m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicSet<?>) e.getValue().getProvider().get());
}
}
if (m.isEmpty()) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(m);
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class ExtensionSpiTest method testSpiOnInjector.
public final void testSpiOnInjector() {
ServletSpiVisitor visitor = new ServletSpiVisitor(true);
int count = 0;
Injector injector = Guice.createInjector(new Module());
for (Binding binding : injector.getBindings().values()) {
assertEquals(count++, binding.acceptTargetVisitor(visitor));
}
validateVisitor(visitor);
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class FilterDefinitionTest method testFilterCreateDispatchDestroySupressChain.
public final void testFilterCreateDispatchDestroySupressChain() throws ServletException, IOException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
final MockFilter mockFilter = new MockFilter() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//suppress rest of chain...
}
};
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(request.getRequestURI()).andReturn("/index.html");
expect(request.getContextPath()).andReturn("").anyTimes();
replay(injector, binding, request);
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
//should fire on mockfilter now
filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
assertTrue(filterDef.getFilter() instanceof MockFilter);
assertTrue("init did not fire", mockFilter.isInit());
Filter matchingFilter = filterDef.getFilterIfMatching(request);
assertSame(mockFilter, matchingFilter);
final boolean[] proceed = new boolean[1];
matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
proceed[0] = true;
}
});
assertFalse("filter did not suppress chain", proceed[0]);
filterDef.destroy(Sets.<Filter>newIdentityHashSet());
assertTrue("destroy did not fire", mockFilter.isDestroy());
verify(injector, request);
}
use of com.google.inject.Binding in project roboguice by roboguice.
the class FilterDefinitionTest method testFilterCreateDispatchDestroy.
public final void testFilterCreateDispatchDestroy() throws ServletException, IOException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
final MockFilter mockFilter = new MockFilter();
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(request.getRequestURI()).andReturn("/index.html");
expect(request.getContextPath()).andReturn("").anyTimes();
replay(injector, binding, request);
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
//should fire on mockfilter now
filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
assertTrue(filterDef.getFilter() instanceof MockFilter);
assertTrue("Init did not fire", mockFilter.isInit());
Filter matchingFilter = filterDef.getFilterIfMatching(request);
assertSame(mockFilter, matchingFilter);
final boolean[] proceed = new boolean[1];
matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
proceed[0] = true;
}
});
assertTrue("Filter did not proceed down chain", proceed[0]);
filterDef.destroy(Sets.<Filter>newIdentityHashSet());
assertTrue("Destroy did not fire", mockFilter.isDestroy());
verify(injector, request);
}
Aggregations