aboutsummaryrefslogtreecommitdiff
path: root/api/src/test/java
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2018-08-28 14:40:27 -0700
committerGitHub <noreply@github.com>2018-08-28 14:40:27 -0700
commiteabc800c3749ca6f8e3a17f057ae11c8d1385d0c (patch)
tree3abf79995ac893ae49f8de9d68e6aa4c97277784 /api/src/test/java
parent884015cffa807a168e26ce36e8ae6d5ab426d8bb (diff)
downloadopencensus-java-eabc800c3749ca6f8e3a17f057ae11c8d1385d0c.tar.gz
Avoid doing string formatting when calling checkArgument. (#1394)
Diffstat (limited to 'api/src/test/java')
-rw-r--r--api/src/test/java/io/opencensus/internal/UtilsTest.java39
1 files changed, 39 insertions, 0 deletions
diff --git a/api/src/test/java/io/opencensus/internal/UtilsTest.java b/api/src/test/java/io/opencensus/internal/UtilsTest.java
index 983264f6..608a8fe0 100644
--- a/api/src/test/java/io/opencensus/internal/UtilsTest.java
+++ b/api/src/test/java/io/opencensus/internal/UtilsTest.java
@@ -30,6 +30,10 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public final class UtilsTest {
private static final String TEST_MESSAGE = "test message";
+ private static final String TEST_MESSAGE_TEMPLATE = "I ate %s eggs.";
+ private static final int TEST_MESSAGE_VALUE = 2;
+ private static final String FORMATED_SIMPLE_TEST_MESSAGE = "I ate 2 eggs.";
+ private static final String FORMATED_COMPLEX_TEST_MESSAGE = "I ate 2 eggs. [2]";
@Rule public ExpectedException thrown = ExpectedException.none();
@@ -42,6 +46,27 @@ public final class UtilsTest {
}
@Test
+ public void checkArgument_NullErrorMessage() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("null");
+ Utils.checkArgument(false, null);
+ }
+
+ @Test
+ public void checkArgument_WithSimpleFormat() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(FORMATED_SIMPLE_TEST_MESSAGE);
+ Utils.checkArgument(false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE);
+ }
+
+ @Test
+ public void checkArgument_WithComplexFormat() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(FORMATED_COMPLEX_TEST_MESSAGE);
+ Utils.checkArgument(false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE, TEST_MESSAGE_VALUE);
+ }
+
+ @Test
public void checkState() {
Utils.checkNotNull(true, TEST_MESSAGE);
thrown.expect(IllegalStateException.class);
@@ -50,6 +75,13 @@ public final class UtilsTest {
}
@Test
+ public void checkState_NullErrorMessage() {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("null");
+ Utils.checkState(false, null);
+ }
+
+ @Test
public void checkNotNull() {
Utils.checkNotNull(new Object(), TEST_MESSAGE);
thrown.expect(NullPointerException.class);
@@ -58,6 +90,13 @@ public final class UtilsTest {
}
@Test
+ public void checkNotNull_NullErrorMessage() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("null");
+ Utils.checkNotNull(null, null);
+ }
+
+ @Test
public void checkIndex_Valid() {
Utils.checkIndex(1, 2);
}