Fix test to use count down latch, fix whitespace
[tbgof.git] / src / test / java / net / pterodactylus / tbgof / core / CoreTest.java
1 package net.pterodactylus.tbgof.core;
2
3 import java.util.concurrent.CountDownLatch;
4 import java.util.concurrent.TimeUnit;
5
6 import com.google.common.util.concurrent.MoreExecutors;
7 import com.google.common.util.concurrent.Service.Listener;
8 import com.google.common.util.concurrent.Service.State;
9 import org.hamcrest.MatcherAssert;
10 import org.hamcrest.Matchers;
11 import org.junit.Before;
12 import org.junit.Test;
13
14 /**
15  * Unit test for {@link Core}.
16  *
17  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
18  */
19 public class CoreTest {
20
21         private final Core core = new Core();
22         private final CountDownLatch started = new CountDownLatch(1);
23         private final CountDownLatch stopped = new CountDownLatch(1);
24
25         @Before
26         public void setupCoreListener() {
27                 core.addListener(new Listener() {
28                         @Override
29                         public void running() {
30                                 started.countDown();
31                         }
32
33                         @Override
34                         public void terminated(State from) {
35                                 stopped.countDown();
36                         }
37                 }, MoreExecutors.directExecutor());
38         }
39
40         @Test
41         public void coreCanStartUp() throws InterruptedException {
42                 core.startAsync().awaitRunning();
43                 MatcherAssert.assertThat(started.await(1, TimeUnit.SECONDS), Matchers.is(true));
44         }
45
46         @Test
47         public void coreCanShutDown() throws InterruptedException {
48                 core.startAsync().awaitRunning();
49                 core.stopAsync().awaitTerminated();
50                 MatcherAssert.assertThat(stopped.await(1, TimeUnit.SECONDS), Matchers.is(true));
51         }
52
53 }