View Javadoc
1   package com.github.davidmoten.guavamini;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNull;
5   import static org.junit.Assert.assertTrue;
6   
7   import org.junit.Assert;
8   import org.junit.Test;
9   
10  import com.github.davidmoten.junit.Asserts;
11  
12  public class PreconditionsTest {
13  
14      @Test(expected = NullPointerException.class)
15      public void testNullThrowsException() {
16          Preconditions.checkNotNull(null);
17      }
18  
19      @Test
20      public void testNotNullDoesNotThrowException() {
21          Preconditions.checkNotNull(new Object());
22      }
23  
24      @Test
25      public void testNotNullReturnsTypedObject() {
26          Integer n = 1;
27          Integer m = Preconditions.checkNotNull(n);
28          assertEquals(n, m);
29      }
30  
31      @Test
32      public void testCheckArgumentThrowsIAE() {
33          try {
34              Preconditions.checkArgument(false, "hi");
35              Assert.fail();
36          } catch (IllegalArgumentException e) {
37              assertEquals("hi", e.getMessage());
38          }
39      }
40  
41      @Test
42      public void testCheckArgumentDoesNotThrowIAE() {
43          Preconditions.checkArgument(true, "hi");
44      }
45  
46      @Test
47      public void testCheckArgumentWithoutMessageThrowsIAE() {
48          try {
49              Preconditions.checkArgument(false);
50              Assert.fail();
51          } catch (IllegalArgumentException e) {
52              assertNull(e.getMessage());
53          }
54      }
55  
56      @Test
57      public void testCheckArgumentWithoutMessageDoesNotThrowIAE() {
58          Preconditions.checkArgument(true);
59      }
60  
61      @Test
62      public void testCoverage() {
63          Asserts.assertIsUtilityClass(Preconditions.class);
64      }
65  
66      @Test
67      public void testCheckArgumentNotNullThrowsIAE() {
68          try {
69              Preconditions.checkArgumentNotNull(null);
70              Assert.fail();
71          } catch (IllegalArgumentException e) {
72              assertEquals("argument cannot be null", e.getMessage());
73          }
74      }
75  
76      @Test
77      public void testCheckArgumentNotNullReturnsWhenNotNull() {
78          Object o = new Object();
79          assertTrue(o == Preconditions.checkArgumentNotNull(o));
80      }
81  
82  }