StringUtilsTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.gmail.nossr50.util.text;
  2. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  3. import org.bukkit.Material;
  4. import org.bukkit.entity.EntityType;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Test;
  7. import static org.junit.jupiter.api.Assertions.*;
  8. class StringUtilsTest {
  9. @BeforeEach
  10. void setUp() {
  11. // Clear caches before each test to ensure test isolation
  12. clearCaches();
  13. }
  14. /**
  15. * Utility method to clear all caches in StringUtils.
  16. * Reflection is used since the caches are private.
  17. */
  18. private void clearCaches() {
  19. try {
  20. java.lang.reflect.Field entityCache = StringUtils.class.getDeclaredField("formattedEntityStrings");
  21. entityCache.setAccessible(true);
  22. ((java.util.Map<?, ?>) entityCache.get(null)).clear();
  23. java.lang.reflect.Field superAbilityCache = StringUtils.class.getDeclaredField("formattedSuperAbilityStrings");
  24. superAbilityCache.setAccessible(true);
  25. ((java.util.Map<?, ?>) superAbilityCache.get(null)).clear();
  26. java.lang.reflect.Field materialCache = StringUtils.class.getDeclaredField("formattedMaterialStrings");
  27. materialCache.setAccessible(true);
  28. ((java.util.Map<?, ?>) materialCache.get(null)).clear();
  29. } catch (NoSuchFieldException | IllegalAccessException e) {
  30. fail("Failed to clear caches: " + e.getMessage());
  31. }
  32. }
  33. // Tests for getCapitalized(String target)
  34. @Test
  35. void testGetCapitalized_NullInput() {
  36. assertNull(StringUtils.getCapitalized(null));
  37. }
  38. @Test
  39. void testGetCapitalized_EmptyString() {
  40. assertEquals("", StringUtils.getCapitalized(""));
  41. }
  42. @Test
  43. void testGetCapitalized_SingleCharacter() {
  44. assertEquals("A", StringUtils.getCapitalized("a"));
  45. assertEquals("Z", StringUtils.getCapitalized("Z"));
  46. }
  47. @Test
  48. void testGetCapitalized_AllUppercase() {
  49. assertEquals("Test", StringUtils.getCapitalized("TEST"));
  50. }
  51. @Test
  52. void testGetCapitalized_AllLowercase() {
  53. assertEquals("Test", StringUtils.getCapitalized("test"));
  54. }
  55. @Test
  56. void testGetCapitalized_MixedCase() {
  57. assertEquals("Test", StringUtils.getCapitalized("tEsT"));
  58. }
  59. @Test
  60. void testGetCapitalized_NonASCII() {
  61. assertEquals("Ñandú", StringUtils.getCapitalized("ñandú"));
  62. }
  63. // Tests for ticksToSeconds(double ticks)
  64. @Test
  65. void testTicksToSeconds_PositiveTicks() {
  66. assertEquals("1.5", StringUtils.ticksToSeconds(30));
  67. }
  68. @Test
  69. void testTicksToSeconds_ZeroTicks() {
  70. assertEquals("0.0", StringUtils.ticksToSeconds(0));
  71. }
  72. @Test
  73. void testTicksToSeconds_FractionalTicks() {
  74. assertEquals("1.5", StringUtils.ticksToSeconds(30.0));
  75. assertEquals("1.5", StringUtils.ticksToSeconds(30.0));
  76. assertEquals("1.0", StringUtils.ticksToSeconds(20.0));
  77. assertEquals("0.1", StringUtils.ticksToSeconds(2.0));
  78. }
  79. @Test
  80. void testTicksToSeconds_NegativeTicks() {
  81. assertEquals("-1.0", StringUtils.ticksToSeconds(-20));
  82. }
  83. // Tests for getPrettySuperAbilityString(SuperAbilityType superAbilityType)
  84. @Test
  85. void testGetPrettySuperAbilityString_NullInput() {
  86. assertThrows(NullPointerException.class, () -> {
  87. StringUtils.getPrettySuperAbilityString(null);
  88. });
  89. }
  90. @Test
  91. void testGetPrettySuperAbilityString_ValidInput() {
  92. SuperAbilityType superAbilityType = SuperAbilityType.SUPER_BREAKER;
  93. String expected = "Super Breaker";
  94. String actual = StringUtils.getPrettySuperAbilityString(superAbilityType);
  95. assertEquals(expected, actual);
  96. }
  97. @Test
  98. void testGetPrettySuperAbilityString_Caching() {
  99. SuperAbilityType superAbilityType = SuperAbilityType.SUPER_BREAKER;
  100. // First call should compute and cache
  101. String firstCall = StringUtils.getPrettySuperAbilityString(superAbilityType);
  102. // Second call should retrieve from cache
  103. String secondCall = StringUtils.getPrettySuperAbilityString(superAbilityType);
  104. assertSame(firstCall, secondCall, "Cached value should be the same instance");
  105. }
  106. // Tests for getPrettyEntityTypeString(EntityType entityType)
  107. @Test
  108. void testGetPrettyEntityTypeString_ValidInput() {
  109. EntityType zombie = EntityType.ZOMBIE;
  110. String expected = "Zombie";
  111. String actual = StringUtils.getPrettyEntityTypeString(zombie);
  112. assertEquals(expected, actual);
  113. }
  114. @Test
  115. void testGetPrettyEntityTypeString_WithUnderscores() {
  116. EntityType entity = EntityType.SKELETON_HORSE;
  117. String expected = "Skeleton Horse";
  118. String actual = StringUtils.getPrettyEntityTypeString(entity);
  119. assertEquals(expected, actual);
  120. }
  121. @Test
  122. void testGetPrettyEntityTypeString_Caching() {
  123. EntityType skeleton = EntityType.SKELETON;
  124. // First call should compute and cache
  125. String firstCall = StringUtils.getPrettyEntityTypeString(skeleton);
  126. // Second call should retrieve from cache
  127. String secondCall = StringUtils.getPrettyEntityTypeString(skeleton);
  128. assertSame(firstCall, secondCall, "Cached value should be the same instance");
  129. }
  130. // Tests for getFormattedMaterialString(Material material)
  131. @Test
  132. void testGetPrettyMaterialString_ValidInput() {
  133. Material diamondSword = Material.DIAMOND_SWORD;
  134. String expected = "Diamond Sword";
  135. String actual = StringUtils.getPrettyMaterialString(diamondSword);
  136. assertEquals(expected, actual);
  137. }
  138. @Test
  139. void testGetPrettyMaterialString_WithUnderscores() {
  140. Material goldenApple = Material.GOLDEN_APPLE;
  141. String expected = "Golden Apple";
  142. String actual = StringUtils.getPrettyMaterialString(goldenApple);
  143. assertEquals(expected, actual);
  144. }
  145. @Test
  146. void testGetPrettyMaterialString_Caching() {
  147. Material ironPickaxe = Material.IRON_PICKAXE;
  148. // First call should compute and cache
  149. String firstCall = StringUtils.getPrettyMaterialString(ironPickaxe);
  150. // Second call should retrieve from cache
  151. String secondCall = StringUtils.getPrettyMaterialString(ironPickaxe);
  152. assertSame(firstCall, secondCall, "Cached value should be the same instance");
  153. }
  154. // Tests for buildStringAfterNthElement(String[] args, int index)
  155. @Test
  156. void testBuildStringAfterNthElement_IndexZero() {
  157. String[] args = {"Hello", "World", "Test"};
  158. String expected = "Hello World Test";
  159. String actual = StringUtils.buildStringAfterNthElement(args, 0);
  160. assertEquals(expected, actual);
  161. }
  162. @Test
  163. void testBuildStringAfterNthElement_IndexMiddle() {
  164. String[] args = {"This", "is", "a", "test"};
  165. String expected = "a test";
  166. String actual = StringUtils.buildStringAfterNthElement(args, 2);
  167. assertEquals(expected, actual);
  168. }
  169. @Test
  170. void testBuildStringAfterNthElement_IndexLast() {
  171. String[] args = {"Only", "One"};
  172. String expected = "One";
  173. String actual = StringUtils.buildStringAfterNthElement(args, 1);
  174. assertEquals(expected, actual);
  175. }
  176. @Test
  177. void testBuildStringAfterNthElement_IndexOutOfBounds() {
  178. String[] args = {"Too", "Short"};
  179. String expected = "";
  180. String actual = StringUtils.buildStringAfterNthElement(args, 5);
  181. assertEquals(expected, actual);
  182. }
  183. @Test
  184. void testBuildStringAfterNthElement_EmptyArray() {
  185. String[] args = {};
  186. String expected = "";
  187. String actual = StringUtils.buildStringAfterNthElement(args, 0);
  188. assertEquals(expected, actual);
  189. }
  190. @Test
  191. void testBuildStringAfterNthElement_ArgsWithSpaces() {
  192. String[] args = {"Multiple", " ", "Spaces"};
  193. String expected = " Spaces";
  194. String actual = StringUtils.buildStringAfterNthElement(args, 1);
  195. assertEquals(expected, actual);
  196. }
  197. // Tests for isInt(String string)
  198. @Test
  199. void testIsInt_ValidIntegers() {
  200. assertTrue(StringUtils.isInt("123"));
  201. assertTrue(StringUtils.isInt("-456"));
  202. assertTrue(StringUtils.isInt("0"));
  203. }
  204. @Test
  205. void testIsInt_InvalidIntegers() {
  206. assertFalse(StringUtils.isInt("123.45"));
  207. assertFalse(StringUtils.isInt("abc"));
  208. assertFalse(StringUtils.isInt(""));
  209. assertFalse(StringUtils.isInt(" "));
  210. assertFalse(StringUtils.isInt(null)); // This will throw NullPointerException
  211. }
  212. // Tests for isDouble(String string)
  213. @Test
  214. void testIsDouble_ValidDoubles() {
  215. assertTrue(StringUtils.isDouble("123.45"));
  216. assertTrue(StringUtils.isDouble("-456.78"));
  217. assertTrue(StringUtils.isDouble("0.0"));
  218. assertTrue(StringUtils.isDouble("1e10"));
  219. }
  220. @Test
  221. void testIsDouble_InvalidDoubles() {
  222. assertFalse(StringUtils.isDouble("abc"));
  223. assertFalse(StringUtils.isDouble(""));
  224. assertFalse(StringUtils.isDouble(" "));
  225. assertFalse(StringUtils.isDouble("123.45.67"));
  226. }
  227. @Test
  228. void testIsDouble_NullInput() {
  229. assertThrows(NullPointerException.class, () -> {
  230. StringUtils.isDouble(null);
  231. });
  232. }
  233. @Test
  234. void testCachingMechanism_EntityType() {
  235. EntityType zombie = EntityType.ZOMBIE;
  236. String firstCall = StringUtils.getPrettyEntityTypeString(zombie);
  237. String secondCall = StringUtils.getPrettyEntityTypeString(zombie);
  238. assertSame(firstCall, secondCall, "EntityType caching failed");
  239. }
  240. @Test
  241. void testCachingMechanism_Material() {
  242. Material diamondSword = Material.DIAMOND_SWORD;
  243. String firstCall = StringUtils.getPrettyMaterialString(diamondSword);
  244. String secondCall = StringUtils.getPrettyMaterialString(diamondSword);
  245. assertSame(firstCall, secondCall, "Material caching failed");
  246. }
  247. // Tests for createPrettyString via public methods
  248. @Test
  249. void testCreatePrettyString_Spaces() {
  250. String[] args = {"hello", "world"};
  251. String expected = "hello world";
  252. String actual = StringUtils.buildStringAfterNthElement(args, 0);
  253. assertEquals(expected, actual);
  254. }
  255. @Test
  256. void testPrettify_Substrings() {
  257. Material goldenApple = Material.GOLDEN_APPLE;
  258. String expected = "Golden Apple";
  259. String actual = StringUtils.getPrettyMaterialString(goldenApple);
  260. assertEquals(expected, actual);
  261. }
  262. }