feat: implement identifier-based equals/hashCode for PromptReference (#514)
Ensure PromptReference equality is based solely on identifier and type fields, ignoring other fields like title Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
parent
a96da37140
commit
6eb8a26796
2 changed files with 103 additions and 0 deletions
|
|
@ -2351,6 +2351,22 @@ public final class McpSchema {
|
||||||
public String identifier() {
|
public String identifier() {
|
||||||
return name();
|
return name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
PromptReference that = (PromptReference) obj;
|
||||||
|
return java.util.Objects.equals(identifier(), that.identifier())
|
||||||
|
&& java.util.Objects.equals(type(), that.type());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return java.util.Objects.hash(identifier(), type());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 - 2025 the original author or authors.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.modelcontextprotocol.spec;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class to verify the equals method implementation for PromptReference.
|
||||||
|
*/
|
||||||
|
class PromptReferenceEqualsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithSameIdentifierAndType() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title");
|
||||||
|
|
||||||
|
assertTrue(ref1.equals(ref2), "PromptReferences with same identifier and type should be equal");
|
||||||
|
assertEquals(ref1.hashCode(), ref2.hashCode(), "Equal objects should have same hash code");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithDifferentIdentifier() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt-1", "Test Title");
|
||||||
|
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt-2", "Test Title");
|
||||||
|
|
||||||
|
assertFalse(ref1.equals(ref2), "PromptReferences with different identifiers should not be equal");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithDifferentType() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/other", "test-prompt", "Test Title");
|
||||||
|
|
||||||
|
assertFalse(ref1.equals(ref2), "PromptReferences with different types should not be equal");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithNull() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
|
||||||
|
assertFalse(ref1.equals(null), "PromptReference should not be equal to null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithDifferentClass() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
String other = "not a PromptReference";
|
||||||
|
|
||||||
|
assertFalse(ref1.equals(other), "PromptReference should not be equal to different class");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsWithSameInstance() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
|
||||||
|
assertTrue(ref1.equals(ref1), "PromptReference should be equal to itself");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEqualsIgnoresTitle() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 1");
|
||||||
|
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 2");
|
||||||
|
McpSchema.PromptReference ref3 = new McpSchema.PromptReference("ref/prompt", "test-prompt", null);
|
||||||
|
|
||||||
|
assertTrue(ref1.equals(ref2), "PromptReferences should be equal regardless of title");
|
||||||
|
assertTrue(ref1.equals(ref3), "PromptReferences should be equal even when one has null title");
|
||||||
|
assertTrue(ref2.equals(ref3), "PromptReferences should be equal even when one has null title");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHashCodeConsistency() {
|
||||||
|
McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title");
|
||||||
|
McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title");
|
||||||
|
|
||||||
|
assertEquals(ref1.hashCode(), ref2.hashCode(), "Objects that are equal should have the same hash code");
|
||||||
|
|
||||||
|
// Call hashCode multiple times to ensure consistency
|
||||||
|
int hashCode1 = ref1.hashCode();
|
||||||
|
int hashCode2 = ref1.hashCode();
|
||||||
|
assertEquals(hashCode1, hashCode2, "Hash code should be consistent across multiple calls");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue