Skip to content

Commit

Permalink
add: BuildConfigurationDocumentTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jun 6, 2024
1 parent d1602e3 commit 0bc0d2a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 107 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ dependencies {
include(implementation "org.quartz-scheduler:quartz:${project.quartz_version}")
include(implementation "org.reflections:reflections:${project.reflections_version}")
include(implementation "org.javassist:javassist:${project.javaassist_version}")

testImplementation(platform('org.junit:junit-bom:5.10.2'))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly('org.junit.platform:junit-platform-launcher')
}

processResources {
Expand Down Expand Up @@ -137,3 +141,8 @@ tasks.register('replaceReadme') {
// todo
}
tasks.modrinthSyncBody.dependsOn(tasks.replaceReadme)


test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import assets.fuji.Cat;
import com.google.gson.*;
import io.github.sakurawald.config.serializer.CommentTypeAdapterFactory;
import io.github.sakurawald.module.initializer.works.work_type.Work;
import io.github.sakurawald.util.ScheduleUtil;
import lombok.Cleanup;
Expand All @@ -28,7 +27,6 @@ public abstract class ConfigHandler<T> {
.setPrettyPrinting()
.disableHtmlEscaping()
.serializeNulls()
.registerTypeAdapterFactory(new CommentTypeAdapterFactory())
.registerTypeAdapter(Work.class, new Work.WorkTypeAdapter())
.create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Common {
public Quartz quartz = new Quartz();

public class Quartz {
// @Comment("the default logger level is warn")
@Comment("the default logger level is warn")
public String logger_level = "WARN";
}

Expand Down Expand Up @@ -303,7 +303,7 @@ public class Tpa {
public class Works {
public boolean enable = false;

public int sample_time_ms = FabricLoader.getInstance().isDevelopmentEnvironment() ? 60 * 1000 : 60 * 1000 * 60;
public int sample_time_ms = 60 * 1000 * 60;
public int sample_distance_limit = 512;
public int sample_counter_top_n = 20;
}
Expand Down

This file was deleted.

This file was deleted.

81 changes: 81 additions & 0 deletions src/test/java/BuildConfigurationDocumentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import com.google.gson.*;
import io.github.sakurawald.config.model.ConfigModel;
import io.github.sakurawald.config.serializer.Comment;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

public class BuildConfigurationDocumentTest {

private JsonObject buildJsonObjectWithComments(Object obj) {
JsonObject jsonObject = new JsonObject();
processFieldsWithComments(obj, jsonObject);
return jsonObject;
}

private void processFieldsWithComments(Object obj, JsonObject jsonObject) {
Class<?> clazz = obj.getClass();
for (Field field : clazz.getFields()) {
try {
String fieldName = field.getName();
Object value = field.get(obj);

/* insert related comment property */
if (field.isAnnotationPresent(Comment.class)) {
Comment commentAnnotation = field.getAnnotation(Comment.class);
jsonObject.addProperty(fieldName + "@comment", commentAnnotation.value());
}

/* switch type */
if (value != null) {
if (isPrimitiveOrWrapper(field.getType())) {
jsonObject.addProperty(fieldName, value.toString());
} else if (List.class.isAssignableFrom(field.getType())) {
JsonArray jsonArray = new JsonArray();
for (Object listItem : (List<?>) value) {
if (listItem != null) {
jsonArray.add(new Gson().toJsonTree(listItem));
}
}
jsonObject.add(fieldName, jsonArray);
} else {
JsonObject nestedJsonObject = new JsonObject();
processFieldsWithComments(value, nestedJsonObject);
jsonObject.add(fieldName, nestedJsonObject);
}
}


} catch (IllegalAccessException e) {
e.printStackTrace();
}

}
}

private boolean isPrimitiveOrWrapper(Class<?> clazz) {
return clazz.isPrimitive() || clazz == Boolean.class || clazz == Character.class ||
Number.class.isAssignableFrom(clazz) || clazz == String.class;
}

private void writeToFile(String fileName, JsonObject content) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String filePath = "./build/document/" + fileName;
new File(filePath).getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(gson.toJson(content));
System.out.println("File " + fileName + " has been written successfully.");
} catch (IOException e) {
System.err.println("An error occurred while writing file: " + e.getMessage());
}
}

@Test
void buildConfigurationDocument() {
writeToFile("config.json", buildJsonObjectWithComments(new ConfigModel()));
}
}

0 comments on commit 0bc0d2a

Please sign in to comment.