Ajoute des tests unitaires JUnit 5 (CP9 - plans de tests)

14 tests couvrant VoteConfig (Spigot + Velocity) et YamlPendingVoteStore
(Spigot + Velocity) : valeurs par defaut, portee per-server/global,
merge de config sans ecraser l'existant, incrementation/reset des
votes en attente, colorisation des messages.

Mockito pour VoteConfig/YamlPendingVoteStore cote Spigot (mock de
VoteNetworkSpigot, pas besoin de serveur Bukkit reel). Cote Velocity,
tests directs sans framework (classes deja decouplees de Velocity-API).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
SarTron-NorthBlue
2026-07-13 09:14:12 +04:00
co-authored by Claude Sonnet 5
parent d5f6e0cfb3
commit 5abe49fdd0
8 changed files with 325 additions and 0 deletions
+12
View File
@@ -42,6 +42,18 @@
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
@@ -0,0 +1,70 @@
package fr.votenetwork.spigot;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class VoteConfigTest {
@Test
void defaultsAreUsedWhenKeysAbsent() {
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
when(plugin.getConfig()).thenReturn(new YamlConfiguration());
VoteConfig config = new VoteConfig(plugin);
assertTrue(config.isMysqlStorage());
assertEquals("127.0.0.1", config.getMysqlHost());
assertEquals(3306, config.getMysqlPort());
assertEquals("votenetwork", config.getMysqlDatabase());
assertEquals(100, config.getVotePartyVotesRequired());
assertFalse(config.isPerServerScope());
assertFalse(config.isVoteBroadcastEnabled());
}
@Test
void mysqlStorageIsFalseOnlyForYaml() {
YamlConfiguration yaml = new YamlConfiguration();
yaml.set("storage.type", "yaml");
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
when(plugin.getConfig()).thenReturn(yaml);
VoteConfig config = new VoteConfig(plugin);
assertFalse(config.isMysqlStorage());
assertEquals("yaml", config.getStorageType());
}
@Test
void perServerScopeReadFromConfig() {
YamlConfiguration yaml = new YamlConfiguration();
yaml.set("storage.pending-votes-scope", "per-server");
yaml.set("server-name", "gen1");
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
when(plugin.getConfig()).thenReturn(yaml);
VoteConfig config = new VoteConfig(plugin);
assertTrue(config.isPerServerScope());
assertEquals("gen1", config.getServerName());
}
@Test
void messagesAreColorizedAndPlaceholdersPreserved() {
YamlConfiguration yaml = new YamlConfiguration();
yaml.set("messages.claim-success", "&aTu as recupere &e%amount% &avotes !");
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
when(plugin.getConfig()).thenReturn(yaml);
VoteConfig config = new VoteConfig(plugin);
String message = config.getMessageClaimSuccess();
assertFalse(message.contains("&a"), "les codes couleur '&' doivent etre traduits");
assertTrue(message.contains("%amount%"), "le placeholder %amount% doit rester intact pour etre remplace plus tard");
}
}
@@ -0,0 +1,67 @@
package fr.votenetwork.spigot.storage;
import fr.votenetwork.spigot.VoteNetworkSpigot;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class YamlPendingVoteStoreTest {
@Test
void peekReturnsZeroWhenNothingStored(@TempDir File dataFolder) {
YamlPendingVoteStore store = newStore(dataFolder);
UUID uuid = UUID.randomUUID();
AtomicInteger result = new AtomicInteger(-1);
store.peek(uuid, result::set, e -> { throw new AssertionError(e); });
assertEquals(0, result.get());
}
@Test
void fetchAndClearResetsCounterToZeroAfterReading(@TempDir File dataFolder) throws Exception {
YamlPendingVoteStore store = newStore(dataFolder);
UUID uuid = UUID.randomUUID();
writeInitialVotes(dataFolder, uuid, 3);
AtomicInteger firstRead = new AtomicInteger(-1);
store.fetchAndClear(uuid, firstRead::set, e -> { throw new AssertionError(e); });
assertEquals(3, firstRead.get());
AtomicInteger secondRead = new AtomicInteger(-1);
store.peek(uuid, secondRead::set, e -> { throw new AssertionError(e); });
assertEquals(0, secondRead.get(), "fetchAndClear doit remettre le compteur a 0");
}
@Test
void testConnectionReportsWritableDirectory(@TempDir File dataFolder) {
YamlPendingVoteStore store = newStore(dataFolder);
AtomicReference<Boolean> success = new AtomicReference<>();
store.testConnection((ok, message) -> success.set(ok));
assertEquals(Boolean.TRUE, success.get(), "un dossier temporaire accessible en ecriture doit etre rapporte comme fonctionnel");
}
private YamlPendingVoteStore newStore(File dataFolder) {
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
when(plugin.getDataFolder()).thenReturn(dataFolder);
return new YamlPendingVoteStore(plugin);
}
private void writeInitialVotes(File dataFolder, UUID uuid, int votes) throws Exception {
org.bukkit.configuration.file.YamlConfiguration yaml = new org.bukkit.configuration.file.YamlConfiguration();
yaml.set("players." + uuid + ".votes", votes);
File file = new File(dataFolder, "pending-votes.yml");
yaml.save(file);
}
}