1 /* 2 * Copyright 2018, OpenCensus 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 io.opencensus.internal; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import java.util.Date; 23 import org.junit.Rule; 24 import org.junit.Test; 25 import org.junit.rules.ExpectedException; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Tests for {@link Utils}. */ 30 @RunWith(JUnit4.class) 31 public final class UtilsTest { 32 private static final String TEST_MESSAGE = "test message"; 33 private static final String TEST_MESSAGE_TEMPLATE = "I ate %s eggs."; 34 private static final int TEST_MESSAGE_VALUE = 2; 35 private static final String FORMATED_SIMPLE_TEST_MESSAGE = "I ate 2 eggs."; 36 private static final String FORMATED_COMPLEX_TEST_MESSAGE = "I ate 2 eggs. [2]"; 37 38 @Rule public ExpectedException thrown = ExpectedException.none(); 39 40 @Test checkArgument()41 public void checkArgument() { 42 Utils.checkArgument(true, TEST_MESSAGE); 43 thrown.expect(IllegalArgumentException.class); 44 thrown.expectMessage(TEST_MESSAGE); 45 Utils.checkArgument(false, TEST_MESSAGE); 46 } 47 48 @Test checkArgument_NullErrorMessage()49 public void checkArgument_NullErrorMessage() { 50 thrown.expect(IllegalArgumentException.class); 51 thrown.expectMessage("null"); 52 Utils.checkArgument(false, null); 53 } 54 55 @Test checkArgument_WithSimpleFormat()56 public void checkArgument_WithSimpleFormat() { 57 thrown.expect(IllegalArgumentException.class); 58 thrown.expectMessage(FORMATED_SIMPLE_TEST_MESSAGE); 59 Utils.checkArgument(false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE); 60 } 61 62 @Test checkArgument_WithComplexFormat()63 public void checkArgument_WithComplexFormat() { 64 thrown.expect(IllegalArgumentException.class); 65 thrown.expectMessage(FORMATED_COMPLEX_TEST_MESSAGE); 66 Utils.checkArgument(false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE, TEST_MESSAGE_VALUE); 67 } 68 69 @Test checkState()70 public void checkState() { 71 Utils.checkNotNull(true, TEST_MESSAGE); 72 thrown.expect(IllegalStateException.class); 73 thrown.expectMessage(TEST_MESSAGE); 74 Utils.checkState(false, TEST_MESSAGE); 75 } 76 77 @Test checkState_NullErrorMessage()78 public void checkState_NullErrorMessage() { 79 thrown.expect(IllegalStateException.class); 80 thrown.expectMessage("null"); 81 Utils.checkState(false, null); 82 } 83 84 @Test checkNotNull()85 public void checkNotNull() { 86 Utils.checkNotNull(new Object(), TEST_MESSAGE); 87 thrown.expect(NullPointerException.class); 88 thrown.expectMessage(TEST_MESSAGE); 89 Utils.checkNotNull(null, TEST_MESSAGE); 90 } 91 92 @Test checkNotNull_NullErrorMessage()93 public void checkNotNull_NullErrorMessage() { 94 thrown.expect(NullPointerException.class); 95 thrown.expectMessage("null"); 96 Utils.checkNotNull(null, null); 97 } 98 99 @Test checkIndex_Valid()100 public void checkIndex_Valid() { 101 Utils.checkIndex(1, 2); 102 } 103 104 @Test checkIndex_NegativeSize()105 public void checkIndex_NegativeSize() { 106 thrown.expect(IllegalArgumentException.class); 107 thrown.expectMessage("Negative size: -1"); 108 Utils.checkIndex(0, -1); 109 } 110 111 @Test checkIndex_NegativeIndex()112 public void checkIndex_NegativeIndex() { 113 thrown.expect(IndexOutOfBoundsException.class); 114 thrown.expectMessage("Index out of bounds: size=10, index=-2"); 115 Utils.checkIndex(-2, 10); 116 } 117 118 @Test checkIndex_IndexEqualToSize()119 public void checkIndex_IndexEqualToSize() { 120 thrown.expect(IndexOutOfBoundsException.class); 121 thrown.expectMessage("Index out of bounds: size=5, index=5"); 122 Utils.checkIndex(5, 5); 123 } 124 125 @Test checkIndex_IndexGreaterThanSize()126 public void checkIndex_IndexGreaterThanSize() { 127 thrown.expect(IndexOutOfBoundsException.class); 128 thrown.expectMessage("Index out of bounds: size=10, index=11"); 129 Utils.checkIndex(11, 10); 130 } 131 132 @Test equalsObjects_Equal()133 public void equalsObjects_Equal() { 134 assertTrue(Utils.equalsObjects(null, null)); 135 assertTrue(Utils.equalsObjects(new Date(1L), new Date(1L))); 136 } 137 138 @Test equalsObjects_Unequal()139 public void equalsObjects_Unequal() { 140 assertFalse(Utils.equalsObjects(null, new Object())); 141 assertFalse(Utils.equalsObjects(new Object(), null)); 142 assertFalse(Utils.equalsObjects(new Object(), new Object())); 143 } 144 } 145