View Javadoc

1   package com.atlassian.plugin.manager;
2   
3   import com.atlassian.plugin.manager.StateTracker.State;
4   
5   import org.junit.After;
6   import org.junit.Before;
7   import org.junit.Test;
8   import org.junit.runner.RunWith;
9   import org.mockito.runners.MockitoJUnitRunner;
10  
11  import static com.atlassian.plugin.manager.StateTracker.State.DELAYED;
12  import static com.atlassian.plugin.manager.StateTracker.State.NOT_STARTED;
13  import static com.atlassian.plugin.manager.StateTracker.State.RESUMING;
14  import static com.atlassian.plugin.manager.StateTracker.State.SHUTDOWN;
15  import static com.atlassian.plugin.manager.StateTracker.State.SHUTTING_DOWN;
16  import static com.atlassian.plugin.manager.StateTracker.State.STARTED;
17  import static com.atlassian.plugin.manager.StateTracker.State.STARTING;
18  import static com.atlassian.plugin.manager.StateTracker.State.WARM_RESTARTING;
19  
20  import static org.hamcrest.MatcherAssert.assertThat;
21  import static org.hamcrest.core.Is.is;
22  import static org.junit.Assert.fail;
23  
24  
25  @RunWith (MockitoJUnitRunner.class)
26  public class TestStateTracker
27  {
28      private StateTracker stateTracker;
29  
30      @Before
31      public void setup()
32      {
33          stateTracker = new StateTracker();
34      }
35  
36      @After
37      public void tearDown()
38      {
39          stateTracker = null;
40      }
41  
42      @Test
43      public void testStandardTransitions() throws Exception
44      {
45          assertThat(stateTracker.get(), is(NOT_STARTED));
46          // The standardTransitions is one loop of the path StateTracker usually runs through.
47          // This is not just State.values() - it omits the start point NOT_STARTED, and also
48          // WARM_RESTARTING which is tested independently later.
49          final State[] standardTransitions = { STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN, SHUTDOWN, STARTING };
50          for(final State state : standardTransitions)
51          {
52              stateTracker.setState(state);
53              assertThat(stateTracker.get(), is(state));
54          }
55      }
56  
57      private void performStateSequence(final State... desiredStates)
58      {
59          for(final State state : desiredStates)
60          {
61              stateTracker.setState(state);
62          }
63      }
64  
65      @Test
66      public void testIllegalNotStartedTransitions() throws Exception
67      {
68          performStateSequence();
69          validateIllegalStates(NOT_STARTED, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTDOWN);
70      }
71  
72      @Test
73      public void testIllegalStartingTransitions() throws Exception
74      {
75          performStateSequence(STARTING);
76          validateIllegalStates(NOT_STARTED, STARTING, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
77      }
78  
79      @Test
80      public void testIllegalDelayedTransitions() throws Exception
81      {
82          performStateSequence(STARTING, DELAYED);
83          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
84      }
85  
86      @Test
87      public void testIllegalResumingTransitions() throws Exception
88      {
89          performStateSequence(STARTING, DELAYED, RESUMING);
90          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
91      }
92  
93      @Test
94      public void testIllegalStartedTransitions() throws Exception
95      {
96          performStateSequence(STARTING, DELAYED, RESUMING, STARTED);
97          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, SHUTDOWN);
98      }
99  
100     @Test
101     public void testIllegalWarmRestartingTransitions() throws Exception
102     {
103         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING);
104         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
105     }
106 
107     @Test
108     public void testIllegalWarmRestartTransitions() throws Exception
109     {
110         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING, STARTED);
111         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, SHUTDOWN);
112     }
113 
114     @Test
115     public void testIllegalShuttingDownTransitions() throws Exception
116     {
117         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN);
118         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN);
119     }
120 
121     @Test
122     public void testIllegalShutdownTransitions() throws Exception
123     {
124         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN, SHUTDOWN);
125         validateIllegalStates(NOT_STARTED, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
126     }
127 
128     private void validateIllegalStates(final State... illegalStates)
129     {
130         final State initialState = stateTracker.get();
131         for (final State illegalState : illegalStates)
132         {
133             try
134             {
135                 stateTracker.setState(illegalState);
136                 fail(initialState + " should not be allowed to transition to: " + illegalState);
137             }
138             catch (final IllegalStateException expected)
139             {
140                 assertThat(stateTracker.get(), is(initialState));
141             }
142         }
143     }
144 }