Add j2cl and j2objc targets for protobuf/current/javatests/com/google/protobuf/util TIME_SRCS

Because not everything is supported in j2cl and j2objc some of the methods had to be marked as incompatible (reflection , String.format...)

PiperOrigin-RevId: 691368238
pull/18896/head
Protobuf Team Bot 4 weeks ago committed by Copybara-Service
parent 1f5df18680
commit afb20af442
  1. 1
      java/util/BUILD.bazel
  2. 15
      java/util/src/main/java/com/google/protobuf/util/Durations.java
  3. 41
      java/util/src/main/java/com/google/protobuf/util/Timestamps.java
  4. 20
      java/util/src/test/java/com/google/protobuf/util/DurationsTest.java
  5. 62
      java/util/src/test/java/com/google/protobuf/util/TimestampsTest.java

@ -92,6 +92,7 @@ junit_tests(
"//java/core:generic_test_protos_java_proto",
"@protobuf_maven//:com_google_code_gson_gson",
"@protobuf_maven//:com_google_guava_guava",
"@protobuf_maven//:com_google_j2objc_j2objc_annotations",
"@protobuf_maven//:com_google_truth_truth",
"@protobuf_maven//:junit_junit",
],

@ -19,8 +19,11 @@ import static com.google.protobuf.util.Timestamps.NANOS_PER_MICROSECOND;
import static com.google.protobuf.util.Timestamps.NANOS_PER_MILLISECOND;
import static com.google.protobuf.util.Timestamps.NANOS_PER_SECOND;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CompileTimeConstant;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import com.google.protobuf.Duration;
import java.io.Serializable;
import java.text.ParseException;
@ -140,6 +143,8 @@ public final class Durations {
* @throws NullPointerException if {@code duration} is {@code null}
*/
@CanIgnoreReturnValue
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public static Duration checkNotNegative(Duration duration) {
checkArgument(!isNegative(duration), "duration (%s) must not be negative", toString(duration));
return duration;
@ -152,6 +157,8 @@ public final class Durations {
* @throws NullPointerException if {@code duration} is {@code null}
*/
@CanIgnoreReturnValue
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public static Duration checkPositive(Duration duration) {
checkArgument(isPositive(duration), "duration (%s) must be positive", toString(duration));
return duration;
@ -164,7 +171,7 @@ public final class Durations {
int nanos = duration.getNanos();
if (!isValid(seconds, nanos)) {
throw new IllegalArgumentException(
String.format(
Strings.lenientFormat(
"Duration is not valid. See proto definition for valid values. "
+ "Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. "
+ "Nanos (%s) must be in range [-999,999,999, +999,999,999]. "
@ -193,6 +200,8 @@ public final class Durations {
* @return The string representation of the given duration.
* @throws IllegalArgumentException if the given duration is not in the valid range.
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public static String toString(Duration duration) {
checkValid(duration);
@ -220,6 +229,8 @@ public final class Durations {
* @return a Duration parsed from the string
* @throws ParseException if the string is not in the duration format
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public static Duration parse(String value) throws ParseException {
// Must end with "s".
if (value.isEmpty() || value.charAt(value.length() - 1) != 's') {
@ -269,6 +280,8 @@ public final class Durations {
* @return a {@link Duration} parsed from the string
* @throws IllegalArgumentException if parsing fails
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public static Duration parseUnchecked(@CompileTimeConstant String value) {
try {
return parse(value);

@ -13,6 +13,8 @@ import static com.google.common.math.LongMath.checkedAdd;
import static com.google.common.math.LongMath.checkedMultiply;
import static com.google.common.math.LongMath.checkedSubtract;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CompileTimeConstant;
import com.google.j2objc.annotations.J2ObjCIncompatible;
@ -62,6 +64,8 @@ public final class Timestamps {
*/
public static final Timestamp EPOCH = Timestamp.newBuilder().setSeconds(0).setNanos(0).build();
@GwtIncompatible("Date formatting is not supported in non JVM java.time")
@J2ObjCIncompatible
private static final ThreadLocal<SimpleDateFormat> timestampFormat =
new ThreadLocal<SimpleDateFormat>() {
@Override
@ -70,6 +74,8 @@ public final class Timestamps {
}
};
@GwtIncompatible("Date formatting is not supported in non JVM java.time")
@J2ObjCIncompatible
private static SimpleDateFormat createTimestampFormat() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
@ -163,7 +169,7 @@ public final class Timestamps {
int nanos = timestamp.getNanos();
if (!isValid(seconds, nanos)) {
throw new IllegalArgumentException(
String.format(
Strings.lenientFormat(
"Timestamp is not valid. See proto definition for valid values. "
+ "Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. "
+ "Nanos (%s) must be in range [0, +999,999,999].",
@ -193,6 +199,8 @@ public final class Timestamps {
* @return The string representation of the given timestamp.
* @throws IllegalArgumentException if the given timestamp is not in the valid range.
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public static String toString(Timestamp timestamp) {
checkValid(timestamp);
@ -222,6 +230,8 @@ public final class Timestamps {
* @return a Timestamp parsed from the string
* @throws ParseException if parsing fails
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public static Timestamp parse(String value) throws ParseException {
int dayOffset = value.indexOf('T');
if (dayOffset == -1) {
@ -287,6 +297,8 @@ public final class Timestamps {
* @return a {@link Timestamp} parsed from the string
* @throws IllegalArgumentException if parsing fails
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public static Timestamp parseUnchecked(@CompileTimeConstant String value) {
try {
return parse(value);
@ -299,12 +311,23 @@ public final class Timestamps {
// the following 3 constants contain references to java.time.Instant methods (if that class is
// available at runtime); otherwise, they are null.
@Nullable private static final Method INSTANT_NOW = instantMethod("now");
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_NOW = instantMethod("now");
@Nullable private static final Method INSTANT_GET_EPOCH_SECOND = instantMethod("getEpochSecond");
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_GET_EPOCH_SECOND = instantMethod("getEpochSecond");
@Nullable private static final Method INSTANT_GET_NANO = instantMethod("getNano");
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_GET_NANO = instantMethod("getNano");
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
@Nullable
private static Method instantMethod(String methodName) {
try {
@ -321,6 +344,8 @@ public final class Timestamps {
* you're unable to mock the current time. Instead, you may want to consider injecting a clock
* instance to read the current time.
*/
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
public static Timestamp now() {
if (INSTANT_NOW != null) {
try {
@ -469,7 +494,7 @@ public final class Timestamps {
// when normalized.
if (!isValidSeconds(seconds)) {
throw new IllegalArgumentException(
String.format(
Strings.lenientFormat(
"Timestamp is not valid. Input seconds is too large. "
+ "Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. ",
seconds));
@ -488,6 +513,8 @@ public final class Timestamps {
return checkValid(timestamp);
}
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
private static long parseTimezoneOffset(String value) throws ParseException {
int pos = value.indexOf(':');
if (pos == -1) {
@ -504,6 +531,8 @@ public final class Timestamps {
}
}
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
static int parseNanos(String value) throws ParseException {
int result = 0;
for (int i = 0; i < 9; ++i) {
@ -519,6 +548,8 @@ public final class Timestamps {
}
/** Format the nano part of a timestamp or a duration. */
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
static String formatNanos(int nanos) {
// Determine whether to use 3, 6, or 9 digits for the nano part.
if (nanos % NANOS_PER_MILLISECOND == 0) {

@ -12,7 +12,9 @@ import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.protobuf.util.Durations.toSecondsAsDouble;
import static org.junit.Assert.fail;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Lists;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import java.text.ParseException;
@ -47,6 +49,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testCheckNotNegative() {
Durations.checkNotNegative(Durations.ZERO);
Durations.checkNotNegative(Durations.fromNanos(1));
@ -68,6 +72,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testCheckPositive() {
Durations.checkPositive(Durations.fromNanos(1));
Durations.checkPositive(Durations.fromSeconds(1));
@ -163,6 +169,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testParse_outOfRange() throws ParseException {
try {
Durations.parse("316576000000.123456789123456789s");
@ -174,6 +182,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testDurationStringFormat() throws Exception {
Timestamp start = Timestamps.parse("0001-01-01T00:00:00Z");
Timestamp end = Timestamps.parse("9999-12-31T23:59:59.999999999Z");
@ -226,6 +236,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testDurationInvalidFormat() {
// Value too small.
try {
@ -346,6 +358,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testDurationConversion() throws Exception {
Duration duration = Durations.parse("1.111111111s");
assertThat(Durations.toNanos(duration)).isEqualTo(1111111111);
@ -377,6 +391,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimeOperations() throws Exception {
Timestamp start = Timestamps.parse("0001-01-01T00:00:00Z");
Timestamp end = Timestamps.parse("9999-12-31T23:59:59.999999999Z");
@ -406,6 +422,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testToString() {
assertThat(Durations.toString(duration(1, 1))).isEqualTo("1.000000001s");
assertThat(Durations.toString(duration(-1, -1))).isEqualTo("-1.000000001s");
@ -504,6 +522,8 @@ public class DurationsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testOverflows() throws Exception {
try {
Durations.toNanos(duration(315576000000L, 999999999));

@ -11,7 +11,9 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.protobuf.util.DurationsTest.duration;
import static org.junit.Assert.fail;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Lists;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import java.text.ParseException;
@ -51,6 +53,8 @@ public class TimestampsTest {
Timestamp.newBuilder().setSeconds(Long.MIN_VALUE).setNanos(Integer.MIN_VALUE).build();
@Test
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
public void testNow() {
Timestamp now = Timestamps.now();
long epochSeconds = System.currentTimeMillis() / 1000;
@ -59,6 +63,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ObjCIncompatible
public void testNowWithSubMillisecondPrecision() {
try {
// throws if we're not on Java9+
@ -110,6 +116,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampStringFormat() throws Exception {
Timestamp start = Timestamps.parse("0001-01-01T00:00:00Z");
Timestamp end = Timestamps.parse("9999-12-31T23:59:59.999999999Z");
@ -159,6 +167,8 @@ public class TimestampsTest {
private volatile boolean stopParsingThreads = false;
private volatile String errorMessage = "";
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
private class ParseTimestampThread extends Thread {
private final String[] strings;
private final Timestamp[] values;
@ -192,6 +202,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampConcurrentParsing() throws Exception {
String[] timestampStrings =
new String[] {
@ -225,6 +237,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampInvalidFormatValueTooSmall() throws Exception {
try {
// Value too small.
@ -237,6 +251,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampInvalidFormatValueTooLarge() throws Exception {
try {
// Value too large.
@ -249,6 +265,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampInvalidFormatNanosTooSmall() throws Exception {
try {
// Invalid nanos value.
@ -260,6 +278,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampInvalidFormatNanosTooLarge() throws Exception {
try {
// Invalid nanos value.
@ -271,6 +291,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidFormatDateTooSmall() {
try {
Timestamps.parse("0000-01-01T00:00:00Z");
@ -288,6 +310,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidFormatDateTooLarge() {
try {
Timestamps.parse("10000-01-01T00:00:00Z");
@ -304,6 +328,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidFormatMissingT() {
try {
Timestamps.parse("1970-01-01 00:00:00Z");
@ -320,6 +346,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidFormatMissingZ() {
try {
Timestamps.parse("1970-01-01T00:00:00");
@ -336,6 +364,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidOffset() {
try {
Timestamps.parse("1970-01-01T00:00:00+0000");
@ -352,6 +382,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidOffsetWithDot() {
try {
Timestamps.parse("2021-08-19T10:24:25-07.:00");
@ -368,6 +400,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidTrailingText() {
try {
Timestamps.parse("1970-01-01T00:00:00Z0");
@ -384,6 +418,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimestampInvalidNanoSecond() {
try {
Timestamps.parse("1970-01-01T00:00:00.ABCZ");
@ -400,6 +436,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testTimestampConversion() throws Exception {
Timestamp timestamp = Timestamps.parse("1970-01-01T00:00:01.111111111Z");
assertThat(Timestamps.toNanos(timestamp)).isEqualTo(1111111111);
@ -431,6 +469,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testFromDate() {
Date date = new Date(1111);
Timestamp timestamp = Timestamps.fromDate(date);
@ -438,6 +478,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Calendar is not supported in non JVM java.time")
@J2ObjCIncompatible
public void testFromDate_after9999CE() {
// protobuf still requires Java 7 so no java.time :-(
Calendar calendar = Calendar.getInstance();
@ -454,6 +496,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Calendar is not supported in non JVM java.time")
@J2ObjCIncompatible
public void testFromDate_beforeYear1() {
// protobuf still requires Java 7 so no java.time :-(
Calendar calendar = Calendar.getInstance();
@ -470,6 +514,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Calendar is not supported in non JVM java.time")
@J2ObjCIncompatible
public void testFromDate_after2262CE() {
// protobuf still requires Java 7 so no java.time :-(
Calendar calendar = Calendar.getInstance();
@ -484,6 +530,8 @@ public class TimestampsTest {
/* Timestamp only stores integral seconds in the Date parent class and stores the nanosecond
* adjustment in the Timestamp class. */
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testFromSqlTimestampSubMillisecondPrecision() {
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(1111);
sqlTimestamp.setNanos(sqlTimestamp.getNanos() + 234567);
@ -492,6 +540,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testFromSqlTimestamp() {
Date date = new java.sql.Timestamp(1111);
Timestamp timestamp = Timestamps.fromDate(date);
@ -499,6 +549,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testFromSqlTimestamp_beforeEpoch() {
Date date = new java.sql.Timestamp(-1111);
Timestamp timestamp = Timestamps.fromDate(date);
@ -506,6 +558,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testFromSqlTimestamp_beforeEpochWholeSecond() {
Date date = new java.sql.Timestamp(-2000);
Timestamp timestamp = Timestamps.fromDate(date);
@ -513,6 +567,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testTimeOperations() throws Exception {
Timestamp start = Timestamps.parse("0001-01-01T00:00:00Z");
Timestamp end = Timestamps.parse("9999-12-31T23:59:59.999999999Z");
@ -603,6 +659,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ObjCIncompatible
public void testOverflowsArithmeticException() throws Exception {
try {
Timestamps.toNanos(Timestamps.parse("9999-12-31T23:59:59.999999999Z"));
@ -702,6 +760,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testMaxNanosecondsConversion() {
assertThat(Timestamps.toString(Timestamps.fromNanos(Long.MAX_VALUE)))
.isEqualTo("2262-04-11T23:47:16.854775807Z");
@ -726,6 +786,8 @@ public class TimestampsTest {
}
@Test
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ObjCIncompatible
public void testMinNanosecondsConversion() {
assertThat(Timestamps.toString(Timestamps.fromNanos(Long.MIN_VALUE)))
.isEqualTo("1677-09-21T00:12:43.145224192Z");

Loading…
Cancel
Save