private int getGraphSize() { Map<String, Object> cache = graph.getRootComponent().getCache(); return cache == null ? 0 :cache.size(); }
@Test public void should_throw_MvcGraphException_when_mvcGraph_destroy_method_encounters_PokeException() throws ProvideException, ProviderConflictException, ProviderMissingException { navigationManager.logger = mock(Logger.class); Navigator navigator = navigationManager.navigate(this); Mvc.graph().getRootComponent().register(new Object(){ @Provides public X x() { return new X_1(); } }); navigator.with(X.class); Mvc.graph().getRootComponent().unregister(X.class, null); navigator.destroy(); verify(navigationManager.logger).warn(anyString(), anyString()); }
graph.getRootComponent().register(new Object() { @Provides @EventBusC
@Test (expected = IllegalStateException.class) public void should_throw_out_exceptions_when_registering_component() throws ProvideException, ProviderConflictException, Graph.IllegalRootComponentException { // Arrange MvcComponent badComponent = mock(MvcComponent.class); MvcGraph mvcGraph = new MvcGraph(); mvcGraph.setRootComponent(badComponent); Object obj = new Object(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new IllegalStateException(); } }).when(badComponent).register(any(Object.class)); // Act mvcGraph.getRootComponent().register(obj); }
@Test(expected = MvcGraphException.class) public void should_catch_invocation_exception_when_NPE_detected_on_injection() throws Exception { Object com = new Object() { @Provides @Singleton @Slower2 TimerController timerSlowerX2() { return new TimerController() { { onCreated(); } @Override public void setInitialValue(long value) { super.setInitialValue(value * 2); } }; } }; graph.getRootComponent().register(com); Annotation slower2Qualifier = SlowXHolder.class.getAnnotation(Slower2.class); graph.use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { //Controller should have now been released Assert.assertEquals(0, instance.getInitialValue()); } }); graph.getRootComponent().unregister(com); }
@After public void tearDown() throws Exception { Component rootComponent = Mvc.graph().getRootComponent(); rootComponent.detach(overridingComponent); } }
@Test public void testInjectionOfMockController() throws Exception { final PrintController mockPrintController = mock(PrintController.class); graph.getRootComponent().register(new Provider<PrintController>(PrintController.class) { @Override protected PrintController createInstance() throws ProvideException { return mockPrintController; } }); TestView testView = new TestView(); graph.inject(testView); testView.present(); verify(mockPrintController, times(1)).print(); }
graph.getRootComponent().attach(component, true);
@Test public void testOnDisposeShouldBeCalledWhenControllerReleased() throws Exception { final LifeCycleTestController.Proxy lifeCycleProxy = mock(LifeCycleTestController.Proxy.class); graph.getRootComponent().register(new LifeCycleTestControllerModule(lifeCycleProxy)); TestLifCycleView testView = new TestLifCycleView(); verify(lifeCycleProxy, times(0)).onCreated(); graph.inject(testView); verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); TestLifCycleView testView1 = new TestLifCycleView(); graph.inject(testView1); //Should be 1 still since a cached instance will be reused. verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView1); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView); verify(lifeCycleProxy, times(1)).onDestroy(); }
@Test public void use_method_should_retain_and_release_instance_correctly() throws ProvideException, ProviderConflictException { graph.getRootComponent().register(new DeviceModule()); do_use_method_should_retain_and_release_instance_correctly(); }
@Before public void setUp() throws Exception { super.setUp(); executorService = mock(ExecutorService.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return null; } }).when(executorService).submit(any(Runnable.class)); Mvc.graph().getRootComponent().attach(new Component().register(new Object() { @Provides protected ExecutorService createExecutorService() { return executorService; } }), true); navigationManager = new NavigationManager(); graph.inject(navigationManager); navigationManager.onCreated(); }
Component rootComponent = Mvc.graph().getRootComponent();
Mvc.graph().getRootComponent().attach(testComponent, overrideExistingProviders); Mvc.graph().getRootComponent().detach(testComponent); Car familyCar = new Car(); Mvc.graph().inject(familyCar);
@Test public void controllerShouldBeOverrideAfterExplicitlyRegistration() throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException { graph.getRootComponent().register(new PrinterModule(new MockPrinter())); TestView testView = new TestView(); graph.inject(testView); Assert.assertEquals(testView.printController.getClass(), MockPrinter.class); Assert.assertEquals(testView.printController.print(), ((MockPrinter)testView.printController).printContent); }
@Test public void shouldRecoverDefaultUsingDefaultImplAfterExplicitlyUnregistration() throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException { PrinterModule component = new PrinterModule(new MockPrinter()); graph.getRootComponent().register(component); TestView testView = new TestView(); graph.inject(testView); Assert.assertEquals(testView.printController.getClass(), MockPrinter.class); Assert.assertEquals(testView.printController.print(), ((MockPrinter)testView.printController).printContent); graph.getRootComponent().unregister(component); TestView testView1 = new TestView(); graph.inject(testView1); Assert.assertEquals(testView1.printController.getClass(), PrintController.class); PrintModel printModel = new PrintModel(); printModel.setContent("Hello Poke"); testView1.printController.bindModel(printModel); Assert.assertEquals(testView1.printController.print(), testView1.printController.getModel().getContent()); }
@Test public void should_be_able_to_reinject_new_instance_without_default_constructor() throws ProvideException, ProviderConflictException, ProviderMissingException, CircularDependenciesException { AndroidModule component = new AndroidModule(); graph.getRootComponent().register(component); AndroidView androidView = new AndroidView(); Assert.assertNull(androidView.androidPart); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); graph.release(androidView); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); }
Mvc.graph().getRootComponent().attach(componentWithCache); Mvc.graph().getRootComponent().detach(componentWithCache); Mvc.graph().getRootComponent().attach(componentWithoutCache);
Mvc.graph().getRootComponent().attach(componentWithCache); Mvc.graph().getRootComponent().detach(componentWithCache); Mvc.graph().getRootComponent().attach(componentWithoutCache);
Mvc.graph().getRootComponent().register(module); Mvc.graph().getRootComponent().unregister(module);
@Test public void use_method_should_retain_and_release_instance_without_qualifier_correctly() throws ProvideException, ProviderConflictException { graph.getRootComponent().register(new DeviceModule());