feat(tests): Add unit tests for Assert and Utils classes (#70)

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
codeboyzhou 2025-03-25 14:57:43 +08:00 • committed by Christian Tzolov
commit e401e9e848
2 changed files with 86 additions and 0 deletions

View file

@ -0,0 +1,46 @@
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.util;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class AssertTests {
@Test
void testCollectionNotEmpty() {
IllegalArgumentException e1 = assertThrows(IllegalArgumentException.class,
() -> Assert.notEmpty(null, "collection is null"));
assertEquals("collection is null", e1.getMessage());
IllegalArgumentException e2 = assertThrows(IllegalArgumentException.class,
() -> Assert.notEmpty(List.of(), "collection is empty"));
assertEquals("collection is empty", e2.getMessage());
assertDoesNotThrow(() -> Assert.notEmpty(List.of("test"), "collection is not empty"));
}
@Test
void testObjectNotNull() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> Assert.notNull(null, "object is null"));
assertEquals("object is null", e.getMessage());
assertDoesNotThrow(() -> Assert.notNull("test", "object is not null"));
}
@Test
void testStringHasText() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> Assert.hasText(null, "string is null"));
assertEquals("string is null", e.getMessage());
assertDoesNotThrow(() -> Assert.hasText("test", "string is not empty"));
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.util;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class UtilsTests {
@Test
void testHasText() {
assertFalse(Utils.hasText(null));
assertFalse(Utils.hasText(""));
assertFalse(Utils.hasText(" "));
assertTrue(Utils.hasText("test"));
}
@Test
void testCollectionIsEmpty() {
assertTrue(Utils.isEmpty((Collection<?>) null));
assertTrue(Utils.isEmpty(List.of()));
assertFalse(Utils.isEmpty(List.of("test")));
}
@Test
void testMapIsEmpty() {
assertTrue(Utils.isEmpty((Map<?, ?>) null));
assertTrue(Utils.isEmpty(Map.of()));
assertFalse(Utils.isEmpty(Map.of("key", "value")));
}
}