Skip to content

Commit d0333d9

Browse files
committed
Fix tests creating test.yml and not removing
1 parent cdd7a7b commit d0333d9

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

SimpleAPI/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
<timestamp>${maven.build.timestamp}</timestamp>
1414
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
1515
<build.number>NOTSET</build.number>
16-
<maven.compiler.source>17</maven.compiler.source>
17-
<maven.compiler.target>17</maven.compiler.target>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
18+
<maven.compiler.release>8</maven.compiler.release>
1819
</properties>
1920
<build>
2021
<sourceDirectory>src/main/java</sourceDirectory>

SimpleAPI/src/test/java/com/bencodez/simpleapi/tests/VelocityYMLFileTest.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
811
import java.util.ArrayList;
912
import java.util.Arrays;
1013
import java.util.List;
1114

15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
1217
import org.junit.jupiter.api.Test;
1318

1419
import com.bencodez.simpleapi.file.velocity.VelocityYMLFile;
@@ -17,62 +22,79 @@
1722

1823
public class VelocityYMLFileTest {
1924

25+
private static final String TEST_FILE_PATH = "target/test.yml";
26+
private File testFile;
27+
28+
@BeforeEach
29+
public void setUp() throws IOException {
30+
testFile = new File(TEST_FILE_PATH);
31+
if (!testFile.getParentFile().exists()) {
32+
testFile.getParentFile().mkdirs();
33+
}
34+
testFile.createNewFile();
35+
}
36+
37+
@AfterEach
38+
public void tearDown() throws IOException {
39+
Files.deleteIfExists(Paths.get(TEST_FILE_PATH));
40+
}
41+
2042
@Test
2143
public void getBooleanReturnsDefaultWhenNodeDoesNotExist() {
22-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
44+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
2345
ConfigurationNode node = velocityYMLFile.getNode("non.existent.path");
2446
assertFalse(velocityYMLFile.getBoolean(node, false));
2547
}
2648

2749
@Test
2850
public void getBooleanReturnsValueWhenNodeExists() {
29-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
51+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
3052
ConfigurationNode node = velocityYMLFile.getNode("config.enabled");
3153
node.setValue(true);
3254
assertTrue(velocityYMLFile.getBoolean(node, false));
3355
}
3456

3557
@Test
3658
public void getIntReturnsDefaultWhenNodeDoesNotExist() {
37-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
59+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
3860
ConfigurationNode node = velocityYMLFile.getNode("non.existent.path");
3961
assertEquals(42, velocityYMLFile.getInt(node, 42));
4062
}
4163

4264
@Test
4365
public void getIntReturnsValueWhenNodeExists() {
44-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
66+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
4567
ConfigurationNode node = velocityYMLFile.getNode("config.port");
4668
node.setValue(8080);
4769
assertEquals(8080, velocityYMLFile.getInt(node, 42));
4870
}
4971

5072
@Test
5173
public void getStringReturnsDefaultWhenNodeDoesNotExist() {
52-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
74+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
5375
ConfigurationNode node = velocityYMLFile.getNode("non.existent.path");
5476
assertEquals("default", velocityYMLFile.getString(node, "default"));
5577
}
5678

5779
@Test
5880
public void getStringReturnsValueWhenNodeExists() {
59-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
81+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
6082
ConfigurationNode node = velocityYMLFile.getNode("config.name");
6183
node.setValue("Velocity");
6284
assertEquals("Velocity", velocityYMLFile.getString(node, "default"));
6385
}
6486

6587
@Test
6688
public void getStringListReturnsDefaultWhenNodeDoesNotExist() {
67-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
89+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
6890
ConfigurationNode node = velocityYMLFile.getNode("non.existent.path");
6991
ArrayList<String> defaultList = new ArrayList<>(Arrays.asList("default"));
7092
assertEquals(defaultList, velocityYMLFile.getStringList(node, defaultList));
7193
}
7294

7395
@Test
7496
public void getStringListReturnsValueWhenNodeExists() {
75-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
97+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
7698
ConfigurationNode node = velocityYMLFile.getNode("config.list");
7799
ArrayList<String> list = new ArrayList<>(Arrays.asList("item1", "item2"));
78100
node.setValue(list);
@@ -81,14 +103,14 @@ public void getStringListReturnsValueWhenNodeExists() {
81103

82104
@Test
83105
public void getKeysReturnsEmptyListWhenNodeDoesNotExist() {
84-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
106+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
85107
ConfigurationNode node = velocityYMLFile.getNode("non.existent.path");
86108
assertTrue(velocityYMLFile.getKeys(node).isEmpty());
87109
}
88110

89111
@Test
90112
public void getKeysReturnsListOfKeysWhenNodeExists() {
91-
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(new File("test.yml"));
113+
VelocityYMLFile velocityYMLFile = new VelocityYMLFile(testFile);
92114
ConfigurationNode node = velocityYMLFile.getNode("config");
93115
node.getNode("key1").setValue("value1");
94116
node.getNode("key2").setValue("value2");

SimpleAPI/src/test/java/com/bencodez/simpleapi/tests/YMLFileTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public class YMLFileTest {
1818

1919
@Test
2020
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+
}
2126
// Create a temporary file
22-
File tempFile = Files.createTempFile("test", ".yml").toFile();
27+
File tempFile = Files.createTempFile(targetDir.toPath(), "test", ".yml").toFile();
2328
tempFile.deleteOnExit();
2429

2530
// Mock JavaPlugin

0 commit comments

Comments
 (0)