Home | History | Annotate | Download | only in base
      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 
     20 import com.google.common.annotations.GwtCompatible;
     21 
     22 import junit.framework.AssertionFailedError;
     23 import junit.framework.TestCase;
     24 
     25 /**
     26  * Unit test for {@link com.google.common.base.Verify}.
     27  */
     28 @GwtCompatible
     29 public class VerifyTest extends TestCase {
     30   public void testVerify_simple_success() {
     31     verify(true);
     32   }
     33 
     34   public void testVerify_simple_failure() {
     35     try {
     36       verify(false);
     37       fail();
     38     } catch (VerifyException expected) {
     39     }
     40   }
     41 
     42   public void testVerify_simpleMessage_success() {
     43     verify(true, "message");
     44   }
     45 
     46   public void testVerify_simpleMessage_failure() {
     47     try {
     48       verify(false, "message");
     49       fail();
     50     } catch (VerifyException expected) {
     51       assertEquals("message", expected.getMessage());
     52     }
     53   }
     54 
     55   public void testVerify_complexMessage_success() {
     56     verify(true, "%s", IGNORE_ME);
     57   }
     58 
     59   public void testVerify_complexMessage_failure() {
     60     try {
     61       verify(false, FORMAT, 5);
     62       fail();
     63     } catch (VerifyException expected) {
     64       checkMessage(expected);
     65     }
     66   }
     67 
     68   private static final String NON_NULL_STRING = "foo";
     69 
     70   public void testVerifyNotNull_simple_success() {
     71     String result = verifyNotNull(NON_NULL_STRING);
     72     assertSame(NON_NULL_STRING, result);
     73   }
     74 
     75   public void testVerifyNotNull_simple_failure() {
     76     try {
     77       verifyNotNull(null);
     78       fail();
     79     } catch (VerifyException expected) {
     80     }
     81   }
     82 
     83   public void testVerifyNotNull_complexMessage_success() {
     84     String result = verifyNotNull(NON_NULL_STRING, "%s", IGNORE_ME);
     85     assertSame(NON_NULL_STRING, result);
     86   }
     87 
     88   public void testVerifyNotNull_simpleMessage_failure() {
     89     try {
     90       verifyNotNull(null, FORMAT, 5);
     91       fail();
     92     } catch (VerifyException expected) {
     93       checkMessage(expected);
     94     }
     95   }
     96 
     97   private static final Object IGNORE_ME = new Object() {
     98     @Override public String toString() {
     99       throw new AssertionFailedError();
    100     }
    101   };
    102 
    103   private static final String FORMAT = "I ate %s pies.";
    104 
    105   private static void checkMessage(Exception e) {
    106     assertEquals("I ate 5 pies.", e.getMessage());
    107   }
    108 }
    109