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          final State[] standardTransitions = { STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN, SHUTDOWN, STARTING };
47          for(final State state : standardTransitions)
48          {
49              stateTracker.setState(state);
50              assertThat(stateTracker.get(), is(state));
51          }
52      }
53  
54      private void performStateSequence(final State... desiredStates)
55      {
56          for(final State state : desiredStates)
57          {
58              stateTracker.setState(state);
59          }
60      }
61  
62      @Test
63      public void testIllegalNotStartedTransitions() throws Exception
64      {
65          performStateSequence();
66          validateIllegalStates(NOT_STARTED, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTDOWN);
67      }
68  
69      @Test
70      public void testIllegalStartingTransitions() throws Exception
71      {
72          performStateSequence(STARTING);
73          validateIllegalStates(NOT_STARTED, STARTING, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
74      }
75  
76      @Test
77      public void testIllegalDelayedTransitions() throws Exception
78      {
79          performStateSequence(STARTING, DELAYED);
80          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
81      }
82  
83      @Test
84      public void testIllegalResumingTransitions() throws Exception
85      {
86          performStateSequence(STARTING, DELAYED, RESUMING);
87          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
88      }
89  
90      @Test
91      public void testIllegalStartedTransitions() throws Exception
92      {
93          performStateSequence(STARTING, DELAYED, RESUMING, STARTED);
94          validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, SHUTDOWN);
95      }
96  
97      @Test
98      public void testIllegalWarmRestartingTransitions() throws Exception
99      {
100         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING);
101         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
102     }
103 
104     @Test
105     public void testIllegalWarmRestartTransitions() throws Exception
106     {
107         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING, STARTED);
108         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, SHUTDOWN);
109     }
110 
111     @Test
112     public void testIllegalShuttingDownTransitions() throws Exception
113     {
114         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN);
115         validateIllegalStates(NOT_STARTED, STARTING, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN);
116     }
117 
118     @Test
119     public void testIllegalShutdownTransitions() throws Exception
120     {
121         performStateSequence(STARTING, DELAYED, RESUMING, STARTED, SHUTTING_DOWN, SHUTDOWN);
122         validateIllegalStates(NOT_STARTED, DELAYED, RESUMING, STARTED, WARM_RESTARTING, SHUTTING_DOWN, SHUTDOWN);
123     }
124 
125     private void validateIllegalStates(final State... illegalStates)
126     {
127         final State initialState = stateTracker.get();
128         for (final State illegalState : illegalStates)
129         {
130             try
131             {
132                 stateTracker.setState(illegalState);
133                 fail(initialState + " should not be allowed to transition to: " + illegalState);
134             }
135             catch (final IllegalStateException expected)
136             {
137                 assertThat(stateTracker.get(), is(initialState));
138             }
139         }
140     }
141 }