Search in sources :

Example 1 with MenuAction

use of com.iluwatar.flux.action.MenuAction in project java-design-patterns by iluwatar.

the class MenuStore method onAction.

@Override
public void onAction(Action action) {
    if (action.getType().equals(ActionType.MENU_ITEM_SELECTED)) {
        MenuAction menuAction = (MenuAction) action;
        selected = menuAction.getMenuItem();
        notifyChange();
    }
}
Also used : MenuAction(com.iluwatar.flux.action.MenuAction)

Example 2 with MenuAction

use of com.iluwatar.flux.action.MenuAction in project java-design-patterns by iluwatar.

the class ContentStoreTest method testOnAction.

@Test
public void testOnAction() throws Exception {
    final ContentStore contentStore = new ContentStore();
    final View view = mock(View.class);
    contentStore.registerView(view);
    verifyZeroInteractions(view);
    // Content should not react on menu action ...
    contentStore.onAction(new MenuAction(MenuItem.PRODUCTS));
    verifyZeroInteractions(view);
    // ... but it should react on a content action
    contentStore.onAction(new ContentAction(Content.COMPANY));
    verify(view, times(1)).storeChanged(eq(contentStore));
    verifyNoMoreInteractions(view);
    assertEquals(Content.COMPANY, contentStore.getContent());
}
Also used : MenuAction(com.iluwatar.flux.action.MenuAction) ContentAction(com.iluwatar.flux.action.ContentAction) View(com.iluwatar.flux.view.View) Test(org.junit.Test)

Example 3 with MenuAction

use of com.iluwatar.flux.action.MenuAction in project java-design-patterns by iluwatar.

the class DispatcherTest method testMenuItemSelected.

@Test
public void testMenuItemSelected() throws Exception {
    final Dispatcher dispatcher = Dispatcher.getInstance();
    final Store store = mock(Store.class);
    dispatcher.registerStore(store);
    dispatcher.menuItemSelected(MenuItem.HOME);
    dispatcher.menuItemSelected(MenuItem.COMPANY);
    // We expect 4 events, 2 menu selections and 2 content change actions
    final ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class);
    verify(store, times(4)).onAction(actionCaptor.capture());
    verifyNoMoreInteractions(store);
    final List<Action> actions = actionCaptor.getAllValues();
    final List<MenuAction> menuActions = actions.stream().filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)).map(a -> (MenuAction) a).collect(Collectors.toList());
    final List<ContentAction> contentActions = actions.stream().filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)).map(a -> (ContentAction) a).collect(Collectors.toList());
    assertEquals(2, menuActions.size());
    assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count());
    assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count());
    assertEquals(2, contentActions.size());
    assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count());
    assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count());
}
Also used : MenuAction(com.iluwatar.flux.action.MenuAction) MenuItem(com.iluwatar.flux.action.MenuItem) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) Content(com.iluwatar.flux.action.Content) Mockito.verify(org.mockito.Mockito.verify) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ActionType(com.iluwatar.flux.action.ActionType) Store(com.iluwatar.flux.store.Store) ContentAction(com.iluwatar.flux.action.ContentAction) Action(com.iluwatar.flux.action.Action) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) MenuAction(com.iluwatar.flux.action.MenuAction) ContentAction(com.iluwatar.flux.action.ContentAction) Action(com.iluwatar.flux.action.Action) Store(com.iluwatar.flux.store.Store) ContentAction(com.iluwatar.flux.action.ContentAction) MenuAction(com.iluwatar.flux.action.MenuAction) Test(org.junit.Test)

Example 4 with MenuAction

use of com.iluwatar.flux.action.MenuAction in project java-design-patterns by iluwatar.

the class MenuStoreTest method testOnAction.

@Test
public void testOnAction() throws Exception {
    final MenuStore menuStore = new MenuStore();
    final View view = mock(View.class);
    menuStore.registerView(view);
    verifyZeroInteractions(view);
    // Menu should not react on content action ...
    menuStore.onAction(new ContentAction(Content.COMPANY));
    verifyZeroInteractions(view);
    // ... but it should react on a menu action
    menuStore.onAction(new MenuAction(MenuItem.PRODUCTS));
    verify(view, times(1)).storeChanged(eq(menuStore));
    verifyNoMoreInteractions(view);
    assertEquals(MenuItem.PRODUCTS, menuStore.getSelected());
}
Also used : MenuAction(com.iluwatar.flux.action.MenuAction) ContentAction(com.iluwatar.flux.action.ContentAction) View(com.iluwatar.flux.view.View) Test(org.junit.Test)

Aggregations

MenuAction (com.iluwatar.flux.action.MenuAction)4 ContentAction (com.iluwatar.flux.action.ContentAction)3 Test (org.junit.Test)3 View (com.iluwatar.flux.view.View)2 Action (com.iluwatar.flux.action.Action)1 ActionType (com.iluwatar.flux.action.ActionType)1 Content (com.iluwatar.flux.action.Content)1 MenuItem (com.iluwatar.flux.action.MenuItem)1 Store (com.iluwatar.flux.store.Store)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertSame (org.junit.Assert.assertSame)1 Before (org.junit.Before)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1