@Before public void setUp() throws Exception { graph = new MvcGraph(); graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC public EventBus eventBusC() { return new EventBusImpl(); } @Provides @EventBusV public EventBus eventBusV() { return new EventBusImpl(); } @Provides public ExecutorService provideExe() { return mock(ExecutorService.class); } })); }
MvcComponent component = new MvcComponent(""); component.register(new Object(){ @Provides UiThreadRunner uiThreadRunner() { graph.getRootComponent().attach(component, true);
private int getGraphSize() { Map<String, Object> cache = graph.getRootComponent().getCache(); return cache == null ? 0 :cache.size(); }
@Test(expected = MvcGraphException.class) public void should_throw_exception_when_set_attached_root_component_to_graph_on_main_thread() throws Graph.IllegalRootComponentException, Component.MultiParentException, ProviderConflictException { MvcComponent parent = new MvcComponent("parent"); MvcComponent child = new MvcComponent(""); parent.attach(child); graph.setRootComponent(child); }
impClass = (Class<T>) Class.forName(getClassName(type)); } catch (ClassNotFoundException e) { String msg = String.format("Can't find implementation class for %s. Make sure class %s exists, or its implementation is registered to Mvc.graph().getRootComponent()", type.getName(), getClassName(type)); throw new ProviderMissingException(msg); if (qualifier == null) { msg = String.format("Can't find implementation class for %s. Make sure class %s without qualifier %s exists, or its implementation is registered to graph's root component.", type.getName(), getClassName(type), provider.getQualifier().toString()); } else { msg = String.format("Can't find implementation class for %s. Make sure class %s with qualifier %s exists, or its implementation is registered to graph's root component.", type.getName(), getClassName(type), qualifier.toString()); register(provider); } catch (ProviderConflictException e) {
@Test public void run_car_with_better_components() throws Exception { MvcComponent testComponent = new MvcComponent("TestComponent"); testComponent.register(new Object(){ @Provides public Engine v8Engine() { Mvc.graph().getRootComponent().attach(testComponent, overrideExistingProviders); Mvc.graph().getRootComponent().detach(testComponent); Car familyCar = new Car(); Mvc.graph().inject(familyCar);
@Override protected void prepareGraph(MvcComponent overriddingComponent) throws Exception { super.prepareGraph(overriddingComponent); overriddingComponent.register(new Object(){ /** * Mock resource service * @return */ @Provides public ResourceService resourceService() { resourceServiceMock = mock(ResourceService.class); return resourceServiceMock; } /** * Prepare objects to mock http calls * @return * @throws IOException */ @Provides public ServiceFactory serviceFactory() throws IOException { ipServiceCallMock = mock(Call.class); IpService ipServiceMock = mock(IpService.class); when(ipServiceMock.getIp(anyString())).thenReturn(ipServiceCallMock); ServiceFactory serviceFactoryMock = mock(ServiceFactory.class); when(serviceFactoryMock.createService(IpService.class)).thenReturn(ipServiceMock); return serviceFactoryMock; } }); }
@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()); }
@Test public void should_throw_exception_when_mvc_graph_set_rootComponent_on_non_main_thread() throws Graph.IllegalRootComponentException { graph.setRootComponent(new MvcComponent("")); when(graph.uiThreadRunner.isOnUiThread()).thenReturn(false); graph.setRootComponent(new MvcComponent("")); }
@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(); }
@Test public void test_injection_with_different_component_for_different_scoping() throws Exception{ MvcComponent componentWithCache = new MvcComponent("SmallLender"); componentWithCache.register(new Object(){ @Provides public Car car() { Mvc.graph().getRootComponent().attach(componentWithCache); Mvc.graph().getRootComponent().detach(componentWithCache); MvcComponent componentWithoutCache = new MvcComponent("BigLender", useScopeCache); componentWithoutCache.register(new Object(){ @Provides public Car car() { Mvc.graph().getRootComponent().attach(componentWithoutCache);
@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); }
@Test(expected = MvcGraphException.class) public void should_throw_exception_when_set_attached_root_component_to_graph_on_non_main_thread() throws Graph.IllegalRootComponentException, Component.MultiParentException, ProviderConflictException { when(graph.uiThreadRunner.isOnUiThread()).thenReturn(false); MvcComponent parent = new MvcComponent("parent"); MvcComponent child = new MvcComponent(""); parent.attach(child); graph.setRootComponent(child); }
@Test public void test_injection_with_different_component_for_different_scoping() throws Exception{ MvcComponent componentWithCache = new MvcComponent("SmallLender"); componentWithCache.register(new Object(){ @Provides public Car car() { Mvc.graph().getRootComponent().attach(componentWithCache); Mvc.graph().getRootComponent().detach(componentWithCache); MvcComponent componentWithoutCache = new MvcComponent("BigLender", useScopeCache); componentWithoutCache.register(new Object(){ @Provides public Car car() { Mvc.graph().getRootComponent().attach(componentWithoutCache);
@Test public void should_throw_ProviderConflictException_on_graph_preparation_by_duplicate_registration() throws Component.MultiParentException, ProviderConflictException, ProvideException { MvcComponent component = new MvcComponent(""); component.register(new Object() { @Provides public UiThreadRunner uiThreadRunner() { return mock(UiThreadRunner.class); } }); boolean caught = false; try { graph.prepareInternalGraph(new Graph(), component); } catch (MvcGraphException e) { if (e.getCause() instanceof ProviderConflictException) { caught = true; } } Assert.assertTrue(caught); }
graph.getRootComponent().register(new Object() { @Provides @EventBusC
@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_throw_IllegalRootComponentException_on_graph_preparation_with_attached_component() throws Component.MultiParentException, ProviderConflictException { MvcComponent parent = new MvcComponent(""); MvcComponent child = new MvcComponent(""); parent.attach(child); boolean caught = false; try { graph.prepareInternalGraph(new Graph(), child); } catch (MvcGraphException e) { if (e.getCause() instanceof Graph.IllegalRootComponentException) { caught = true; } } Assert.assertTrue(caught); }
graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC