Merge changes from topic "get-rid-of-joda-time"

* changes:
  Guard against null values for date during mail parsing
  Provide Joda time only for Elasticsearch
  Don't export Joda time for plugins anymore
  Remove joda-time from BUILD files for core Gerrit
  Switch to Java Date/Time API for TimeUtil
  Switch to Java Date/Time API for mail related code
  Switch to Java Date/Time API in ScheduleConfig
  Switch to Java Date/Time API in ResourceServletTest
  Switch to Java Date/Time API in ChangeBundleTest
  OutputStreamQueryTest: Remove test proving equivalence to Joda time
  Switch to Java Date/Time API in OutputStreamQuery
This commit is contained in:
Alice Kober-Sotzek
2017-10-02 12:35:12 +00:00
committed by Gerrit Code Review
33 changed files with 194 additions and 144 deletions

View File

@@ -192,18 +192,6 @@ maven_jar(
sha1 = "de80fe047052445869b96f6def6baca7182c95af",
)
maven_jar(
name = "joda_time",
artifact = "joda-time:joda-time:2.9.9",
sha1 = "f7b520c458572890807d143670c9b24f4de90897",
)
maven_jar(
name = "joda_convert",
artifact = "org.joda:joda-convert:1.8.1",
sha1 = "675642ac208e0b741bc9118dcbcae44c271b992a",
)
load("//lib:guava.bzl", "GUAVA_VERSION", "GUAVA_BIN_SHA1")
maven_jar(
@@ -941,6 +929,18 @@ maven_jar(
sha1 = "e2a604a584e6633545ac6b1fe99ef888ab96dae9",
)
maven_jar(
name = "joda_time",
artifact = "joda-time:joda-time:2.9.9",
sha1 = "f7b520c458572890807d143670c9b24f4de90897",
)
maven_jar(
name = "joda_convert",
artifact = "org.joda:joda-convert:1.8.1",
sha1 = "675642ac208e0b741bc9118dcbcae44c271b992a",
)
maven_jar(
name = "compress_lzf",
artifact = "com.ning:compress-lzf:1.0.2",

View File

@@ -4,7 +4,4 @@ acceptance_tests(
srcs = ["ChangeEditIT.java"],
group = "edit",
labels = ["edit"],
deps = [
"//lib/joda:joda-time",
],
)

View File

@@ -16,7 +16,6 @@ java_library(
srcs = ["AbstractPushForReview.java"],
deps = [
"//gerrit-acceptance-tests:lib",
"//lib/joda:joda-time",
],
)

View File

@@ -15,7 +15,6 @@ acceptance_tests(
labels = ["rest"],
deps = [
":submit_util",
"//lib/joda:joda-time",
],
)

View File

@@ -23,8 +23,8 @@ import com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput;
import com.google.gerrit.extensions.client.Comment;
import com.google.gerrit.extensions.client.Side;
import com.google.gerrit.server.mail.receive.MailMessage;
import java.time.Instant;
import java.util.HashMap;
import org.joda.time.DateTime;
import org.junit.Ignore;
@Ignore
@@ -36,7 +36,7 @@ public class AbstractMailIT extends AbstractDaemonTest {
b.from(user.emailAddress);
b.addTo(user.emailAddress); // Not evaluated
b.subject("");
b.dateReceived(new DateTime());
b.dateReceived(Instant.now());
return b;
}

View File

@@ -2,7 +2,6 @@ load("//gerrit-acceptance-tests:tests.bzl", "acceptance_tests")
DEPS = [
"//lib/greenmail",
"//lib/joda:joda-time",
"//lib/mail",
]

View File

@@ -28,7 +28,6 @@ gwt_module(
"//lib:gwtorm_client",
"//lib:servlet-api-3_1",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/log:api",
],
gwt_xml = SRC + "Common.gwt.xml",
@@ -53,7 +52,6 @@ java_library(
"//lib:gwtorm",
"//lib:servlet-api-3_1",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/log:api",
],
)

View File

@@ -15,14 +15,21 @@
package com.google.gerrit.common;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting;
import java.sql.Timestamp;
import org.joda.time.DateTimeUtils;
import java.util.function.LongSupplier;
/** Static utility methods for dealing with dates and times. */
@GwtIncompatible("Unemulated org.joda.time.DateTimeUtils")
@GwtIncompatible("Unemulated Java 8 functionalities")
public class TimeUtil {
private static final LongSupplier SYSTEM_CURRENT_MILLIS_SUPPLIER = System::currentTimeMillis;
private static volatile LongSupplier currentMillisSupplier = SYSTEM_CURRENT_MILLIS_SUPPLIER;
public static long nowMs() {
return DateTimeUtils.currentTimeMillis();
// We should rather use Instant.now(Clock).toEpochMilli() instead but this would require some
// changes in our testing code as we wouldn't have clock steps anymore.
return currentMillisSupplier.getAsLong();
}
public static Timestamp nowTs() {
@@ -33,5 +40,15 @@ public class TimeUtil {
return new Timestamp((t.getTime() / 1000) * 1000);
}
@VisibleForTesting
public static void setCurrentMillisSupplier(LongSupplier customCurrentMillisSupplier) {
currentMillisSupplier = customCurrentMillisSupplier;
}
@VisibleForTesting
public static void resetCurrentMillisSupplier() {
currentMillisSupplier = SYSTEM_CURRENT_MILLIS_SUPPLIER;
}
private TimeUtil() {}
}

View File

@@ -17,10 +17,10 @@ java_library(
"//lib/elasticsearch",
"//lib/elasticsearch:jest",
"//lib/elasticsearch:jest-common",
"//lib/elasticsearch:joda-time",
"//lib/guice",
"//lib/guice:guice-assistedinject",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/log:api",
"//lib/lucene:lucene-analyzers-common",
"//lib/lucene:lucene-core",

View File

@@ -77,6 +77,5 @@ junit_tests(
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/jgit/org.eclipse.jgit.junit:junit",
"//lib/joda:joda-time",
],
)

View File

@@ -36,9 +36,11 @@ import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.GZIPInputStream;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.Test;
@@ -91,7 +93,12 @@ public class ResourceServletTest {
@Before
public void setUp() {
fs = Jimfs.newFileSystem(Configuration.unix());
ts = new AtomicLong(ISODateTimeFormat.dateTime().parseMillis("2010-01-30T12:00:00.000-08:00"));
ts =
new AtomicLong(
LocalDateTime.of(2010, Month.JANUARY, 30, 12, 0, 0)
.atOffset(ZoneOffset.ofHours(-8))
.toInstant()
.toEpochMilli());
}
@Test

View File

@@ -22,7 +22,6 @@ BASE_JETTY_DEPS = [
"//lib/guice:guice-assistedinject",
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/log:api",
"//lib/log:log4j",
]

View File

@@ -29,7 +29,6 @@ EXPORTS = [
"//lib/httpcomponents:httpcore",
"//lib/jgit/org.eclipse.jgit.http.server:jgit-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/log:api",
"//lib/log:log4j",
"//lib/mina:sshd",

View File

@@ -92,7 +92,6 @@ java_library(
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit.archive:jgit-archive",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/joda:joda-time",
"//lib/jsoup",
"//lib/log:api",
"//lib/log:jsonevent-layout",
@@ -181,7 +180,6 @@ TESTUTIL_DEPS = [
"//lib/guice:guice-servlet",
"//lib/jgit/org.eclipse.jgit:jgit",
"//lib/jgit/org.eclipse.jgit.junit:junit",
"//lib/joda:joda-time",
"//lib/log:api",
"//lib/log:impl_log4j",
"//lib/log:log4j",

View File

@@ -16,16 +16,16 @@ package com.google.gerrit.server.config;
import com.google.common.annotations.VisibleForTesting;
import java.text.MessageFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,16 +49,16 @@ public class ScheduleConfig {
}
public ScheduleConfig(Config rc, String section, String subsection) {
this(rc, section, subsection, DateTime.now());
this(rc, section, subsection, ZonedDateTime.now());
}
public ScheduleConfig(
Config rc, String section, String subsection, String keyInterval, String keyStartTime) {
this(rc, section, subsection, keyInterval, keyStartTime, DateTime.now());
this(rc, section, subsection, keyInterval, keyStartTime, ZonedDateTime.now());
}
@VisibleForTesting
ScheduleConfig(Config rc, String section, String subsection, DateTime now) {
ScheduleConfig(Config rc, String section, String subsection, ZonedDateTime now) {
this(rc, section, subsection, KEY_INTERVAL, KEY_STARTTIME, now);
}
@@ -69,7 +69,7 @@ public class ScheduleConfig {
String subsection,
String keyInterval,
String keyStartTime,
DateTime now) {
ZonedDateTime now) {
this.rc = rc;
this.section = section;
this.subsection = subsection;
@@ -122,31 +122,24 @@ public class ScheduleConfig {
String section,
String subsection,
String keyStartTime,
DateTime now,
ZonedDateTime now,
long interval) {
long delay = MISSING_CONFIG;
String start = rc.getString(section, subsection, keyStartTime);
try {
if (start != null) {
DateTimeFormatter formatter;
MutableDateTime startTime = now.toMutableDateTime();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("[E ]HH:mm").withLocale(Locale.US);
LocalTime firstStartTime = LocalTime.parse(start, formatter);
ZonedDateTime startTime = now.with(firstStartTime);
try {
formatter = ISODateTimeFormat.hourMinute();
LocalTime firstStartTime = formatter.parseLocalTime(start);
startTime.hourOfDay().set(firstStartTime.getHourOfDay());
startTime.minuteOfHour().set(firstStartTime.getMinuteOfHour());
} catch (IllegalArgumentException e1) {
formatter = DateTimeFormat.forPattern("E HH:mm").withLocale(Locale.US);
LocalDateTime firstStartDateTime = formatter.parseLocalDateTime(start);
startTime.dayOfWeek().set(firstStartDateTime.getDayOfWeek());
startTime.hourOfDay().set(firstStartDateTime.getHourOfDay());
startTime.minuteOfHour().set(firstStartDateTime.getMinuteOfHour());
DayOfWeek dayOfWeek = formatter.parse(start, DayOfWeek::from);
startTime = startTime.with(dayOfWeek);
} catch (DateTimeParseException ignored) {
// Day of week is an optional parameter.
}
startTime.secondOfMinute().set(0);
startTime.millisOfSecond().set(0);
long s = startTime.getMillis();
long n = now.getMillis();
delay = (s - n) % interval;
startTime = startTime.truncatedTo(ChronoUnit.MINUTES);
delay = Duration.between(now, startTime).toMillis() % interval;
if (delay <= 0) {
delay += interval;
}

View File

@@ -18,7 +18,7 @@ import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.server.mail.Address;
import org.joda.time.DateTime;
import java.time.Instant;
/**
* A simplified representation of an RFC 2045-2047 mime email message used for representing received
@@ -40,7 +40,7 @@ public abstract class MailMessage {
public abstract ImmutableList<Address> cc();
// Metadata
public abstract DateTime dateReceived();
public abstract Instant dateReceived();
public abstract ImmutableList<String> additionalHeaders();
// Content
@@ -84,7 +84,7 @@ public abstract class MailMessage {
return this;
}
public abstract Builder dateReceived(DateTime val);
public abstract Builder dateReceived(Instant instant);
public abstract ImmutableList.Builder<String> additionalHeadersBuilder();

View File

@@ -33,7 +33,6 @@ import org.apache.james.mime4j.dom.Multipart;
import org.apache.james.mime4j.dom.TextBody;
import org.apache.james.mime4j.dom.address.Mailbox;
import org.apache.james.mime4j.message.DefaultMessageBuilder;
import org.joda.time.DateTime;
/** Parses raw email content received through POP3 or IMAP into an internal {@link MailMessage}. */
public class RawMailParser {
@@ -66,7 +65,9 @@ public class RawMailParser {
if (mimeMessage.getSubject() != null) {
messageBuilder.subject(mimeMessage.getSubject());
}
messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
if (mimeMessage.getDate() != null) {
messageBuilder.dateReceived(mimeMessage.getDate().toInstant());
}
// Add From, To and Cc
if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {

View File

@@ -42,17 +42,19 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,7 +67,10 @@ import org.slf4j.LoggerFactory;
public class OutputStreamQuery {
private static final Logger log = LoggerFactory.getLogger(OutputStreamQuery.class);
private static final DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss zzz");
private static final DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss zzz")
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
public enum OutputFormat {
TEXT,
@@ -402,7 +407,7 @@ public class OutputStreamQuery {
out.print('\n');
} else if (value instanceof Long && isDateField(field)) {
out.print(' ');
out.print(dtf.print(((Long) value) * 1000L));
out.print(dtf.format(Instant.ofEpochSecond((Long) value)));
out.print('\n');
} else if (isPrimitive(value)) {
out.print(' ');

View File

@@ -20,15 +20,19 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.joda.time.DateTime;
import org.junit.Test;
public class ScheduleConfigTest {
// Friday June 13, 2014 10:00 UTC
private static final DateTime NOW = DateTime.parse("2014-06-13T10:00:00-00:00");
private static final ZonedDateTime NOW =
LocalDateTime.of(2014, Month.JUNE, 13, 10, 0, 0).atOffset(ZoneOffset.UTC).toZonedDateTime();
@Test
public void initialDelay() throws Exception {

View File

@@ -20,9 +20,9 @@ import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Comment;
import com.google.gerrit.server.mail.Address;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateTime;
import org.junit.Ignore;
@Ignore
@@ -85,7 +85,7 @@ public class AbstractParserTest {
MailMessage.Builder b = MailMessage.builder();
b.id("id");
b.from(new Address("Foo Bar", "foo@bar.com"));
b.dateReceived(new DateTime());
b.dateReceived(Instant.now());
b.subject("");
return b;
}

View File

@@ -20,8 +20,10 @@ import static com.google.gerrit.server.mail.MetadataName.toHeaderWithDelimiter;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.MetadataName;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Test;
public class MetadataParserTest {
@@ -31,7 +33,7 @@ public class MetadataParserTest {
// email headers of the message.
MailMessage.Builder b = MailMessage.builder();
b.id("");
b.dateReceived(new DateTime());
b.dateReceived(Instant.now());
b.subject("");
b.addAdditionalHeader(toHeaderWithDelimiter(MetadataName.CHANGE_NUMBER) + "123");
@@ -48,8 +50,11 @@ public class MetadataParserTest {
assertThat(meta.changeNumber).isEqualTo(123);
assertThat(meta.patchSet).isEqualTo(1);
assertThat(meta.messageType).isEqualTo("comment");
assertThat(meta.timestamp.getTime())
.isEqualTo(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC).getMillis());
assertThat(meta.timestamp.toInstant())
.isEqualTo(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
}
@Test
@@ -58,7 +63,7 @@ public class MetadataParserTest {
// the text body of the message.
MailMessage.Builder b = MailMessage.builder();
b.id("");
b.dateReceived(new DateTime());
b.dateReceived(Instant.now());
b.subject("");
StringBuilder stringBuilder = new StringBuilder();
@@ -77,8 +82,11 @@ public class MetadataParserTest {
assertThat(meta.changeNumber).isEqualTo(123);
assertThat(meta.patchSet).isEqualTo(1);
assertThat(meta.messageType).isEqualTo("comment");
assertThat(meta.timestamp.getTime())
.isEqualTo(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC).getMillis());
assertThat(meta.timestamp.toInstant())
.isEqualTo(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
}
@Test
@@ -87,7 +95,7 @@ public class MetadataParserTest {
// the HTML body of the message.
MailMessage.Builder b = MailMessage.builder();
b.id("");
b.dateReceived(new DateTime());
b.dateReceived(Instant.now());
b.subject("");
StringBuilder stringBuilder = new StringBuilder();
@@ -111,7 +119,10 @@ public class MetadataParserTest {
assertThat(meta.changeNumber).isEqualTo(123);
assertThat(meta.patchSet).isEqualTo(1);
assertThat(meta.messageType).isEqualTo("comment");
assertThat(meta.timestamp.getTime())
.isEqualTo(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC).getMillis());
assertThat(meta.timestamp.toInstant())
.isEqualTo(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
}
}

View File

@@ -65,7 +65,7 @@ public class RawMailParserTest extends GerritBaseTests {
assertThat(have.to()).isEqualTo(want.to());
assertThat(have.from()).isEqualTo(want.from());
assertThat(have.cc()).isEqualTo(want.cc());
assertThat(have.dateReceived().getMillis()).isEqualTo(want.dateReceived().getMillis());
assertThat(have.dateReceived()).isEqualTo(want.dateReceived());
assertThat(have.additionalHeaders()).isEqualTo(want.additionalHeaders());
assertThat(have.subject()).isEqualTo(want.subject());
assertThat(have.textContent()).isEqualTo(want.textContent());

View File

@@ -16,8 +16,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/**
@@ -82,7 +83,10 @@ public class AttachmentMessage extends RawMailMessage {
.htmlContent("<div dir=\"ltr\">Contains unwanted attachment</div>")
.subject("Test Subject")
.addAdditionalHeader("MIME-Version: 1.0")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
return expect.build();
}
}

View File

@@ -16,8 +16,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/** Tests parsing a Base64 encoded subject. */
@@ -58,7 +59,10 @@ public class Base64HeaderMessage extends RawMailMessage {
.addTo(new Address("ekempin", "ekempin@google.com"))
.textContent(textContent)
.subject("\uD83D\uDE1B test")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
return expect.build();
}
}

View File

@@ -16,8 +16,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/** Tests a message containing mime/alternative (text + html) content. */
@@ -98,7 +99,10 @@ public class HtmlMimeMessage extends RawMailMessage {
.htmlContent(unencodedHtmlContent)
.subject("Change in gerrit[master]: Implement receiver class structure and bindings")
.addAdditionalHeader("MIME-Version: 1.0")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
return expect.build();
}
}

View File

@@ -15,8 +15,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/** Tests that non-UTF8 encodings are handled correctly. */
@@ -62,7 +63,10 @@ public class NonUTF8Message extends RawMailMessage {
.addTo(new Address("ekempin", "ekempin@google.com"))
.textContent(textContent)
.subject("\uD83D\uDE1B test")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
return expect.build();
}
}

View File

@@ -16,8 +16,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/** Tests parsing a quoted printable encoded subject */
@@ -59,7 +60,10 @@ public class QuotedPrintableHeaderMessage extends RawMailMessage {
.addTo(new Address("ekempin", "ekempin@google.com"))
.textContent(textContent)
.subject("âme vulgaire")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant());
return expect.build();
}
}

View File

@@ -16,8 +16,9 @@ package com.google.gerrit.server.mail.receive.data;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.mail.receive.MailMessage;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.junit.Ignore;
/** Tests parsing a simple text message with different headers. */
@@ -124,7 +125,10 @@ public class SimpleTextMessage extends RawMailMessage {
.addCc(new Address("Patrick Hiesel", "hiesel@google.com"))
.textContent(textContent)
.subject("Change in gerrit[master]: (Re)enable voting buttons for merged changes")
.dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC))
.dateReceived(
LocalDateTime.of(2016, Month.OCTOBER, 25, 9, 11, 35)
.atOffset(ZoneOffset.UTC)
.toInstant())
.addAdditionalHeader(
"Authentication-Results: mx.google.com; dkim=pass header.i=@google.com;")
.addAdditionalHeader(

View File

@@ -46,13 +46,14 @@ import com.google.gerrit.testutil.TestTimeUtil;
import com.google.gwtorm.protobuf.CodecFactory;
import com.google.gwtorm.protobuf.ProtobufCodec;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -67,6 +68,7 @@ public class ChangeBundleTest extends GerritBaseTests {
CodecFactory.encoder(PatchSetApproval.class);
private static final ProtobufCodec<PatchLineComment> PATCH_LINE_COMMENT_CODEC =
CodecFactory.encoder(PatchLineComment.class);
private static final String TIMEZONE_ID = "US/Eastern";
private String systemTimeZoneProperty;
private TimeZone systemTimeZone;
@@ -76,10 +78,9 @@ public class ChangeBundleTest extends GerritBaseTests {
@Before
public void setUp() {
String tz = "US/Eastern";
systemTimeZoneProperty = System.setProperty("user.timezone", tz);
systemTimeZoneProperty = System.setProperty("user.timezone", TIMEZONE_ID);
systemTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone(tz));
TimeZone.setDefault(TimeZone.getTimeZone(TIMEZONE_ID));
long maxMs = ChangeRebuilderImpl.MAX_WINDOW_MS;
assertThat(maxMs).isGreaterThan(1000L);
TestTimeUtil.resetWithClockStep(maxMs * 2, MILLISECONDS);
@@ -1517,8 +1518,11 @@ public class ChangeBundleTest extends GerritBaseTests {
PatchSetApproval a2 = clone(a1);
a2.setGranted(
new Timestamp(
new DateTime(1900, 1, 1, 0, 0, 0, DateTimeZone.forTimeZone(TimeZone.getDefault()))
.getMillis()));
LocalDate.of(1900, Month.JANUARY, 1)
.atStartOfDay()
.atZone(ZoneId.of(TIMEZONE_ID))
.toInstant()
.toEpochMilli()));
// Both are ReviewDb, exact match is required.
ChangeBundle b1 =

View File

@@ -1232,7 +1232,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
long thirtyHoursInMs = MILLISECONDS.convert(30, HOURS);
resetTimeWithClockStep(thirtyHoursInMs, MILLISECONDS);
TestRepository<Repo> repo = createProject("repo");
long startMs = TestTimeUtil.START.getMillis();
long startMs = TestTimeUtil.START.toEpochMilli();
Change change1 = insert(repo, newChange(repo), null, new Timestamp(startMs));
Change change2 = insert(repo, newChange(repo), null, new Timestamp(startMs + thirtyHoursInMs));
@@ -1259,7 +1259,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
long thirtyHoursInMs = MILLISECONDS.convert(30, HOURS);
resetTimeWithClockStep(thirtyHoursInMs, MILLISECONDS);
TestRepository<Repo> repo = createProject("repo");
long startMs = TestTimeUtil.START.getMillis();
long startMs = TestTimeUtil.START.toEpochMilli();
Change change1 = insert(repo, newChange(repo), null, new Timestamp(startMs));
Change change2 = insert(repo, newChange(repo), null, new Timestamp(startMs + thirtyHoursInMs));
TestTimeUtil.setClockStep(0, MILLISECONDS);
@@ -1281,7 +1281,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
long thirtyHoursInMs = MILLISECONDS.convert(30, HOURS);
resetTimeWithClockStep(thirtyHoursInMs, MILLISECONDS);
TestRepository<Repo> repo = createProject("repo");
long startMs = TestTimeUtil.START.getMillis();
long startMs = TestTimeUtil.START.toEpochMilli();
Change change1 = insert(repo, newChange(repo), null, new Timestamp(startMs));
Change change2 = insert(repo, newChange(repo), null, new Timestamp(startMs + thirtyHoursInMs));
TestTimeUtil.setClockStep(0, MILLISECONDS);

View File

@@ -17,18 +17,21 @@ package com.google.gerrit.testutil;
import static com.google.common.base.Preconditions.checkState;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import com.google.gerrit.common.TimeUtil;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeUtils.MillisProvider;
import org.joda.time.DateTimeZone;
/** Static utility methods for dealing with dates and times in tests. */
public class TestTimeUtil {
public static final DateTime START =
new DateTime(2009, 9, 30, 17, 0, 0, DateTimeZone.forOffsetHours(-4));
public static final Instant START =
LocalDateTime.of(2009, Month.SEPTEMBER, 30, 17, 0, 0)
.atOffset(ZoneOffset.ofHours(-4))
.toInstant();
private static Long clockStepMs;
private static AtomicLong clockMs;
@@ -43,7 +46,7 @@ public class TestTimeUtil {
*/
public static synchronized void resetWithClockStep(long clockStep, TimeUnit clockStepUnit) {
// Set an arbitrary start point so tests are more repeatable.
clockMs = new AtomicLong(START.getMillis());
clockMs = new AtomicLong(START.toEpochMilli());
setClockStep(clockStep, clockStepUnit);
}
@@ -56,13 +59,7 @@ public class TestTimeUtil {
public static synchronized void setClockStep(long clockStep, TimeUnit clockStepUnit) {
checkState(clockMs != null, "call resetWithClockStep first");
clockStepMs = MILLISECONDS.convert(clockStep, clockStepUnit);
DateTimeUtils.setCurrentMillisProvider(
new MillisProvider() {
@Override
public long getMillis() {
return clockMs.getAndAdd(clockStepMs);
}
});
TimeUtil.setCurrentMillisSupplier(() -> clockMs.getAndAdd(clockStepMs));
}
/**
@@ -89,7 +86,7 @@ public class TestTimeUtil {
/** Reset the clock to use the actual system clock. */
public static synchronized void useSystemTime() {
clockMs = null;
DateTimeUtils.setCurrentMillisSystem();
TimeUtil.resetCurrentMillisSupplier();
}
private TestTimeUtil() {}

View File

@@ -8,13 +8,13 @@ java_library(
":compress-lzf",
":hppc",
":jna",
":joda-time",
":jsr166e",
":netty",
":t-digest",
"//lib/jackson:jackson-core",
"//lib/jackson:jackson-dataformat-cbor",
"//lib/jackson:jackson-dataformat-smile",
"//lib/joda:joda-time",
"//lib/lucene:lucene-codecs",
"//lib/lucene:lucene-highlighter",
"//lib/lucene:lucene-join",
@@ -47,6 +47,19 @@ java_library(
],
)
java_library(
name = "joda-time",
data = ["//lib:LICENSE-Apache2.0"],
exports = ["@joda_time//jar"],
runtime_deps = ["joda-convert"],
)
java_library(
name = "joda-convert",
data = ["//lib:LICENSE-Apache2.0"],
exports = ["@joda_convert//jar"],
)
java_library(
name = "compress-lzf",
data = ["//lib:LICENSE-Apache2.0"],

View File

@@ -1,13 +0,0 @@
java_library(
name = "joda-time",
data = ["//lib:LICENSE-Apache2.0"],
visibility = ["//visibility:public"],
exports = ["@joda_time//jar"],
runtime_deps = ["joda-convert"],
)
java_library(
name = "joda-convert",
data = ["//lib:LICENSE-Apache2.0"],
exports = ["@joda_convert//jar"],
)