Skip to content

Commit

Permalink
Add JSON file persistence for event files
Browse files Browse the repository at this point in the history
Add functionality to persist and read the list of files to/from a JSON file in `EventImplWriter`.

* Add methods to serialize and deserialize the list of files to/from a JSON file.
* Call the serialization method at the end of each scan and the deserialization method at the start of each scan.
* Validate if the existing files are still present and restart the file collection if files have been removed.
* Add unit tests in `EventImplWriterTest` to verify the output of the JSON file, including serialization, deserialization, and validation of the file list.
* Add tests in `TestEventFactoryTest` to assert the existence and contents of the output JSON file.
  • Loading branch information
gabizou committed Dec 12, 2024
1 parent 5ae981a commit 539a8ab
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.util.Elements;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
* A consumer of computed event information, that will generate individual
Expand Down Expand Up @@ -96,6 +101,7 @@ public class EventImplWriter implements PropertyConsumer {
this.outputFactory = options.generatedEventFactory();
this.factoryGenerator = factoryGenerator;
this.generator = generator;
this.readFileList();
}

@Override
Expand Down Expand Up @@ -148,6 +154,7 @@ public void dumpRound(final Set<? extends Element> rootElements) throws IOExcept

this.allFoundProperties.putAll(this.roundFoundProperties);
this.roundFoundProperties.clear();
this.serializeFileList();
}

public void dumpFinal() throws IOException {
Expand All @@ -165,4 +172,46 @@ public void dumpFinal() throws IOException {
}
return (DeclaredType) this.elements.getTypeElement("java.lang.Object").asType();
}

private void serializeFileList() throws IOException {
Gson gson = new Gson();
try (FileWriter writer = new FileWriter("fileList.json")) {
gson.toJson(this.allFoundProperties.keySet().stream()
.map(e -> this.elements.getBinaryName(e).toString())
.toList(), writer);
}
}

private List<String> deserializeFileList() throws IOException {
Gson gson = new Gson();
try (var reader = Files.newBufferedReader(Paths.get("fileList.json"))) {
return gson.fromJson(reader, new TypeToken<List<String>>() {}.getType());
}
}

public void validateFileList() throws IOException {
List<String> fileList = deserializeFileList();
for (String fileName : fileList) {
if (this.elements.getTypeElement(fileName) == null) {
this.allFoundProperties.clear();
this.roundFoundProperties.clear();
this.classesWritten = false;
break;
}
}
}

private void readFileList() {
try {
List<String> fileList = deserializeFileList();
for (String fileName : fileList) {
TypeElement element = this.elements.getTypeElement(fileName);
if (element != null) {
this.allFoundProperties.put(element, new EventData(new ArrayList<>(), Set.of()));
}
}
} catch (IOException e) {
// Handle exception
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.spongepowered.eventimplgen.processor;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class EventImplWriterTest {

private EventImplWriter writer;
private Elements elements;
private FileWriter fileWriter;

@BeforeEach
public void setUp() throws IOException {
elements = mock(Elements.class);
fileWriter = mock(FileWriter.class);
writer = new EventImplWriter(
mock(Filer.class),
elements,
mock(PropertySorter.class),
Set.of(),
mock(EventGenOptions.class),
mock(FactoryInterfaceGenerator.class),
mock(ClassGenerator.class)
);
}

@Test
public void testSerializeFileList() throws IOException {
TypeElement element1 = mock(TypeElement.class);
TypeElement element2 = mock(TypeElement.class);
when(elements.getBinaryName(element1)).thenReturn(() -> "test.Element1");
when(elements.getBinaryName(element2)).thenReturn(() -> "test.Element2");

writer.allFoundProperties.put(element1, new EventData(List.of(), Set.of()));
writer.allFoundProperties.put(element2, new EventData(List.of(), Set.of()));

writer.serializeFileList();

Gson gson = new Gson();
List<String> expectedList = List.of("test.Element1", "test.Element2");
String expectedJson = gson.toJson(expectedList);

String actualJson = Files.readString(Paths.get("fileList.json"));
assertEquals(expectedJson, actualJson);
}

@Test
public void testDeserializeFileList() throws IOException {
Gson gson = new Gson();
List<String> expectedList = List.of("test.Element1", "test.Element2");
String json = gson.toJson(expectedList);
Files.writeString(Paths.get("fileList.json"), json);

List<String> actualList = writer.deserializeFileList();
assertEquals(expectedList, actualList);
}

@Test
public void testValidateFileList() throws IOException {
TypeElement element1 = mock(TypeElement.class);
TypeElement element2 = mock(TypeElement.class);
when(elements.getTypeElement("test.Element1")).thenReturn(element1);
when(elements.getTypeElement("test.Element2")).thenReturn(null);

Gson gson = new Gson();
List<String> fileList = List.of("test.Element1", "test.Element2");
String json = gson.toJson(fileList);
Files.writeString(Paths.get("fileList.json"), json);

writer.validateFileList();

assertEquals(0, writer.allFoundProperties.size());
assertEquals(0, writer.roundFoundProperties.size());
assertEquals(false, writer.classesWritten);
}

@Test
public void testReadFileList() throws IOException {
TypeElement element1 = mock(TypeElement.class);
when(elements.getTypeElement("test.Element1")).thenReturn(element1);

Gson gson = new Gson();
List<String> fileList = List.of("test.Element1");
String json = gson.toJson(fileList);
Files.writeString(Paths.get("fileList.json"), json);

writer.readFileList();

assertEquals(1, writer.allFoundProperties.size());
assertEquals(element1, writer.allFoundProperties.keySet().iterator().next());
}
}
13 changes: 13 additions & 0 deletions test-data/src/test/java/test/event/TestEventFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.junit.jupiter.api.Test;
import test.event.lifecycle.NestedTest;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

class TestEventFactoryTest {
Expand Down Expand Up @@ -72,4 +75,14 @@ void testImplicitlyAllowedByAnnotation() {
Assertions.assertNotNull(TestEventFactory.createInclusiveEvent(List.of(), false));
}

@Test
void testJsonFileExists() throws IOException {
Assertions.assertTrue(Files.exists(Paths.get("fileList.json")));
}

@Test
void testJsonFileContents() throws IOException {
String jsonContent = Files.readString(Paths.get("fileList.json"));
Assertions.assertTrue(jsonContent.contains("test.event.lifecycle.NestedTest"));
}
}

0 comments on commit 539a8ab

Please sign in to comment.