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