|
| 1 | +/* |
| 2 | +* Copyright 2025 - 2025 the original author or authors. |
| 3 | +*/ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.spec; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test class to verify the equals method implementation for PromptReference. |
| 13 | + */ |
| 14 | +class PromptReferenceEqualsTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + void testEqualsWithSameIdentifierAndType() { |
| 18 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 19 | + McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title"); |
| 20 | + |
| 21 | + assertTrue(ref1.equals(ref2), "PromptReferences with same identifier and type should be equal"); |
| 22 | + assertEquals(ref1.hashCode(), ref2.hashCode(), "Equal objects should have same hash code"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void testEqualsWithDifferentIdentifier() { |
| 27 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt-1", "Test Title"); |
| 28 | + McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt-2", "Test Title"); |
| 29 | + |
| 30 | + assertFalse(ref1.equals(ref2), "PromptReferences with different identifiers should not be equal"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testEqualsWithDifferentType() { |
| 35 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 36 | + McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/other", "test-prompt", "Test Title"); |
| 37 | + |
| 38 | + assertFalse(ref1.equals(ref2), "PromptReferences with different types should not be equal"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testEqualsWithNull() { |
| 43 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 44 | + |
| 45 | + assertFalse(ref1.equals(null), "PromptReference should not be equal to null"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testEqualsWithDifferentClass() { |
| 50 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 51 | + String other = "not a PromptReference"; |
| 52 | + |
| 53 | + assertFalse(ref1.equals(other), "PromptReference should not be equal to different class"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testEqualsWithSameInstance() { |
| 58 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 59 | + |
| 60 | + assertTrue(ref1.equals(ref1), "PromptReference should be equal to itself"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testEqualsIgnoresTitle() { |
| 65 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 1"); |
| 66 | + McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Title 2"); |
| 67 | + McpSchema.PromptReference ref3 = new McpSchema.PromptReference("ref/prompt", "test-prompt", null); |
| 68 | + |
| 69 | + assertTrue(ref1.equals(ref2), "PromptReferences should be equal regardless of title"); |
| 70 | + assertTrue(ref1.equals(ref3), "PromptReferences should be equal even when one has null title"); |
| 71 | + assertTrue(ref2.equals(ref3), "PromptReferences should be equal even when one has null title"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testHashCodeConsistency() { |
| 76 | + McpSchema.PromptReference ref1 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Test Title"); |
| 77 | + McpSchema.PromptReference ref2 = new McpSchema.PromptReference("ref/prompt", "test-prompt", "Different Title"); |
| 78 | + |
| 79 | + assertEquals(ref1.hashCode(), ref2.hashCode(), "Objects that are equal should have the same hash code"); |
| 80 | + |
| 81 | + // Call hashCode multiple times to ensure consistency |
| 82 | + int hashCode1 = ref1.hashCode(); |
| 83 | + int hashCode2 = ref1.hashCode(); |
| 84 | + assertEquals(hashCode1, hashCode2, "Hash code should be consistent across multiple calls"); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments