Clean up assertions in `DurationsTest` and `TimestampsTest` a bit.

PiperOrigin-RevId: 572279223
pull/14336/head
Kurt Alfred Kluever 1 year ago committed by Copybara-Service
parent 19aad63844
commit 833ded7298
  1. 41
      java/util/src/test/java/com/google/protobuf/util/DurationsTest.java
  2. 111
      java/util/src/test/java/com/google/protobuf/util/TimestampsTest.java

@ -165,7 +165,7 @@ public class DurationsTest {
@Test
public void testParse_outOfRange() throws ParseException {
try {
Duration x = Durations.parse("316576000000.123456789123456789s");
Durations.parse("316576000000.123456789123456789s");
fail("expected ParseException");
} catch (ParseException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Duration value is out of range.");
@ -507,60 +507,65 @@ public class DurationsTest {
public void testOverflows() throws Exception {
try {
Durations.toNanos(duration(315576000000L, 999999999));
assertWithMessage("Expected an ArithmeticException to be thrown").fail();
fail("Expected an ArithmeticException to be thrown");
} catch (ArithmeticException expected) {
}
try {
Durations.add(Durations.MAX_VALUE, Durations.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.subtract(Durations.MAX_VALUE, Durations.MIN_VALUE);
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.subtract(Durations.MIN_VALUE, Durations.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toNanos(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toMicros(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toMillis(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toSeconds(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toNanos(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toMicros(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toMillis(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.toSeconds(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
@ -568,17 +573,17 @@ public class DurationsTest {
.isEqualTo("9223372036.854775807s");
try {
Durations.fromMicros(Long.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.fromMillis(Long.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.fromSeconds(Long.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
@ -586,17 +591,17 @@ public class DurationsTest {
.isEqualTo("-9223372036.854775808s");
try {
Durations.fromMicros(Long.MIN_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.fromMillis(Long.MIN_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
try {
Durations.fromSeconds(Long.MIN_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}

@ -8,8 +8,8 @@
package com.google.protobuf.util;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.protobuf.util.DurationsTest.duration;
import static org.junit.Assert.fail;
import com.google.common.collect.Lists;
import com.google.protobuf.Duration;
@ -21,7 +21,6 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -77,7 +76,7 @@ public class TimestampsTest {
return;
}
}
assertWithMessage("no timestamp had sub-millisecond precision").fail();
fail("no timestamp had sub-millisecond precision");
}
@Test
@ -232,7 +231,7 @@ public class TimestampsTest {
Timestamp value =
Timestamp.newBuilder().setSeconds(Timestamps.TIMESTAMP_SECONDS_MIN - 1).build();
Timestamps.toString(value);
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
}
}
@ -244,7 +243,7 @@ public class TimestampsTest {
Timestamp value =
Timestamp.newBuilder().setSeconds(Timestamps.TIMESTAMP_SECONDS_MAX + 1).build();
Timestamps.toString(value);
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
}
}
@ -255,7 +254,7 @@ public class TimestampsTest {
// Invalid nanos value.
Timestamp value = Timestamp.newBuilder().setNanos(-1).build();
Timestamps.toString(value);
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
}
}
@ -266,7 +265,7 @@ public class TimestampsTest {
// Invalid nanos value.
Timestamp value = Timestamp.newBuilder().setNanos(1000000000).build();
Timestamps.toString(value);
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
}
}
@ -275,16 +274,16 @@ public class TimestampsTest {
public void testTimestampInvalidFormatDateTooSmall() {
try {
Timestamps.parse("0000-01-01T00:00:00Z");
Assert.fail();
fail();
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
assertThat(expected).hasCauseThat().isNotNull();
}
try {
Timestamps.parseUnchecked("0000-01-01T00:00:00Z");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -292,15 +291,15 @@ public class TimestampsTest {
public void testTimestampInvalidFormatDateTooLarge() {
try {
Timestamps.parse("10000-01-01T00:00:00Z");
Assert.fail();
fail();
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("10000-01-01T00:00:00Z");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -308,15 +307,15 @@ public class TimestampsTest {
public void testTimestampInvalidFormatMissingT() {
try {
Timestamps.parse("1970-01-01 00:00:00Z");
Assert.fail();
fail();
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("1970-01-01 00:00:00Z");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -324,15 +323,15 @@ public class TimestampsTest {
public void testTimestampInvalidFormatMissingZ() {
try {
Timestamps.parse("1970-01-01T00:00:00");
assertWithMessage("ParseException is expected.").fail();
fail("ParseException is expected.");
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("1970-01-01T00:00:00");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -340,15 +339,15 @@ public class TimestampsTest {
public void testTimestampInvalidOffset() {
try {
Timestamps.parse("1970-01-01T00:00:00+0000");
assertWithMessage("ParseException is expected.").fail();
fail("ParseException is expected.");
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("1970-01-01T00:00:00+0000");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -356,13 +355,13 @@ public class TimestampsTest {
public void testTimestampInvalidOffsetWithDot() {
try {
Timestamps.parse("2021-08-19T10:24:25-07.:00");
assertWithMessage("ParseException is expected.").fail();
fail("ParseException is expected.");
} catch (ParseException expected) {
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("2021-08-19T10:24:25-07.:00");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessageThat().isNotNull();
}
@ -372,15 +371,15 @@ public class TimestampsTest {
public void testTimestampInvalidTrailingText() {
try {
Timestamps.parse("1970-01-01T00:00:00Z0");
assertWithMessage("ParseException is expected.").fail();
fail("ParseException is expected.");
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("1970-01-01T00:00:00Z0");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -388,15 +387,15 @@ public class TimestampsTest {
public void testTimestampInvalidNanoSecond() {
try {
Timestamps.parse("1970-01-01T00:00:00.ABCZ");
assertWithMessage("ParseException is expected.").fail();
fail("ParseException is expected.");
} catch (ParseException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
try {
Timestamps.parseUnchecked("1970-01-01T00:00:00.ABCZ");
assertWithMessage("IllegalArgumentException is expected.").fail();
fail("IllegalArgumentException is expected.");
} catch (IllegalArgumentException expected) {
Assert.assertNotNull(expected.getMessage());
assertThat(expected).hasMessageThat().isNotNull();
}
}
@ -448,7 +447,7 @@ public class TimestampsTest {
Date date = calendar.getTime();
try {
Timestamps.fromDate(date);
Assert.fail("should have thrown IllegalArgumentException");
fail("should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessageThat().startsWith("Timestamp is not valid.");
}
@ -464,7 +463,7 @@ public class TimestampsTest {
Date date = calendar.getTime();
try {
Timestamps.fromDate(date);
Assert.fail("should have thrown IllegalArgumentException");
fail("should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessageThat().startsWith("Timestamp is not valid.");
}
@ -607,7 +606,7 @@ public class TimestampsTest {
public void testOverflowsArithmeticException() throws Exception {
try {
Timestamps.toNanos(Timestamps.parse("9999-12-31T23:59:59.999999999Z"));
assertWithMessage("Expected an ArithmeticException to be thrown").fail();
fail("Expected an ArithmeticException to be thrown");
} catch (ArithmeticException expected) {
}
}
@ -616,7 +615,7 @@ public class TimestampsTest {
public void testPositiveOverflow() {
try {
Timestamps.add(Timestamps.MAX_VALUE, Durations.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -625,7 +624,7 @@ public class TimestampsTest {
public void testNegativeOverflow() {
try {
Timestamps.subtract(Timestamps.MIN_VALUE, Durations.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -634,7 +633,7 @@ public class TimestampsTest {
public void testInvalidMaxNanosecondsOverflow() {
try {
Timestamps.toNanos(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -643,7 +642,7 @@ public class TimestampsTest {
public void testInvalidMaxMicrosecondsOverflow() {
try {
Timestamps.toMicros(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -652,7 +651,7 @@ public class TimestampsTest {
public void testInvalidMaxMillisecondsOverflow() {
try {
Timestamps.toMillis(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -661,7 +660,7 @@ public class TimestampsTest {
public void testInvalidMaxSecondsOverflow() {
try {
Timestamps.toSeconds(INVALID_MAX);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -670,7 +669,7 @@ public class TimestampsTest {
public void testInvalidMinNanosecondsOverflow() {
try {
Timestamps.toNanos(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -679,7 +678,7 @@ public class TimestampsTest {
public void testInvalidMicrosecondsMinOverflow() {
try {
Timestamps.toMicros(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -688,7 +687,7 @@ public class TimestampsTest {
public void testInvalidMinMillisecondsOverflow() {
try {
Timestamps.toMillis(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -697,7 +696,7 @@ public class TimestampsTest {
public void testOverInvalidMinSecondsflow() {
try {
Timestamps.toSeconds(INVALID_MIN);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -712,7 +711,7 @@ public class TimestampsTest {
public void testIllegalArgumentExceptionForMaxMicroseconds() {
try {
Timestamps.fromMicros(Long.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -721,7 +720,7 @@ public class TimestampsTest {
public void testIllegalArgumentExceptionForMaxMilliseconds() {
try {
Durations.fromMillis(Long.MAX_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -736,7 +735,7 @@ public class TimestampsTest {
public void testIllegalArgumentExceptionForMinMicroseconds() {
try {
Timestamps.fromMicros(Long.MIN_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -745,7 +744,7 @@ public class TimestampsTest {
public void testIllegalArgumentExceptionForMinMilliseconds() {
try {
Timestamps.fromMillis(Long.MIN_VALUE);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -802,7 +801,7 @@ public class TimestampsTest {
long timestampMaxSeconds = 253402300799L;
try {
Timestamps.fromMillis((timestampMaxSeconds + 1) * MILLIS_PER_SECOND);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}
@ -812,7 +811,7 @@ public class TimestampsTest {
long timestampMinSeconds = -62135596800L;
try {
Timestamps.fromMillis((timestampMinSeconds - 1) * MILLIS_PER_SECOND);
assertWithMessage("Expected an IllegalArgumentException to be thrown").fail();
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException expected) {
}
}

Loading…
Cancel
Save