Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON file persistence for event files #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion annotations/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
// This way we can still use the internal package, but not
// to external developers.
exports org.spongepowered.eventgen.annotations.internal to org.spongepowered.eventimplgen;
}

requires com.google.gson;
}
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ dependencies {

compileOnlyApi libs.jetbrainsAnnotations

api(libs.gson)

// Tests
testImplementation libs.assertj
testImplementation libs.joor
Expand All @@ -119,4 +121,4 @@ dependencies {
testImplementation libs.junit.params
testRuntimeOnly libs.junit.engine
testRuntimeOnly libs.junit.launcher
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ checkerQual = "3.26.0"
dagger = "2.52"
errorprone = "2.28.0"
junit = "5.10.2"
gson = "2.8.8"

[libraries]
autoService-annotations = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
Expand All @@ -19,6 +20,7 @@ javapoet = { module = "io.soabase.java-composer:java-composer", version = "1.0"
jetbrainsAnnotations = { module = "org.jetbrains:annotations", version = "24.1.0"}
jakartaInject = { module = "jakarta.inject:jakarta.inject-api", version = "2.0.1" }
javaxInject = { module = "javax.inject:javax.inject", version = "1" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }

# Tests
assertj = { module = "org.assertj:assertj-core", version = "3.26.0" }
Expand Down
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,133 @@
/*
* This file is part of Event Implementation Generator, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
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"));
}
}
Loading