Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions modules/MedalCabinet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
<url>https://repo.helpch.at/releases/</url>
</repository>
</repositories>

Expand All @@ -78,8 +78,26 @@
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.1</version>
<version>2.11.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ public static SQLManager getInstance() {
return instance == null ? instance = new SQLManager() : instance;
}

public static void setInstance(SQLManager instance) {
SQLManager.instance = instance;
}

private SQLManager() {
connectMySQL();
}

protected SQLManager(Connection connection) {
this.connection = connection;
}

private void connectMySQL() {
String ip = MedalCabinet.getPlugin().getConfig().getString("mysql.ip");
String databaseName = MedalCabinet.getPlugin().getConfig().getString("mysql.databasename");
Expand Down Expand Up @@ -168,12 +176,17 @@ public boolean setMainMedal(String playerID, String medalID) {
public Medal getMainMedal(String playerID) {
Medal medal = null;
try (PreparedStatement ps = connection.prepareStatement(
"SELECT medal_id FROM `player_main_medal` WHERE player_id = ?"
"SELECT m.* FROM `medal` m INNER JOIN `player_main_medal` pmm ON m.medal_id = pmm.medal_id WHERE pmm.player_id = ?"
)) {
ps.setString(1, playerID);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
medal = getMedal(rs.getString("medal_id"));
medal = new Medal(
rs.getString("medal_id"),
rs.getString("medal_name"),
rs.getString("medal_material"),
rs.getString("medal_description")
);
}
} catch (SQLException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.mcatk.medalcabinet.sql;

import com.mcatk.medalcabinet.medal.Medal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class SQLManagerTest {

@Mock
private Connection connection;

@Mock
private PreparedStatement preparedStatement1;

@Mock
private ResultSet resultSet1;

private SQLManager sqlManager;

@BeforeEach
public void setUp() {
sqlManager = new SQLManager(connection);
SQLManager.setInstance(sqlManager);
}

@Test
public void testGetMainMedal_Optimized() throws SQLException {
String playerId = "player123";
String medalId = "medal456";

// Setup for the single JOIN query
when(connection.prepareStatement(contains("INNER JOIN"))).thenReturn(preparedStatement1);
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PreparedStatement is not always closed on method exit.

Copilot uses AI. Check for mistakes.
when(preparedStatement1.executeQuery()).thenReturn(resultSet1);
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ResultSet is not always closed on method exit.

Copilot uses AI. Check for mistakes.
when(resultSet1.next()).thenReturn(true);
when(resultSet1.getString("medal_id")).thenReturn(medalId);
when(resultSet1.getString("medal_name")).thenReturn("Test Medal");
when(resultSet1.getString("medal_material")).thenReturn("GOLD");
when(resultSet1.getString("medal_description")).thenReturn("A test medal");

// Execute
Medal medal = sqlManager.getMainMedal(playerId);

// Verify
assertNotNull(medal);
assertEquals(medalId, medal.getId());
assertEquals("Test Medal", medal.getName());

// Verify that prepareStatement was called only ONCE
verify(connection, times(1)).prepareStatement(anyString());
// Verify that it was the JOIN query
verify(connection).prepareStatement(contains("INNER JOIN"));
}
}
Loading