implemented automatic generator and serializer tests

This commit is contained in:
JanikNex 2023-12-28 22:38:58 +01:00
parent a5253a4530
commit c87caff0cc
12 changed files with 336 additions and 0 deletions

View file

@ -72,6 +72,12 @@
<exclude>META-INF/**</exclude>
</excludes>
</filter>
<filter>
<artifact>commons-io:commons-io</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
@ -79,6 +85,28 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>

17
pom.xml
View file

@ -85,6 +85,12 @@
<exclude>META-INF/**</exclude>
</excludes>
</filter>
<filter>
<artifact>commons-io:commons-io</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
@ -99,6 +105,17 @@
<artifactId>picocli</artifactId>
<version>4.7.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore</artifactId>

View file

@ -0,0 +1,42 @@
import de.nexus.mmlcli.generator.EmfResourceBuilder;
import de.nexus.mmlcli.generator.SerializedDocument;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;
public class GeneratorTests {
private final static File TEST_DATA_FOLDER = new File("src/test/java/data");
static Stream<TestBundle> testBundleProvider() {
return TestUtils.getTestBundles(TEST_DATA_FOLDER).stream();
}
@Disabled
@ParameterizedTest
@MethodSource("testBundleProvider")
void testCorrectEcoreGeneration(TestBundle bundle, @TempDir File workingDir) throws IOException {
String serializedContent = Files.readString(bundle.getSerializedFile().toPath());
SerializedDocument[] result = Objects.requireNonNull(SerializedDocument.deserialize(serializedContent));
EmfResourceBuilder.buildEmfResources(result, bundle.getBundleName(), workingDir);
String ecoreContent = Files.readString(bundle.getEcoreFile().toPath());
Path generatedEcore = workingDir.toPath().resolve("model").resolve(bundle.getBundleName() + "_" + bundle.getBundleName() + ".ecore");
Assertions.assertTrue(generatedEcore.toFile().exists());
String generatedEcoreContent = Files.readString(generatedEcore);
Assertions.assertEquals(ecoreContent, generatedEcoreContent);
}
}

View file

@ -0,0 +1,25 @@
import java.io.File;
public class TestBundle {
private final String bundleName;
private final File ecoreFile;
private final File serializedFile;
public TestBundle(String bundleName, File ecoreFile, File serializedFile) {
this.bundleName = bundleName;
this.ecoreFile = ecoreFile;
this.serializedFile = serializedFile;
}
public String getBundleName() {
return bundleName;
}
public File getEcoreFile() {
return ecoreFile;
}
public File getSerializedFile() {
return serializedFile;
}
}

View file

@ -0,0 +1,51 @@
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class TestUtils {
public static ArrayList<TestBundle> getTestBundles(File dataFolder) {
if (dataFolder == null || !dataFolder.exists() || !dataFolder.isDirectory()) {
throw new IllegalArgumentException("Data folder is not directory or does not exist!");
}
Set<String> knownFilenames = new HashSet<>();
ArrayList<TestBundle> testBundles = new ArrayList<>();
for (File file : Objects.requireNonNull(dataFolder.listFiles())) {
String fileName = FilenameUtils.getBaseName(file.getName());
String extension = FilenameUtils.getExtension(file.getName());
if (knownFilenames.contains(fileName)) {
continue;
}
knownFilenames.add(fileName);
File ecoreFile, jsonFile;
if (extension.equals("ecore")) {
ecoreFile = file;
jsonFile = file.toPath().resolveSibling(fileName + ".json").toFile();
if (!jsonFile.exists() || jsonFile.isDirectory()) {
System.err.println("Skipping file due to missing json match: " + file.getAbsolutePath());
continue;
}
} else if (extension.equals("json")) {
ecoreFile = file.toPath().resolveSibling(fileName + ".ecore").toFile();
jsonFile = file;
if (!ecoreFile.exists() || ecoreFile.isDirectory()) {
System.err.println("Skipping file due to missing ecore match: " + file.getAbsolutePath());
continue;
}
} else {
System.err.println("Skipping file due to extension mismatch: " + file.getAbsolutePath());
continue;
}
testBundles.add(new TestBundle(fileName, ecoreFile, jsonFile));
}
return testBundles;
}
}

View file

@ -0,0 +1,51 @@
import de.nexus.mmlcli.entities.model.PackageEntity;
import de.nexus.mmlcli.generator.EmfResourceBuilder;
import de.nexus.mmlcli.generator.SerializedDocument;
import de.nexus.mmlcli.serializer.EmfResourceLoader;
import de.nexus.mmlcli.serializer.MmlSerializedGenerator;
import org.eclipse.emf.ecore.EPackage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;
public class TwoWayEcoreTests {
private final static File TEST_DATA_FOLDER = new File("src/test/java/data");
static Stream<TestBundle> testBundleProvider() {
return TestUtils.getTestBundles(TEST_DATA_FOLDER).stream();
}
@Disabled
@ParameterizedTest
@MethodSource("testBundleProvider")
void testCorrectTwowaySerialization(TestBundle bundle, @TempDir File workingDir) throws IOException {
if (!bundle.getBundleName().equals("HospitalExample")) {
return;
}
EPackage ePackage = EmfResourceLoader.loadEmfResources(bundle.getEcoreFile());
PackageEntity packageEntity = MmlSerializedGenerator.buildEntities(ePackage);
String generatedSerialization = MmlSerializedGenerator.serializeEntities(packageEntity, bundle.getEcoreFile().toURI());
SerializedDocument[] result = Objects.requireNonNull(SerializedDocument.deserialize(generatedSerialization));
EmfResourceBuilder.buildEmfResources(result, bundle.getBundleName(), workingDir);
String ecoreContent = Files.readString(bundle.getEcoreFile().toPath());
Path generatedEcore = workingDir.toPath().resolve("model").resolve(bundle.getBundleName() + "_" + bundle.getBundleName() + ".ecore");
Assertions.assertTrue(generatedEcore.toFile().exists());
String generatedEcoreContent = Files.readString(generatedEcore);
Assertions.assertEquals(ecoreContent, generatedEcoreContent);
}
}

View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="CarOwnership" nsURI="platform:/resource/CarOwnership/model/CarOwnership_CarOwnership.ecore"
nsPrefix="CarOwnership">
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="alter" ordered="false"
unique="false" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="kinder" ordered="false"
unique="false" upperBound="-1" eType="#//Person" resolveProxies="false" eOpposite="#//Person/eltern"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="eltern" ordered="false"
unique="false" lowerBound="2" eType="#//Person" resolveProxies="false" eOpposite="#//Person/kinder"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="autos" ordered="false"
unique="false" upperBound="-1" eType="#//Auto" resolveProxies="false" eOpposite="#//Auto/besitzer"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Auto">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="kennzeichen" ordered="false"
unique="false" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="marke" ordered="false"
unique="false" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="erstzulassung" ordered="false"
unique="false" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="baujahr" ordered="false"
unique="false" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="besitzer" ordered="false"
unique="false" lowerBound="1" eType="#//Person" resolveProxies="false" eOpposite="#//Person/autos"/>
</eClassifiers>
</ecore:EPackage>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="FerrymanProblem" nsURI="platform:/resource/FerrymanProblem/model/FerrymanProblem_FerrymanProblem.ecore"
nsPrefix="FerrymanProblem">
<eClassifiers xsi:type="ecore:EClass" name="Model">
<eStructuralFeatures xsi:type="ecore:EReference" name="leftBank" ordered="false"
unique="false" lowerBound="1" eType="#//Bank" resolveProxies="false"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="rightBank" ordered="false"
unique="false" lowerBound="1" eType="#//Bank" resolveProxies="false"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="subjects" ordered="false"
unique="false" upperBound="-1" eType="#//Subject" resolveProxies="false"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Bank"/>
<eClassifiers xsi:type="ecore:EClass" name="Subject" abstract="true">
<eStructuralFeatures xsi:type="ecore:EReference" name="isAt" ordered="false" unique="false"
lowerBound="1" eType="#//Bank" resolveProxies="false"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="FerryMan" eSuperTypes="#//Subject"/>
<eClassifiers xsi:type="ecore:EClass" name="Thing" abstract="true" eSuperTypes="#//Subject">
<eStructuralFeatures xsi:type="ecore:EReference" name="likesToEat" ordered="false"
unique="false" upperBound="-1" eType="#//Thing" resolveProxies="false"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Wolf" eSuperTypes="#//Thing"/>
<eClassifiers xsi:type="ecore:EClass" name="Goat" eSuperTypes="#//Thing"/>
<eClassifiers xsi:type="ecore:EClass" name="Cabbage" eSuperTypes="#//Thing"/>
</ecore:EPackage>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="HospitalExample" nsURI="platform:/resource/HospitalExample/model/HospitalExample_HospitalExample.ecore"
nsPrefix="HospitalExample">
<eClassifiers xsi:type="ecore:EClass" name="Hospital">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="reception" lowerBound="1"
eType="#//Reception" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="staff" ordered="false"
upperBound="-1" eType="#//Staff" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="department" upperBound="-1"
eType="#//Department" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Reception">
<eStructuralFeatures xsi:type="ecore:EReference" name="waits" upperBound="-1"
eType="#//Patient" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Staff" abstract="true">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="staffID" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
defaultValueLiteral="-1"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="works" eType="#//Department"
eOpposite="#//Department/staff"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Doctor" eSuperTypes="#//Staff">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="patientCapacity" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="caresfor" upperBound="-1"
eType="#//Patient" eOpposite="#//Patient/doctor"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Nurse" eSuperTypes="#//Staff">
<eStructuralFeatures xsi:type="ecore:EReference" name="responsible" eType="#//Room"
eOpposite="#//Room/nurses"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Department">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="dID" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
defaultValueLiteral="-1" iD="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="maxRoomCount" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="rooms" upperBound="-1"
eType="#//Room" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="staff" upperBound="-1"
eType="#//Staff" eOpposite="#//Staff/works"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Patient">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="patientID" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
defaultValueLiteral="-1"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="level" eType="#//Carelevel"
defaultValueLiteral="PENDING"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="doctor" lowerBound="1"
eType="#//Doctor" eOpposite="#//Doctor/caresfor"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Room">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="capacity" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="level" eType="#//Carelevel"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="lies" upperBound="-1" eType="#//Patient"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="nurses" upperBound="-1"
eType="#//Nurse" eOpposite="#//Nurse/responsible"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Carelevel">
<eLiterals name="WEAK"/>
<eLiterals name="MEDIUM" value="1"/>
<eLiterals name="STRONG" value="2"/>
<eLiterals name="PENDING" value="3"/>
</eClassifiers>
</ecore:EPackage>

File diff suppressed because one or more lines are too long