|
1 | | - |
2 | 1 | package com.bencodez.simpleapi.tests; |
3 | 2 |
|
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.*; |
6 | 4 |
|
7 | 5 | import java.io.File; |
8 | | -import java.nio.file.Files; |
9 | 6 |
|
10 | 7 | import org.bukkit.plugin.java.JavaPlugin; |
11 | 8 | import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.io.TempDir; |
12 | 10 | import org.mockito.Mockito; |
13 | 11 |
|
14 | 12 | import com.bencodez.simpleapi.file.YMLFile; |
15 | 13 | import com.bencodez.simpleapi.scheduler.BukkitScheduler; |
16 | 14 |
|
17 | 15 | public class YMLFileTest { |
18 | 16 |
|
| 17 | + @TempDir |
| 18 | + File tempDir; |
| 19 | + |
19 | 20 | @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"); |
32 | 23 |
|
33 | | - // Mock BukkitScheduler |
| 24 | + JavaPlugin mockPlugin = Mockito.mock(JavaPlugin.class); |
34 | 25 | BukkitScheduler mockScheduler = Mockito.mock(BukkitScheduler.class); |
35 | 26 |
|
36 | | - // Create an instance of YMLFile with the mocked scheduler |
37 | 27 | YMLFile ymlFile = new YMLFile(mockPlugin, tempFile, mockScheduler) { |
38 | 28 | @Override |
39 | 29 | public void onFileCreation() { |
40 | | - |
| 30 | + // no-op |
41 | 31 | } |
42 | 32 | }; |
43 | 33 |
|
44 | | - // Perform setup |
| 34 | + // Setup should create file |
45 | 35 | ymlFile.setup(); |
46 | | - |
47 | | - // Assert that the file was created |
48 | 36 | assertTrue(tempFile.exists()); |
| 37 | + assertTrue(ymlFile.isJustCreated()); |
49 | 38 |
|
50 | | - // Set a value |
| 39 | + // Write value |
51 | 40 | ymlFile.setValue("key", "value"); |
52 | 41 |
|
53 | | - // Reload data |
| 42 | + // Reload |
54 | 43 | ymlFile.reloadData(); |
55 | 44 |
|
56 | | - // Assert that the value was saved and reloaded correctly |
| 45 | + // Verify |
57 | 46 | 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")); |
58 | 70 | } |
| 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 | + |
59 | 118 | } |
0 commit comments