1 /*
2  * Copyright (C) 2013 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.base;
16 
17 import static com.google.common.base.Verify.verify;
18 import static com.google.common.base.Verify.verifyNotNull;
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import junit.framework.AssertionFailedError;
23 import junit.framework.TestCase;
24 
25 /** Unit test for {@link com.google.common.base.Verify}. */
26 @GwtCompatible
27 public class VerifyTest extends TestCase {
testVerify_simple_success()28   public void testVerify_simple_success() {
29     verify(true);
30   }
31 
testVerify_simple_failure()32   public void testVerify_simple_failure() {
33     try {
34       verify(false);
35       fail();
36     } catch (VerifyException expected) {
37     }
38   }
39 
testVerify_simpleMessage_success()40   public void testVerify_simpleMessage_success() {
41     verify(true, "message");
42   }
43 
testVerify_simpleMessage_failure()44   public void testVerify_simpleMessage_failure() {
45     try {
46       verify(false, "message");
47       fail();
48     } catch (VerifyException expected) {
49       assertThat(expected).hasMessageThat().isEqualTo("message");
50     }
51   }
52 
testVerify_complexMessage_success()53   public void testVerify_complexMessage_success() {
54     verify(true, "%s", IGNORE_ME);
55   }
56 
testVerify_complexMessage_failure()57   public void testVerify_complexMessage_failure() {
58     try {
59       verify(false, FORMAT, 5);
60       fail();
61     } catch (VerifyException expected) {
62       checkMessage(expected);
63     }
64   }
65 
66   private static final String NON_NULL_STRING = "foo";
67 
testVerifyNotNull_simple_success()68   public void testVerifyNotNull_simple_success() {
69     String result = verifyNotNull(NON_NULL_STRING);
70     assertSame(NON_NULL_STRING, result);
71   }
72 
testVerifyNotNull_simple_failure()73   public void testVerifyNotNull_simple_failure() {
74     try {
75       verifyNotNull(null);
76       fail();
77     } catch (VerifyException expected) {
78     }
79   }
80 
testVerifyNotNull_complexMessage_success()81   public void testVerifyNotNull_complexMessage_success() {
82     String result = verifyNotNull(NON_NULL_STRING, "%s", IGNORE_ME);
83     assertSame(NON_NULL_STRING, result);
84   }
85 
testVerifyNotNull_simpleMessage_failure()86   public void testVerifyNotNull_simpleMessage_failure() {
87     try {
88       verifyNotNull(null, FORMAT, 5);
89       fail();
90     } catch (VerifyException expected) {
91       checkMessage(expected);
92     }
93   }
94 
95   private static final Object IGNORE_ME =
96       new Object() {
97         @Override
98         public String toString() {
99           throw new AssertionFailedError();
100         }
101       };
102 
103   private static final String FORMAT = "I ate %s pies.";
104 
checkMessage(Exception e)105   private static void checkMessage(Exception e) {
106     assertThat(e).hasMessageThat().isEqualTo("I ate 5 pies.");
107   }
108 }
109