From a3963e54e5a7c2f6af8fa556e968485069545735 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Feb 2026 16:21:57 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Improve=20GemExecutor=20performance?= =?UTF-8?q?=20by=20removing=20redundant=20DB=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Optimized `GemExecutor.addGems` to reuse `getGems` result, saving one DB query. - Added `GemExecutorTest` to verify single invocation of `getGems`. - Updated `pom.xml` with test dependencies (JUnit, Mockito) and fixed PlaceholderAPI repository. Co-authored-by: acsoto <59144459+acsoto@users.noreply.github.com> --- modules/Gem/pom.xml | 34 +++++++- .../main/java/com/mcatk/gem/GemExecutor.java | 2 +- .../java/com/mcatk/gem/GemExecutorTest.java | 79 +++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 modules/Gem/src/test/java/com/mcatk/gem/GemExecutorTest.java diff --git a/modules/Gem/pom.xml b/modules/Gem/pom.xml index 6476837..eaa98ad 100644 --- a/modules/Gem/pom.xml +++ b/modules/Gem/pom.xml @@ -64,7 +64,7 @@ placeholderapi - https://repo.extendedclip.com/content/repositories/placeholderapi/ + https://repo.helpch.at/releases/ @@ -78,8 +78,38 @@ me.clip placeholderapi - 2.11.1 + 2.11.5 provided + + junit + junit + 4.13.2 + test + + + org.mockito + mockito-core + 5.2.0 + test + + + org.mockito + mockito-inline + 5.2.0 + test + + + net.bytebuddy + byte-buddy + 1.14.19 + test + + + net.bytebuddy + byte-buddy-agent + 1.14.19 + test + diff --git a/modules/Gem/src/main/java/com/mcatk/gem/GemExecutor.java b/modules/Gem/src/main/java/com/mcatk/gem/GemExecutor.java index 0fe8194..30f9dc6 100644 --- a/modules/Gem/src/main/java/com/mcatk/gem/GemExecutor.java +++ b/modules/Gem/src/main/java/com/mcatk/gem/GemExecutor.java @@ -44,7 +44,7 @@ public Integer getTotalGems(String name) { public void addGems(String name, int addGems) { Integer gems = MySQLManager.getInstance().getGems(name); - if (MySQLManager.getInstance().getGems(name) == null) { + if (gems == null) { MySQLManager.getInstance().insertData(name); gems = 0; } diff --git a/modules/Gem/src/test/java/com/mcatk/gem/GemExecutorTest.java b/modules/Gem/src/test/java/com/mcatk/gem/GemExecutorTest.java new file mode 100644 index 0000000..330152c --- /dev/null +++ b/modules/Gem/src/test/java/com/mcatk/gem/GemExecutorTest.java @@ -0,0 +1,79 @@ +package com.mcatk.gem; + +import com.mcatk.gem.sql.MySQLManager; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.lang.reflect.Field; +import java.util.logging.Logger; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class GemExecutorTest { + + @Mock + private MySQLManager mySQLManagerMock; + + @Mock + private Gem gemMock; + + private GemExecutor gemExecutor; + + @Before + public void setUp() throws Exception { + // Mock MySQLManager.instance + setStaticField(MySQLManager.class, "instance", mySQLManagerMock); + + // Mock Gem.plugin + setStaticField(Gem.class, "plugin", gemMock); + + gemExecutor = new GemExecutor(); + } + + @After + public void tearDown() throws Exception { + setStaticField(MySQLManager.class, "instance", null); + setStaticField(Gem.class, "plugin", null); + } + + private void setStaticField(Class clazz, String fieldName, Object value) throws Exception { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + field.set(null, value); + } + + @Test + public void testAddGems_Performance() { + String playerName = "TestPlayer"; + int gemsToAdd = 100; + + // Simulate new user: getGems returns null + when(mySQLManagerMock.getGems(playerName)).thenReturn(null); + + // Mock getTotal to return a value (e.g. 0) + when(mySQLManagerMock.getTotal(playerName)).thenReturn(0); + + gemExecutor.addGems(playerName, gemsToAdd); + + // Verify getGems is called exactly ONCE (target behavior) + verify(mySQLManagerMock, times(1)).getGems(playerName); + + // Verify insertData is called + verify(mySQLManagerMock).insertData(playerName); + + // Verify update calls + verify(mySQLManagerMock).setGems(eq(playerName), eq(gemsToAdd)); + verify(mySQLManagerMock).setTotal(eq(playerName), eq(gemsToAdd)); + + // Verify log + verify(gemMock).log(anyString()); + } +}