Search in sources :

Example 1 with MockInterface2

use of org.apache.cayenne.di.mock.MockInterface2 in project cayenne by apache.

the class DefaultInjectorCircularInjectionTest method testFieldInjection_CircularDependency.

@Test
public void testFieldInjection_CircularDependency() {
    Module module = 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 (DIRuntimeException e) {
    // expected
    } catch (StackOverflowError e) {
        fail("Circular dependency is not detected, causing stack overflow");
    }
}
Also used : MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Assert.fail(org.junit.Assert.fail) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) Assert.assertEquals(org.junit.Assert.assertEquals) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Module(org.apache.cayenne.di.Module) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test)

Example 2 with MockInterface2

use of org.apache.cayenne.di.mock.MockInterface2 in project cayenne by apache.

the class DefaultInjectorCircularInjectionTest method testProviderInjection_CircularDependency.

@Test
public void testProviderInjection_CircularDependency() {
    Module module = 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());
}
Also used : MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Assert.fail(org.junit.Assert.fail) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) Assert.assertEquals(org.junit.Assert.assertEquals) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test)

Example 3 with MockInterface2

use of org.apache.cayenne.di.mock.MockInterface2 in project cayenne by apache.

the class DefaultInjectorInjectionTest method testTypedListInjection.

@Test
public void testTypedListInjection() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1_ListConfigurationMock5.class);
        binder.bind(MockInterface2.class).to(MockImplementation2_ListConfiguration.class);
        // Bind list for MockImplementation2_ListConfiguration
        binder.bindList(Object.class, "xyz").add("xvalue").add("yvalue").add(MockImplementation5.class);
        // Bind list for MockImplementation1_ListConfigurationMock5
        binder.bindList(MockInterface5.class).add(MockImplementation5.class).add(new MockInterface5() {

            @Override
            public String toString() {
                return "abc";
            }
        });
        binder.bindList(Object.class).add("avalue").add("bvalue").add(MockImplementation5.class);
        // Add to list for MockImplementation1_ListConfigurationMock5
        binder.bindList(MockInterface5.class).add(new MockInterface5() {

            @Override
            public String toString() {
                return "cde";
            }
        });
        // Create named list for MockInterface5
        binder.bindList(MockInterface5.class, "another_binding").add(new MockInterface5() {

            @Override
            public String toString() {
                return "fgh";
            }
        });
    };
    DefaultInjector injector = new DefaultInjector(module);
    MockInterface1 service = injector.getInstance(MockInterface1.class);
    assertNotNull(service);
    assertEquals(";xyz;abc;cde", service.getName());
    MockInterface2 service2 = injector.getInstance(MockInterface2.class);
    assertNotNull(service2);
    assertTrue(service2 instanceof MockImplementation2_ListConfiguration);
    assertEquals(";xvalue;yvalue;xyz", service2.getName());
}
Also used : MockImplementation1_ListConfigurationMock5(org.apache.cayenne.di.mock.MockImplementation1_ListConfigurationMock5) Module(org.apache.cayenne.di.Module) MockImplementation2_ListConfiguration(org.apache.cayenne.di.mock.MockImplementation2_ListConfiguration) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1_WithInjector(org.apache.cayenne.di.mock.MockImplementation1_WithInjector) Map(java.util.Map) Key(org.apache.cayenne.di.Key) MockImplementation1_ListConfiguration(org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration) MockImplementation1_MapConfiguration(org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Collection(java.util.Collection) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) MockImplementation2_Named(org.apache.cayenne.di.mock.MockImplementation2_Named) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation4(org.apache.cayenne.di.mock.MockImplementation4) MockImplementation5(org.apache.cayenne.di.mock.MockImplementation5) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockImplementation1_MapWithWildcards(org.apache.cayenne.di.mock.MockImplementation1_MapWithWildcards) MockImplementation2_ConstructorProvider(org.apache.cayenne.di.mock.MockImplementation2_ConstructorProvider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation4Alt2(org.apache.cayenne.di.mock.MockImplementation4Alt2) MockImplementation2Sub1(org.apache.cayenne.di.mock.MockImplementation2Sub1) MockImplementation4Alt(org.apache.cayenne.di.mock.MockImplementation4Alt) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) MockInterface4(org.apache.cayenne.di.mock.MockInterface4) Assert(org.junit.Assert) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) MockInterface5(org.apache.cayenne.di.mock.MockInterface5) MockImplementation5(org.apache.cayenne.di.mock.MockImplementation5) MockImplementation2_ListConfiguration(org.apache.cayenne.di.mock.MockImplementation2_ListConfiguration) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) MockImplementation1_ListConfigurationMock5(org.apache.cayenne.di.mock.MockImplementation1_ListConfigurationMock5) MockInterface5(org.apache.cayenne.di.mock.MockInterface5) Test(org.junit.Test)

