Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(904)

Unified Diff: test/com/google/gwt/inject/client/binding/InstanceBindingTest.java

Issue 196047: toInstnace() binding implementation Base URL: http://google-gin.googlecode.com/svn/trunk/
Patch Set: Created 14 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/com/google/gwt/inject/client/binding/InstanceBindingTest.java
===================================================================
--- test/com/google/gwt/inject/client/binding/InstanceBindingTest.java (revision 0)
+++ test/com/google/gwt/inject/client/binding/InstanceBindingTest.java (revision 0)
@@ -0,0 +1,130 @@
+/*
+ (c) 2009 Creative Development LLC. All rights reserved.
+ http://www.ecwid.com/
+*/
+
+
+package com.google.gwt.inject.client.binding;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.inject.client.AbstractGinModule;
+import com.google.gwt.inject.client.GinModules;
+import com.google.gwt.inject.client.Ginjector;
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.inject.Inject;
+import com.google.inject.Provides;
+import com.google.inject.name.Named;
+
+/**
+ *
+ * @author Dmitry Negoda <Dmitry.Negoda@gmail.com>
+ */
+public class InstanceBindingTest extends GWTTestCase {
+
+ public void testEagerSingletonBoundInjection() {
+ Tree.constructorCalls = 0;
+ FruitInBasketGinjector ginjector = GWT.create(FruitInBasketGinjector.class);
+ assertEquals(0, Tree.constructorCalls);
+ ginjector.setBasket(new Basket());
+ ginjector.setSomeId(1);
+ assertEquals(0, Tree.constructorCalls);
+ ginjector.getPlant();
+ assertEquals(1, Tree.constructorCalls);
+ ginjector.getPlant();
+ assertEquals(1, Tree.constructorCalls);
+
+ assertEquals(1, ginjector.getSomeId());
+ }
+
+ public void testSetAnnotatedObjects() {
+ BasketsGinjector ginjector = GWT.create(BasketsGinjector.class);
+ Basket big = new Basket();
+ Basket small = new Basket();
+ ginjector.setBigBasket(big);
+ ginjector.setSmallBasket(small);
+
+ Baskets baskets = ginjector.getBaskets();
+
+ assertEquals(big, baskets.big);
+ assertEquals(small, baskets.small);
+
+ baskets = ginjector.getReverseBaskets();
+
+ assertEquals(big, baskets.small);
+ assertEquals(small, baskets.big);
+ }
+
+ @GinModules({BasketsModule.class})
+ public interface BasketsGinjector extends Ginjector {
+ void setSmallBasket(@Named("small") Basket small);
+ void setBigBasket(@Named("big") Basket big);
+
+ Baskets getBaskets();
+ @Named("reverse") Baskets getReverseBaskets();
+ }
+
+ public static class BasketsModule extends AbstractGinModule {
+
+ protected void configure() {
+ bind(Baskets.class).asEagerSingleton();
+ // the following line is necessary because Guice doe not understand setters
+ //bind(Basket.class).annotatedWith(Named.class).toInstance();
+ }
+
+ @Provides
+ @Named("reverse")
+ public Baskets getReverseBuskets(@Named("big") Basket big, @Named("small") Basket small) {
+ return new Baskets(small, big);
+ }
+
+ }
+ public static class Baskets {
+ Basket big, small;
+
+ @Inject
+ public Baskets(@Named("big") Basket big, @Named("small") Basket small) {
+ this.big = big; this.small = small;
+ }
+ }
+
+ public void testUninitializedUse() {
+ FruitInBasketGinjector ginjector = GWT.create(FruitInBasketGinjector.class);
+ try {
+ ginjector.getLeaves();
+ fail("Did not throw IllegalStateException");
+ } catch (IllegalStateException iie) {
+ }
+ ginjector.setSomeId(1);
+ try {
+ ginjector.getLeaves();
+ fail("Did not throw IllegalStateException");
+ } catch (IllegalStateException iie) {
+ }
+ }
+
+ public void testInitializeSingletons() {
+ Tree.constructorCalls = 0;
+ FruitInBasketGinjector ginjector = GWT.create(FruitInBasketGinjector.class);
+ assertEquals(0, Tree.constructorCalls);
+ ginjector.setBasket(new Basket());
+ ginjector.setSomeId(1);
+ ginjector.initializeSingletons();
+ assertEquals(1, Tree.constructorCalls);
+ ginjector.getPlant();
+ assertEquals(1, Tree.constructorCalls);
+ ginjector.getPlant();
+ assertEquals(1, Tree.constructorCalls);
+ }
+
+ public void testDependencyOnInstance() {
+ FruitInBasketGinjector ginjector = GWT.create(FruitInBasketGinjector.class);
+ ginjector.setBasket(new Basket());
+ ginjector.setSomeId(123);
+ assertEquals(123, ginjector.getMyObject().id);
+ }
+
+ public String getModuleName() {
+ return "com.google.gwt.inject.InjectTest";
+ }
+
+}

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b