Skip to content

Commit 332fbbd

Browse files
committed
Add tests for case-insensitive YML file operations
1 parent 37a62c4 commit 332fbbd

File tree

1 file changed

+84
-25
lines changed

1 file changed

+84
-25
lines changed
Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,118 @@
1-
21
package com.bencodez.simpleapi.tests;
32

4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.*;
64

75
import java.io.File;
8-
import java.nio.file.Files;
96

107
import org.bukkit.plugin.java.JavaPlugin;
118
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.io.TempDir;
1210
import org.mockito.Mockito;
1311

1412
import com.bencodez.simpleapi.file.YMLFile;
1513
import com.bencodez.simpleapi.scheduler.BukkitScheduler;
1614

1715
public class YMLFileTest {
1816

17+
@TempDir
18+
File tempDir;
19+
1920
@Test
20-
public void testYMLFileOperations() throws Exception {
21-
// Create a test directory inside the target folder
22-
File targetDir = new File("target");
23-
if (!targetDir.exists()) {
24-
targetDir.mkdirs();
25-
}
26-
// Create a temporary file
27-
File tempFile = Files.createTempFile(targetDir.toPath(), "test", ".yml").toFile();
28-
tempFile.deleteOnExit();
29-
30-
// Mock JavaPlugin
31-
JavaPlugin mockPlugin = Mockito.mock(JavaPlugin.class);
21+
public void testBasicYMLFileOperations() throws Exception {
22+
File tempFile = new File(tempDir, "test.yml");
3223

33-
// Mock BukkitScheduler
24+
JavaPlugin mockPlugin = Mockito.mock(JavaPlugin.class);
3425
BukkitScheduler mockScheduler = Mockito.mock(BukkitScheduler.class);
3526

36-
// Create an instance of YMLFile with the mocked scheduler
3727
YMLFile ymlFile = new YMLFile(mockPlugin, tempFile, mockScheduler) {
3828
@Override
3929
public void onFileCreation() {
40-
30+
// no-op
4131
}
4232
};
4333

44-
// Perform setup
34+
// Setup should create file
4535
ymlFile.setup();
46-
47-
// Assert that the file was created
4836
assertTrue(tempFile.exists());
37+
assertTrue(ymlFile.isJustCreated());
4938

50-
// Set a value
39+
// Write value
5140
ymlFile.setValue("key", "value");
5241

53-
// Reload data
42+
// Reload
5443
ymlFile.reloadData();
5544

56-
// Assert that the value was saved and reloaded correctly
45+
// Verify
5746
assertEquals("value", ymlFile.getData().getString("key"));
47+
assertFalse(ymlFile.isFailedToRead());
48+
}
49+
50+
@Test
51+
public void testIgnoreCaseRead() {
52+
File tempFile = new File(tempDir, "case.yml");
53+
54+
JavaPlugin plugin = Mockito.mock(JavaPlugin.class);
55+
BukkitScheduler scheduler = Mockito.mock(BukkitScheduler.class);
56+
57+
YMLFile ymlFile = new YMLFile(plugin, tempFile, scheduler) {
58+
@Override
59+
public void onFileCreation() {
60+
}
61+
};
62+
63+
ymlFile.setIgnoreCase(true);
64+
ymlFile.setup();
65+
66+
ymlFile.getData().set("Rewards.Commands.Console", true);
67+
68+
assertTrue(ymlFile.getData().getBoolean("rewards.commands.console"));
69+
assertTrue(ymlFile.getData().getBoolean("REWARDS.COMMANDS.CONSOLE"));
5870
}
71+
72+
@Test
73+
public void testIgnoreCaseWrite() {
74+
File tempFile = new File(tempDir, "case-write.yml");
75+
76+
JavaPlugin plugin = Mockito.mock(JavaPlugin.class);
77+
BukkitScheduler scheduler = Mockito.mock(BukkitScheduler.class);
78+
79+
YMLFile ymlFile = new YMLFile(plugin, tempFile, scheduler) {
80+
@Override
81+
public void onFileCreation() {
82+
}
83+
};
84+
85+
ymlFile.setIgnoreCase(true);
86+
ymlFile.setup();
87+
88+
ymlFile.getData().set("rewards.commands.console", true);
89+
90+
assertTrue(ymlFile.getData().getBoolean("Rewards.Commands.Console"));
91+
}
92+
93+
@Test
94+
public void testToggleIgnoreCaseAfterLoad() {
95+
File tempFile = new File(tempDir, "toggle.yml");
96+
97+
JavaPlugin plugin = Mockito.mock(JavaPlugin.class);
98+
BukkitScheduler scheduler = Mockito.mock(BukkitScheduler.class);
99+
100+
YMLFile ymlFile = new YMLFile(plugin, tempFile, scheduler) {
101+
@Override
102+
public void onFileCreation() {
103+
}
104+
};
105+
106+
ymlFile.setup();
107+
ymlFile.getData().set("Rewards.Commands.Console", true);
108+
109+
// Default: case-sensitive
110+
assertFalse(ymlFile.getData().getBoolean("rewards.commands.console"));
111+
112+
ymlFile.setIgnoreCase(true);
113+
114+
// Now case-insensitive
115+
assertTrue(ymlFile.getData().getBoolean("rewards.commands.console"));
116+
}
117+
59118
}

0 commit comments

Comments
 (0)