Example 4 with MockInterface2

use of org.apache.cayenne.di.mock.MockInterface2 in project cayenne by apache.

the class DefaultInjectorInjectionTest method testFieldInjection.

@Test
public void testFieldInjection() {
    Module module = 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());
}
Also used : MockImplementation1_ListConfigurationMock5(org.apache.cayenne.di.mock.MockImplementation1_ListConfigurationMock5) Module(org.apache.cayenne.di.Module) MockImplementation2_ListConfiguration(org.apache.cayenne.di.mock.MockImplementation2_ListConfiguration) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1_WithInjector(org.apache.cayenne.di.mock.MockImplementation1_WithInjector) Map(java.util.Map) Key(org.apache.cayenne.di.Key) MockImplementation1_ListConfiguration(org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration) MockImplementation1_MapConfiguration(org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Collection(java.util.Collection) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) MockImplementation2_Named(org.apache.cayenne.di.mock.MockImplementation2_Named) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation4(org.apache.cayenne.di.mock.MockImplementation4) MockImplementation5(org.apache.cayenne.di.mock.MockImplementation5) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockImplementation1_MapWithWildcards(org.apache.cayenne.di.mock.MockImplementation1_MapWithWildcards) MockImplementation2_ConstructorProvider(org.apache.cayenne.di.mock.MockImplementation2_ConstructorProvider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation4Alt2(org.apache.cayenne.di.mock.MockImplementation4Alt2) MockImplementation2Sub1(org.apache.cayenne.di.mock.MockImplementation2Sub1) MockImplementation4Alt(org.apache.cayenne.di.mock.MockImplementation4Alt) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) MockInterface4(org.apache.cayenne.di.mock.MockInterface4) Assert(org.junit.Assert) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) MockInterface5(org.apache.cayenne.di.mock.MockInterface5) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test)

Example 5 with MockInterface2

use of org.apache.cayenne.di.mock.MockInterface2 in project cayenne by apache.

the class DefaultInjectorCircularInjectionTest method testConstructorInjection_WithFieldInjectionDeps.

@Test
public void testConstructorInjection_WithFieldInjectionDeps() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2Constructor.class);
        binder.bind(MockInterface2.class).to(MockImplementation2_I3Dependency.class);
        binder.bind(MockInterface3.class).to(MockImplementation3.class);
    };
    DefaultInjector injector = new DefaultInjector(module);
    try {
        injector.getInstance(MockInterface1.class);
    } catch (DIRuntimeException e) {
        fail("Circular dependency is detected incorrectly: " + e.getMessage());
    }
}
Also used : MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Assert.fail(org.junit.Assert.fail) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) Assert.assertEquals(org.junit.Assert.assertEquals) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Module(org.apache.cayenne.di.Module) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) Test(org.junit.Test)

Aggregations

DIRuntimeException (org.apache.cayenne.di.DIRuntimeException)9 Module (org.apache.cayenne.di.Module)9 MockImplementation2 (org.apache.cayenne.di.mock.MockImplementation2)9 MockImplementation3 (org.apache.cayenne.di.mock.MockImplementation3)9 MockInterface1 (org.apache.cayenne.di.mock.MockInterface1)9 MockInterface2 (org.apache.cayenne.di.mock.MockInterface2)9 MockInterface3 (org.apache.cayenne.di.mock.MockInterface3)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)5 Map (java.util.Map)5 Key (org.apache.cayenne.di.Key)5 MockImplementation1 (org.apache.cayenne.di.mock.MockImplementation1)5 MockImplementation1Alt (org.apache.cayenne.di.mock.MockImplementation1Alt)5 MockImplementation1Alt2 (org.apache.cayenne.di.mock.MockImplementation1Alt2)5 MockImplementation1_ListConfiguration (org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration)5 MockImplementation1_ListConfigurationMock5 (org.apache.cayenne.di.mock.MockImplementation1_ListConfigurationMock5)5 MockImplementation1_MapConfiguration (org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration)5 MockImplementation1_MapWithWildcards (org.apache.cayenne.di.mock.MockImplementation1_MapWithWildcards)5 MockImplementation1_WithInjector (org.apache.cayenne.di.mock.MockImplementation1_WithInjector)5