Revision: 1495
Author: sberlin
Date: Wed Feb 16 06:18:38 2011
Log: issue 602 - allow grapher to be rooted at keys. contributed by bojan, thanks!
http://code.google.com/p/google-guice/source/detail?r=1495

Added:
/trunk/extensions/grapher/test/com/google/inject/grapher/InjectorGrapherTest.java
Modified:
/trunk/extensions/grapher/src/com/google/inject/grapher/InjectorGrapher.java

=======================================
--- /dev/null
+++ /trunk/extensions/grapher/test/com/google/inject/grapher/InjectorGrapherTest.java Wed Feb 16 06:18:38 2011
@@ -0,0 +1,142 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+
+package com.google.inject.grapher;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Binding;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.Provides;
+import com.google.inject.internal.util.ImmutableSet;
+import com.google.inject.internal.util.Sets;
+import com.google.inject.name.Named;
+import com.google.inject.name.Names;
+import com.google.inject.spi.BindingTargetVisitor;
+import com.google.inject.spi.ConstructorBinding;
+import com.google.inject.spi.ConvertedConstantBinding;
+import com.google.inject.spi.ExposedBinding;
+import com.google.inject.spi.InstanceBinding;
+import com.google.inject.spi.LinkedKeyBinding;
+import com.google.inject.spi.ProviderBinding;
+import com.google.inject.spi.ProviderInstanceBinding;
+import com.google.inject.spi.ProviderKeyBinding;
+import com.google.inject.spi.UntargettedBinding;
+import java.util.Collection;
+import java.util.Set;
+import junit.framework.TestCase;
+
+/**
+ * Tests for {@link InjectorGrapher}.
+ *
+ * @author [email protected] (Bojan Djordjevic)
+ */
+public class InjectorGrapherTest extends TestCase {
+ private static class FakeGraphingVisitor implements BindingTargetVisitor<Object, Void> {
+    private final Set<Key> keys = Sets.newHashSet();
+
+    public Void visit(InstanceBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ProviderInstanceBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ProviderKeyBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(LinkedKeyBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ExposedBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(UntargettedBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ConstructorBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ConvertedConstantBinding<?> binding) {
+      return record(binding);
+    }
+    public Void visit(ProviderBinding<?> binding) {
+      return record(binding);
+    }
+
+    public Set<Key> getKeys() {
+      return keys;
+    }
+
+    private Void record(Binding<?> binding) {
+      keys.add(binding.getKey());
+      return null;
+    }
+  }
+
+  private static class A {}
+  private static class B {}
+  private static class C {}
+  private static class D {}
+  private static class E {}
+
+  private static class ClassModule extends AbstractModule {
+    @Override protected void configure() {
+      bind(D.class).toInstance(new D());
+      bind(E.class).toInstance(new E());
+    }
+
+    @Provides A provideA(B b, @Named("test") C c) {
+      return new A();
+    }
+
+    @Provides B provideB(D d, E e) {
+      return new B();
+    }
+
+    @Provides @Named("test") C provideC(D d, E e) {
+      return new C();
+    }
+  }
+
+ private final BindingTargetVisitor<Object, Collection<Key<?>>> keyVisitor =
+      new TransitiveDependencyVisitor();
+  private final Renderer renderer = new Renderer() {
+    public void render() {}
+  };
+
+  private FakeGraphingVisitor graphingVisitor;
+  private Injector injector;
+  private InjectorGrapher grapher;
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+
+    graphingVisitor = new FakeGraphingVisitor();
+    injector = Guice.createInjector(new ClassModule());
+    grapher = new InjectorGrapher(keyVisitor, graphingVisitor, renderer);
+  }
+
+  /** Tests making a graph rooted at a {@link Class}. */
+  public void testRootedAtClass() throws Exception {
+    grapher.of(injector)
+        .rootedAt(B.class)
+        .graph();
+    assertEquals(ImmutableSet.<Key<?>>of(
+        Key.get(B.class),
+        Key.get(D.class),
+        Key.get(E.class)), graphingVisitor.getKeys());
+  }
+
+  /** Tests making a graph rooted at a {@link Key}. */
+  public void testRootedAtKey() throws Exception {
+    Key cKey = Key.get(C.class, Names.named("test"));
+    grapher.of(injector)
+        .rootedAt(cKey)
+        .graph();
+    assertEquals(ImmutableSet.of(
+        cKey,
+        Key.get(D.class),
+        Key.get(E.class)), graphingVisitor.getKeys());
+  }
+}
=======================================
--- /trunk/extensions/grapher/src/com/google/inject/grapher/InjectorGrapher.java Sat Jul 3 08:51:31 2010 +++ /trunk/extensions/grapher/src/com/google/inject/grapher/InjectorGrapher.java Wed Feb 16 06:18:38 2011
@@ -37,8 +37,9 @@
  * {@link Renderer}.
  * <p>
  * By default, this will graph the entire {@link Injector}. Use
- * {@link #rootedAt(Class...)} to specify an initial set of {@link Class}es to
- * use, and this will graph their transitive bindings and dependencies.
+ * {@link #rootedAt(Class...)} or {@link #rootedAt(Key...)} to specify an
+ * initial set of {@link Class}es or {@link Key}s to use, and this will graph
+ * their transitive bindings and dependencies.
  *
  * @author [email protected] (Pete Hopkins)
  */
@@ -82,6 +83,21 @@

     return this;
   }
+
+  /**
+   * Sets an initial group of {@link Key}s to use as the starting point for
+   * the graph. The graph will be of these keys and their transitive
+   * dependencies and bindings.
+   */
+  public InjectorGrapher rootedAt(Key<?>... keys) {
+    this.root = Sets.newHashSet();
+
+    for (Key<?> key : keys) {
+      this.root.add(key);
+    }
+
+    return this;
+  }

   /**
    * Renders a graph with the bound {@link Renderer}. The {@link Injector}

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to