1 /*
2  * Copyright (C) 2013 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.util.concurrent;
18 
19 import static com.google.common.util.concurrent.Service.State.FAILED;
20 import static com.google.common.util.concurrent.Service.State.NEW;
21 import static com.google.common.util.concurrent.Service.State.RUNNING;
22 import static com.google.common.util.concurrent.Service.State.STARTING;
23 import static com.google.common.util.concurrent.Service.State.STOPPING;
24 import static com.google.common.util.concurrent.Service.State.TERMINATED;
25 
26 import java.util.Locale;
27 import junit.framework.TestCase;
28 
29 /** Unit tests for {@link Service} */
30 public class ServiceTest extends TestCase {
31 
32   /** Assert on the comparison ordering of the State enum since we guarantee it. */
testStateOrdering()33   public void testStateOrdering() {
34     // List every valid (direct) state transition.
35     assertLessThan(NEW, STARTING);
36     assertLessThan(NEW, TERMINATED);
37 
38     assertLessThan(STARTING, RUNNING);
39     assertLessThan(STARTING, STOPPING);
40     assertLessThan(STARTING, FAILED);
41 
42     assertLessThan(RUNNING, STOPPING);
43     assertLessThan(RUNNING, FAILED);
44 
45     assertLessThan(STOPPING, FAILED);
46     assertLessThan(STOPPING, TERMINATED);
47   }
48 
assertLessThan(T a, T b)49   private static <T extends Comparable<? super T>> void assertLessThan(T a, T b) {
50     if (a.compareTo(b) >= 0) {
51       fail(String.format(Locale.ROOT, "Expected %s to be less than %s", a, b));
52     }
53   }
54 }
55