PageRenderTime 1588ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/grapher/test/com/google/inject/grapher/AbstractInjectorGrapherTest.java

https://gitlab.com/metamorphiccode/guice
Java | 184 lines | 138 code | 25 blank | 21 comment | 0 complexity | b848a29458fe2254aa40b7578245596b MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.grapher;
  17. import com.google.common.collect.ImmutableList;
  18. import com.google.common.collect.ImmutableSet;
  19. import com.google.common.collect.Sets;
  20. import com.google.inject.AbstractModule;
  21. import com.google.inject.BindingAnnotation;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Inject;
  24. import com.google.inject.Key;
  25. import com.google.inject.Provider;
  26. import com.google.inject.spi.InjectionPoint;
  27. import junit.framework.TestCase;
  28. import java.lang.annotation.Retention;
  29. import java.lang.annotation.RetentionPolicy;
  30. import java.lang.reflect.Member;
  31. import java.util.Set;
  32. /**
  33. * Test cases for {@link AbstractInjectorGrapher}. This indirectly tests most classes in this
  34. * package.
  35. *
  36. * @author bojand@google.com (Bojan Djordjevic)
  37. */
  38. public class AbstractInjectorGrapherTest extends TestCase {
  39. private static final String TEST_STRING = "test";
  40. private static class FakeGrapher extends AbstractInjectorGrapher {
  41. final Set<Node> nodes = Sets.newHashSet();
  42. final Set<Edge> edges = Sets.newHashSet();
  43. @Override protected void reset() {
  44. nodes.clear();
  45. edges.clear();
  46. }
  47. @Override protected void newInterfaceNode(InterfaceNode node) {
  48. assertFalse(nodes.contains(node));
  49. nodes.add(node);
  50. }
  51. @Override protected void newImplementationNode(ImplementationNode node) {
  52. assertFalse(nodes.contains(node));
  53. nodes.add(node);
  54. }
  55. @Override protected void newInstanceNode(InstanceNode node) {
  56. assertFalse(nodes.contains(node));
  57. nodes.add(node);
  58. }
  59. @Override protected void newDependencyEdge(DependencyEdge edge) {
  60. assertFalse(edges.contains(edge));
  61. edges.add(edge);
  62. }
  63. @Override protected void newBindingEdge(BindingEdge edge) {
  64. assertFalse(edges.contains(edge));
  65. edges.add(edge);
  66. }
  67. @Override protected void postProcess() {}
  68. }
  69. private static final class Wrapper<T> {
  70. T value;
  71. }
  72. @BindingAnnotation
  73. @Retention(RetentionPolicy.RUNTIME)
  74. private static @interface Ann {}
  75. private static interface IA {}
  76. private static class A implements IA {
  77. @Inject public A(String str) {}
  78. }
  79. private static class A2 implements IA {
  80. @Inject public A2(Provider<String> strProvider) {}
  81. }
  82. private Node aNode;
  83. private Node a2Node;
  84. private Node iaNode;
  85. private Node iaAnnNode;
  86. private Node stringNode;
  87. private Node stringInstanceNode;
  88. private FakeGrapher grapher;
  89. @Override protected void setUp() throws Exception {
  90. super.setUp();
  91. grapher = new FakeGrapher();
  92. Node.ignoreSourceInComparisons = true;
  93. aNode = new ImplementationNode(NodeId.newTypeId(Key.get(A.class)), null,
  94. ImmutableList.<Member>of(A.class.getConstructor(String.class)));
  95. a2Node = new ImplementationNode(NodeId.newTypeId(Key.get(A2.class)), null,
  96. ImmutableList.<Member>of(A2.class.getConstructor(Provider.class)));
  97. iaNode = new InterfaceNode(NodeId.newTypeId(Key.get(IA.class)), null);
  98. iaAnnNode = new InterfaceNode(NodeId.newTypeId(Key.get(IA.class, Ann.class)), null);
  99. stringNode = new InterfaceNode(NodeId.newTypeId(Key.get(String.class)), null);
  100. stringInstanceNode = new InstanceNode(NodeId.newInstanceId(Key.get(String.class)), null,
  101. TEST_STRING, ImmutableList.<Member>of());
  102. }
  103. public void testLinkedAndInstanceBindings() throws Exception {
  104. grapher.graph(Guice.createInjector(new AbstractModule() {
  105. @Override protected void configure() {
  106. bind(IA.class).to(A.class);
  107. bind(IA.class).annotatedWith(Ann.class).to(A.class);
  108. bind(String.class).toInstance(TEST_STRING);
  109. }
  110. }));
  111. Set<Node> expectedNodes =
  112. ImmutableSet.<Node>of(iaNode, iaAnnNode, aNode, stringNode, stringInstanceNode);
  113. Set<Edge> expectedEdges = ImmutableSet.<Edge>of(
  114. new BindingEdge(iaNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL),
  115. new BindingEdge(iaAnnNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL),
  116. new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL),
  117. new DependencyEdge(aNode.getId(), stringNode.getId(),
  118. InjectionPoint.forConstructor(A.class.getConstructor(String.class))));
  119. assertEquals(expectedNodes, grapher.nodes);
  120. assertEquals(expectedEdges, grapher.edges);
  121. }
  122. public void testProviderBindings() throws Exception {
  123. final Wrapper<Provider<A2>> wrapper = new Wrapper<Provider<A2>>();
  124. grapher.graph(Guice.createInjector(new AbstractModule() {
  125. @Override protected void configure() {
  126. wrapper.value = getProvider(A2.class);
  127. bind(IA.class).toProvider(wrapper.value);
  128. bind(A2.class);
  129. bind(String.class).toInstance(TEST_STRING);
  130. }
  131. }));
  132. Node a2ProviderNode = new InstanceNode(NodeId.newInstanceId(Key.get(IA.class)), null,
  133. wrapper.value, ImmutableList.<Member>of());
  134. Set<Node> expectedNodes =
  135. ImmutableSet.<Node>of(iaNode, stringNode, a2Node, stringInstanceNode, a2ProviderNode);
  136. Set<Edge> expectedEdges = ImmutableSet.<Edge>of(
  137. new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL),
  138. new BindingEdge(iaNode.getId(), a2ProviderNode.getId(), BindingEdge.Type.PROVIDER),
  139. new DependencyEdge(a2Node.getId(), stringNode.getId(),
  140. InjectionPoint.forConstructor(A2.class.getConstructor(Provider.class))),
  141. new DependencyEdge(a2ProviderNode.getId(), a2Node.getId(), null));
  142. assertEquals("wrong nodes", expectedNodes, grapher.nodes);
  143. assertEquals("wrong edges", expectedEdges, grapher.edges);
  144. }
  145. public void testGraphWithGivenRoot() throws Exception {
  146. grapher.graph(Guice.createInjector(new AbstractModule() {
  147. @Override protected void configure() {
  148. bind(IA.class).to(A.class);
  149. bind(IA.class).annotatedWith(Ann.class).to(A.class);
  150. bind(String.class).toInstance(TEST_STRING);
  151. }
  152. }), ImmutableSet.<Key<?>>of(Key.get(String.class)));
  153. Set<Node> expectedNodes = ImmutableSet.<Node>of(stringNode, stringInstanceNode);
  154. Set<Edge> expectedEdges = ImmutableSet.<Edge>of(
  155. new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL));
  156. assertEquals(expectedNodes, grapher.nodes);
  157. assertEquals(expectedEdges, grapher.edges);
  158. }
  159. }