|
| 1 | +package world.bentobox.bentobox.managers.island; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | + |
| 17 | +import world.bentobox.bentobox.database.objects.Island; |
| 18 | + |
| 19 | +/** |
| 20 | + * Grid test |
| 21 | + */ |
| 22 | +@RunWith(PowerMockRunner.class) |
| 23 | +public class IslandGridTest { |
| 24 | + |
| 25 | + private IslandGrid ig; |
| 26 | + @Mock |
| 27 | + private IslandCache im; |
| 28 | + @Mock |
| 29 | + private Island island; |
| 30 | + @Mock |
| 31 | + private Island island2; |
| 32 | + @Mock |
| 33 | + private Island overlappingIsland; |
| 34 | + @Mock |
| 35 | + private Island original; |
| 36 | + @Mock |
| 37 | + private Island updated; |
| 38 | + @Mock |
| 39 | + private Island a; |
| 40 | + @Mock |
| 41 | + private Island b; |
| 42 | + @Mock |
| 43 | + private Island big; |
| 44 | + @Mock |
| 45 | + private Island small; |
| 46 | + @Mock |
| 47 | + private Island zIsland; |
| 48 | + |
| 49 | + /** |
| 50 | + * @throws java.lang.Exception |
| 51 | + */ |
| 52 | + @Before |
| 53 | + public void setUp() throws Exception { |
| 54 | + // Islands |
| 55 | + when(island.getMinX()).thenReturn(356); |
| 56 | + when(island.getMinZ()).thenReturn(5678); |
| 57 | + when(island.getRange()).thenReturn(64); |
| 58 | + when(island.getUniqueId()).thenReturn("island"); |
| 59 | + when(overlappingIsland.getMinX()).thenReturn(360); |
| 60 | + when(overlappingIsland.getMinZ()).thenReturn(5678); |
| 61 | + when(overlappingIsland.getRange()).thenReturn(64); |
| 62 | + when(overlappingIsland.getUniqueId()).thenReturn("overlappingIsland"); |
| 63 | + when(island2.getMinX()).thenReturn(-32); |
| 64 | + when(island2.getMinZ()).thenReturn(-32); |
| 65 | + when(island2.getRange()).thenReturn(64); |
| 66 | + when(island2.getUniqueId()).thenReturn("island2"); |
| 67 | + when(im.getIslandById("island")).thenReturn(island); |
| 68 | + when(im.getIslandById("island2")).thenReturn(island2); |
| 69 | + when(im.getIslandById("overlappingIsland")).thenReturn(overlappingIsland); |
| 70 | + ig = new IslandGrid(im); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @throws java.lang.Exception |
| 75 | + */ |
| 76 | + @After |
| 77 | + public void tearDown() throws Exception { |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#addToGrid(world.bentobox.bentobox.database.objects.Island)}. |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void testAddToGrid() { |
| 85 | + assertTrue(ig.addToGrid(island)); |
| 86 | + assertFalse(ig.addToGrid(overlappingIsland)); |
| 87 | + assertTrue(ig.addToGrid(island2)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#removeFromGrid(world.bentobox.bentobox.database.objects.Island)}. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + public void testRemoveFromGrid() { |
| 95 | + assertTrue(ig.addToGrid(island)); |
| 96 | + assertTrue(ig.removeFromGrid(island)); |
| 97 | + assertFalse(ig.removeFromGrid(island2)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#getIslandAt(int, int)}. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + public void testGetIslandAt() { |
| 105 | + assertNull(ig.getIslandAt(0, 0)); |
| 106 | + assertTrue(ig.addToGrid(island)); |
| 107 | + assertTrue(ig.addToGrid(island2)); |
| 108 | + assertEquals(island, ig.getIslandAt(360, 5700)); |
| 109 | + assertEquals(island2, ig.getIslandAt(0, 0)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#isIslandAt(int, int)}. |
| 114 | + */ |
| 115 | + @Test |
| 116 | + public void testIsIslandAt() { |
| 117 | + assertFalse(ig.isIslandAt(0, 0)); |
| 118 | + assertTrue(ig.addToGrid(island2)); |
| 119 | + assertTrue(ig.isIslandAt(0, 0)); |
| 120 | + assertTrue(ig.addToGrid(island)); |
| 121 | + assertTrue(ig.isIslandAt(360, 5700)); |
| 122 | + assertFalse(ig.isIslandAt(-1000, 1000)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#getIslandStringAt(int, int)}. |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void testGetIslandStringAt() { |
| 130 | + assertNull(ig.getIslandStringAt(0, 0)); |
| 131 | + assertTrue(ig.addToGrid(island2)); |
| 132 | + assertEquals("island2", ig.getIslandStringAt(0, 0)); |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#getSize()}. |
| 138 | + */ |
| 139 | + @Test |
| 140 | + public void testGetSize() { |
| 141 | + assertEquals(0, ig.getSize()); |
| 142 | + assertTrue(ig.addToGrid(island2)); |
| 143 | + assertEquals(1, ig.getSize()); |
| 144 | + assertTrue(ig.addToGrid(island)); |
| 145 | + assertEquals(2, ig.getSize()); |
| 146 | + ig.removeFromGrid(island); |
| 147 | + assertEquals(1, ig.getSize()); |
| 148 | + ig.removeFromGrid(island2); |
| 149 | + assertEquals(0, ig.getSize()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test method for {@link world.bentobox.bentobox.managers.island.IslandGrid#getGrid()}. |
| 154 | + */ |
| 155 | + @Test |
| 156 | + public void testGetGrid() { |
| 157 | + assertNotNull(ig.getGrid()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testUpdateIslandCoordinatesKeepsSingleEntry() { |
| 162 | + // original at (100,100) range 20 |
| 163 | + when(original.getMinX()).thenReturn(100); |
| 164 | + when(original.getMinZ()).thenReturn(100); |
| 165 | + when(original.getRange()).thenReturn(20); |
| 166 | + when(original.getUniqueId()).thenReturn("orig"); |
| 167 | + |
| 168 | + // updated has same id but moved to (300,300) range 20 |
| 169 | + when(updated.getMinX()).thenReturn(300); |
| 170 | + when(updated.getMinZ()).thenReturn(300); |
| 171 | + when(updated.getRange()).thenReturn(20); |
| 172 | + when(updated.getUniqueId()).thenReturn("orig"); |
| 173 | + |
| 174 | + when(im.getIslandById("orig")).thenReturn(original); |
| 175 | + |
| 176 | + // add original |
| 177 | + assertTrue(ig.addToGrid(original)); |
| 178 | + assertEquals(1, ig.getSize()); |
| 179 | + |
| 180 | + // add updated (same id) -> should update, keep size 1 |
| 181 | + assertTrue(ig.addToGrid(updated)); |
| 182 | + assertEquals(1, ig.getSize()); |
| 183 | + |
| 184 | + // original location should no longer contain the island |
| 185 | + assertNull(ig.getIslandStringAt(110, 110)); |
| 186 | + |
| 187 | + // new location should contain the island |
| 188 | + assertEquals("orig", ig.getIslandStringAt(310, 310)); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testAdjacentIslandsAllowedWhenEdgesTouch() { |
| 193 | + // island a covers x:[0,20) z:[0,20) |
| 194 | + when(a.getMinX()).thenReturn(0); |
| 195 | + when(a.getMinZ()).thenReturn(0); |
| 196 | + when(a.getRange()).thenReturn(10); |
| 197 | + when(a.getUniqueId()).thenReturn("a"); |
| 198 | + |
| 199 | + // island b starts exactly at x=20 (touching edge), z same |
| 200 | + when(b.getMinX()).thenReturn(20); |
| 201 | + when(b.getMinZ()).thenReturn(0); |
| 202 | + when(b.getRange()).thenReturn(10); |
| 203 | + when(b.getUniqueId()).thenReturn("b"); |
| 204 | + |
| 205 | + when(im.getIslandById("a")).thenReturn(a); |
| 206 | + when(im.getIslandById("b")).thenReturn(b); |
| 207 | + |
| 208 | + assertTrue(ig.addToGrid(a)); |
| 209 | + // touching edge should be allowed |
| 210 | + assertTrue(ig.addToGrid(b)); |
| 211 | + |
| 212 | + // verify both retrievable at representative coords |
| 213 | + assertEquals("a", ig.getIslandStringAt(10, 10)); |
| 214 | + assertEquals("b", ig.getIslandStringAt(21, 10)); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void testLargeExistingIslandShouldBlockSmallIslandEvenIfMinXOutsideSubMapWindow() { |
| 219 | + // big island minX = 0, range = 1000 |
| 220 | + when(big.getMinX()).thenReturn(0); |
| 221 | + when(big.getMinZ()).thenReturn(0); |
| 222 | + when(big.getRange()).thenReturn(1000); |
| 223 | + when(big.getUniqueId()).thenReturn("big"); |
| 224 | + |
| 225 | + // small island minX = 1500, range = 10 -> would overlap big |
| 226 | + when(small.getMinX()).thenReturn(1500); |
| 227 | + when(small.getMinZ()).thenReturn(10); |
| 228 | + when(small.getRange()).thenReturn(10); |
| 229 | + when(small.getUniqueId()).thenReturn("small"); |
| 230 | + |
| 231 | + when(im.getIslandById("big")).thenReturn(big); |
| 232 | + when(im.getIslandById("small")).thenReturn(small); |
| 233 | + |
| 234 | + assertTrue(ig.addToGrid(big)); |
| 235 | + |
| 236 | + // Expected: adding small should be rejected because it lies inside big |
| 237 | + // If this test fails, it reveals the current subMap window is too small to find big. |
| 238 | + assertFalse("Small island overlaps big island; should have been rejected", ig.addToGrid(small)); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void testGetIslandStringAtWhenXEntryExistsButNoZEntryApplies() { |
| 243 | + // island exists at minX=100 minZ=100 range=10 (covers z [110,110)) |
| 244 | + when(zIsland.getMinX()).thenReturn(100); |
| 245 | + when(zIsland.getMinZ()).thenReturn(100); |
| 246 | + when(zIsland.getRange()).thenReturn(10); |
| 247 | + when(zIsland.getUniqueId()).thenReturn("z"); |
| 248 | + |
| 249 | + when(im.getIslandById("z")).thenReturn(zIsland); |
| 250 | + |
| 251 | + assertTrue(ig.addToGrid(zIsland)); |
| 252 | + |
| 253 | + // Query an x within island x-range but z is below any minZ -> should return null |
| 254 | + assertNull(ig.getIslandStringAt(110, 50)); |
| 255 | + } |
| 256 | + |
| 257 | +} |
0 commit comments