Replace use of Date with OffsetDateTime and Instant in Actuator

Closes gh-10976
This commit is contained in:
Andy Wilkinson 2018-01-12 10:40:55 +00:00
parent a99adb1047
commit 2b99962a85
29 changed files with 150 additions and 157 deletions

View File

@ -321,6 +321,12 @@
<artifactId>spring-session-core</artifactId> <artifactId>spring-session-core</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- Runtime -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Annotation processing --> <!-- Annotation processing -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,10 @@
package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation; package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;
import java.time.ZonedDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import org.junit.Test; import org.junit.Test;
@ -74,10 +73,9 @@ public class AuditEventsEndpointDocumentationTests
@Test @Test
public void filteredAuditEvents() throws Exception { public void filteredAuditEvents() throws Exception {
ZonedDateTime now = ZonedDateTime.now(); OffsetDateTime now = OffsetDateTime.now();
String queryTimestamp = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now); String queryTimestamp = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now);
Date date = new Date(now.toEpochSecond() * 1000); given(this.repository.find("alice", now.toInstant(), "logout")).willReturn(
given(this.repository.find("alice", date, "logout")).willReturn(
Arrays.asList(new AuditEvent("alice", "logout", Collections.emptyMap()))); Arrays.asList(new AuditEvent("alice", "logout", Collections.emptyMap())));
this.mockMvc this.mockMvc
.perform(get("/actuator/auditevents").param("principal", "alice") .perform(get("/actuator/auditevents").param("principal", "alice")
@ -94,7 +92,7 @@ public class AuditEventsEndpointDocumentationTests
parameterWithName("type").description( parameterWithName("type").description(
"Restricts the events to those with the given " "Restricts the events to those with the given "
+ "type. Optional.")))); + "type. Optional."))));
verify(this.repository).find("alice", date, "logout"); verify(this.repository).find("alice", now.toInstant(), "logout");
} }
@Configuration @Configuration

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,12 +17,11 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.io.Serializable; import java.io.Serializable;
import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
@ -46,7 +45,7 @@ import org.springframework.util.Assert;
@JsonInclude(Include.NON_EMPTY) @JsonInclude(Include.NON_EMPTY)
public class AuditEvent implements Serializable { public class AuditEvent implements Serializable {
private final Date timestamp; private final Instant timestamp;
private final String principal; private final String principal;
@ -61,7 +60,7 @@ public class AuditEvent implements Serializable {
* @param data The event data * @param data The event data
*/ */
public AuditEvent(String principal, String type, Map<String, Object> data) { public AuditEvent(String principal, String type, Map<String, Object> data) {
this(new Date(), principal, type, data); this(Instant.now(), principal, type, data);
} }
/** /**
@ -72,7 +71,7 @@ public class AuditEvent implements Serializable {
* @param data The event data in the form 'key=value' or simply 'key' * @param data The event data in the form 'key=value' or simply 'key'
*/ */
public AuditEvent(String principal, String type, String... data) { public AuditEvent(String principal, String type, String... data) {
this(new Date(), principal, type, convert(data)); this(Instant.now(), principal, type, convert(data));
} }
/** /**
@ -82,7 +81,7 @@ public class AuditEvent implements Serializable {
* @param type the event type * @param type the event type
* @param data The event data * @param data The event data
*/ */
public AuditEvent(Date timestamp, String principal, String type, public AuditEvent(Instant timestamp, String principal, String type,
Map<String, Object> data) { Map<String, Object> data) {
Assert.notNull(timestamp, "Timestamp must not be null"); Assert.notNull(timestamp, "Timestamp must not be null");
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
@ -107,11 +106,10 @@ public class AuditEvent implements Serializable {
} }
/** /**
* Returns the date/time that the even was logged. * Returns the date/time that the event was logged.
* @return the time stamp * @return the timestamp
*/ */
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ") public Instant getTimestamp() {
public Date getTimestamp() {
return this.timestamp; return this.timestamp;
} }

View File

@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Date; import java.time.Instant;
import java.util.List; import java.util.List;
/** /**
@ -43,6 +43,6 @@ public interface AuditEventRepository {
* @return audit events of specified type relating to the principal * @return audit events of specified type relating to the principal
* @since 1.4.0 * @since 1.4.0
*/ */
List<AuditEvent> find(String principal, Date after, String type); List<AuditEvent> find(String principal, Instant after, String type);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Date; import java.time.OffsetDateTime;
import java.util.List; import java.util.List;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@ -40,10 +40,10 @@ public class AuditEventsEndpoint {
} }
@ReadOperation @ReadOperation
public AuditEventsDescriptor eventsWithPrincipalDateAfterAndType(String principal, public AuditEventsDescriptor events(String principal, OffsetDateTime after,
Date after, String type) { String type) {
return new AuditEventsDescriptor( return new AuditEventsDescriptor(this.auditEventRepository.find(principal,
this.auditEventRepository.find(principal, after, type)); after == null ? null : after.toInstant(), type));
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Date; import java.time.OffsetDateTime;
import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor; import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
@ -40,10 +40,9 @@ public class AuditEventsEndpointWebExtension {
} }
@ReadOperation @ReadOperation
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType( public WebEndpointResponse<AuditEventsDescriptor> events(@Nullable String principal,
@Nullable String principal, Date after, @Nullable String type) { OffsetDateTime after, @Nullable String type) {
AuditEventsDescriptor auditEvents = this.delegate AuditEventsDescriptor auditEvents = this.delegate.events(principal, after, type);
.eventsWithPrincipalDateAfterAndType(principal, after, type);
return new WebEndpointResponse<>(auditEvents); return new WebEndpointResponse<>(auditEvents);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Date; import java.time.OffsetDateTime;
import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor; import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
@ -39,15 +39,14 @@ public class AuditEventsJmxEndpointExtension {
} }
@ReadOperation @ReadOperation
public AuditEventsDescriptor eventsWithDateAfter(Date dateAfter) { public AuditEventsDescriptor eventsAfter(OffsetDateTime after) {
return this.delegate.eventsWithPrincipalDateAfterAndType(null, dateAfter, null); return this.delegate.events(null, after, null);
} }
@ReadOperation @ReadOperation
public AuditEventsDescriptor eventsWithPrincipalAndDateAfter(String principal, public AuditEventsDescriptor eventsWithPrincipalAndAfter(String principal,
Date dateAfter) { OffsetDateTime after) {
return this.delegate.eventsWithPrincipalDateAfterAndType(principal, dateAfter, return this.delegate.events(principal, after, null);
null);
} }
} }

View File

@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Date; import java.time.Instant;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -70,7 +70,7 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
} }
@Override @Override
public List<AuditEvent> find(String principal, Date after, String type) { public List<AuditEvent> find(String principal, Instant after, String type) {
LinkedList<AuditEvent> events = new LinkedList<>(); LinkedList<AuditEvent> events = new LinkedList<>();
synchronized (this.monitor) { synchronized (this.monitor) {
for (int i = 0; i < this.events.length; i++) { for (int i = 0; i < this.events.length; i++) {
@ -83,10 +83,11 @@ public class InMemoryAuditEventRepository implements AuditEventRepository {
return events; return events;
} }
private boolean isMatch(String principal, Date after, String type, AuditEvent event) { private boolean isMatch(String principal, Instant after, String type,
AuditEvent event) {
boolean match = true; boolean match = true;
match = match && (principal == null || event.getPrincipal().equals(principal)); match = match && (principal == null || event.getPrincipal().equals(principal));
match = match && (after == null || event.getTimestamp().compareTo(after) >= 0); match = match && (after == null || event.getTimestamp().isAfter(after));
match = match && (type == null || event.getType().equals(type)); match = match && (type == null || event.getType().equals(type));
return match; return match;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.audit.listener; package org.springframework.boot.actuate.audit.listener;
import java.util.Date; import java.time.Instant;
import java.util.Map; import java.util.Map;
import org.springframework.boot.actuate.audit.AuditEvent; import org.springframework.boot.actuate.audit.AuditEvent;
@ -60,13 +60,13 @@ public class AuditApplicationEvent extends ApplicationEvent {
/** /**
* Create a new {@link AuditApplicationEvent} that wraps a newly created * Create a new {@link AuditApplicationEvent} that wraps a newly created
* {@link AuditEvent}. * {@link AuditEvent}.
* @param timestamp the time stamp * @param timestamp the timestamp
* @param principal the principal * @param principal the principal
* @param type the event type * @param type the event type
* @param data the event data * @param data the event data
* @see AuditEvent#AuditEvent(Date, String, String, Map) * @see AuditEvent#AuditEvent(Instant, String, String, Map)
*/ */
public AuditApplicationEvent(Date timestamp, String principal, String type, public AuditApplicationEvent(Instant timestamp, String principal, String type,
Map<String, Object> data) { Map<String, Object> data) {
this(new AuditEvent(timestamp, principal, type, data)); this(new AuditEvent(timestamp, principal, type, data));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,29 +18,25 @@ package org.springframework.boot.actuate.endpoint.convert;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* A {@link String} to {@link Date} {@link Converter} that uses * A {@link String} to {@link OffsetDateTime} {@link Converter} that uses
* {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME ISO offset} parsing. * {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME ISO offset} parsing.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
public class IsoOffsetDateTimeConverter implements Converter<String, Date> { public class IsoOffsetDateTimeConverter implements Converter<String, OffsetDateTime> {
@Override @Override
public Date convert(String source) { public OffsetDateTime convert(String source) {
if (StringUtils.hasLength(source)) { if (StringUtils.hasLength(source)) {
OffsetDateTime offsetDateTime = OffsetDateTime.parse(source, return OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
return new Date(TimeUnit.SECONDS.toMillis(offsetDateTime.toEpochSecond()));
} }
return null; return null;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,9 +18,9 @@ package org.springframework.boot.actuate.endpoint.jmx.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -113,7 +113,7 @@ class JmxEndpointOperationFactory implements OperationFactory<JmxOperation> {
if (type.isEnum()) { if (type.isEnum()) {
return String.class; return String.class;
} }
if (Date.class.isAssignableFrom(type)) { if (Instant.class.isAssignableFrom(type)) {
return String.class; return String.class;
} }
if (type.getName().startsWith("java.")) { if (type.getName().startsWith("java.")) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.flyway; package org.springframework.boot.actuate.flyway;
import java.util.Date; import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -99,7 +99,7 @@ public class FlywayEndpoint {
private final String installedBy; private final String installedBy;
private final Date installedOn; private final Instant installedOn;
private final Integer installedRank; private final Integer installedRank;
@ -113,7 +113,7 @@ public class FlywayEndpoint {
this.script = info.getScript(); this.script = info.getScript();
this.state = info.getState(); this.state = info.getState();
this.installedBy = info.getInstalledBy(); this.installedBy = info.getInstalledBy();
this.installedOn = info.getInstalledOn(); this.installedOn = Instant.ofEpochMilli(info.getInstalledOn().getTime());
this.installedRank = info.getInstalledRank(); this.installedRank = info.getInstalledRank();
this.executionTime = info.getExecutionTime(); this.executionTime = info.getExecutionTime();
} }
@ -150,7 +150,7 @@ public class FlywayEndpoint {
return this.installedBy; return this.installedBy;
} }
public Date getInstalledOn() { public Instant getInstalledOn() {
return this.installedOn; return this.installedOn;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.info; package org.springframework.boot.actuate.info;
import java.util.Date; import java.time.Instant;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -59,7 +59,7 @@ public class GitInfoContributor extends InfoPropertiesInfoContributor<GitPropert
/** /**
* Post-process the content to expose. By default, well known keys representing dates * Post-process the content to expose. By default, well known keys representing dates
* are converted to {@link Date} instances. * are converted to {@link Instant} instances.
* @param content the content to expose * @param content the content to expose
*/ */
@Override @Override
@ -67,7 +67,7 @@ public class GitInfoContributor extends InfoPropertiesInfoContributor<GitPropert
replaceValue(getNestedMap(content, "commit"), "time", replaceValue(getNestedMap(content, "commit"), "time",
getProperties().getCommitTime()); getProperties().getCommitTime());
replaceValue(getNestedMap(content, "build"), "time", replaceValue(getNestedMap(content, "build"), "time",
getProperties().getDate("build.time")); getProperties().getInstant("build.time"));
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.liquibase; package org.springframework.boot.actuate.liquibase;
import java.util.Date; import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -119,7 +119,7 @@ public class LiquibaseEndpoint {
private final Set<String> contexts; private final Set<String> contexts;
private final Date dateExecuted; private final Instant dateExecuted;
private final String deploymentId; private final String deploymentId;
@ -142,7 +142,8 @@ public class LiquibaseEndpoint {
this.changeLog = ranChangeSet.getChangeLog(); this.changeLog = ranChangeSet.getChangeLog();
this.comments = ranChangeSet.getComments(); this.comments = ranChangeSet.getComments();
this.contexts = ranChangeSet.getContextExpression().getContexts(); this.contexts = ranChangeSet.getContextExpression().getContexts();
this.dateExecuted = ranChangeSet.getDateExecuted(); this.dateExecuted = Instant
.ofEpochMilli(ranChangeSet.getDateExecuted().getTime());
this.deploymentId = ranChangeSet.getDeploymentId(); this.deploymentId = ranChangeSet.getDeploymentId();
this.description = ranChangeSet.getDescription(); this.description = ranChangeSet.getDescription();
this.execType = ranChangeSet.getExecType(); this.execType = ranChangeSet.getExecType();
@ -170,7 +171,7 @@ public class LiquibaseEndpoint {
return this.contexts; return this.contexts;
} }
public Date getDateExecuted() { public Instant getDateExecuted() {
return this.dateExecuted; return this.dateExecuted;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,9 +16,9 @@
package org.springframework.boot.actuate.trace; package org.springframework.boot.actuate.trace;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -66,7 +66,7 @@ public class InMemoryTraceRepository implements TraceRepository {
@Override @Override
public void add(Map<String, Object> map) { public void add(Map<String, Object> map) {
Trace trace = new Trace(new Date(), map); Trace trace = new Trace(Instant.now(), map);
synchronized (this.traces) { synchronized (this.traces) {
while (this.traces.size() >= this.capacity) { while (this.traces.size() >= this.capacity) {
this.traces.remove(this.reverse ? this.capacity - 1 : 0); this.traces.remove(this.reverse ? this.capacity - 1 : 0);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.trace; package org.springframework.boot.actuate.trace;
import java.util.Date; import java.time.Instant;
import java.util.Map; import java.util.Map;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -29,18 +29,18 @@ import org.springframework.util.Assert;
*/ */
public final class Trace { public final class Trace {
private final Date timestamp; private final Instant timestamp;
private final Map<String, Object> info; private final Map<String, Object> info;
public Trace(Date timestamp, Map<String, Object> info) { public Trace(Instant timestamp, Map<String, Object> info) {
Assert.notNull(timestamp, "Timestamp must not be null"); Assert.notNull(timestamp, "Timestamp must not be null");
Assert.notNull(info, "Info must not be null"); Assert.notNull(info, "Info must not be null");
this.timestamp = timestamp; this.timestamp = timestamp;
this.info = info; this.info = info;
} }
public Date getTimestamp() { public Instant getTimestamp() {
return this.timestamp; return this.timestamp;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,8 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.time.OffsetDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
@ -44,18 +44,16 @@ public class AuditEventsEndpointTests {
public void eventsWithType() { public void eventsWithType() {
given(this.repository.find(null, null, "type")) given(this.repository.find(null, null, "type"))
.willReturn(Collections.singletonList(this.event)); .willReturn(Collections.singletonList(this.event));
List<AuditEvent> result = this.endpoint List<AuditEvent> result = this.endpoint.events(null, null, "type").getEvents();
.eventsWithPrincipalDateAfterAndType(null, null, "type").getEvents();
assertThat(result).isEqualTo(Collections.singletonList(this.event)); assertThat(result).isEqualTo(Collections.singletonList(this.event));
} }
@Test @Test
public void eventsWithDateAfter() { public void eventsCreatedAfter() {
Date date = new Date(); OffsetDateTime now = OffsetDateTime.now();
given(this.repository.find(null, date, null)) given(this.repository.find(null, now.toInstant(), null))
.willReturn(Collections.singletonList(this.event)); .willReturn(Collections.singletonList(this.event));
List<AuditEvent> result = this.endpoint List<AuditEvent> result = this.endpoint.events(null, now, null).getEvents();
.eventsWithPrincipalDateAfterAndType(null, date, null).getEvents();
assertThat(result).isEqualTo(Collections.singletonList(this.event)); assertThat(result).isEqualTo(Collections.singletonList(this.event));
} }
@ -63,8 +61,7 @@ public class AuditEventsEndpointTests {
public void eventsWithPrincipal() { public void eventsWithPrincipal() {
given(this.repository.find("Joan", null, null)) given(this.repository.find("Joan", null, null))
.willReturn(Collections.singletonList(this.event)); .willReturn(Collections.singletonList(this.event));
List<AuditEvent> result = this.endpoint List<AuditEvent> result = this.endpoint.events("Joan", null, null).getEvents();
.eventsWithPrincipalDateAfterAndType("Joan", null, null).getEvents();
assertThat(result).isEqualTo(Collections.singletonList(this.event)); assertThat(result).isEqualTo(Collections.singletonList(this.event));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.boot.actuate.audit;
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import net.minidev.json.JSONArray; import net.minidev.json.JSONArray;
import org.junit.Test; import org.junit.Test;
@ -105,7 +104,7 @@ public class AuditEventsEndpointWebIntegrationTests {
} }
private AuditEvent createEvent(String instant, String principal, String type) { private AuditEvent createEvent(String instant, String principal, String type) {
return new AuditEvent(Date.from(Instant.parse(instant)), principal, type, return new AuditEvent(Instant.parse(instant), principal, type,
Collections.emptyMap()); Collections.emptyMap());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,8 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.time.OffsetDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
@ -42,21 +42,21 @@ public class AuditEventsJmxEndpointExtensionTests {
Collections.singletonMap("a", "alpha")); Collections.singletonMap("a", "alpha"));
@Test @Test
public void eventsWithDateAfter() { public void eventsCreatedAfter() {
Date date = new Date(); OffsetDateTime now = OffsetDateTime.now();
given(this.repository.find(null, date, null)) given(this.repository.find(null, now.toInstant(), null))
.willReturn(Collections.singletonList(this.event)); .willReturn(Collections.singletonList(this.event));
List<AuditEvent> result = this.extension.eventsWithDateAfter(date).getEvents(); List<AuditEvent> result = this.extension.eventsAfter(now).getEvents();
assertThat(result).isEqualTo(Collections.singletonList(this.event)); assertThat(result).isEqualTo(Collections.singletonList(this.event));
} }
@Test @Test
public void eventsWithPrincipalAndDateAfter() { public void eventsWithPrincipalAndDateAfter() {
Date date = new Date(); OffsetDateTime now = OffsetDateTime.now();
given(this.repository.find("Joan", date, null)) given(this.repository.find("Joan", now.toInstant(), null))
.willReturn(Collections.singletonList(this.event)); .willReturn(Collections.singletonList(this.event));
List<AuditEvent> result = this.extension List<AuditEvent> result = this.extension.eventsWithPrincipalAndAfter("Joan", now)
.eventsWithPrincipalAndDateAfter("Joan", date).getEvents(); .getEvents();
assertThat(result).isEqualTo(Collections.singletonList(this.event)); assertThat(result).isEqualTo(Collections.singletonList(this.event));
} }

View File

@ -16,8 +16,8 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Calendar; import java.time.Instant;
import java.util.Date; import java.time.temporal.ChronoUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -99,20 +99,17 @@ public class InMemoryAuditEventRepositoryTests {
@Test @Test
public void findByDate() { public void findByDate() {
Calendar calendar = Calendar.getInstance(); Instant instant = Instant.now();
calendar.set(2000, 1, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository(); InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
repository.add(new AuditEvent(calendar.getTime(), "dave", "a", data)); repository.add(new AuditEvent(instant, "dave", "a", data));
calendar.add(Calendar.DAY_OF_YEAR, 1); repository
repository.add(new AuditEvent(calendar.getTime(), "phil", "b", data)); .add(new AuditEvent(instant.plus(1, ChronoUnit.DAYS), "phil", "b", data));
calendar.add(Calendar.DAY_OF_YEAR, 1); repository
Date after = calendar.getTime(); .add(new AuditEvent(instant.plus(2, ChronoUnit.DAYS), "dave", "c", data));
repository.add(new AuditEvent(calendar.getTime(), "dave", "c", data)); repository
calendar.add(Calendar.DAY_OF_YEAR, 1); .add(new AuditEvent(instant.plus(3, ChronoUnit.DAYS), "phil", "d", data));
repository.add(new AuditEvent(calendar.getTime(), "phil", "d", data)); Instant after = instant.plus(1, ChronoUnit.DAYS);
calendar.add(Calendar.DAY_OF_YEAR, 1);
List<AuditEvent> events = repository.find(null, after, null); List<AuditEvent> events = repository.find(null, after, null);
assertThat(events.size()).isEqualTo(2); assertThat(events.size()).isEqualTo(2);
assertThat(events.get(0).getType()).isEqualTo("c"); assertThat(events.get(0).getType()).isEqualTo("c");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.endpoint.convert; package org.springframework.boot.actuate.endpoint.convert;
import java.util.Date; import java.time.OffsetDateTime;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -77,7 +77,8 @@ public class ConversionServiceParameterMapperTests {
@Test @Test
public void createShouldRegisterIsoOffsetDateTimeConverter() { public void createShouldRegisterIsoOffsetDateTimeConverter() {
ConversionServiceParameterMapper mapper = new ConversionServiceParameterMapper(); ConversionServiceParameterMapper mapper = new ConversionServiceParameterMapper();
Date mapped = mapper.mapParameter("2011-12-03T10:15:30+01:00", Date.class); OffsetDateTime mapped = mapper.mapParameter("2011-12-03T10:15:30+01:00",
OffsetDateTime.class);
assertThat(mapped).isNotNull(); assertThat(mapped).isNotNull();
} }
@ -87,7 +88,7 @@ public class ConversionServiceParameterMapperTests {
ConversionServiceParameterMapper mapper = new ConversionServiceParameterMapper( ConversionServiceParameterMapper mapper = new ConversionServiceParameterMapper(
conversionService); conversionService);
this.thrown.expect(ParameterMappingException.class); this.thrown.expect(ParameterMappingException.class);
mapper.mapParameter("2011-12-03T10:15:30+01:00", Date.class); mapper.mapParameter("2011-12-03T10:15:30+01:00", OffsetDateTime.class);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.endpoint.convert; package org.springframework.boot.actuate.endpoint.convert;
import java.util.Date; import java.time.OffsetDateTime;
import org.junit.Test; import org.junit.Test;
@ -34,16 +34,17 @@ public class IsoOffsetDateTimeConverterTests {
@Test @Test
public void convertShouldConvertIsoDate() { public void convertShouldConvertIsoDate() {
IsoOffsetDateTimeConverter converter = new IsoOffsetDateTimeConverter(); IsoOffsetDateTimeConverter converter = new IsoOffsetDateTimeConverter();
Date date = converter.convert("2011-12-03T10:15:30+01:00"); OffsetDateTime time = converter.convert("2011-12-03T10:15:30+01:00");
assertThat(date).isNotNull(); assertThat(time).isNotNull();
} }
@Test @Test
public void registerConverterShouldRegister() { public void registerConverterShouldRegister() {
DefaultConversionService service = new DefaultConversionService(); DefaultConversionService service = new DefaultConversionService();
IsoOffsetDateTimeConverter.registerConverter(service); IsoOffsetDateTimeConverter.registerConverter(service);
Date date = service.convert("2011-12-03T10:15:30+01:00", Date.class); OffsetDateTime time = service.convert("2011-12-03T10:15:30+01:00",
assertThat(date).isNotNull(); OffsetDateTime.class);
assertThat(time).isNotNull();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.actuate.info; package org.springframework.boot.actuate.info;
import java.util.Date; import java.time.Instant;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -45,8 +45,8 @@ public class GitInfoContributorTests {
assertThat(content.get("commit")).isInstanceOf(Map.class); assertThat(content.get("commit")).isInstanceOf(Map.class);
Map<String, Object> commit = (Map<String, Object>) content.get("commit"); Map<String, Object> commit = (Map<String, Object>) content.get("commit");
Object commitTime = commit.get("time"); Object commitTime = commit.get("time");
assertThat(commitTime).isInstanceOf(Date.class); assertThat(commitTime).isInstanceOf(Instant.class);
assertThat(((Date) commitTime).getTime()).isEqualTo(1457098593000L); assertThat(((Instant) commitTime).toEpochMilli()).isEqualTo(1457098593000L);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,7 +79,7 @@ public class ProjectInfoAutoConfigurationTests {
assertThat(buildProperties.getArtifact()).isEqualTo("demo"); assertThat(buildProperties.getArtifact()).isEqualTo("demo");
assertThat(buildProperties.getName()).isEqualTo("Demo Project"); assertThat(buildProperties.getName()).isEqualTo("Demo Project");
assertThat(buildProperties.getVersion()).isEqualTo("0.0.1-SNAPSHOT"); assertThat(buildProperties.getVersion()).isEqualTo("0.0.1-SNAPSHOT");
assertThat(buildProperties.getTime().getTime()).isEqualTo(1457100965000L); assertThat(buildProperties.getTime().toEpochMilli()).isEqualTo(1457100965000L);
} }
@Test @Test
@ -90,7 +90,7 @@ public class ProjectInfoAutoConfigurationTests {
assertThat(buildProperties.getArtifact()).isEqualTo("acme"); assertThat(buildProperties.getArtifact()).isEqualTo("acme");
assertThat(buildProperties.getName()).isEqualTo("acme"); assertThat(buildProperties.getName()).isEqualTo("acme");
assertThat(buildProperties.getVersion()).isEqualTo("1.0.1-SNAPSHOT"); assertThat(buildProperties.getVersion()).isEqualTo("1.0.1-SNAPSHOT");
assertThat(buildProperties.getTime().getTime()).isEqualTo(1457088120000L); assertThat(buildProperties.getTime().toEpochMilli()).isEqualTo(1457088120000L);
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,7 @@ package org.springframework.boot.info;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.time.Instant;
import java.util.Properties; import java.util.Properties;
/** /**
@ -77,8 +77,8 @@ public class BuildProperties extends InfoProperties {
* @return the build time * @return the build time
* @see #get(String) * @see #get(String)
*/ */
public Date getTime() { public Instant getTime() {
return getDate("time"); return getInstant("time");
} }
private static Properties processEntries(Properties properties) { private static Properties processEntries(Properties properties) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,7 @@ package org.springframework.boot.info;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.time.Instant;
import java.util.Properties; import java.util.Properties;
/** /**
@ -73,8 +73,8 @@ public class GitProperties extends InfoProperties {
* @return the commit time * @return the commit time
* @see #get(String) * @see #get(String)
*/ */
public Date getCommitTime() { public Instant getCommitTime() {
return getDate("commit.time"); return getInstant("commit.time");
} }
private static Properties processEntries(Properties properties) { private static Properties processEntries(Properties properties) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
package org.springframework.boot.info; package org.springframework.boot.info;
import java.util.Date; import java.time.Instant;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -55,16 +55,16 @@ public class InfoProperties implements Iterable<InfoProperties.Entry> {
} }
/** /**
* Return the value of the specified property as a {@link Date} or {@code null} if the * Return the value of the specified property as an {@link Instant} or {@code null} if
* value is not a valid {@link Long} representation of an epoch time. * the value is not a valid {@link Long} representation of an epoch time.
* @param key the key of the property * @param key the key of the property
* @return the property value * @return the property value
*/ */
public Date getDate(String key) { public Instant getInstant(String key) {
String s = get(key); String s = get(key);
if (s != null) { if (s != null) {
try { try {
return new Date(Long.parseLong(s)); return Instant.ofEpochMilli(Long.parseLong(s));
} }
catch (NumberFormatException ex) { catch (NumberFormatException ex) {
// Not valid epoch time // Not valid epoch time

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,7 +38,7 @@ public class BuildPropertiesTests {
assertThat(properties.getVersion()).isEqualTo("0.0.1"); assertThat(properties.getVersion()).isEqualTo("0.0.1");
assertThat(properties.getTime()).isNotNull(); assertThat(properties.getTime()).isNotNull();
assertThat(properties.get("time")).isEqualTo("1457098593000"); assertThat(properties.get("time")).isEqualTo("1457098593000");
assertThat(properties.getTime().getTime()).isEqualTo(1457098593000L); assertThat(properties.getTime().toEpochMilli()).isEqualTo(1457098593000L);
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -53,7 +53,7 @@ public class GitPropertiesTests {
createProperties("master", "abcdefg", null, "1457527123")); createProperties("master", "abcdefg", null, "1457527123"));
assertThat(properties.getCommitTime()).isNotNull(); assertThat(properties.getCommitTime()).isNotNull();
assertThat(properties.get("commit.time")).isEqualTo("1457527123000"); assertThat(properties.get("commit.time")).isEqualTo("1457527123000");
assertThat(properties.getCommitTime().getTime()).isEqualTo(1457527123000L); assertThat(properties.getCommitTime().toEpochMilli()).isEqualTo(1457527123000L);
} }
@Test @Test
@ -62,7 +62,7 @@ public class GitPropertiesTests {
createProperties("master", "abcdefg", null, "2016-03-04T14:36:33+0100")); createProperties("master", "abcdefg", null, "2016-03-04T14:36:33+0100"));
assertThat(properties.getCommitTime()).isNotNull(); assertThat(properties.getCommitTime()).isNotNull();
assertThat(properties.get("commit.time")).isEqualTo("1457098593000"); assertThat(properties.get("commit.time")).isEqualTo("1457098593000");
assertThat(properties.getCommitTime().getTime()).isEqualTo(1457098593000L); assertThat(properties.getCommitTime().toEpochMilli()).isEqualTo(1457098593000L);
} }
@Test @Test