cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r882986 - in /cayenne/sandbox/cayenne-di/src: main/java/org/apache/cayenne/di/ main/java/org/apache/cayenne/di/spi/ test/java/org/apache/cayenne/di/spi/
Date Sat, 21 Nov 2009 20:21:15 GMT
Author: aadamchik
Date: Sat Nov 21 20:21:12 2009
New Revision: 882986

URL: http://svn.apache.org/viewvc?rev=882986&view=rev
Log:
playing with DI ideas

* unit test reorg
* scopes
* specific instance binding

Added:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scope.java
      - copied, changed from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scopes.java
      - copied, changed from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
      - copied, changed from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/SingletonProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/InstanceProvider.java
      - copied, changed from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorBindingTest.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorCircularInjectionTest.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
Removed:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/SingletonProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java
Modified:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultMapBuilder.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorTest.java

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java?rev=882986&r1=882985&r2=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java (original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java Sat
Nov 21 20:21:12 2009
@@ -25,8 +25,12 @@
  */
 public interface BindingBuilder<T> {
 
-    void to(Class<? extends T> implementation) throws DIException;
+    BindingBuilder<T> to(Class<? extends T> implementation) throws DIException;
 
-    void toProvider(Class<? extends Provider<? extends T>> providerType)
+    BindingBuilder<T> toInstance(T instance) throws DIException;
+
+    BindingBuilder<T> toProvider(Class<? extends Provider<? extends T>>
providerType)
             throws DIException;
+
+    void in(Scope scope);
 }

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scope.java (from r882956,
cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scope.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scope.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java&r1=882956&r2=882986&rev=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scope.java Sat Nov 21 20:21:12
2009
@@ -16,20 +16,9 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.di.spi;
+package org.apache.cayenne.di;
 
-import org.apache.cayenne.di.DIException;
-import org.apache.cayenne.di.Provider;
+public interface Scope {
 
-class ValueProvider<T> implements Provider<T> {
-
-    private T value;
-
-    ValueProvider(T value) {
-        this.value = value;
-    }
-
-    public T get() throws DIException {
-        return value;
-    }
+    <T> Provider<T> scope(Provider<T> unscoped);
 }

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scopes.java (from r882956,
cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scopes.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scopes.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java&r1=882956&r2=882986&rev=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/BindingBuilder.java (original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/Scopes.java Sat Nov 21
20:21:12 2009
@@ -18,15 +18,36 @@
  ****************************************************************/
 package org.apache.cayenne.di;
 
-/**
- * A binding builder that helps with fluent binding creation.
- * 
- * @param <T> An interface type of the service being bound.
- */
-public interface BindingBuilder<T> {
+public final class Scopes {
 
-    void to(Class<? extends T> implementation) throws DIException;
+    public static final Scope NO_SCOPE;
 
-    void toProvider(Class<? extends Provider<? extends T>> providerType)
-            throws DIException;
+    public static final Scope SINGLETON;
+
+    static {
+
+        NO_SCOPE = new Scope() {
+
+            public <T> Provider<T> scope(Provider<T> unscoped) {
+                return unscoped;
+            }
+
+            @Override
+            public String toString() {
+                return "Scopes.NO_SCOPE";
+            }
+        };
+
+        SINGLETON = new Scope() {
+
+            public <T> Provider<T> scope(Provider<T> unscoped) {
+                return new SingletonProvider<T>(unscoped);
+            }
+
+            @Override
+            public String toString() {
+                return "Scopes.SINGLETON";
+            }
+        };
+    }
 }

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
(from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/SingletonProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/SingletonProvider.java&r1=882956&r2=882986&rev=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/SingletonProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
Sat Nov 21 20:21:12 2009
@@ -16,9 +16,7 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.di.spi;
-
-import org.apache.cayenne.di.Provider;
+package org.apache.cayenne.di;
 
 class SingletonProvider<T> implements Provider<T> {
 

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java?rev=882986&r1=882985&r2=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
Sat Nov 21 20:21:12 2009
@@ -21,49 +21,69 @@
 import org.apache.cayenne.di.BindingBuilder;
 import org.apache.cayenne.di.DIException;
 import org.apache.cayenne.di.Provider;
+import org.apache.cayenne.di.Scope;
+import org.apache.cayenne.di.Scopes;
 
 class DefaultBindingBuilder<T> implements BindingBuilder<T> {
 
     private Class<T> interfaceType;
     private DefaultInjector injector;
+    private Scope scope;
+    private String key;
 
     DefaultBindingBuilder(Class<T> interfaceType, DefaultInjector injector) {
         this.interfaceType = interfaceType;
         this.injector = injector;
+        this.key = KeyGenerator.toKey(interfaceType);
     }
 
-    public void to(Class<? extends T> implementation) throws DIException {
+    public BindingBuilder<T> to(Class<? extends T> implementation) throws DIException
{
 
-        String key = KeyGenerator.toKey(interfaceType);
-
-        // creating a provider chain for lazy resolution of singletons...
-        ConstructorProvider<T> provider0 = new ConstructorProvider<T>(
+        Provider<T> provider0 = new ConstructorProvider<T>(
                 interfaceType,
                 implementation,
                 injector);
-        FieldInjectingProvider<T> provider1 = new FieldInjectingProvider<T>(
-                provider0,
-                injector,
-                key);
-        SingletonProvider<T> provider2 = new SingletonProvider<T>(provider1);
+        Provider<T> provider1 = new FieldInjectingProvider<T>(provider0, injector,
key);
+        return bindInScope(provider1);
+    }
+
+    public BindingBuilder<T> toInstance(T instance) throws DIException {
+        return bindInScope(new InstanceProvider<T>(instance));
+    };
+
+    public BindingBuilder<T> toProvider(
+            Class<? extends Provider<? extends T>> providerType) {
+
+        Provider<T> provider0 = new ProviderConstructorProvider<T>(providerType);
+        Provider<T> provider1 = new FieldInjectingProvider<T>(provider0, injector,
key);
+
+        return bindInScope(provider1);
+    }
+
+    public void in(Scope scope) {
+
+        if (this.scope != scope) {
 
-        // TODO: andrus 11/15/2009 - report overriding the binding??
-        injector.getBindings().put(key, provider2);
+            if (this.scope != null) {
+                throw new IllegalStateException(
+                        "Can't change binding scope. It is already set to " + this.scope);
+            }
+
+            this.scope = scope;
+
+            if (key != null) {
+                Provider<?> provider = injector.getBindings().get(key);
+                injector.getBindings().put(key, scope.scope(provider));
+            }
+        }
     }
 
-    public void toProvider(Class<? extends Provider<? extends T>> providerType)
{
-        String key = KeyGenerator.toKey(interfaceType);
+    private BindingBuilder<T> bindInScope(Provider<T> provider) {
 
-        // creating a provider chain for lazy resolution of singletons...
-        ProviderConstructorProvider<T> provider0 = new ProviderConstructorProvider<T>(
-                providerType);
-        FieldInjectingProvider<T> provider1 = new FieldInjectingProvider<T>(
-                provider0,
-                injector,
-                key);
-        SingletonProvider<T> provider2 = new SingletonProvider<T>(provider1);
+        Scope scope = this.scope != null ? this.scope : Scopes.NO_SCOPE;
 
         // TODO: andrus 11/15/2009 - report overriding existing binding??
-        injector.getBindings().put(key, provider2);
+        injector.getBindings().put(key, scope.scope(provider));
+        return this;
     }
 }

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java?rev=882986&r1=882985&r2=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
Sat Nov 21 20:21:12 2009
@@ -46,7 +46,7 @@
 
     public <E> ListBuilder<T> add(E value) throws DIException {
 
-        ValueProvider<E> provider = new ValueProvider<E>(value);
+        InstanceProvider<E> provider = new InstanceProvider<E>(value);
 
         ListProvider listProvider = injector.getListConfigurations().get(
                 implementationTypeKey);

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultMapBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultMapBuilder.java?rev=882986&r1=882985&r2=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultMapBuilder.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultMapBuilder.java
Sat Nov 21 20:21:12 2009
@@ -48,7 +48,7 @@
 
     public <E> MapBuilder<T> put(String key, E value) throws DIException {
 
-        ValueProvider<E> provider = new ValueProvider<E>(value);
+        InstanceProvider<E> provider = new InstanceProvider<E>(value);
 
         MapProvider mapProvider = injector.getMapConfigurations().get(
                 implementationTypeKey);

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/InstanceProvider.java
(from r882956, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/InstanceProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/InstanceProvider.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java&r1=882956&r2=882986&rev=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ValueProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/InstanceProvider.java
Sat Nov 21 20:21:12 2009
@@ -21,11 +21,11 @@
 import org.apache.cayenne.di.DIException;
 import org.apache.cayenne.di.Provider;
 
-class ValueProvider<T> implements Provider<T> {
+class InstanceProvider<T> implements Provider<T> {
 
     private T value;
 
-    ValueProvider(T value) {
+    InstanceProvider(T value) {
         this.value = value;
     }
 

Added: cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorBindingTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorBindingTest.java?rev=882986&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorBindingTest.java
(added)
+++ cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorBindingTest.java
Sat Nov 21 20:21:12 2009
@@ -0,0 +1,100 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.di.spi;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.mock.MockImplementation1;
+import org.apache.cayenne.di.mock.MockImplementation1Alt;
+import org.apache.cayenne.di.mock.MockInterface1;
+import org.apache.cayenne.di.mock.MockInterface1Provider;
+
+public class DefaultInjectorBindingTest extends TestCase {
+
+    public void testClassBinding() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals("MyName", service.getName());
+    }
+
+    public void testProviderBinding() {
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder
+                        .bind(MockInterface1.class)
+                        .toProvider(MockInterface1Provider.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals("MyName", service.getName());
+    }
+
+    public void testInstanceBinding() {
+
+        final MockImplementation1 instance = new MockImplementation1();
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).toInstance(instance);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertSame(instance, service);
+    }
+
+    public void testClassReBinding() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+                binder.bind(MockInterface1.class).to(MockImplementation1Alt.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals("alt", service.getName());
+    }
+
+}

Added: cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorCircularInjectionTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorCircularInjectionTest.java?rev=882986&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorCircularInjectionTest.java
(added)
+++ cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorCircularInjectionTest.java
Sat Nov 21 20:21:12 2009
@@ -0,0 +1,74 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.di.spi;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.DIException;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.mock.MockImplementation1_DepOn2;
+import org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider;
+import org.apache.cayenne.di.mock.MockImplementation2;
+import org.apache.cayenne.di.mock.MockInterface1;
+import org.apache.cayenne.di.mock.MockInterface2;
+
+public class DefaultInjectorCircularInjectionTest extends TestCase {
+
+    public void testFieldInjection_CircularDependency() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2.class);
+                binder.bind(MockInterface2.class).to(MockImplementation2.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        try {
+            injector.getInstance(MockInterface1.class);
+            fail("Circular dependency is not detected.");
+        }
+        catch (DIException e) {
+            // expected
+        }
+        catch (StackOverflowError e) {
+            fail("Circular dependency is not detected, causing stack overflow");
+        }
+    }
+
+    public void testProviderInjection_CircularDependency() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(
+                        MockImplementation1_DepOn2Provider.class);
+                binder.bind(MockInterface2.class).to(MockImplementation2.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertEquals("MockImplementation2Name", service.getName());
+    }
+}

Added: cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java?rev=882986&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
(added)
+++ cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
Sat Nov 21 20:21:12 2009
@@ -0,0 +1,172 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.di.spi;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.mock.MockImplementation1;
+import org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration;
+import org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration;
+import org.apache.cayenne.di.mock.MockImplementation2;
+import org.apache.cayenne.di.mock.MockImplementation2Sub1;
+import org.apache.cayenne.di.mock.MockImplementation2_ConstructorProvider;
+import org.apache.cayenne.di.mock.MockImplementation3;
+import org.apache.cayenne.di.mock.MockImplementation4;
+import org.apache.cayenne.di.mock.MockImplementation5;
+import org.apache.cayenne.di.mock.MockInterface1;
+import org.apache.cayenne.di.mock.MockInterface2;
+import org.apache.cayenne.di.mock.MockInterface3;
+import org.apache.cayenne.di.mock.MockInterface4;
+import org.apache.cayenne.di.mock.MockInterface5;
+
+public class DefaultInjectorInjectionTest extends TestCase {
+
+    public void testFieldInjection() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+                binder.bind(MockInterface2.class).to(MockImplementation2.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface2 service = injector.getInstance(MockInterface2.class);
+        assertNotNull(service);
+        assertEquals("altered_MyName", service.getAlteredName());
+    }
+
+    public void testFieldInjectionSuperclass() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+                binder.bind(MockInterface2.class).to(MockImplementation2Sub1.class);
+                binder.bind(MockInterface3.class).to(MockImplementation3.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface2 service = injector.getInstance(MockInterface2.class);
+        assertNotNull(service);
+        assertEquals("altered_MyName:XName", service.getAlteredName());
+    }
+
+    public void testConstructorInjection() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+                binder.bind(MockInterface4.class).to(MockImplementation4.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface4 service = injector.getInstance(MockInterface4.class);
+        assertNotNull(service);
+        assertEquals("constructor_MyName", service.getName());
+    }
+
+    public void testProviderInjection_Constructor() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+                binder.bind(MockInterface2.class).to(
+                        MockImplementation2_ConstructorProvider.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface2 service = injector.getInstance(MockInterface2.class);
+        assertEquals("altered_MyName", service.getAlteredName());
+    }
+
+    public void testMapInjection() {
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(
+                        MockImplementation1_MapConfiguration.class);
+
+                binder
+                        .bindMap(MockInterface1.class)
+                        .put("x", "xvalue")
+                        .put("y", "yvalue")
+                        .put("x", "xvalue1");
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals(";x=xvalue1;y=yvalue", service.getName());
+    }
+
+    public void testListInjection_addValue() {
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(
+                        MockImplementation1_ListConfiguration.class);
+
+                binder.bindList(MockInterface1.class).add("xvalue").add("yvalue");
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals(";xvalue;yvalue", service.getName());
+    }
+
+    public void testListInjection_addType() {
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface5.class).to(MockImplementation5.class);
+
+                binder.bind(MockInterface1.class).to(
+                        MockImplementation1_ListConfiguration.class);
+
+                binder.bindList(MockInterface1.class).add(MockInterface5.class).add(
+                        "yvalue");
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals(";xyz;yvalue", service.getName());
+    }
+
+}

Added: cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java?rev=882986&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
(added)
+++ cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
Sat Nov 21 20:21:12 2009
@@ -0,0 +1,104 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.di.spi;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.Scopes;
+import org.apache.cayenne.di.mock.MockImplementation1;
+import org.apache.cayenne.di.mock.MockInterface1;
+
+public class DefaultInjectorScopeTest extends TestCase {
+
+    public void testNoScope() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 instance1 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance2 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance3 = injector.getInstance(MockInterface1.class);
+
+        assertNotNull(instance1);
+        assertNotNull(instance2);
+        assertNotNull(instance3);
+
+        assertNotSame(instance1, instance2);
+        assertNotSame(instance2, instance3);
+        assertNotSame(instance3, instance1);
+    }
+
+    public void testNoScope_Explicit() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class).in(
+                        Scopes.NO_SCOPE);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 instance1 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance2 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance3 = injector.getInstance(MockInterface1.class);
+
+        assertNotNull(instance1);
+        assertNotNull(instance2);
+        assertNotNull(instance3);
+
+        assertNotSame(instance1, instance2);
+        assertNotSame(instance2, instance3);
+        assertNotSame(instance3, instance1);
+    }
+
+    public void testSingletonScope() {
+
+        Module module = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(MockImplementation1.class).in(
+                        Scopes.SINGLETON);
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 instance1 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance2 = injector.getInstance(MockInterface1.class);
+        MockInterface1 instance3 = injector.getInstance(MockInterface1.class);
+
+        assertNotNull(instance1);
+        assertNotNull(instance2);
+        assertNotNull(instance3);
+
+        assertSame(instance1, instance2);
+        assertSame(instance2, instance3);
+    }
+
+}

Modified: cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorTest.java?rev=882986&r1=882985&r2=882986&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorTest.java
(original)
+++ cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorTest.java
Sat Nov 21 20:21:12 2009
@@ -21,26 +21,7 @@
 import junit.framework.TestCase;
 
 import org.apache.cayenne.di.Binder;
-import org.apache.cayenne.di.DIException;
 import org.apache.cayenne.di.Module;
-import org.apache.cayenne.di.mock.MockImplementation1;
-import org.apache.cayenne.di.mock.MockImplementation1Alt;
-import org.apache.cayenne.di.mock.MockImplementation1_DepOn2;
-import org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider;
-import org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration;
-import org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration;
-import org.apache.cayenne.di.mock.MockImplementation2;
-import org.apache.cayenne.di.mock.MockImplementation2Sub1;
-import org.apache.cayenne.di.mock.MockImplementation2_ConstructorProvider;
-import org.apache.cayenne.di.mock.MockImplementation3;
-import org.apache.cayenne.di.mock.MockImplementation4;
-import org.apache.cayenne.di.mock.MockImplementation5;
-import org.apache.cayenne.di.mock.MockInterface1;
-import org.apache.cayenne.di.mock.MockInterface1Provider;
-import org.apache.cayenne.di.mock.MockInterface2;
-import org.apache.cayenne.di.mock.MockInterface3;
-import org.apache.cayenne.di.mock.MockInterface4;
-import org.apache.cayenne.di.mock.MockInterface5;
 
 public class DefaultInjectorTest extends TestCase {
 
@@ -86,224 +67,4 @@
         assertTrue(configureCalled[1]);
     }
 
-    public void testClassBinding() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals("MyName", service.getName());
-    }
-
-    public void testFieldInjection() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-                binder.bind(MockInterface2.class).to(MockImplementation2.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface2 service = injector.getInstance(MockInterface2.class);
-        assertNotNull(service);
-        assertEquals("altered_MyName", service.getAlteredName());
-    }
-
-    public void testFieldInjectionSuperclass() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-                binder.bind(MockInterface2.class).to(MockImplementation2Sub1.class);
-                binder.bind(MockInterface3.class).to(MockImplementation3.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface2 service = injector.getInstance(MockInterface2.class);
-        assertNotNull(service);
-        assertEquals("altered_MyName:XName", service.getAlteredName());
-    }
-
-    public void testConstructorInjection() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-                binder.bind(MockInterface4.class).to(MockImplementation4.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface4 service = injector.getInstance(MockInterface4.class);
-        assertNotNull(service);
-        assertEquals("constructor_MyName", service.getName());
-    }
-
-    public void testProviderBinding() {
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder
-                        .bind(MockInterface1.class)
-                        .toProvider(MockInterface1Provider.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals("MyName", service.getName());
-    }
-
-    public void testClassReBinding() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-                binder.bind(MockInterface1.class).to(MockImplementation1Alt.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals("alt", service.getName());
-    }
-
-    public void testMapInjection() {
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(
-                        MockImplementation1_MapConfiguration.class);
-
-                binder
-                        .bindMap(MockInterface1.class)
-                        .put("x", "xvalue")
-                        .put("y", "yvalue")
-                        .put("x", "xvalue1");
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals(";x=xvalue1;y=yvalue", service.getName());
-    }
-
-    public void testListInjection_addValue() {
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(
-                        MockImplementation1_ListConfiguration.class);
-
-                binder.bindList(MockInterface1.class).add("xvalue").add("yvalue");
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals(";xvalue;yvalue", service.getName());
-    }
-
-    public void testListInjection_addType() {
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface5.class).to(MockImplementation5.class);
-
-                binder.bind(MockInterface1.class).to(
-                        MockImplementation1_ListConfiguration.class);
-
-                binder.bindList(MockInterface1.class).add(MockInterface5.class).add(
-                        "yvalue");
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertNotNull(service);
-        assertEquals(";xyz;yvalue", service.getName());
-    }
-
-    public void testFieldInjection_CircularDependency() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2.class);
-                binder.bind(MockInterface2.class).to(MockImplementation2.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        try {
-            injector.getInstance(MockInterface1.class);
-            fail("Circular dependency is not detected.");
-        }
-        catch (DIException e) {
-            // expected
-        }
-        catch (StackOverflowError e) {
-            fail("Circular dependency is not detected, causing stack overflow");
-        }
-    }
-
-    public void testProviderInjection_Constructor() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(MockImplementation1.class);
-                binder.bind(MockInterface2.class).to(
-                        MockImplementation2_ConstructorProvider.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface2 service = injector.getInstance(MockInterface2.class);
-        assertEquals("altered_MyName", service.getAlteredName());
-    }
-
-    public void testProviderInjection_CircularDependency() {
-
-        Module module = new Module() {
-
-            public void configure(Binder binder) {
-                binder.bind(MockInterface1.class).to(
-                        MockImplementation1_DepOn2Provider.class);
-                binder.bind(MockInterface2.class).to(MockImplementation2.class);
-            }
-        };
-
-        DefaultInjector injector = new DefaultInjector(module);
-
-        MockInterface1 service = injector.getInstance(MockInterface1.class);
-        assertEquals("MockImplementation2Name", service.getName());
-    }
 }



Mime
View raw message