1 package com.atlassian.plugin.manager.store;
2
3 import com.atlassian.plugin.manager.PluginPersistentState;
4 import com.atlassian.plugin.manager.PluginPersistentStateStore;
5
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.mockito.Mock;
10 import org.mockito.runners.MockitoJUnitRunner;
11
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.hamcrest.Matchers.any;
14 import static org.hamcrest.Matchers.is;
15 import static org.mockito.Matchers.argThat;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 @RunWith (MockitoJUnitRunner.class)
22 public class TestDelegatingPluginPersistentStateStore
23 {
24 @Mock
25 PluginPersistentState state;
26
27 @Mock
28 PluginPersistentStateStore delegate;
29
30 DelegatingPluginPersistentStateStore delegatingStore;
31
32 @Before
33 public void setUp()
34 {
35 delegatingStore = new DelegatingPluginPersistentStateStore()
36 {
37 @Override
38 public PluginPersistentStateStore getDelegate()
39 {
40 return delegate;
41 }
42 };
43 }
44
45 @Test
46 public void getDelegateIsCalledBeforeDispatch()
47 {
48 final PluginPersistentStateStore oldDelegate = delegate;
49 final PluginPersistentStateStore newDelegate = mock(PluginPersistentStateStore.class);
50
51
52 delegatingStore.save(state);
53
54 delegate = newDelegate;
55 delegatingStore.load();
56
57 verify(oldDelegate).save(state);
58 verify(newDelegate, never()).save(argThat(any(PluginPersistentState.class)));
59 verify(oldDelegate, never()).load();
60 verify(newDelegate).load();
61 }
62
63 @Test
64 public void saveIsForwarded()
65 {
66 delegatingStore.save(state);
67 verify(delegate).save(state);
68 }
69
70 @Test
71 public void loadIsForwarded()
72 {
73 when(delegate.load()).thenReturn(state);
74 final PluginPersistentState loadedState = delegatingStore.load();
75 assertThat(loadedState, is(state));
76 verify(delegate).load();
77 }
78 }