Skip to content

Commit 3dc370e

Browse files
committed
Add proper tests for flux pattern
1 parent 4181514 commit 3dc370e

8 files changed

Lines changed: 322 additions & 0 deletions

File tree

flux/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.iluwatar.flux.action;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotNull;
7+
8+
/**
9+
* Date: 12/12/15 - 10:11 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class ContentTest {
14+
15+
@Test
16+
public void testToString() throws Exception {
17+
for (final Content content : Content.values()) {
18+
final String toString = content.toString();
19+
assertNotNull(toString);
20+
assertFalse(toString.trim().isEmpty());
21+
}
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.iluwatar.flux.action;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotNull;
7+
8+
/**
9+
* Date: 12/12/15 - 10:15 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class MenuItemTest {
14+
15+
@Test
16+
public void testToString() throws Exception {
17+
for (final MenuItem menuItem : MenuItem.values()) {
18+
final String toString = menuItem.toString();
19+
assertNotNull(toString);
20+
assertFalse(toString.trim().isEmpty());
21+
}
22+
}
23+
24+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.iluwatar.flux.dispatcher;
2+
3+
import com.iluwatar.flux.action.Action;
4+
import com.iluwatar.flux.action.ActionType;
5+
import com.iluwatar.flux.action.Content;
6+
import com.iluwatar.flux.action.ContentAction;
7+
import com.iluwatar.flux.action.MenuAction;
8+
import com.iluwatar.flux.action.MenuItem;
9+
import com.iluwatar.flux.store.Store;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.mockito.ArgumentCaptor;
14+
15+
import java.lang.reflect.Constructor;
16+
import java.lang.reflect.Field;
17+
import java.util.List;
18+
import java.util.stream.Collectors;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertSame;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.verifyNoMoreInteractions;
27+
28+
/**
29+
* Date: 12/12/15 - 8:22 PM
30+
*
31+
* @author Jeroen Meulemeester
32+
*/
33+
public class DispatcherTest {
34+
35+
/**
36+
* Dispatcher is a singleton with no way to reset it's internal state back to the beginning.
37+
* Replace the instance with a fresh one before each test to make sure test cases have no
38+
* influence on each other.
39+
*/
40+
@Before
41+
public void setUp() throws Exception {
42+
final Constructor<Dispatcher> constructor;
43+
constructor = Dispatcher.class.getDeclaredConstructor();
44+
constructor.setAccessible(true);
45+
46+
final Field field = Dispatcher.class.getDeclaredField("instance");
47+
field.setAccessible(true);
48+
field.set(Dispatcher.getInstance(), constructor.newInstance());
49+
}
50+
51+
@Test
52+
public void testGetInstance() throws Exception {
53+
assertNotNull(Dispatcher.getInstance());
54+
assertSame(Dispatcher.getInstance(), Dispatcher.getInstance());
55+
}
56+
57+
@Test
58+
public void testMenuItemSelected() throws Exception {
59+
final Dispatcher dispatcher = Dispatcher.getInstance();
60+
61+
final Store store = mock(Store.class);
62+
dispatcher.registerStore(store);
63+
dispatcher.menuItemSelected(MenuItem.HOME);
64+
dispatcher.menuItemSelected(MenuItem.COMPANY);
65+
66+
// We expect 4 events, 2 menu selections and 2 content change actions
67+
final ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class);
68+
verify(store, times(4)).onAction(actionCaptor.capture());
69+
verifyNoMoreInteractions(store);
70+
71+
final List<Action> actions = actionCaptor.getAllValues();
72+
final List<MenuAction> menuActions = actions.stream()
73+
.filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED))
74+
.map(a -> (MenuAction) a)
75+
.collect(Collectors.toList());
76+
77+
final List<ContentAction> contentActions = actions.stream()
78+
.filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED))
79+
.map(a -> (ContentAction) a)
80+
.collect(Collectors.toList());
81+
82+
assertEquals(2, menuActions.size());
83+
assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count());
84+
assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count());
85+
86+
assertEquals(2, contentActions.size());
87+
assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count());
88+
assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count());
89+
90+
}
91+
92+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.iluwatar.flux.store;
2+
3+
import com.iluwatar.flux.action.Content;
4+
import com.iluwatar.flux.action.ContentAction;
5+
import com.iluwatar.flux.action.MenuAction;
6+
import com.iluwatar.flux.action.MenuItem;
7+
import com.iluwatar.flux.view.View;
8+
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.mockito.Matchers.eq;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
import static org.mockito.Mockito.verifyNoMoreInteractions;
17+
import static org.mockito.Mockito.verifyZeroInteractions;
18+
19+
/**
20+
* Date: 12/12/15 - 10:18 PM
21+
*
22+
* @author Jeroen Meulemeester
23+
*/
24+
public class ContentStoreTest {
25+
26+
@Test
27+
public void testOnAction() throws Exception {
28+
final ContentStore contentStore = new ContentStore();
29+
30+
final View view = mock(View.class);
31+
contentStore.registerView(view);
32+
33+
verifyZeroInteractions(view);
34+
35+
// Content should not react on menu action ...
36+
contentStore.onAction(new MenuAction(MenuItem.PRODUCTS));
37+
verifyZeroInteractions(view);
38+
39+
// ... but it should react on a content action
40+
contentStore.onAction(new ContentAction(Content.COMPANY));
41+
verify(view, times(1)).storeChanged(eq(contentStore));
42+
verifyNoMoreInteractions(view);
43+
assertEquals(Content.COMPANY, contentStore.getContent());
44+
45+
}
46+
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.iluwatar.flux.store;
2+
3+
import com.iluwatar.flux.action.Content;
4+
import com.iluwatar.flux.action.ContentAction;
5+
import com.iluwatar.flux.action.MenuAction;
6+
import com.iluwatar.flux.action.MenuItem;
7+
import com.iluwatar.flux.view.View;
8+
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.mockito.Matchers.eq;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
import static org.mockito.Mockito.verifyNoMoreInteractions;
17+
import static org.mockito.Mockito.verifyZeroInteractions;
18+
19+
/**
20+
* Date: 12/12/15 - 10:18 PM
21+
*
22+
* @author Jeroen Meulemeester
23+
*/
24+
public class MenuStoreTest {
25+
26+
@Test
27+
public void testOnAction() throws Exception {
28+
final MenuStore menuStore = new MenuStore();
29+
30+
final View view = mock(View.class);
31+
menuStore.registerView(view);
32+
33+
verifyZeroInteractions(view);
34+
35+
// Menu should not react on content action ...
36+
menuStore.onAction(new ContentAction(Content.COMPANY));
37+
verifyZeroInteractions(view);
38+
39+
// ... but it should react on a menu action
40+
menuStore.onAction(new MenuAction(MenuItem.PRODUCTS));
41+
verify(view, times(1)).storeChanged(eq(menuStore));
42+
verifyNoMoreInteractions(view);
43+
assertEquals(MenuItem.PRODUCTS, menuStore.getSelected());
44+
45+
}
46+
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.flux.view;
2+
3+
import com.iluwatar.flux.action.Content;
4+
import com.iluwatar.flux.store.ContentStore;
5+
6+
import org.junit.Test;
7+
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.times;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.verifyNoMoreInteractions;
12+
import static org.mockito.Mockito.when;
13+
14+
/**
15+
* Date: 12/12/15 - 10:31 PM
16+
*
17+
* @author Jeroen Meulemeester
18+
*/
19+
public class ContentViewTest {
20+
21+
@Test
22+
public void testStoreChanged() throws Exception {
23+
final ContentStore store = mock(ContentStore.class);
24+
when(store.getContent()).thenReturn(Content.PRODUCTS);
25+
26+
final ContentView view = new ContentView();
27+
view.storeChanged(store);
28+
29+
verify(store, times(1)).getContent();
30+
verifyNoMoreInteractions(store);
31+
}
32+
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iluwatar.flux.view;
2+
3+
import com.iluwatar.flux.action.Action;
4+
import com.iluwatar.flux.action.MenuItem;
5+
import com.iluwatar.flux.dispatcher.Dispatcher;
6+
import com.iluwatar.flux.store.MenuStore;
7+
import com.iluwatar.flux.store.Store;
8+
9+
import org.junit.Test;
10+
11+
import static org.mockito.Matchers.any;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.times;
14+
import static org.mockito.Mockito.verify;
15+
import static org.mockito.Mockito.verifyNoMoreInteractions;
16+
import static org.mockito.Mockito.when;
17+
18+
/**
19+
* Date: 12/12/15 - 10:31 PM
20+
*
21+
* @author Jeroen Meulemeester
22+
*/
23+
public class MenuViewTest {
24+
25+
@Test
26+
public void testStoreChanged() throws Exception {
27+
final MenuStore store = mock(MenuStore.class);
28+
when(store.getSelected()).thenReturn(MenuItem.HOME);
29+
30+
final MenuView view = new MenuView();
31+
view.storeChanged(store);
32+
33+
verify(store, times(1)).getSelected();
34+
verifyNoMoreInteractions(store);
35+
}
36+
37+
@Test
38+
public void testItemClicked() throws Exception {
39+
final Store store = mock(Store.class);
40+
Dispatcher.getInstance().registerStore(store);
41+
42+
final MenuView view = new MenuView();
43+
view.itemClicked(MenuItem.PRODUCTS);
44+
45+
// We should receive a menu click action and a content changed action
46+
verify(store, times(2)).onAction(any(Action.class));
47+
48+
}
49+
50+
}

0 commit comments

Comments
 (0)