Skip to content

Commit

Permalink
Merge pull request #5 from BentoBoxWorld/develop
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
tastybento authored Nov 24, 2024
2 parents 96c57c6 + cd13d31 commit b98644e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@
</issueManagement>

<distributionManagement>
<snapshotRepository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.org/repository/maven-snapshots</url>
</snapshotRepository>
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.org/repository/maven-releases</url>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
</distributionManagement>

Expand All @@ -58,8 +54,8 @@
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.19.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.23.0</bentobox.version>
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
<!-- AOneBlock addon version -->
<aoneblock.version>1.12.3-SNAPSHOT</aoneblock.version>
<!-- Panel Utils version -->
Expand All @@ -69,7 +65,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.0.1</build.version>
<build.version>1.1.0</build.version>
<sonar.projectKey>BentoBoxWorld_TopBlock</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand Down Expand Up @@ -129,6 +125,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
Expand Down Expand Up @@ -355,7 +355,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<version>0.8.10</version>
<configuration>
<append>true</append>
<excludes>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: TopBlock
main: world.bentobox.topblock.TopBlock
version: ${version}${build.number}
icon: GRASS_BLOCK
api-version: 1.21.0
api-version: 2.7.1

authors: tastybento

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: BentoBox-TopBlock
main: world.bentobox.topblock.TopBlockPladdon
version: ${project.version}${build.number}
api-version: "1.19"
api-version: "1.21""
authors: [tastybento]
contributors: ["The BentoBoxWorld Community"]
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/world/bentobox/topblock/TopBlockManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
Expand All @@ -13,26 +14,35 @@
import java.util.Optional;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
import world.bentobox.aoneblock.listeners.BlockListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.topblock.TopBlockManager.TopTenData;
import world.bentobox.topblock.config.ConfigSettings;
import world.bentobox.topblock.mocks.ServerMocks;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(Bukkit.class)
public class TopBlockManagerTest {

@Mock
Expand All @@ -43,8 +53,7 @@ public class TopBlockManagerTest {
private TopBlockManager tbm;
@Mock
private AOneBlock aob;
@Mock
private BlockListener bl;

@Mock
private IslandsManager im;

Expand All @@ -54,6 +63,11 @@ public class TopBlockManagerTest {
*/
@Before
public void setUp() throws Exception {
Server server = ServerMocks.newServer();

PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getServer()).thenReturn(server);

List<OneBlockIslands> list = new ArrayList<>();
OneBlockIslands i = new OneBlockIslands(UUID.randomUUID().toString());
i.setLifetime(100);
Expand All @@ -65,12 +79,20 @@ public void setUp() throws Exception {
when(addon.getIslands()).thenReturn(im);
when(im.getIslandById(anyString())).thenReturn(Optional.of(island));
// AOneBlock
BlockListener bl = mock(BlockListener.class); // This class uses static initializations so if it is mocked as a field, it will spark an issue
when(bl.getAllIslands()).thenReturn(list);
when(aob.getBlockListener()).thenReturn(bl);
when(addon.getaOneBlock()).thenReturn(aob);
tbm = new TopBlockManager(addon);
}

@After
public void tearDown() {
ServerMocks.unsetBukkitServer();
User.clearUsers();
Mockito.framework().clearInlineMocks();
}

/**
* Test method for {@link world.bentobox.topblock.TopBlockManager#TopBlockManager(world.bentobox.topblock.TopBlock)}.
*/
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/world/bentobox/topblock/mocks/ServerMocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package world.bentobox.topblock.mocks;

import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.UnsafeValues;
import org.eclipse.jdt.annotation.NonNull;

public final class ServerMocks {

public static @NonNull Server newServer() {
Server mock = mock(Server.class);

Logger noOp = mock(Logger.class);
when(mock.getLogger()).thenReturn(noOp);
when(mock.isPrimaryThread()).thenReturn(true);

// Unsafe
UnsafeValues unsafe = mock(UnsafeValues.class);
when(mock.getUnsafe()).thenReturn(unsafe);

// Server must be available before tags can be mocked.
Bukkit.setServer(mock);

// Bukkit has a lot of static constants referencing registry values. To initialize those, the
// registries must be able to be fetched before the classes are touched.
Map<Class<? extends Keyed>, Object> registers = new HashMap<>();

doAnswer(invocationGetRegistry -> registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> {
Registry<?> registry = mock(Registry.class);
Map<NamespacedKey, Keyed> cache = new HashMap<>();
doAnswer(invocationGetEntry -> {
NamespacedKey key = invocationGetEntry.getArgument(0);
// Some classes (like BlockType and ItemType) have extra generics that will be
// erased during runtime calls. To ensure accurate typing, grab the constant's field.
// This approach also allows us to return null for unsupported keys.
Class<? extends Keyed> constantClazz;
try {
//noinspection unchecked
constantClazz = (Class<? extends Keyed>) clazz
.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType();
} catch (ClassCastException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
return null;
}

return cache.computeIfAbsent(key, key1 -> {
Keyed keyed = mock(constantClazz);
doReturn(key).when(keyed).getKey();
return keyed;
});
}).when(registry).get(notNull());
return registry;
})).when(mock).getRegistry(notNull());

// Tags are dependent on registries, but use a different method.
// This will set up blank tags for each constant; all that needs to be done to render them
// functional is to re-mock Tag#getValues.
doAnswer(invocationGetTag -> {
Tag<?> tag = mock(Tag.class);
doReturn(invocationGetTag.getArgument(1)).when(tag).getKey();
doReturn(Set.of()).when(tag).getValues();
doAnswer(invocationIsTagged -> {
Keyed keyed = invocationIsTagged.getArgument(0);
Class<?> type = invocationGetTag.getArgument(2);
if (!type.isAssignableFrom(keyed.getClass())) {
return null;
}
// Since these are mocks, the exact instance might not be equal. Consider equal keys equal.
return tag.getValues().contains(keyed)
|| tag.getValues().stream().anyMatch(value -> value.getKey().equals(keyed.getKey()));
}).when(tag).isTagged(notNull());
return tag;
}).when(mock).getTag(notNull(), notNull(), notNull());

// Once the server is all set up, touch BlockType and ItemType to initialize.
// This prevents issues when trying to access dependent methods from a Material constant.
try {
Class.forName("org.bukkit.inventory.ItemType");
Class.forName("org.bukkit.block.BlockType");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}

return mock;
}

public static void unsetBukkitServer() {
try {
Field server = Bukkit.class.getDeclaredField("server");
server.setAccessible(true);
server.set(null, null);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private ServerMocks() {
}

}

0 comments on commit b98644e

Please sign in to comment.