Search in sources :

Example 1 with MockInterface1

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

the class DefaultInjectorBindingTest method testClassBinding.

@Test
public void testClassBinding() {
    Module module = 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());
}
Also used : MockInterface1Provider(org.apache.cayenne.di.mock.MockInterface1Provider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Module(org.apache.cayenne.di.Module) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test) Assert(org.junit.Assert) Key(org.apache.cayenne.di.Key) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 2 with MockInterface1

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

the class DefaultInjectorBindingTest method testClassNamedBinding.

@Test
public void testClassNamedBinding() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1.class);
        binder.bind(Key.get(MockInterface1.class, "abc")).to(MockImplementation1Alt.class);
        binder.bind(Key.get(MockInterface1.class, "xyz")).to(MockImplementation1Alt2.class);
    };
    DefaultInjector injector = new DefaultInjector(module);
    MockInterface1 defaultObject = injector.getInstance(MockInterface1.class);
    assertNotNull(defaultObject);
    assertEquals("MyName", defaultObject.getName());
    MockInterface1 abcObject = injector.getInstance(Key.get(MockInterface1.class, "abc"));
    assertNotNull(abcObject);
    assertEquals("alt", abcObject.getName());
    MockInterface1 xyzObject = injector.getInstance(Key.get(MockInterface1.class, "xyz"));
    assertNotNull(xyzObject);
    assertEquals("alt2", xyzObject.getName());
}
Also used : MockInterface1Provider(org.apache.cayenne.di.mock.MockInterface1Provider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Module(org.apache.cayenne.di.Module) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test) Assert(org.junit.Assert) Key(org.apache.cayenne.di.Key) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test)

Example 3 with MockInterface1

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

the class DefaultInjectorBindingTest method testProviderBinding.

@Test
public void testProviderBinding() {
    Module module = 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());
}
Also used : MockInterface1Provider(org.apache.cayenne.di.mock.MockInterface1Provider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Module(org.apache.cayenne.di.Module) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test) Assert(org.junit.Assert) Key(org.apache.cayenne.di.Key) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 4 with MockInterface1

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

the class DefaultInjectorBindingTest method testClassReBinding.

@Test
public void testClassReBinding() {
    Module module = 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());
}
Also used : MockInterface1Provider(org.apache.cayenne.di.mock.MockInterface1Provider) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockImplementation1Alt2(org.apache.cayenne.di.mock.MockImplementation1Alt2) Module(org.apache.cayenne.di.Module) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test) Assert(org.junit.Assert) Key(org.apache.cayenne.di.Key) MockImplementation1Alt(org.apache.cayenne.di.mock.MockImplementation1Alt) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) Module(org.apache.cayenne.di.Module) MockImplementation1(org.apache.cayenne.di.mock.MockImplementation1) Test(org.junit.Test)

Example 5 with MockInterface1

use of org.apache.cayenne.di.mock.MockInterface1 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)

Aggregations

Module (org.apache.cayenne.di.Module)46 MockInterface1 (org.apache.cayenne.di.mock.MockInterface1)46 Test (org.junit.Test)46 MockImplementation1 (org.apache.cayenne.di.mock.MockImplementation1)41 Assert (org.junit.Assert)37 Key (org.apache.cayenne.di.Key)30 MockImplementation1Alt (org.apache.cayenne.di.mock.MockImplementation1Alt)30 MockImplementation1Alt2 (org.apache.cayenne.di.mock.MockImplementation1Alt2)30 DIRuntimeException (org.apache.cayenne.di.DIRuntimeException)29 MockImplementation2 (org.apache.cayenne.di.mock.MockImplementation2)29 MockImplementation3 (org.apache.cayenne.di.mock.MockImplementation3)29 MockInterface2 (org.apache.cayenne.di.mock.MockInterface2)29 MockInterface3 (org.apache.cayenne.di.mock.MockInterface3)29 ArrayList (java.util.ArrayList)25 Collection (java.util.Collection)25 Map (java.util.Map)25 MockImplementation1_ListConfiguration (org.apache.cayenne.di.mock.MockImplementation1_ListConfiguration)25 MockImplementation1_ListConfigurationMock5 (org.apache.cayenne.di.mock.MockImplementation1_ListConfigurationMock5)25 MockImplementation1_MapConfiguration (org.apache.cayenne.di.mock.MockImplementation1_MapConfiguration)25 MockImplementation1_MapWithWildcards (org.apache.cayenne.di.mock.MockImplementation1_MapWithWildcards)25