mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Use "instanceof pattern matching" in WebSocketExtension.equals()
This commit is contained in:
+2
-5
@@ -104,11 +104,8 @@ public class WebSocketExtension {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || !(other instanceof WebSocketExtension)) {
|
||||
return false;
|
||||
}
|
||||
WebSocketExtension otherExt = (WebSocketExtension) other;
|
||||
return (this.name.equals(otherExt.name) && this.parameters.equals(otherExt.parameters));
|
||||
return (other instanceof WebSocketExtension that &&
|
||||
this.name.equals(that.name) && this.parameters.equals(that.parameters));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+97
@@ -17,7 +17,9 @@
|
||||
package org.springframework.web.socket;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -27,6 +29,7 @@ import static org.assertj.core.api.Assertions.entry;
|
||||
* Test fixture for {@link WebSocketExtension}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
class WebSocketExtensionTests {
|
||||
|
||||
@@ -51,4 +54,98 @@ class WebSocketExtensionTests {
|
||||
.containsExactly("x-foo-extension", "x-bar-extension");
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class EqualsTests {
|
||||
|
||||
@Test
|
||||
void equalToSelf() {
|
||||
WebSocketExtension extension = new WebSocketExtension("x-test");
|
||||
assertThat(extension).isEqualTo(extension);
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualToNull() {
|
||||
WebSocketExtension extension = new WebSocketExtension("x-test");
|
||||
assertThat(extension).isNotEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualToDifferentType() {
|
||||
WebSocketExtension extension = new WebSocketExtension("x-test");
|
||||
assertThat(extension).isNotEqualTo("x-test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalWithSameName() {
|
||||
assertThat(new WebSocketExtension("x-test"))
|
||||
.isEqualTo(new WebSocketExtension("x-test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalWithSameNameAndParameter() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")))
|
||||
.isEqualTo(new WebSocketExtension("x-test", Map.of("foo", "bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalWithSameNameAndMultipleParameters() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "1", "bar", "2")))
|
||||
.isEqualTo(new WebSocketExtension("x-test", Map.of("foo", "1", "bar", "2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualWithDifferentName() {
|
||||
assertThat(new WebSocketExtension("x-foo"))
|
||||
.isNotEqualTo(new WebSocketExtension("x-bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualWithDifferentNameCase() {
|
||||
assertThat(new WebSocketExtension("x-test"))
|
||||
.isNotEqualTo(new WebSocketExtension("X-TEST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualWithDifferentParameterValue() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")))
|
||||
.isNotEqualTo(new WebSocketExtension("x-test", Map.of("foo", "baz")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualWithDifferentParameterKey() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")))
|
||||
.isNotEqualTo(new WebSocketExtension("x-test", Map.of("baz", "bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notEqualWithDifferentParameterCount() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")))
|
||||
.isNotEqualTo(new WebSocketExtension("x-test", Map.of("foo", "bar", "baz", "qux")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class HashCodeTests {
|
||||
|
||||
@Test
|
||||
void sameHashCodeForEqualExtensions() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")).hashCode())
|
||||
.isEqualTo(new WebSocketExtension("x-test", Map.of("foo", "bar")).hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void differentHashCodeForDifferentNames() {
|
||||
assertThat(new WebSocketExtension("x-foo").hashCode())
|
||||
.isNotEqualTo(new WebSocketExtension("x-bar").hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void differentHashCodeForDifferentParameterValues() {
|
||||
assertThat(new WebSocketExtension("x-test", Map.of("foo", "bar")).hashCode())
|
||||
.isNotEqualTo(new WebSocketExtension("x-test", Map.of("foo", "baz")).hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user