Skip to content

Commit d40de80

Browse files
committed
Add unit tests and improve file handling
1 parent 27a9b19 commit d40de80

File tree

17 files changed

+1112
-19
lines changed

17 files changed

+1112
-19
lines changed

SimpleAPI/.classpath

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<classpath>
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
44
<attributes>
5+
<attribute name="module" value="true"/>
56
<attribute name="maven.pomderived" value="true"/>
67
</attributes>
78
</classpathentry>
@@ -11,23 +12,17 @@
1112
<attribute name="maven.pomderived" value="true"/>
1213
</attributes>
1314
</classpathentry>
14-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
15-
<attributes>
16-
<attribute name="maven.pomderived" value="true"/>
17-
<attribute name="optional" value="true"/>
18-
</attributes>
19-
</classpathentry>
2015
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
2116
<attributes>
17+
<attribute name="test" value="true"/>
2218
<attribute name="optional" value="true"/>
2319
<attribute name="maven.pomderived" value="true"/>
24-
<attribute name="test" value="true"/>
2520
</attributes>
2621
</classpathentry>
2722
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
2823
<attributes>
29-
<attribute name="maven.pomderived" value="true"/>
3024
<attribute name="test" value="true"/>
25+
<attribute name="maven.pomderived" value="true"/>
3126
<attribute name="optional" value="true"/>
3227
</attributes>
3328
</classpathentry>

SimpleAPI/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@
177177
<version>1.5.25</version>
178178
<scope>provided</scope>
179179
</dependency>
180+
<dependency>
181+
<groupId>org.junit.jupiter</groupId>
182+
<artifactId>junit-jupiter-engine</artifactId>
183+
<version>5.3.1</version>
184+
<scope>test</scope>
185+
</dependency>
186+
<dependency>
187+
<groupId>org.mockito</groupId>
188+
<artifactId>mockito-core</artifactId>
189+
<version>5.15.2</version>
190+
<scope>test</scope>
191+
</dependency>
180192
</dependencies>
181193
<profiles>
182194
<profile>

