Add equals/hashCode methods to ServerSentEvent
Closes gh-33606
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.http.codec;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Representation for a Server-Sent Event for use with Spring's reactive Web support.
|
||||
@@ -102,6 +103,21 @@ public final class ServerSentEvent<T> {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof ServerSentEvent<?> that &&
|
||||
ObjectUtils.nullSafeEquals(this.id, that.id) &&
|
||||
ObjectUtils.nullSafeEquals(this.event, that.event) &&
|
||||
ObjectUtils.nullSafeEquals(this.retry, that.retry) &&
|
||||
ObjectUtils.nullSafeEquals(this.comment, that.comment) &&
|
||||
ObjectUtils.nullSafeEquals(this.data, that.data)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return ObjectUtils.nullSafeHash(this.id, this.event, this.retry, this.comment, this.data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ("ServerSentEvent [id = '" + this.id + "', event='" + this.event + "', retry=" +
|
||||
|
||||
@@ -74,20 +74,10 @@ class ServerSentEventHttpMessageReaderTests extends AbstractLeakCheckingTests {
|
||||
request, Collections.emptyMap()).cast(ServerSentEvent.class);
|
||||
|
||||
StepVerifier.create(events)
|
||||
.consumeNextWith(event -> {
|
||||
assertThat(event.id()).isEqualTo("c42");
|
||||
assertThat(event.event()).isEqualTo("foo");
|
||||
assertThat(event.retry()).isEqualTo(Duration.ofMillis(123));
|
||||
assertThat(event.comment()).isEqualTo("bla\nbla bla\nbla bla bla");
|
||||
assertThat(event.data()).isEqualTo("bar");
|
||||
})
|
||||
.consumeNextWith(event -> {
|
||||
assertThat(event.id()).isEqualTo("c43");
|
||||
assertThat(event.event()).isEqualTo("bar");
|
||||
assertThat(event.retry()).isEqualTo(Duration.ofMillis(456));
|
||||
assertThat(event.comment()).isNull();
|
||||
assertThat(event.data()).isEqualTo("baz");
|
||||
})
|
||||
.expectNext(ServerSentEvent.builder().id("c42").event("foo")
|
||||
.retry(Duration.ofMillis(123)).comment("bla\nbla bla\nbla bla bla").data("bar").build())
|
||||
.expectNext(ServerSentEvent.builder().id("c43").event("bar")
|
||||
.retry(Duration.ofMillis(456)).data("baz").build())
|
||||
.consumeNextWith(event -> assertThat(event.data()).isNull())
|
||||
.consumeNextWith(event -> assertThat(event.data()).isNull())
|
||||
.expectComplete()
|
||||
@@ -108,20 +98,10 @@ class ServerSentEventHttpMessageReaderTests extends AbstractLeakCheckingTests {
|
||||
request, Collections.emptyMap()).cast(ServerSentEvent.class);
|
||||
|
||||
StepVerifier.create(events)
|
||||
.consumeNextWith(event -> {
|
||||
assertThat(event.id()).isEqualTo("c42");
|
||||
assertThat(event.event()).isEqualTo("foo");
|
||||
assertThat(event.retry()).isEqualTo(Duration.ofMillis(123));
|
||||
assertThat(event.comment()).isEqualTo("bla\nbla bla\nbla bla bla");
|
||||
assertThat(event.data()).isEqualTo("bar");
|
||||
})
|
||||
.consumeNextWith(event -> {
|
||||
assertThat(event.id()).isEqualTo("c43");
|
||||
assertThat(event.event()).isEqualTo("bar");
|
||||
assertThat(event.retry()).isEqualTo(Duration.ofMillis(456));
|
||||
assertThat(event.comment()).isNull();
|
||||
assertThat(event.data()).isEqualTo("baz");
|
||||
})
|
||||
.expectNext(ServerSentEvent.builder().id("c42").event("foo")
|
||||
.retry(Duration.ofMillis(123)).comment("bla\nbla bla\nbla bla bla").data("bar").build())
|
||||
.expectNext(ServerSentEvent.builder().id("c43").event("bar")
|
||||
.retry(Duration.ofMillis(456)).data("baz").build())
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user