diff options
author | Kristen Kozak <sebright@google.com> | 2018-04-03 16:37:22 -0700 |
---|---|---|
committer | Kristen Kozak <sebright@google.com> | 2018-04-04 19:16:28 -0700 |
commit | ba8aea851d53764ec2cd45ccb76ff224c8846358 (patch) | |
tree | a9665f00a2315a9e12e43f0a99f47ab8b16cf86a /api/src/test/java/io/opencensus | |
parent | 1e09da2b34cce91cab3bd40290a0e16d60042afe (diff) | |
download | opencensus-java-ba8aea851d53764ec2cd45ccb76ff224c8846358.tar.gz |
Remove usages of Guava Precondtions from opencensus-api (issue #1081).
This commit replaces the most commonly used precondtion checks with methods in
a new utility class, io.opencensus.internal.Utils.
Diffstat (limited to 'api/src/test/java/io/opencensus')
-rw-r--r-- | api/src/test/java/io/opencensus/internal/UtilsTest.java | 88 |
1 files changed, 88 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 new file mode 100644 index 00000000..2416eeb8 --- /dev/null +++ b/api/src/test/java/io/opencensus/internal/UtilsTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.internal; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link Utils}. */ +@RunWith(JUnit4.class) +public final class UtilsTest { + private static final String TEST_MESSAGE = "test message"; + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void checkArgument() { + Utils.checkArgument(true, TEST_MESSAGE); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(TEST_MESSAGE); + Utils.checkArgument(false, TEST_MESSAGE); + } + + @Test + public void checkState() { + Utils.checkNotNull(true, TEST_MESSAGE); + thrown.expect(IllegalStateException.class); + thrown.expectMessage(TEST_MESSAGE); + Utils.checkState(false, TEST_MESSAGE); + } + + @Test + public void checkNotNull() { + Utils.checkNotNull(new Object(), TEST_MESSAGE); + thrown.expect(NullPointerException.class); + thrown.expectMessage(TEST_MESSAGE); + Utils.checkNotNull(null, TEST_MESSAGE); + } + + @Test + public void checkIndex_Valid() { + Utils.checkIndex(1, 2); + } + + @Test + public void checkIndex_NegativeSize() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Negative size: -1"); + Utils.checkIndex(0, -1); + } + + @Test + public void checkIndex_NegativeIndex() { + thrown.expect(IndexOutOfBoundsException.class); + thrown.expectMessage("Index out of bounds: size=10, index=-2"); + Utils.checkIndex(-2, 10); + } + + @Test + public void checkIndex_IndexEqualToSize() { + thrown.expect(IndexOutOfBoundsException.class); + thrown.expectMessage("Index out of bounds: size=5, index=5"); + Utils.checkIndex(5, 5); + } + + @Test + public void checkIndex_IndexGreaterThanSize() { + thrown.expect(IndexOutOfBoundsException.class); + thrown.expectMessage("Index out of bounds: size=10, index=11"); + Utils.checkIndex(11, 10); + } +} |