SimpleAPI/src/main/java/com/bencodez/simpleapi/command/CommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public abstract class CommandHandler {
6060

6161
@Getter
6262
@Setter
63-
private String perm;
63+
private String perm = "";
6464

6565
@Getter
6666
private JavaPlugin plugin;

SimpleAPI/src/main/java/com/bencodez/simpleapi/file/BungeeJsonFile.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public BungeeJsonFile(File file) {
3333

3434
if (!file.exists()) {
3535
try {
36-
file.getParentFile().mkdirs();
36+
File parentDir = file.getParentFile();
37+
if (parentDir != null && !parentDir.exists()) {
38+
parentDir.mkdirs();
39+
}
3740
file.createNewFile();
3841
conf = new JsonObject();
3942
save(); // Save file with empty JsonObject upon creation
@@ -138,8 +141,15 @@ public List<String> getStringList(String path, List<String> def) {
138141
}
139142

140143
public List<String> getKeys(String path) {
141-
JsonObject node = navigateToNode(path);
144+
JsonObject parentNode = navigateToNode(path);
145+
String lastPart = getLastPathPart(path);
146+
JsonObject node = parentNode.getAsJsonObject(lastPart);
147+
if (path.split("\\.").length == 1) {
148+
node = parentNode;
149+
}
150+
142151
if (node != null) {
152+
//System.out.println(node.toString());
143153
List<String> keys = new ArrayList<>();
144154
for (Map.Entry<String, JsonElement> entry : node.entrySet()) {
145155
keys.add(entry.getKey());
@@ -173,29 +183,25 @@ public synchronized void setInt(String path, int value) {
173183
JsonObject node = ensureParentObjectsExist(path);
174184
String lastPart = getLastPathPart(path);
175185
node.addProperty(lastPart, value);
176-
save();
177186
}
178187

179188
public synchronized void setString(String path, String value) {
180189
JsonObject node = ensureParentObjectsExist(path);
181190
String lastPart = getLastPathPart(path);
182191
node.addProperty(lastPart, value);
183-
save();
184192
}
185193

186194
public synchronized void setBoolean(String path, boolean value) {
187195
JsonObject node = ensureParentObjectsExist(path);
188196
String lastPart = getLastPathPart(path);
189197
node.addProperty(lastPart, value);
190-
save();
191198
}
192199

193200
public synchronized void setLong(String path, long value) {
194201
JsonObject node = ensureParentObjectsExist(path);
195202
if (node != null) {
196203
String lastPart = getLastPathPart(path);
197204
node.addProperty(lastPart, value);
198-
save();
199205
}
200206
}
201207

@@ -208,7 +214,6 @@ public synchronized void setStringList(String path, List<String> value) {
208214
jsonArray.add(item);
209215
}
210216
node.add(lastPart, jsonArray);
211-
save();
212217
}
213218
}
214219

@@ -217,7 +222,6 @@ public synchronized void remove(String path) {
217222
if (node != null) {
218223
String lastPart = getLastPathPart(path);
219224
node.remove(lastPart);
220-
save();
221225
}
222226
}
223227

SimpleAPI/src/main/java/com/bencodez/simpleapi/file/YMLFile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ public YMLFile(JavaPlugin plugin, File file) {
4242
scheduler = new BukkitScheduler(plugin);
4343
}
4444

45+
public YMLFile(JavaPlugin plugin, File file, BukkitScheduler scheduler) {
46+
this.dFile = file;
47+
this.plugin = plugin;
48+
this.scheduler = scheduler;
49+
}
50+
51+
public YMLFile(JavaPlugin plugin, File file, BukkitScheduler scheduler, boolean setup) {
52+
this(plugin, file, scheduler);
53+
if (setup) {
54+
setup();
55+
}
56+
}
57+
4558
public YMLFile(JavaPlugin plugin, File file, boolean setup) {
4659
dFile = file;
4760
this.plugin = plugin;

SimpleAPI/src/main/java/com/bencodez/simpleapi/file/velocity/VelocityYMLFile.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ public VelocityYMLFile(File file) {
2626
this.file = file;
2727
if (!file.exists()) {
2828
try {
29-
file.getParentFile().mkdirs();
3029
file.createNewFile();
30+
if (file.getParentFile() != null && !file.getParentFile().exists()) {
31+
file.getParentFile().mkdirs();
32+
}
3133
} catch (IOException e) {
32-
// TODO Auto-generated catch block
3334
e.printStackTrace();
3435
}
3536
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.bencodez.simpleapi.tests;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.lang.reflect.Field;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
import com.bencodez.simpleapi.file.annotation.ConfigDataBoolean;
14+
import com.bencodez.simpleapi.file.annotation.ConfigDataConfigurationSection;
15+
import com.bencodez.simpleapi.file.annotation.ConfigDataDouble;
16+
import com.bencodez.simpleapi.file.annotation.ConfigDataInt;
17+
import com.bencodez.simpleapi.file.annotation.ConfigDataListString;
18+
import com.bencodez.simpleapi.file.annotation.ConfigDataString;
19+
20+
public class AnnotationTest {
21+
22+
@Test
23+
public void testConfigDataBoolean() throws NoSuchFieldException, IllegalAccessException {
24+
Field field = SampleConfig.class.getDeclaredField("featureEnabled");
25+
ConfigDataBoolean annotation = field.getAnnotation(ConfigDataBoolean.class);
26+
assertNotNull(annotation);
27+
assertEquals("feature.enabled", annotation.path());
28+
assertTrue(annotation.defaultValue());
29+
30+
// Process annotation
31+
SampleConfig config = new SampleConfig();
32+
field.setAccessible(true);
33+
field.set(config, annotation.defaultValue());
34+
35+
// Get current value
36+
boolean currentValue = (boolean) field.get(config);
37+
assertTrue(currentValue);
38+
}
39+
40+
@Test
41+
public void testConfigDataConfigurationSection() throws NoSuchFieldException, IllegalAccessException {
42+
Field field = SampleConfig.class.getDeclaredField("databaseSettings");
43+
ConfigDataConfigurationSection annotation = field.getAnnotation(ConfigDataConfigurationSection.class);
44+
assertNotNull(annotation);
45+
assertEquals("database.settings", annotation.path());
46+
47+
// Process annotation
48+
SampleConfig config = new SampleConfig();
49+
field.setAccessible(true);
50+
field.set(config, new Object()); // Assuming default value is a new object
51+
52+
// Get current value
53+
Object currentValue = field.get(config);
54+
assertNotNull(currentValue);
55+
}
56+
57+
@Test
58+
public void testConfigDataDouble() throws NoSuchFieldException, IllegalAccessException {
59+
Field field = SampleConfig.class.getDeclaredField("thresholdLimit");
60+
ConfigDataDouble annotation = field.getAnnotation(ConfigDataDouble.class);
61+
assertNotNull(annotation);
62+
assertEquals("threshold.limit", annotation.path());
63+
assertEquals(0.75, annotation.defaultValue(), 0.01);
64+
65+
// Process annotation
66+
SampleConfig config = new SampleConfig();
67+
field.setAccessible(true);
68+
field.set(config, annotation.defaultValue());
69+
70+
// Get current value
71+
double currentValue = (double) field.get(config);
72+
assertEquals(0.75, currentValue, 0.01);
73+
}
74+
75+
@Test
76+
public void testConfigDataInteger() throws NoSuchFieldException, IllegalAccessException {
77+
Field field = SampleConfig.class.getDeclaredField("maxConnections");
78+
ConfigDataInt annotation = field.getAnnotation(ConfigDataInt.class);
79+
assertNotNull(annotation);
80+
assertEquals("max.connections", annotation.path());
81+
assertEquals(10, annotation.defaultValue());
82+
83+
// Process annotation
84+
SampleConfig config = new SampleConfig();
85+
field.setAccessible(true);
86+
field.set(config, annotation.defaultValue());
87+
88+
// Get current value
89+
int currentValue = (int) field.get(config);
90+
assertEquals(10, currentValue);
91+
}
92+
93+
@Test
94+
public void testConfigDataList() throws NoSuchFieldException, IllegalAccessException {
95+
Field field = SampleConfig.class.getDeclaredField("userRoles");
96+
ConfigDataListString annotation = field.getAnnotation(ConfigDataListString.class);
97+
assertNotNull(annotation);
98+
assertEquals("user.roles", annotation.path());
99+
100+
// Process annotation
101+
SampleConfig config = new SampleConfig();
102+
field.setAccessible(true);
103+
field.set(config, Arrays.asList("default")); // Assuming default value is a list with "default"
104+
105+
// Get current value
106+
@SuppressWarnings("unchecked")
107+
List<String> currentValue = (List<String>) field.get(config);
108+
assertNotNull(currentValue);
109+
}
110+
111+
@Test
112+
public void testConfigDataString() throws NoSuchFieldException, IllegalAccessException {
113+
Field field = SampleConfig.class.getDeclaredField("welcomeMessage");
114+
ConfigDataString annotation = field.getAnnotation(ConfigDataString.class);
115+
assertNotNull(annotation);
116+
assertEquals("welcome.message", annotation.path());
117+
assertEquals("Welcome!", annotation.defaultValue());
118+
119+
// Process annotation
120+
SampleConfig config = new SampleConfig();
121+
field.setAccessible(true);
122+
field.set(config, annotation.defaultValue());
123+
124+
// Get current value
125+
String currentValue = (String) field.get(config);
126+
assertEquals("Welcome!", currentValue);
127+
}
128+
129+
}

0 commit comments

Comments
 (0)