From 2ed8d2bce227658489b9ce12100c7fb09c059a91 Mon Sep 17 00:00:00 2001 From: Martin Armbruster Date: Thu, 27 Jul 2023 21:47:16 +0200 Subject: [PATCH] Extracted and generalized the output of Java models in the XMI format (#23). --- .../tools/mdsd/jamopp/test/OutputUtility.java | 83 +++++++++++++++++++ .../test/xmi/JavaXMISerializationTest.java | 59 +------------ 2 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 jamopp.tests/src/test/java/tools/mdsd/jamopp/test/OutputUtility.java diff --git a/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/OutputUtility.java b/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/OutputUtility.java new file mode 100644 index 00000000..6719a0e4 --- /dev/null +++ b/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/OutputUtility.java @@ -0,0 +1,83 @@ +package tools.mdsd.jamopp.test; + +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.xmi.XMIResource; + +import tools.mdsd.jamopp.model.java.containers.CompilationUnit; +import tools.mdsd.jamopp.model.java.containers.JavaRoot; +import tools.mdsd.jamopp.model.java.containers.Package; + +public class OutputUtility { + public record TransferResult(ResourceSet targetSet, Map sourceTargetMapping) {}; + + public static TransferResult transferToOutput(ResourceSet sourceSet, String outputFolder, String fileExtension, boolean includeAllResources) { + int emptyFileName = 0; + + ResourceSet targetSet = new ResourceSetImpl(); + HashMap srcTrgMap = new HashMap<>(); + + for (Resource javaResource : new ArrayList<>(sourceSet.getResources())) { + if (javaResource.getContents().isEmpty()) { + System.out.println("WARNING: Emtpy Resource: " + javaResource.getURI()); + continue; + } + if (!includeAllResources && !javaResource.getURI().isFile()) { + continue; + } + + JavaRoot root = (JavaRoot) javaResource.getContents().get(0); + String outputFileName = "ERROR"; + if (root instanceof CompilationUnit cu) { + outputFileName = cu.getNamespacesAsString().replace(".", File.separator) + File.separator; + if (cu.getClassifiers().size() > 0) { + outputFileName += cu.getClassifiers().get(0).getName(); + } else { + outputFileName += emptyFileName++; + } + } else if (root instanceof Package) { + outputFileName = root.getNamespacesAsString() + .replace(".", File.separator) + File.separator + "package-info"; + if (outputFileName.startsWith(File.separator)) { + outputFileName = outputFileName.substring(1); + } + } else if (root instanceof tools.mdsd.jamopp.model.java.containers.Module) { + outputFileName = root.getNamespacesAsString() + .replace(".", File.separator) + File.separator + "module-info"; + } else { + fail(); + } + + File outputFile = new File("." + File.separator + outputFolder + + File.separator + outputFileName); + URI fileURI = URI.createFileURI(outputFile.getAbsolutePath()).appendFileExtension(fileExtension); + + Resource targetResource = targetSet.createResource(fileURI); + if (targetResource instanceof XMIResource xmiResource) { + xmiResource.setEncoding(StandardCharsets.UTF_8.toString()); + } + targetResource.getContents().addAll(javaResource.getContents()); + srcTrgMap.put(javaResource, targetResource); + } + + for (Resource targetResource : targetSet.getResources()) { + try { + targetResource.save(targetSet.getLoadOptions()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + return new TransferResult(targetSet, srcTrgMap); + } +} diff --git a/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/xmi/JavaXMISerializationTest.java b/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/xmi/JavaXMISerializationTest.java index 324687d4..ba44bb19 100644 --- a/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/xmi/JavaXMISerializationTest.java +++ b/jamopp.tests/src/test/java/tools/mdsd/jamopp/test/xmi/JavaXMISerializationTest.java @@ -21,30 +21,24 @@ import static org.junit.jupiter.api.Assertions.fail; import java.io.File; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.Resource.Diagnostic; import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper; -import org.eclipse.emf.ecore.xmi.XMIResource; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import tools.mdsd.jamopp.model.java.containers.CompilationUnit; -import tools.mdsd.jamopp.model.java.containers.JavaRoot; -import tools.mdsd.jamopp.model.java.containers.Package; import tools.mdsd.jamopp.options.ParserOptions; import tools.mdsd.jamopp.parser.jdt.singlefile.JaMoPPJDTSingleFileParser; import tools.mdsd.jamopp.test.AbstractJaMoPPTests; +import tools.mdsd.jamopp.test.OutputUtility; public class JavaXMISerializationTest extends AbstractJaMoPPTests { @@ -88,56 +82,7 @@ public void testXMISerialization() throws Exception { } protected ResourceSet transferToXMI(ResourceSet sourceSet, boolean includeAllResources) throws Exception { - int emptyFileName = 0; - - ResourceSet targetSet = new ResourceSetImpl(); - - for (Resource javaResource : new ArrayList<>(sourceSet.getResources())) { - if (javaResource.getContents().isEmpty()) { - System.out.println("WARNING: Emtpy Resource: " + javaResource.getURI()); - continue; - } - if (!includeAllResources && !javaResource.getURI().isFile()) { - continue; - } - JavaRoot root = (JavaRoot) javaResource.getContents().get(0); - String outputFileName = "ERROR"; - if (root instanceof CompilationUnit) { - outputFileName = root.getNamespacesAsString().replace(".", File.separator) + File.separator; - CompilationUnit cu = (CompilationUnit) root; - if (cu.getClassifiers().size() > 0) { - outputFileName += cu.getClassifiers().get(0).getName(); - } else { - outputFileName += emptyFileName++; - } - - } else if (root instanceof Package) { - outputFileName = root.getNamespacesAsString() - .replace(".", File.separator) + File.separator + "package-info"; - if (outputFileName.startsWith(File.separator)) { - outputFileName = outputFileName.substring(1); - } - } else if (root instanceof tools.mdsd.jamopp.model.java.containers.Module) { - outputFileName = root.getNamespacesAsString() - .replace(".", File.separator) + File.separator + "module-info"; - } else { - fail(); - } - File outputFile = new File("." + File.separator + TEST_OUTPUT_FOLDER_NAME - + File.separator + outputFileName); - URI xmiFileURI = URI.createFileURI(outputFile.getAbsolutePath()).appendFileExtension("xmi"); - XMIResource xmiResource = (XMIResource) targetSet.createResource(xmiFileURI); - xmiResource.setEncoding(StandardCharsets.UTF_8.toString()); - xmiResource.getContents().addAll(javaResource.getContents()); - } - for (Resource xmiResource : targetSet.getResources()) { - try { - xmiResource.save(targetSet.getLoadOptions()); - } catch (Exception e) { - e.printStackTrace(); - } - } - return targetSet; + return OutputUtility.transferToOutput(sourceSet, TEST_OUTPUT_FOLDER_NAME, "xmi", includeAllResources).targetSet(); } protected void compare(ResourceSet rs) throws Exception {