From 0b2fba0bac2d166a2b05f3bf7bf44c85c6ce807a Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 13:44:47 +0100 Subject: [PATCH 01/11] started refactoring pcm <-> javaeditor tests to view-based --- .../java2pcm/Java2PcmClassifier.reactions | 10 +- .../META-INF/MANIFEST.MF | 3 +- .../ClassMappingTransformationTest.java | 224 ++-- .../java2pcm/Java2PcmTransformationTest.java | 961 ++---------------- .../PackageMappingTransformationTest.java | 169 +-- .../SynchronizationAwaitCallback.java | 5 - .../ClassMappingTransformationTest.java | 164 +++ .../ClassMethodMappingTransformationTest.java | 2 +- .../CompilationUnitManipulatorHelper.java | 2 +- .../FieldMappingTransformationTest.java | 2 +- .../InterfaceMappingTransformationTest.java | 2 +- ...oPPParameterMappingTransformationTest.java | 2 +- ...a2PcmPackageMappingTransformationTest.java | 2 +- .../legacy/Java2PcmTransformationTest.java | 941 +++++++++++++++++ .../MethodMappingTransformationTest.java | 2 +- .../PackageMappingTransformationTest.java | 98 ++ ...ypeReferenceMappingTransformationTest.java | 2 +- .../util/BlockingProgressMonitor.java | 84 ++ .../util/JavaEditorManipulationUtil.java | 197 ++++ .../javaeditor/util/JavaEditorView.java | 150 +++ .../javaeditor/util/JavaPcmViewFactory.java | 73 ++ .../javaeditor/util/JavaQueryUtil.java | 18 + .../pcmjava/javaeditor/util/PcmQueryUtil.java | 81 ++ .../pcmjava/pcm2java/Pcm2JavaTestUtils.java | 2 + 24 files changed, 2073 insertions(+), 1123 deletions(-) delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/SynchronizationAwaitCallback.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/ClassMethodMappingTransformationTest.java (95%) rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/CompilationUnitManipulatorHelper.java (97%) rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/FieldMappingTransformationTest.java (99%) rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/InterfaceMappingTransformationTest.java (97%) rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/JaMoPPParameterMappingTransformationTest.java (98%) rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/Java2PcmPackageMappingTransformationTest.java (93%) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/MethodMappingTransformationTest.java (98%) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java rename tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/{ => legacy}/TypeReferenceMappingTransformationTest.java (96%) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/BlockingProgressMonitor.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaPcmViewFactory.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java diff --git a/bundles/tools.vitruv.applications.pcmjava/src/tools/vitruv/applications/pcmjava/java2pcm/Java2PcmClassifier.reactions b/bundles/tools.vitruv.applications.pcmjava/src/tools/vitruv/applications/pcmjava/java2pcm/Java2PcmClassifier.reactions index d2271c5df..1e3fc1bea 100644 --- a/bundles/tools.vitruv.applications.pcmjava/src/tools/vitruv/applications/pcmjava/java2pcm/Java2PcmClassifier.reactions +++ b/bundles/tools.vitruv.applications.pcmjava/src/tools/vitruv/applications/pcmjava/java2pcm/Java2PcmClassifier.reactions @@ -16,6 +16,7 @@ import static tools.vitruv.applications.util.temporary.java.JavaTypeUtil.* import static tools.vitruv.applications.util.temporary.java.JavaPersistenceHelper.* import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimNotMany import static extension tools.vitruv.applications.pcmjava.PcmJavaTransformationUtil.isPackageFor +import tools.vitruv.dsls.reactions.runtime.helper.PersistenceHelper import "http://www.emftext.org/java" as java import "http://palladiosimulator.org/PalladioComponentModel/5.2" as pcm @@ -153,10 +154,11 @@ routine createRepository(java::Package javaPackage, String packageName, String n update { // If the package-info.java is not persisted, do it val packageUri = javaPackage.eResource.URI; - if (!URIUtil.existsResourceAtUri(packageUri)) { - val projectRelativeResourcePath = packageUri.segmentsList.tail.fold("", [a, b|a + "/" + b]) - persistProjectRelative(javaPackage, javaPackage, projectRelativeResourcePath) - } +// if (!URIUtil.existsResourceAtUri(packageUri)) { +// val projectRelativeResourcePath = PersistenceHelper.getRelativeUriPathFromElement(javaPackage) +//// val projectRelativeResourcePath = packageUri.segmentsList.tail.fold("", [a, b|a + "/" + b]) +// persistProjectRelative(javaPackage, javaPackage, projectRelativeResourcePath) +// } pcmRepository.entityName = packageName.toFirstUpper persistProjectRelative(javaPackage, pcmRepository, "model/" + pcmRepository.entityName + ".repository") diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF index 5cddf3e20..031144c58 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF @@ -17,8 +17,9 @@ Require-Bundle: org.apache.log4j, edu.kit.ipd.sdq.commons.util.emf, edu.kit.ipd.sdq.commons.util.java, tools.vitruv.testutils.vsum, + tools.vitruv.framework.views, tools.vitruv.applications.util.temporary, tools.vitruv.applications.pcmjava, tools.vitruv.applications.pcmjava.tests -Export-Package: tools.vitruv.applications.pcmjava.javaeditor.java2pcm +Export-Package: tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy Bundle-Vendor: vitruv.tools diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index afa48525f..8f8d4aa07 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -1,164 +1,118 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; -import static org.junit.jupiter.api.Assertions.fail; +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; -import org.eclipse.emf.ecore.EObject; +import java.util.stream.Collectors; + +import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.CollectionDataType; import org.palladiosimulator.pcm.repository.CompositeComponent; -import org.palladiosimulator.pcm.repository.CompositeDataType; import org.palladiosimulator.pcm.repository.Repository; import org.palladiosimulator.pcm.system.System; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; -public class ClassMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - +public class ClassMappingTransformationTest extends Java2PcmTransformationTest { /** * Class that in mapped package and same name as component + impl--> should be * the new implementing class for the component - * - * @throws Exception */ @Test - public void testAddComponentClassInPackageWithCorrespondingComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent bcForClass = super.addClassInSecondPackage(BasicComponent.class); - - super.assertRepositoryAndPCMName(repo, bcForClass, bc.getEntityName()); + void testAddClassInSecondPackageAsBasicComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, BasicComponent.class); + }); } - + @Test - public void testAddCompositeComponentClassInPackageWithCorrespondingCompositeComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CompositeComponent ccForClass = this.addClassInSecondPackage(CompositeComponent.class); - - super.assertRepositoryAndPCMName(repo, ccForClass, cc.getEntityName()); + void testAddClassInSecondPackageAsCompositeComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME, CompositeComponent.class); + }); } - + @Test - public void testAddSystemClassInPackageWithCorrespondingSystem() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final System pcmSystem = super.addSecondPackageCorrespondsToSystem(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); - final System systemForClass = super.addClassInSecondPackage(System.class); - - super.assertPCMNamedElement(systemForClass, pcmSystem.getEntityName()); + void testAddClassInSecondPackageAsSystem() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.SYSTEM_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.SYSTEM_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.SYSTEM_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + }); + + validatePcmView(view -> { + System system = claimSingleSystem(view); + assertEquals(Pcm2JavaTestUtils.SYSTEM_NAME, system.getEntityName(), "system has wrong name"); + }); } - + /** * Test ii) class in non corresponding package --> should not be mapped to a * Basic Component - * - * @throws Throwable */ @Test - public void testAddClassInPackageWithoutCorrespondingComponent() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); - try { - final EObject eObject = super.addClassInPackage(this.secondPackage, EObject.class); - fail("The class should not have any correspondences, but it has a correspondence to eObject: " + eObject); - } catch (final RuntimeException re) { - // expected exception - } - } - - @Test - public void testAddBasicComponentClassInPackageWithoutCorrespondence() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent newBc = super.addClassInSecondPackage(BasicComponent.class); - - super.assertRepositoryAndPCMName(repo, newBc, newBc.getEntityName()); - } - - @Test - public void testAddCompositeComponentClassInPackageWithoutCorrespondence() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CompositeComponent cc = super.addClassInSecondPackage(CompositeComponent.class); - - super.assertRepositoryAndPCMName(repo, cc, cc.getEntityName()); - } - - @Test - public void testAddSystemClassInPackageWithoutCorrespondence() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); - final System pcmSystem = super.addClassInSecondPackage(System.class); - - this.assertPCMNamedElement(pcmSystem, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); - } - - @Test - public void testAddCompositeDatatypeClassInDatatypePackage() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - - final CompositeDataType cdt = this.addClassThatCorrespondsToCompositeDatatype(); - - this.assertRepositoryAndPCMNameForDatatype(repo, cdt, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); + void addClassInSecondPackageAsNone() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertTrue(repository.getComponents__Repository().isEmpty(), "there must not exist any contained Component"); + assertTrue(view.getRootObjects(System.class).isEmpty(), "there must not exist any System"); + }); } - - @Test - public void testAddCollectionDatatypeClassInDatatypePackage() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CollectionDataType collection = super.addClassInPackage(this.getDatatypesPackage(), - CollectionDataType.class); - - this.assertRepositoryAndPCMNameForDatatype(repo, collection, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); - } - - @Test - public void testAddClassInDatatypePackage() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - try { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); - final EObject eObject = super.addClassInPackage(this.getDatatypesPackage(), EObject.class); - fail("The class should not have any datatype correspondences, but it has a correspondence to eObject: " - + eObject); - } catch (final RuntimeException re) { - // expected Exception - } - } - - @Test - public void testRenameBasicComponentClass() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - this.addSecondPackageCorrespondsWithoutCorrespondences(); - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent basicComponent = this.addClassInSecondPackage(BasicComponent.class); - - final BasicComponent newBasicComponent = super.renameClassifierWithName(basicComponent.getEntityName() + "Impl", - Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME, BasicComponent.class); - - this.assertRepositoryAndPCMName(repo, newBasicComponent, - Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME); - this.assertFilesOnlyForEObjects(newBasicComponent); - } - + + //TODO: add more } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java index 27fa03eb8..c5769c422 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java @@ -1,939 +1,98 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; -import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createPlatformResourceURI; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import static tools.vitruv.applications.util.temporary.java.JavaQueryUtil.getNameFromJaMoPPType; - -import java.io.IOException; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; +import java.util.function.Consumer; -import org.apache.log4j.Logger; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; -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.jdt.core.IBuffer; -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IField; -import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.IMethod; -import org.eclipse.jdt.core.IPackageFragment; -import org.eclipse.jdt.core.IPackageFragmentRoot; -import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.refactoring.IJavaRefactorings; -import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; -import org.eclipse.jdt.internal.core.manipulation.StubUtility; -import org.eclipse.ltk.core.refactoring.Change; -import org.eclipse.ltk.core.refactoring.Refactoring; -import org.eclipse.ltk.core.refactoring.RefactoringContribution; -import org.eclipse.ltk.core.refactoring.RefactoringCore; -import org.eclipse.ltk.core.refactoring.RefactoringStatus; -import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.ReplaceEdit; -import org.eclipse.text.edits.TextEdit; -import org.emftext.language.java.classifiers.Classifier; -import org.emftext.language.java.classifiers.ConcreteClassifier; -import org.emftext.language.java.classifiers.Interface; -import org.emftext.language.java.containers.CompilationUnit; import org.emftext.language.java.containers.ContainersFactory; -import org.emftext.language.java.containers.ContainersPackage; -import org.emftext.language.java.containers.JavaRoot; import org.emftext.language.java.containers.Package; -import org.emftext.language.java.members.ClassMethod; -import org.emftext.language.java.members.Field; -import org.emftext.language.java.members.Member; -import org.emftext.language.java.members.Method; -import org.emftext.language.java.modifiers.AnnotableAndModifiable; -import org.emftext.language.java.types.TypeReference; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.palladiosimulator.pcm.core.entity.NamedElement; -import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.CollectionDataType; -import org.palladiosimulator.pcm.repository.CompositeComponent; -import org.palladiosimulator.pcm.repository.CompositeDataType; -import org.palladiosimulator.pcm.repository.DataType; -import org.palladiosimulator.pcm.repository.OperationInterface; -import org.palladiosimulator.pcm.repository.OperationProvidedRole; -import org.palladiosimulator.pcm.repository.OperationSignature; -import org.palladiosimulator.pcm.repository.Parameter; -import org.palladiosimulator.pcm.repository.PrimitiveDataType; -import org.palladiosimulator.pcm.repository.Repository; -import org.palladiosimulator.pcm.repository.RepositoryComponent; -import org.palladiosimulator.pcm.seff.ResourceDemandingSEFF; -import org.palladiosimulator.pcm.system.System; +import org.junit.jupiter.api.function.ThrowingConsumer; -import edu.kit.ipd.sdq.commons.util.org.eclipse.core.resources.IProjectUtil; -import edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil; -import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmChangePropagationSpecification; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaEditorView; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaPcmViewFactory; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaChangePropagationSpecification; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; +import tools.vitruv.applications.util.temporary.java.JavaPersistenceHelper; import tools.vitruv.applications.util.temporary.java.JavaSetup; -import tools.vitruv.change.composite.description.VitruviusChange; -import tools.vitruv.change.propagation.ChangePropagationMode; -import tools.vitruv.framework.views.changederivation.DefaultStateBasedChangeResolutionStrategy; -import tools.vitruv.framework.views.changederivation.StateBasedChangeResolutionStrategy; -import tools.vitruv.testutils.LegacyVitruvApplicationTest; +import tools.vitruv.change.propagation.ChangePropagationSpecification; +import tools.vitruv.framework.views.CommittableView; +import tools.vitruv.framework.views.View; import tools.vitruv.testutils.TestProject; -import tools.vitruv.testutils.views.UriMode; -import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.ecore.resource.ResourceSetUtil.loadOrCreateResource; - -/** - * Test class that contains utility methods that can be used by JaMoPP2PCM - * transformation tests - * - */ -@SuppressWarnings("restriction") -public abstract class Java2PcmTransformationTest extends LegacyVitruvApplicationTest { - private static final Logger logger = Logger.getLogger(Java2PcmTransformationTest.class); - private static final int MAXIMUM_EDIT_WAITING_TIME = 5000; - - protected Package mainPackage; - protected Package secondPackage; - - private IProject testEclipseProject; - - private Map oldToNewURIsOfModifiedResources; - private volatile boolean isWaitingForFinishingsEdits = false; - - protected IProject getCurrentTestProject() { - return testEclipseProject; - } - - /* - * JDT functionality requires platform URIs. - */ - @Override - protected UriMode getUriMode() { - return UriMode.PLATFORM_URIS; - } - - private void refreshProject() { - try { - testEclipseProject.refreshLocal(IResource.DEPTH_INFINITE, null); - } catch (CoreException e) { - String message = "Could not refresh project " + testEclipseProject.getName(); - logger.error(message, e); - } - } +import tools.vitruv.testutils.ViewBasedVitruvApplicationTest; +public class Java2PcmTransformationTest extends ViewBasedVitruvApplicationTest { + protected JavaPcmViewFactory viewFactory; + @BeforeAll - public static void setupJavaFactories() { + public static final void setupJavaFactories() { JavaSetup.prepareFactories(); } @BeforeEach - public void setupJavaProject(@TestProject Path testProjectFolder) { - String projectName = testProjectFolder.getFileName().toString(); - testEclipseProject = IProjectUtil.createProjectAt(projectName, testProjectFolder); - IProjectUtil.configureAsJavaProject(testEclipseProject); + public final void setupJavaClasspath() { JavaSetup.resetClasspathAndRegisterStandardLibrary(); } - + @BeforeEach - public void disableTransitiveChangePropagation() { - this.getVirtualModel().setChangePropagationMode(ChangePropagationMode.SINGLE_STEP); - } - - @BeforeEach - public void cleanupModifiedResources() { - this.oldToNewURIsOfModifiedResources = new HashMap<>(); - } - - @AfterEach - public void closeJavaProject() throws JavaModelException, CoreException { - getIJavaProject().close(); - } - - private void saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(final URI resourceURI, - final URI newResourceURI) { - oldToNewURIsOfModifiedResources.put(resourceURI, newResourceURI); - // Access the resource such that the initial state is present in the test view - resourceAt(resourceURI); + public final void setupViewFactory(@TestProject Path testProjectFolder) { + viewFactory = new JavaPcmViewFactory(testProjectFolder, getVirtualModel()); } - - private void saveCurrentStateOfResourceAndRegisterForSynchronization(final URI resourceURI) { - saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(resourceURI, resourceURI); + + @Override + protected boolean enableTransitiveCyclicChangePropagation() { + return false; } - - private Resource loadResourceIndependentFromView(final URI resourceURI) { - ResourceSet resourceSet = new ResourceSetImpl(); - Resource resource = loadOrCreateResource(resourceSet, resourceURI); - return resource; + + @Override + protected Iterable getChangePropagationSpecifications() { + return List.of(new Java2PcmChangePropagationSpecification(), new Pcm2JavaChangePropagationSpecification()); } - - protected Package createPackageWithPackageInfo(final String... namespace) throws IOException { - String packageFile = String.join("/", namespace); - packageFile = packageFile + "/package-info.java"; + + /* + * JDT functionality requires platform URIs. + */ +// @Override +// protected UriMode getUriMode() { +// return UriMode.PLATFORM_URIS; +// } + + protected void createRepositoryPackage() throws Exception { + changeJavaView(view -> { + createPackageWithPackageInfo(view, new String[] { Pcm2JavaTestUtils.REPOSITORY_NAME }); + }); + } + + protected Package createPackageWithPackageInfo(View view, final String... namespace) { final Package jaMoPPPackage = ContainersFactory.eINSTANCE.createPackage(); final List namespaceList = Arrays.asList(namespace); jaMoPPPackage.setName(namespaceList.get(namespaceList.size() - 1)); jaMoPPPackage.getNamespaces().addAll(namespaceList.subList(0, namespaceList.size() - 1)); - URI createPackageURI = getUri(Path.of(getPathInProjectForSrcFile(packageFile))); - saveCurrentStateOfResourceAndRegisterForSynchronization(createPackageURI); - final Resource resource = loadResourceIndependentFromView(createPackageURI); - resource.getContents().add(jaMoPPPackage); - resource.save(null); - propagateChanges(); + + URI createPackageURI = getUri(Path.of(JavaPersistenceHelper.buildJavaFilePath(jaMoPPPackage))); + view.registerRoot(jaMoPPPackage, createPackageURI); return jaMoPPPackage; } - - private ICompilationUnit createCompilationUnit(IPackageFragment packageFragment, String cuName) - throws JavaModelException { - URI createdCompilationUnitURI = getURIForElementInPackage(packageFragment, cuName); - saveCurrentStateOfResourceAndRegisterForSynchronization(createdCompilationUnitURI); - ICompilationUnit compilationUnit = packageFragment.createCompilationUnit(cuName + ".java", "", false, null); - return compilationUnit; - } - - public void editCompilationUnit(final ICompilationUnit cu, final TextEdit... edits) throws JavaModelException { - saveCurrentStateOfResourceAndRegisterForSynchronization(URIUtil.createPlatformResourceURI(cu.getResource())); - cu.becomeWorkingCopy(new NullProgressMonitor()); - for (final TextEdit edit : edits) { - cu.applyTextEdit(edit, null); - } - cu.reconcile(ICompilationUnit.NO_AST, false, null, null); - cu.commitWorkingCopy(false, new NullProgressMonitor()); - cu.discardWorkingCopy(); - cu.save(new NullProgressMonitor() { - public void done() { - Java2PcmTransformationTest.this.isWaitingForFinishingsEdits = false; - Java2PcmTransformationTest.this.notifyAll(); - } - }, true); - propagateChanges(); - } - - public void propagateChanges() { - logger.debug("Starting to wait for edits to be finished"); - try { - if (isWaitingForFinishingsEdits) { - wait(MAXIMUM_EDIT_WAITING_TIME); - if (isWaitingForFinishingsEdits) { - logger.error("Waiting for edits timed out in project " + testEclipseProject.getName()); - fail("Waiting for edits timed out"); - } - } - } catch (InterruptedException e) { - fail("An interrupt occurred unexpectedly"); - } - refreshProject(); - StateBasedChangeResolutionStrategy changeResolutionStrategy = new DefaultStateBasedChangeResolutionStrategy(); - for (Entry modifiedResourceURI : oldToNewURIsOfModifiedResources.entrySet()) { - Resource currentResource = loadResourceIndependentFromView(modifiedResourceURI.getValue()); - VitruviusChange change = changeResolutionStrategy.getChangeSequenceBetween(currentResource, - resourceAt(modifiedResourceURI.getKey())); - VitruviusChange unresolvedChange = change.unresolve(); - record(resourceAt(modifiedResourceURI.getKey()).getResourceSet(), - resourceSet -> unresolvedChange.resolveAndApply(resourceSet)); - } - oldToNewURIsOfModifiedResources.clear(); - propagate(); - disposeViewResources(); - } - - protected Repository addRepoContractsAndDatatypesPackage() throws IOException, CoreException { - this.mainPackage = this.createPackageWithPackageInfo(new String[] { Pcm2JavaTestUtils.REPOSITORY_NAME }); - final Repository repo = claimOne(getCorrespondingEObjects(this.mainPackage, Repository.class)); - return repo; + + // === Shortcuts === + + protected void changeJavaView(Consumer modelModification) throws Exception { + viewFactory.changeJavaView(modelModification); } - - protected BasicComponent addSecondPackageCorrespondsToBasicComponent() throws Throwable { - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - return this.createSecondPackage(BasicComponent.class, Pcm2JavaTestUtils.REPOSITORY_NAME, - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + + protected void changeJavaEditorView(ThrowingConsumer modelModification) throws Exception { + viewFactory.changeJavaEditorView(modelModification); } - - protected T createSecondPackage(final Class correspondingType, final String... namespace) - throws Throwable { - this.secondPackage = this.createPackageWithPackageInfo(namespace); - return claimOne(getCorrespondingEObjects(this.secondPackage, correspondingType)); + + protected void changeJavaEditorViewThrowing(ThrowingConsumer modelModification) throws Exception { + } - - private void createSecondPackageWithoutCorrespondence(final String... namespace) throws Throwable { - this.secondPackage = this.createPackageWithPackageInfo(namespace); - } - - protected Package renamePackage(final Package packageToRename, final String newName) throws CoreException { - final Resource resource = packageToRename.eResource(); - final IFile iFile = URIUtil.getIFileForEMFUri(resource.getURI()); - IPath iPath = iFile.getProjectRelativePath(); - iPath = iPath.removeLastSegments(1); - final String newQualifiedName = packageToRename.getNamespacesAsString() + newName; - final IFolder iFolder = iFile.getProject().getFolder(iPath); - final IJavaElement javaPackage = JavaCore.create(iFolder); - String packageFile = String.join("/", packageToRename.getNamespaces()); - packageFile = packageFile + "/" + newName + "/package-info.java"; - saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(resource.getURI(), - getUri(Path.of(getPathInProjectForSrcFile(packageFile)))); - this.refactorRenameJavaElement(newQualifiedName, javaPackage, IJavaRefactorings.RENAME_PACKAGE); - final Package newPackage = this.findJaMoPPPackageWithName(newQualifiedName); - return newPackage; - } - - private void refactorRenameJavaElement(final String newName, final IJavaElement iJavaElement, - final String refactorRenameActionName) throws CoreException { - final RefactoringContribution refacContrib = RefactoringCore - .getRefactoringContribution(refactorRenameActionName); - final RefactoringStatus status = new RefactoringStatus(); - final RenameJavaElementDescriptor desc = (RenameJavaElementDescriptor) refacContrib.createDescriptor(); - desc.setUpdateReferences(true); - desc.setJavaElement(iJavaElement); - desc.setNewName(newName); - final NullProgressMonitor monitor = new NullProgressMonitor(); - final Refactoring refactoring = desc.createRefactoring(status); - refactoring.checkInitialConditions(monitor); - refactoring.checkFinalConditions(monitor); - final Change change = refactoring.createChange(monitor); - change.perform(monitor); - propagateChanges(); - } - - protected T renameClassifierWithName(final String entityName, final String newName, - final Class type) throws Throwable { - try { - final ICompilationUnit cu = CompilationUnitManipulatorHelper - .findICompilationUnitWithClassName(entityName + ".java", this.getCurrentTestProject()); - final int offset = cu.getBuffer().getContents().indexOf(entityName); - if (cu.getBuffer() instanceof IBuffer.ITextEditCapability) { - logger.info(cu.getBuffer()); - } - final ReplaceEdit edit = new ReplaceEdit(offset, entityName.length(), newName); - editCompilationUnit(cu, edit); - final URI uri = createPlatformResourceURI(cu.getResource()); - final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); - return claimOne(getCorrespondingEObjects(jaMoPPClass, type)); - } catch (final Throwable e) { - logger.warn(e.getMessage()); - } - return null; - - } - - private Package findJaMoPPPackageWithName(final String newName) throws JavaModelException { - final IJavaProject javaProject = JavaCore.create(this.getCurrentTestProject()); - for (final IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) { - final IJavaElement[] children = packageFragmentRoot.getChildren(); - for (final IJavaElement iJavaElement : children) { - if (iJavaElement instanceof IPackageFragment) { - final IPackageFragment fragment = (IPackageFragment) iJavaElement; - if (fragment.getElementName().equals(newName)) { - final URI uri = this.getURIForElementInPackage(fragment, "package-info"); - final Package jaMoPPPackage = this.getJaMoPPRootForURI(uri); - return jaMoPPPackage; - } - } - } - } - throw new RuntimeException("Could not find a compilation unit with name " + newName); - } - - private IPackageFragmentRoot getIJavaProject() throws CoreException { - final IProject project = this.getCurrentTestProject(); - final IJavaProject javaProject = JavaCore.create(project); - final IFolder sourceFolder = project.getFolder("src"); - if (!sourceFolder.exists()) { - final boolean force = true; - final boolean local = true; - sourceFolder.create(force, local, new NullProgressMonitor()); - } - final IPackageFragmentRoot packageFragment = javaProject.getPackageFragmentRoot(sourceFolder); - return packageFragment; - } - - private String getPathInProjectForSrcFile(final String srcFilePath) { - return "src/" + srcFilePath; - } - - protected T addClassInSecondPackage(final Class classOfCorrespondingObject) - throws Throwable { - final T createdEObject = this.addClassInPackage(this.secondPackage, classOfCorrespondingObject); - return createdEObject; - } - - protected T addClassInPackage(final Package packageForClass, - final Class classOfCorrespondingObject) throws Throwable { - final String implementingClassName = Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME; - return this.addClassInPackage(packageForClass, classOfCorrespondingObject, implementingClassName); - } - - protected T addClassInPackage(final Package packageForClass, - final Class classOfCorrespondingObject, final String implementingClassName) - throws CoreException, InterruptedException { - final Classifier jaMoPPClass = this.createClassInPackage(packageForClass, implementingClassName); - final Iterable eObjectsByType = getCorrespondingEObjects(jaMoPPClass, classOfCorrespondingObject); - return claimOne(eObjectsByType); - } - - protected Classifier createClassInPackage(final Package packageForClass, final String implementingClassName) - throws CoreException { - String packageName = packageForClass.getName(); - String packageNamespace = packageForClass.getNamespacesAsString() + packageName; - return createClassInPackage(implementingClassName, packageNamespace); - } - - protected Classifier createClassInPackage(final String implementingClassName, String packageNamespace) - throws CoreException { - final IPackageFragment packageFragment = this.getPackageFragment(packageNamespace); - createEmptyClass(packageFragment, implementingClassName); - final URI uri = this.getURIForElementInPackage(packageFragment, implementingClassName); - final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); - return jaMoPPClass; - } - - private URI getURIForElementInPackage(final IPackageFragment packageFragment, final String elementName) { - String uriString = packageFragment.getResource().getFullPath().toString() + "/" + elementName + ".java"; - return URI.createPlatformResourceURI(uriString, true); - } - - private IPackageFragment getPackageFragment(String packageNamespace) throws CoreException { - final IPackageFragmentRoot packageRoot = this.getIJavaProject(); - for (final IJavaElement javaElement : packageRoot.getChildren()) { - if (javaElement instanceof IPackageFragment && javaElement.getElementName().equals(packageNamespace)) { - return (IPackageFragment) javaElement; - } - } - throw new RuntimeException("No packageFragment found for JaMoPP package " + packageNamespace); - } - - protected ConcreteClassifier getJaMoPPClassifierForURI(final URI uri) { - final CompilationUnit cu = this.getJaMoPPRootForURI(uri); - final Classifier jaMoPPClassifier = cu.getClassifiers().get(0); - return (ConcreteClassifier) jaMoPPClassifier; - } - - private T getJaMoPPRootForURI(final URI uri) { - final Resource resource = new ResourceSetImpl().getResource(uri, true); - // unchecked is OK for the test. - @SuppressWarnings("unchecked") - final T javaRoot = (T) resource.getContents().get(0); - return javaRoot; - } - - protected CompositeComponent addSecondPackageCorrespondsToCompositeComponent() throws Throwable { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - return this.createSecondPackage(CompositeComponent.class, Pcm2JavaTestUtils.REPOSITORY_NAME, - Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); - } - - protected org.palladiosimulator.pcm.system.System addSecondPackageCorrespondsToSystem() throws Throwable { - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); - return this.createSecondPackage(System.class, Pcm2JavaTestUtils.SYSTEM_NAME); - } - - protected void addSecondPackageCorrespondsWithoutCorrespondences() throws Throwable { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); - this.createSecondPackageWithoutCorrespondence(Pcm2JavaTestUtils.REPOSITORY_NAME, - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - } - - protected void assertRepositoryAndPCMName(final Repository repo, final RepositoryComponent repoComponent, - final String expectedName) throws Throwable { - - assertEquals(repo.getId(), repoComponent.getRepository__RepositoryComponent().getId(), - "Repository of compoennt is not the repository: " + repo); - - this.assertPCMNamedElement(repoComponent, expectedName); - } - - protected void assertResourceAndFileForEObjects(final EObject... eObjects) throws Throwable { - for (final EObject eObject : eObjects) { - final Resource eResource = eObject.eResource(); - assertNotNull(eResource, "Resource of eObject " + eObject + " is null"); - final IFile iFile = URIUtil.getIFileForEMFUri(eResource.getURI()); - assertTrue(iFile.exists(), "No IFile for eObject " + eObject + " in resource " + eResource + " found."); - } - } - - protected void assertFilesOnlyForEObjects(final EObject... eObjects) throws Throwable { - final Set fullFilePaths = new HashSet(); - for (final EObject eObject : eObjects) { - final IFile iFile = URIUtil.getIFileForEMFUri(eObject.eResource().getURI()); - fullFilePaths.add(iFile.getFullPath().toString()); - } - final IFolder folder = this.getCurrentTestProject().getFolder("model"); - final List foundAdditionalFiles = new ArrayList<>(); - for (final IResource iResource : folder.members()) { - final String iResourcePath = iResource.getFullPath().toString(); - if (!fullFilePaths.contains(iResourcePath)) { - foundAdditionalFiles.add(iResourcePath); - } - } - if (0 < foundAdditionalFiles.size()) { - final StringBuilder failMsg = new StringBuilder("Found addtional files in model folder: "); - foundAdditionalFiles.forEach(str -> failMsg.append(str).append(", ")); - fail(failMsg.toString()); - } - } - - protected void assertRepositoryAndPCMNameForDatatype(final Repository repo, final DataType dt, - final String expectedName) throws Throwable { - - assertEquals(repo.getId(), dt.getRepository__DataType().getId(), - "Repository of compoennt is not the repository: " + repo); - if (dt instanceof CompositeDataType) { - this.assertPCMNamedElement((CompositeDataType) dt, expectedName); - } else if (dt instanceof CollectionDataType) { - this.assertPCMNamedElement((CollectionDataType) dt, expectedName); - } else { - throw new RuntimeException("Primitive data types should not have a correspondence to classes"); - } + + protected void validatePcmView(Consumer viewValidation) throws Exception { + viewFactory.validatePcmView(viewValidation); } - - protected void assertPCMNamedElement(final NamedElement pcmNamedElement, final String expectedName) - throws Throwable { - assertEquals(expectedName, pcmNamedElement.getEntityName(), - "The name of pcm named element is not " + expectedName); - this.assertResourceAndFileForEObjects(pcmNamedElement); - } - - protected void assertPcmParameter(final Parameter pcmParameter, final String expectedName) throws Throwable { - assertEquals(expectedName, pcmParameter.getParameterName(), "The name of pcm parameter is not " + expectedName); - this.assertResourceAndFileForEObjects(pcmParameter); - } - - protected OperationInterface addInterfaceInContractsPackage() throws Throwable { - return this.createInterfaceInPackage("contracts"); - } - - private OperationInterface createInterfaceInPackage(final String packageName) - throws Throwable, CoreException, InterruptedException { - final String interfaceName = Pcm2JavaTestUtils.INTERFACE_NAME; - return this.createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence(packageName, interfaceName); - } - - protected OperationInterface createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence( - final String packageName, final String interfaceName) throws CoreException { - Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); - return this.createInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), - interfaceName, true); - } - - protected ConcreteClassifier createInterfaceInPackageBasedOnJaMoPPPackageWithoutCorrespondence( - final String packageName, final String interfaceName) - throws Throwable, CoreException, InterruptedException { - Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); - return this.createJaMoPPInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), - interfaceName); - } - - protected OperationInterface createInterfaceInPackage(String packageNamespace, final String interfaceName, - boolean claimOne) throws CoreException { - final Classifier jaMoPPIf = createJaMoPPInterfaceInPackage(packageNamespace, interfaceName); - Iterable correspondingOpInterfaces = getCorrespondingEObjects(jaMoPPIf, - OperationInterface.class); - if (claimOne) { - return claimOne(correspondingOpInterfaces); - } - if (null == correspondingOpInterfaces || !correspondingOpInterfaces.iterator().hasNext()) { - return null; - } - logger.warn("More than one corresponding interfaces found for interface " + jaMoPPIf + ". Returning the first"); - return correspondingOpInterfaces.iterator().next(); - } - - protected ConcreteClassifier createJaMoPPInterfaceInPackage(String packageNamespace, final String interfaceName) - throws CoreException { - final IPackageFragment packageFragment = this.getPackageFragment(packageNamespace); - createEmptyInterface(packageFragment, interfaceName); - final URI uri = this.getURIForElementInPackage(packageFragment, interfaceName); - final ConcreteClassifier jaMoPPIf = this.getJaMoPPClassifierForURI(uri); - - return jaMoPPIf; - } - - private void createEmptyCompilationUnit(IPackageFragment packageFragment, String typeName, String cuName) - throws JavaModelException { - String lineDelimiter = null; - lineDelimiter = StubUtility.getLineDelimiterUsed(packageFragment.getJavaProject()); - ICompilationUnit compilationUnit = createCompilationUnit(packageFragment, cuName); - InsertEdit edit = new InsertEdit(0, "package " + packageFragment.getElementName() + ";" + lineDelimiter - + lineDelimiter + "public " + typeName + " " + cuName + " { }"); - editCompilationUnit(compilationUnit, edit); - } - - protected void createEmptyInterface(IPackageFragment packageFragment, String interfaceName) - throws JavaModelException { - createEmptyCompilationUnit(packageFragment, "interface", interfaceName); - } - - protected void createEmptyClass(IPackageFragment packageFragment, String className) throws JavaModelException { - createEmptyCompilationUnit(packageFragment, "class", className); - } - - protected OperationInterface addInterfaceInSecondPackageWithCorrespondence(final String packageName) - throws Throwable { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); - return this.createInterfaceInPackage(packageName); - } - - protected EObject addInterfaceInPackageWithoutCorrespondence(final String packageName) throws Throwable { - this.getUserInteraction().addNextSingleSelection( - Java2PcmUserSelection.SELECT_DONT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); - Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); - return this.createInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), - "I" + packageName, false); - } - - protected Package getPackageWithNameFromCorrespondenceModel(final String name) throws CoreException { - final Iterable packages = getCorrespondingEObjects(ContainersPackage.Literals.PACKAGE, Package.class); - for (final Package currentPackage : packages) { - if (currentPackage.getName().equals(name)) { - return currentPackage; - } - } - throw new RuntimeException("Could not find package with name " + name); - } - - protected OperationSignature addMethodToInterfaceWithCorrespondence(final String interfaceName) throws Throwable { - final String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; - return this.addMethodToInterfaceWithCorrespondence(interfaceName, methodName); - } - - protected OperationSignature addMethodToInterfaceWithCorrespondence(final String interfaceName, - final String methodName) throws Throwable, JavaModelException { - final String methodString = "\nvoid " + methodName + "();\n"; - final ICompilationUnit cu = addMethodToCompilationUnit(interfaceName, methodString, - this.getCurrentTestProject()); - return this.findOperationSignatureForJaMoPPMethodInCompilationUnit(methodName, interfaceName, cu); - } - - protected ResourceDemandingSEFF addClassMethodToClassThatOverridesInterfaceMethod(final String className, - final String methodName) throws Throwable { - final String methodString = "\n\tpublic void " + methodName + " () {\n\t}\n"; - final ICompilationUnit icu = addMethodToCompilationUnit(className, methodString, this.getCurrentTestProject()); - final Method jaMoPPMethod = this.findJaMoPPMethodInICU(icu, methodName); - final ClassMethod classMethod = (ClassMethod) jaMoPPMethod; - return claimOne(getCorrespondingEObjects(classMethod, ResourceDemandingSEFF.class)); - } - - protected OperationSignature findOperationSignatureForJaMoPPMethodInCompilationUnit(final String methodName, - final String interfaceName, final ICompilationUnit cu) throws Throwable { - final URI uri = createPlatformResourceURI(cu.getResource()); - final Classifier classifier = this.getJaMoPPClassifierForURI(uri); - final Interface jaMoPPInterface = (Interface) classifier; - for (final Method jaMoPPMethod : jaMoPPInterface.getMethods()) { - if (jaMoPPMethod.getName().equals(methodName)) { - return claimOne(getCorrespondingEObjects(jaMoPPMethod, OperationSignature.class)); - } - } - logger.warn("No JaMoPP method with name " + methodName + " found in " + interfaceName); - return null; - } - - protected OperationSignature renameMethodInClassWithName(final String className, final String methodName) - throws Throwable { - final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IMethod iMethod = cu.getType(className).getMethod(methodName, null); - final int offset = iMethod.getNameRange().getOffset(); - final int length = iMethod.getNameRange().getLength(); - final String newMethodName = methodName + Pcm2JavaTestUtils.RENAME; - final ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, newMethodName); - editCompilationUnit(cu, replaceEdit); - return this.findOperationSignatureForJaMoPPMethodInCompilationUnit(newMethodName, className, cu); - } - - protected void assertDataTypeName(final TypeReference typeReference, final DataType pcmDataType) { - final String jaMoPPTypeName = getNameFromJaMoPPType(typeReference); - final String pcmTypeName = this.getNameFromPCMDataType(pcmDataType); - assertEquals(jaMoPPTypeName, pcmTypeName, "The name of the PCM datatype does not equal the JaMoPP type name"); - } - - protected String getNameFromPCMDataType(final DataType pcmDataType) { - if (null == pcmDataType) { - return "void"; - } else if (pcmDataType instanceof CollectionDataType) { - return ((CollectionDataType) pcmDataType).getEntityName(); - } else if (pcmDataType instanceof CompositeDataType) { - return ((CompositeDataType) pcmDataType).getEntityName(); - } else if (pcmDataType instanceof PrimitiveDataType) { - return this.getNameFromPrimitveDataType((PrimitiveDataType) pcmDataType); - } - throw new RuntimeException("getNameFromPCMDataType failed"); - } - - private String getNameFromPrimitveDataType(final PrimitiveDataType pcmDataType) { - switch (pcmDataType.getType()) { - case BOOL: - return "boolean"; - case CHAR: - return "char"; - case BYTE: - return "byte"; - case DOUBLE: - return "double"; - case INT: - return "int"; - case LONG: - return "long"; - case STRING: - return "String"; - } - throw new RuntimeException("getNameFromPrimitveDataType"); - } - - protected Parameter addParameterToSignature(final String interfaceName, final String methodName, - final String typeName, final String parameterName, final String[] parameterTypeSignatures) - throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, - this.getCurrentTestProject()); - final IMethod iMethod = icu.getType(interfaceName).getMethod(methodName, parameterTypeSignatures); - final String parameterStr = typeName + " " + parameterName; - return this.insertParameterIntoSignature(methodName, parameterName, icu, iMethod, parameterStr); - } - - protected Parameter insertParameterIntoSignature(final String methodName, final String parameterName, - final ICompilationUnit icu, final IMethod iMethod, final String parameterStr) - throws JavaModelException, Throwable { - final int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; - final InsertEdit insertEdit = new InsertEdit(offset, parameterStr); - editCompilationUnit(icu, insertEdit); - final ConcreteClassifier concreateClassifier = this - .getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); - final Method jaMoPPMethod = (Method) concreateClassifier.getMembersByName(methodName).get(0); - final org.emftext.language.java.parameters.Parameter jaMoPPParam = this - .getJaMoPPParameterFromJaMoPPMethod(jaMoPPMethod, parameterName); - return claimOne(getCorrespondingEObjects(jaMoPPParam, Parameter.class)); - } - - protected OperationSignature addReturnTypeToSignature(final String interfaceName, final String methodName, - final String typeName, String oldTypeName) throws Throwable { - if (null == oldTypeName) { - oldTypeName = "void"; - } - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, - this.getCurrentTestProject()); - final IMethod iMethod = icu.getType(interfaceName).getMethod(methodName, null); - final String retTypeStr = typeName; - final int offset = iMethod.getSourceRange().getOffset() - + iMethod.getSourceRange().toString().indexOf(oldTypeName); - final ReplaceEdit replaceEdit = new ReplaceEdit(offset + 1, oldTypeName.length() + 1, retTypeStr); - editCompilationUnit(icu, replaceEdit); - final ConcreteClassifier concreateClassifier = this - .getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); - final Method jaMoPPMethod = (Method) concreateClassifier.getMembersByName(methodName).get(0); - return claimOne(getCorrespondingEObjects(jaMoPPMethod, OperationSignature.class)); - } - - protected org.emftext.language.java.parameters.Parameter getJaMoPPParameterFromJaMoPPMethod( - final Method jaMoPPMethod, final String parameterName) { - for (final org.emftext.language.java.parameters.Parameter jaMoPPParam : jaMoPPMethod.getParameters()) { - if (jaMoPPParam.getName().equals(parameterName)) { - return jaMoPPParam; - } - } - throw new RuntimeException( - "JaMoPP param with name " + parameterName + " not found in method " + jaMoPPMethod.getName()); - } - - protected org.emftext.language.java.parameters.Parameter findJaMoPPParameterInICU(final ICompilationUnit icu, - final String interfaceName, final String methodName, final String parameterName) { - final Method method = this.findJaMoPPMethodInICU(icu, methodName); - return this.getJaMoPPParameterFromJaMoPPMethod(method, parameterName); - } - - protected Method findJaMoPPMethodInICU(final ICompilationUnit icu, final String methodName) { - final ConcreteClassifier cc = this.getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); - final List jaMoPPMethods = cc.getMembersByName(methodName); - for (final Member member : jaMoPPMethods) { - if (member instanceof Method && member.getName().equals(methodName)) { - return (Method) member; - } - } - throw new RuntimeException("No method with name " + methodName + " found in " + icu); - } - - protected String getNameFromPCMPrimitiveDataType(final PrimitiveDataType primitiveDataType) { - return primitiveDataType.getType().getName(); - } - - protected Package getDatatypesPackage() throws Throwable { - return this.getPackageWithNameFromCorrespondenceModel("datatypes"); - } - - protected IMethod findIMethodByName(final String typeName, final String methodName, final ICompilationUnit icu) - throws JavaModelException { - final IType type = icu.getAllTypes()[0]; - logger.info(type.getMethods()); - for (final IMethod method : type.getMethods()) { - if (method.getElementName().equals(methodName)) { - return method; - } - } - throw new RuntimeException("Method not " + methodName + " not found in classifier " + typeName); - } - - protected IField findIFieldByName(final String className, final String fieldName, final ICompilationUnit icu) - throws JavaModelException { - final IType type = icu.getType(className); - return type.getField(fieldName); - } - - protected String addPackageAndImplementingClass(final String componentName) - throws CoreException, IOException, InterruptedException { - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final Package mediaStorePackage = this.createPackageWithPackageInfo(Pcm2JavaTestUtils.REPOSITORY_NAME, - componentName); - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - - final String implementingClassName = componentName + "Impl"; - this.addClassInPackage(mediaStorePackage, BasicComponent.class, implementingClassName); - return implementingClassName; - } - - protected OperationProvidedRole addImplementsCorrespondingToOperationProvidedRoleToClass(final String className, - final String implementingInterfaceName) throws CoreException { - final ICompilationUnit classCompilationUnit = CompilationUnitManipulatorHelper - .findICompilationUnitWithClassName(className, this.getCurrentTestProject()); - this.importCompilationUnitWithName(implementingInterfaceName, classCompilationUnit); - - final IType classType = classCompilationUnit.getType(className); - final String newSource = " implements " + implementingInterfaceName; - int offset = classType.getSourceRange().getOffset(); - final int firstBracket = classType.getSource().indexOf("{"); - offset = offset + firstBracket - 1; - final InsertEdit insertEdit = new InsertEdit(offset, newSource); - editCompilationUnit(classCompilationUnit, insertEdit); - final org.emftext.language.java.classifiers.Class jaMoPPClass = (org.emftext.language.java.classifiers.Class) this - .getJaMoPPClassifierForURI(createPlatformResourceURI(classCompilationUnit.getResource())); - final EList classImplements = jaMoPPClass.getImplements(); - logger.debug("Found implements: " + classImplements); - for (final TypeReference implementsReference : classImplements) { - logger.debug("Implements data: " + implementsReference.getTarget()); - final Iterable correspondingEObjects = getCorrespondingEObjects(implementsReference, - OperationProvidedRole.class); - logger.debug("Corresponding provided roles: " + correspondingEObjects); - if (null != correspondingEObjects && correspondingEObjects.iterator().hasNext()) { - return correspondingEObjects.iterator().next(); - } - } - throw new RuntimeException("Could not find an operation provided role for the newly created implements"); - } - - protected void importCompilationUnitWithName(final String implementingInterfaceName, - final ICompilationUnit classCompilationUnit) throws JavaModelException { - final ICompilationUnit interfaceCompilationUnit = CompilationUnitManipulatorHelper - .findICompilationUnitWithClassName(implementingInterfaceName, this.getCurrentTestProject()); - final String namespace = interfaceCompilationUnit.getType(implementingInterfaceName).getFullyQualifiedName(); - saveCurrentStateOfResourceAndRegisterForSynchronization( - URIUtil.createPlatformResourceURI(interfaceCompilationUnit.getResource())); - saveCurrentStateOfResourceAndRegisterForSynchronization( - URIUtil.createPlatformResourceURI(classCompilationUnit.getResource())); - classCompilationUnit.createImport(namespace, null, null); - } - - protected T addFieldToClassWithName(final String className, final String fieldType, - final String fieldName, final Class correspondingType) throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - if (!fieldType.equals("String")) { - this.importCompilationUnitWithName(fieldType, icu); - } - final IType iClass = icu.getAllTypes()[0]; - final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); - final String fieldStr = "private " + fieldType + " " + fieldName + ";"; - final InsertEdit insertEdit = new InsertEdit(offset, fieldStr); - editCompilationUnit(icu, insertEdit); - final Field jaMoPPField = this.getJaMoPPFieldFromClass(icu, fieldName); - if (correspondingType == null) { - return null; - } - return claimOne(getCorrespondingEObjects(jaMoPPField, correspondingType)); - } - - protected Field getJaMoPPFieldFromClass(final ICompilationUnit icu, final String fieldName) { - final ConcreteClassifier cc = this.getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); - final Field field = (Field) cc.getMembersByName(fieldName).get(0); - return field; - } - - // add Annotation via the framework - protected T addAnnotationToClassifier(final AnnotableAndModifiable annotable, - final String annotationName, final String annotationParameter, final Class classOfCorrespondingObject, - final String className) throws Throwable { - final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IType type = cu.getType(className); - final int offset = CompilationUnitManipulatorHelper.getOffsetForAddingAnntationToClass(type); - final String composedName = "@" + annotationName - + (annotationParameter != null ? "(\"" + annotationParameter + "\")" : "") - + java.lang.System.lineSeparator(); - final InsertEdit insertEdit = new InsertEdit(offset, composedName); - editCompilationUnit(cu, insertEdit); - final Iterable eObjectsByType = getCorrespondingEObjects(annotable, classOfCorrespondingObject); - return claimOne(eObjectsByType); - } - - // add Annotation via the framework - protected T addAnnotationToField(final String fieldName, final String annotationName, - final String annotationParameter, final Class classOfCorrespondingObject, final String className) - throws Throwable { - final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IType type = cu.getType(className); - final int offset = CompilationUnitManipulatorHelper.getOffsetForAddingAnntationToField(type, fieldName); - final String composedName = "@" + annotationName - + (annotationParameter != null ? "(\"" + annotationParameter + "\")" : "") - + java.lang.System.lineSeparator(); - final InsertEdit insertEdit = new InsertEdit(offset, composedName); - editCompilationUnit(cu, insertEdit); - final Field jaMoPPField = this.getJaMoPPFieldFromClass(cu, fieldName); - final Iterable eObjectsByType = getCorrespondingEObjects(jaMoPPField, classOfCorrespondingObject); - return claimOne(eObjectsByType); - } - - protected void assertCorrespondingSEFF(final ResourceDemandingSEFF correspondingSeff, String methodName) - throws Throwable { - final ClassMethod jaMoPPMethod = claimOne(getCorrespondingEObjects(correspondingSeff, ClassMethod.class)); - assertEquals(jaMoPPMethod.getName(), methodName); - } - - protected void assertOperationInterface(final Repository repo, final OperationInterface opIf, - final String expectedName) { - assertTrue(null != opIf, "The created operation interface is null"); - assertEquals(opIf.getEntityName(), expectedName, - "OperationInterface name does not equals the expected interface Name."); - assertEquals(repo.getId(), opIf.getRepository__Interface().getId(), - "The created operation interface is not in the repository"); - } - - public ICompilationUnit addMethodToCompilationUnit(final String compilationUnitName, final String methodString, - final IProject currentTestProject) throws JavaModelException { - final ICompilationUnit cu = CompilationUnitManipulatorHelper - .findICompilationUnitWithClassName(compilationUnitName, currentTestProject); - final IType firstType = cu.getAllTypes()[0]; - final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); - final InsertEdit insertEdit = new InsertEdit(offset, methodString); - editCompilationUnit(cu, insertEdit); - return cu; - } - } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java index 9ba19a3f0..e47287ff8 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java @@ -1,5 +1,12 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; + import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; @@ -7,92 +14,116 @@ import org.palladiosimulator.pcm.repository.Repository; import org.palladiosimulator.pcm.system.System; +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class PackageMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - +class PackageMappingTransformationTest extends Java2PcmTransformationTest { /** * first package is created --> should be mapped to a repository - * - * @throws Throwable */ @Test - public void testAddFirstPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - assertEquals(Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED, repo.getEntityName(), - "Name of the repository is not the same as the name of the package"); - this.assertResourceAndFileForEObjects(repo); - this.assertFilesOnlyForEObjects(repo); + void testAddFirstPackage() throws Exception { + createRepositoryPackage(); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertEquals(Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED, repository.getEntityName(), "repository has wrong name"); + }); } - + /** - * second packages is added --> should be mapped to a basic component - * - * @throws Throwable + * second package is added --> should be mapped to a basic component */ @Test - public void testAddSecondPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - this.assertRepositoryAndPCMName(repo, bc, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - this.assertFilesOnlyForEObjects(bc); + void testAddSecondPackageAsBasicComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, BasicComponent.class); + }); } - + + /** + * second package is added --> should be mapped to a composite component + */ @Test - public void testCreateCompositeComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); - - this.assertRepositoryAndPCMName(repo, cc, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); - this.assertFilesOnlyForEObjects(cc); + void testAddSecondPackageAsCompositeComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME, CompositeComponent.class); + }); } - + + /** + * second package is added --> should be mapped to a system + */ @Test - public void testCreateSystem() throws Throwable { - final Repository repository = super.addRepoContractsAndDatatypesPackage(); - - final System system = super.addSecondPackageCorrespondsToSystem(); - - this.assertPCMNamedElement(system, Pcm2JavaTestUtils.SYSTEM_NAME); - this.assertFilesOnlyForEObjects(repository, system); + void testAddSecondPackageAsSystem() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.SYSTEM_NAME); + }); + + validatePcmView(view -> { + System system = claimSingleSystem(view); + assertEquals(Pcm2JavaTestUtils.SYSTEM_NAME, system.getEntityName(), "system has wrong name"); + }); } - + + /** + * second package is added --> should be mapped to nothing + */ @Test - public void testCreateNoCorrespondingObject() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - super.addSecondPackageCorrespondsWithoutCorrespondences(); - assertNotNull(repo); - // TODO:what to check? + void testAddSecondPackageAsNone() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertTrue(repository.getComponents__Repository().isEmpty(), "there must not exist any contained Component"); + assertTrue(view.getRootObjects(System.class).isEmpty(), "there must not exist any System"); + }); } - + @Test - public void testRenamePackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsToBasicComponent(); - - final String packageName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; - - final Package renamedPackage = super.renamePackage(this.secondPackage, packageName); - - final BasicComponent bc = claimOne(getCorrespondingEObjects(renamedPackage, BasicComponent.class)); - // repository of basic component has to be the repository - assertEquals(repo.getId(), bc.getRepository__RepositoryComponent().getId(), - "Repository of basic compoennt is not the repository: " + repo); - // name should be changed since there is no implementing class (yet) for the - // component - assertTrue(packageName.toLowerCase().contains(bc.getEntityName().toLowerCase()), - "The name of the basic component, which is '" + bc.getEntityName() - + "', is not contained in the name of the package: " + packageName + " "); - this.assertResourceAndFileForEObjects(bc); + void testRenameRepository() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + String changedPackageName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; + + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().renamePackage(innerPackage, changedPackageName); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertEquals(1, repository.getComponents__Repository().size(), "wrong number of components in repository"); + claimComponent(repository, changedPackageName, BasicComponent.class); + }); } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/SynchronizationAwaitCallback.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/SynchronizationAwaitCallback.java deleted file mode 100644 index 754116093..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/SynchronizationAwaitCallback.java +++ /dev/null @@ -1,5 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; - -public interface SynchronizationAwaitCallback { - void waitForSynchronization(int numberOfExpectedSynchronizationCalls); -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java new file mode 100644 index 000000000..cfee60edf --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java @@ -0,0 +1,164 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; + +import static org.junit.jupiter.api.Assertions.fail; + +import org.eclipse.emf.ecore.EObject; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.CollectionDataType; +import org.palladiosimulator.pcm.repository.CompositeComponent; +import org.palladiosimulator.pcm.repository.CompositeDataType; +import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.system.System; + +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +public class ClassMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { + + /** + * Class that in mapped package and same name as component + impl--> should be + * the new implementing class for the component + * + * @throws Exception + */ + @Test + public void testAddComponentClassInPackageWithCorrespondingComponent() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); + + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + final BasicComponent bcForClass = super.addClassInSecondPackage(BasicComponent.class); + + super.assertRepositoryAndPCMName(repo, bcForClass, bc.getEntityName()); + } + + @Test + public void testAddCompositeComponentClassInPackageWithCorrespondingCompositeComponent() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); + + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + final CompositeComponent ccForClass = this.addClassInSecondPackage(CompositeComponent.class); + + super.assertRepositoryAndPCMName(repo, ccForClass, cc.getEntityName()); + } + + @Test + public void testAddSystemClassInPackageWithCorrespondingSystem() throws Throwable { + super.addRepoContractsAndDatatypesPackage(); + final System pcmSystem = super.addSecondPackageCorrespondsToSystem(); + + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + final System systemForClass = super.addClassInSecondPackage(System.class); + + super.assertPCMNamedElement(systemForClass, pcmSystem.getEntityName()); + } + + /** + * Test ii) class in non corresponding package --> should not be mapped to a + * Basic Component + * + * @throws Throwable + */ + @Test + public void testAddClassInPackageWithoutCorrespondingComponent() throws Throwable { + super.addRepoContractsAndDatatypesPackage(); + super.addSecondPackageCorrespondsWithoutCorrespondences(); + + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + try { + final EObject eObject = super.addClassInPackage(this.secondPackage, EObject.class); + fail("The class should not have any correspondences, but it has a correspondence to eObject: " + eObject); + } catch (final RuntimeException re) { + // expected exception + } + } + + @Test + public void testAddBasicComponentClassInPackageWithoutCorrespondence() throws Throwable { + final Repository repo = this.addRepoContractsAndDatatypesPackage(); + super.addSecondPackageCorrespondsWithoutCorrespondences(); + + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + final BasicComponent newBc = super.addClassInSecondPackage(BasicComponent.class); + + super.assertRepositoryAndPCMName(repo, newBc, newBc.getEntityName()); + } + + @Test + public void testAddCompositeComponentClassInPackageWithoutCorrespondence() throws Throwable { + final Repository repo = this.addRepoContractsAndDatatypesPackage(); + super.addSecondPackageCorrespondsWithoutCorrespondences(); + + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + final CompositeComponent cc = super.addClassInSecondPackage(CompositeComponent.class); + + super.assertRepositoryAndPCMName(repo, cc, cc.getEntityName()); + } + + @Test + public void testAddSystemClassInPackageWithoutCorrespondence() throws Throwable { + this.addRepoContractsAndDatatypesPackage(); + super.addSecondPackageCorrespondsWithoutCorrespondences(); + + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + final System pcmSystem = super.addClassInSecondPackage(System.class); + + this.assertPCMNamedElement(pcmSystem, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); + } + + @Test + public void testAddCompositeDatatypeClassInDatatypePackage() throws Throwable { + final Repository repo = this.addRepoContractsAndDatatypesPackage(); + + final CompositeDataType cdt = this.addClassThatCorrespondsToCompositeDatatype(); + + this.assertRepositoryAndPCMNameForDatatype(repo, cdt, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); + } + + @Test + public void testAddCollectionDatatypeClassInDatatypePackage() throws Throwable { + final Repository repo = this.addRepoContractsAndDatatypesPackage(); + + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + final CollectionDataType collection = super.addClassInPackage(this.getDatatypesPackage(), + CollectionDataType.class); + + this.assertRepositoryAndPCMNameForDatatype(repo, collection, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); + } + + @Test + public void testAddClassInDatatypePackage() throws Throwable { + this.addRepoContractsAndDatatypesPackage(); + try { + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + final EObject eObject = super.addClassInPackage(this.getDatatypesPackage(), EObject.class); + fail("The class should not have any datatype correspondences, but it has a correspondence to eObject: " + + eObject); + } catch (final RuntimeException re) { + // expected Exception + } + } + + @Test + public void testRenameBasicComponentClass() throws Throwable { + final Repository repo = this.addRepoContractsAndDatatypesPackage(); + this.addSecondPackageCorrespondsWithoutCorrespondences(); + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + final BasicComponent basicComponent = this.addClassInSecondPackage(BasicComponent.class); + + final BasicComponent newBasicComponent = super.renameClassifierWithName(basicComponent.getEntityName() + "Impl", + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME, BasicComponent.class); + + this.assertRepositoryAndPCMName(repo, newBasicComponent, + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME); + this.assertFilesOnlyForEObjects(newBasicComponent); + } + +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java similarity index 95% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java index 450f54981..09bfd39cd 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.seff.ResourceDemandingSEFF; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/CompilationUnitManipulatorHelper.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/CompilationUnitManipulatorHelper.java similarity index 97% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/CompilationUnitManipulatorHelper.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/CompilationUnitManipulatorHelper.java index 9d0164cb4..53251e913 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/CompilationUnitManipulatorHelper.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/CompilationUnitManipulatorHelper.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.eclipse.core.resources.IProject; import org.eclipse.jdt.core.ICompilationUnit; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java similarity index 99% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java index fa2203f71..e93123807 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import static org.junit.jupiter.api.Assertions.fail; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java similarity index 97% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java index b040f1665..6e9d28f02 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.eclipse.emf.ecore.EObject; import org.junit.jupiter.api.Test; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/JaMoPPParameterMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java similarity index 98% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/JaMoPPParameterMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java index f51d292d5..e3239566d 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/JaMoPPParameterMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.ILocalVariable; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmPackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java similarity index 93% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmPackageMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java index 507da0053..17a44e81e 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmPackageMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java @@ -1,5 +1,5 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import java.util.Collections; import java.util.List; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java new file mode 100644 index 000000000..1fc223353 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java @@ -0,0 +1,941 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createPlatformResourceURI; +import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.ecore.resource.ResourceSetUtil.loadOrCreateResource; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static tools.vitruv.applications.util.temporary.java.JavaQueryUtil.getNameFromJaMoPPType; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.log4j.Logger; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +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.jdt.core.IBuffer; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.refactoring.IJavaRefactorings; +import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; +import org.eclipse.jdt.internal.core.manipulation.StubUtility; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.Refactoring; +import org.eclipse.ltk.core.refactoring.RefactoringContribution; +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; +import org.emftext.language.java.classifiers.Classifier; +import org.emftext.language.java.classifiers.ConcreteClassifier; +import org.emftext.language.java.classifiers.Interface; +import org.emftext.language.java.containers.CompilationUnit; +import org.emftext.language.java.containers.ContainersFactory; +import org.emftext.language.java.containers.ContainersPackage; +import org.emftext.language.java.containers.JavaRoot; +import org.emftext.language.java.containers.Package; +import org.emftext.language.java.members.ClassMethod; +import org.emftext.language.java.members.Field; +import org.emftext.language.java.members.Member; +import org.emftext.language.java.members.Method; +import org.emftext.language.java.modifiers.AnnotableAndModifiable; +import org.emftext.language.java.types.TypeReference; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.palladiosimulator.pcm.core.entity.NamedElement; +import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.CollectionDataType; +import org.palladiosimulator.pcm.repository.CompositeComponent; +import org.palladiosimulator.pcm.repository.CompositeDataType; +import org.palladiosimulator.pcm.repository.DataType; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationProvidedRole; +import org.palladiosimulator.pcm.repository.OperationSignature; +import org.palladiosimulator.pcm.repository.Parameter; +import org.palladiosimulator.pcm.repository.PrimitiveDataType; +import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.repository.RepositoryComponent; +import org.palladiosimulator.pcm.seff.ResourceDemandingSEFF; +import org.palladiosimulator.pcm.system.System; + +import edu.kit.ipd.sdq.commons.util.org.eclipse.core.resources.IProjectUtil; +import edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil; +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; +import tools.vitruv.applications.util.temporary.java.JavaSetup; +import tools.vitruv.change.composite.description.VitruviusChange; +import tools.vitruv.change.propagation.ChangePropagationMode; +import tools.vitruv.framework.views.changederivation.DefaultStateBasedChangeResolutionStrategy; +import tools.vitruv.framework.views.changederivation.StateBasedChangeResolutionStrategy; +import tools.vitruv.framework.vsum.internal.InternalVirtualModel; +import tools.vitruv.testutils.LegacyVitruvApplicationTest; +import tools.vitruv.testutils.TestProject; +import tools.vitruv.testutils.views.UriMode; + +/** + * Test class that contains utility methods that can be used by JaMoPP2PCM + * transformation tests + * + */ +@SuppressWarnings("restriction") +public abstract class Java2PcmTransformationTest extends LegacyVitruvApplicationTest { + private static final Logger logger = Logger.getLogger(Java2PcmTransformationTest.class); + private static final int MAXIMUM_EDIT_WAITING_TIME = 5000; + + protected Package mainPackage; + protected Package secondPackage; + + private IProject testEclipseProject; + + private Map oldToNewURIsOfModifiedResources; + private volatile boolean isWaitingForFinishingsEdits = false; + + protected IProject getCurrentTestProject() { + return testEclipseProject; + } + + /* + * JDT functionality requires platform URIs. + */ + @Override + protected UriMode getUriMode() { + return UriMode.PLATFORM_URIS; + } + + private void refreshProject() { + try { + testEclipseProject.refreshLocal(IResource.DEPTH_INFINITE, null); + } catch (CoreException e) { + String message = "Could not refresh project " + testEclipseProject.getName(); + logger.error(message, e); + } + } + + @BeforeAll + public static void setupJavaFactories() { + JavaSetup.prepareFactories(); + } + + @BeforeEach + public void setupJavaProject(@TestProject Path testProjectFolder) { + String projectName = testProjectFolder.getFileName().toString(); + testEclipseProject = IProjectUtil.createProjectAt(projectName, testProjectFolder); + IProjectUtil.configureAsJavaProject(testEclipseProject); + JavaSetup.resetClasspathAndRegisterStandardLibrary(); + } + + @BeforeEach + public void disableTransitiveChangePropagation() { + this.getVirtualModel().setChangePropagationMode(ChangePropagationMode.SINGLE_STEP); + } + + @BeforeEach + public void cleanupModifiedResources() { + this.oldToNewURIsOfModifiedResources = new HashMap<>(); + } + + @AfterEach + public void closeJavaProject() throws JavaModelException, CoreException { + getIJavaProject().close(); + } + + private void saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(final URI resourceURI, + final URI newResourceURI) { + oldToNewURIsOfModifiedResources.put(resourceURI, newResourceURI); + // Access the resource such that the initial state is present in the test view + resourceAt(resourceURI); + } + + private void saveCurrentStateOfResourceAndRegisterForSynchronization(final URI resourceURI) { + saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(resourceURI, resourceURI); + } + + private Resource loadResourceIndependentFromView(final URI resourceURI) { + ResourceSet resourceSet = new ResourceSetImpl(); + Resource resource = loadOrCreateResource(resourceSet, resourceURI); + return resource; + } + + protected Package createPackageWithPackageInfo(final String... namespace) throws IOException { + String packageFile = String.join("/", namespace); + packageFile = packageFile + "/package-info.java"; + final Package jaMoPPPackage = ContainersFactory.eINSTANCE.createPackage(); + final List namespaceList = Arrays.asList(namespace); + jaMoPPPackage.setName(namespaceList.get(namespaceList.size() - 1)); + jaMoPPPackage.getNamespaces().addAll(namespaceList.subList(0, namespaceList.size() - 1)); + URI createPackageURI = getUri(Path.of(getPathInProjectForSrcFile(packageFile))); + saveCurrentStateOfResourceAndRegisterForSynchronization(createPackageURI); + final Resource resource = loadResourceIndependentFromView(createPackageURI); + resource.getContents().add(jaMoPPPackage); + resource.save(null); + propagateChanges(); + return jaMoPPPackage; + } + + private ICompilationUnit createCompilationUnit(IPackageFragment packageFragment, String cuName) + throws JavaModelException { + URI createdCompilationUnitURI = getURIForElementInPackage(packageFragment, cuName); + saveCurrentStateOfResourceAndRegisterForSynchronization(createdCompilationUnitURI); + ICompilationUnit compilationUnit = packageFragment.createCompilationUnit(cuName + ".java", "", false, null); + return compilationUnit; + } + + public void editCompilationUnit(final ICompilationUnit cu, final TextEdit... edits) throws JavaModelException { + saveCurrentStateOfResourceAndRegisterForSynchronization(URIUtil.createPlatformResourceURI(cu.getResource())); + cu.becomeWorkingCopy(new NullProgressMonitor()); + for (final TextEdit edit : edits) { + cu.applyTextEdit(edit, null); + } + cu.reconcile(ICompilationUnit.NO_AST, false, null, null); + cu.commitWorkingCopy(false, new NullProgressMonitor()); + cu.discardWorkingCopy(); + cu.save(new NullProgressMonitor() { + public void done() { + Java2PcmTransformationTest.this.isWaitingForFinishingsEdits = false; + Java2PcmTransformationTest.this.notifyAll(); + } + }, true); + propagateChanges(); + } + + public void propagateChanges() { + logger.debug("Starting to wait for edits to be finished"); + try { + if (isWaitingForFinishingsEdits) { + wait(MAXIMUM_EDIT_WAITING_TIME); + if (isWaitingForFinishingsEdits) { + logger.error("Waiting for edits timed out in project " + testEclipseProject.getName()); + fail("Waiting for edits timed out"); + } + } + } catch (InterruptedException e) { + fail("An interrupt occurred unexpectedly"); + } + refreshProject(); + StateBasedChangeResolutionStrategy changeResolutionStrategy = new DefaultStateBasedChangeResolutionStrategy(); +// for (Entry modifiedResourceURI : oldToNewURIsOfModifiedResources.entrySet()) { +// Resource currentResource = loadResourceIndependentFromView(modifiedResourceURI.getValue()); +// Resource virtualModelResource = resourceAt(modifiedResourceURI.getKey()); +// VitruviusChange change = changeResolutionStrategy.getChangeSequenceBetween(currentResource, +// virtualModelResource); +// VitruviusChange unresolvedChange = change.unresolve(); +// record(resourceAt(modifiedResourceURI.getKey()).getResourceSet(), +// resourceSet -> unresolvedChange.resolveAndApply(localUuidResolver)); +// } + oldToNewURIsOfModifiedResources.clear(); + propagate(); + disposeViewResources(); + } + + protected Repository addRepoContractsAndDatatypesPackage() throws IOException, CoreException { + this.mainPackage = this.createPackageWithPackageInfo(new String[] { Pcm2JavaTestUtils.REPOSITORY_NAME }); + final Repository repo = claimOne(getCorrespondingEObjects(this.mainPackage, Repository.class)); + return repo; + } + + protected BasicComponent addSecondPackageCorrespondsToBasicComponent() throws Throwable { + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + return this.createSecondPackage(BasicComponent.class, Pcm2JavaTestUtils.REPOSITORY_NAME, + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + } + + protected T createSecondPackage(final Class correspondingType, final String... namespace) + throws Throwable { + this.secondPackage = this.createPackageWithPackageInfo(namespace); + return claimOne(getCorrespondingEObjects(this.secondPackage, correspondingType)); + } + + private void createSecondPackageWithoutCorrespondence(final String... namespace) throws Throwable { + this.secondPackage = this.createPackageWithPackageInfo(namespace); + } + + protected Package renamePackage(final Package packageToRename, final String newName) throws CoreException { + final Resource resource = packageToRename.eResource(); + final IFile iFile = URIUtil.getIFileForEMFUri(resource.getURI()); + IPath iPath = iFile.getProjectRelativePath(); + iPath = iPath.removeLastSegments(1); + final String newQualifiedName = packageToRename.getNamespacesAsString() + newName; + final IFolder iFolder = iFile.getProject().getFolder(iPath); + final IJavaElement javaPackage = JavaCore.create(iFolder); + String packageFile = String.join("/", packageToRename.getNamespaces()); + packageFile = packageFile + "/" + newName + "/package-info.java"; + saveCurrentStateOfRenamedResourceAndRegisterForChangePropagation(resource.getURI(), + getUri(Path.of(getPathInProjectForSrcFile(packageFile)))); + this.refactorRenameJavaElement(newQualifiedName, javaPackage, IJavaRefactorings.RENAME_PACKAGE); + final Package newPackage = this.findJaMoPPPackageWithName(newQualifiedName); + return newPackage; + } + + private void refactorRenameJavaElement(final String newName, final IJavaElement iJavaElement, + final String refactorRenameActionName) throws CoreException { + final RefactoringContribution refacContrib = RefactoringCore + .getRefactoringContribution(refactorRenameActionName); + final RefactoringStatus status = new RefactoringStatus(); + final RenameJavaElementDescriptor desc = (RenameJavaElementDescriptor) refacContrib.createDescriptor(); + desc.setUpdateReferences(true); + desc.setJavaElement(iJavaElement); + desc.setNewName(newName); + final NullProgressMonitor monitor = new NullProgressMonitor(); + final Refactoring refactoring = desc.createRefactoring(status); + refactoring.checkInitialConditions(monitor); + refactoring.checkFinalConditions(monitor); + final Change change = refactoring.createChange(monitor); + change.perform(monitor); + propagateChanges(); + } + + protected T renameClassifierWithName(final String entityName, final String newName, + final Class type) throws Throwable { + try { + final ICompilationUnit cu = CompilationUnitManipulatorHelper + .findICompilationUnitWithClassName(entityName + ".java", this.getCurrentTestProject()); + final int offset = cu.getBuffer().getContents().indexOf(entityName); + if (cu.getBuffer() instanceof IBuffer.ITextEditCapability) { + logger.info(cu.getBuffer()); + } + final ReplaceEdit edit = new ReplaceEdit(offset, entityName.length(), newName); + editCompilationUnit(cu, edit); + final URI uri = createPlatformResourceURI(cu.getResource()); + final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); + return claimOne(getCorrespondingEObjects(jaMoPPClass, type)); + } catch (final Throwable e) { + logger.warn(e.getMessage()); + } + return null; + + } + + private Package findJaMoPPPackageWithName(final String newName) throws JavaModelException { + final IJavaProject javaProject = JavaCore.create(this.getCurrentTestProject()); + for (final IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) { + final IJavaElement[] children = packageFragmentRoot.getChildren(); + for (final IJavaElement iJavaElement : children) { + if (iJavaElement instanceof IPackageFragment) { + final IPackageFragment fragment = (IPackageFragment) iJavaElement; + if (fragment.getElementName().equals(newName)) { + final URI uri = this.getURIForElementInPackage(fragment, "package-info"); + final Package jaMoPPPackage = this.getJaMoPPRootForURI(uri); + return jaMoPPPackage; + } + } + } + } + throw new RuntimeException("Could not find a compilation unit with name " + newName); + } + + private IPackageFragmentRoot getIJavaProject() throws CoreException { + final IProject project = this.getCurrentTestProject(); + final IJavaProject javaProject = JavaCore.create(project); + final IFolder sourceFolder = project.getFolder("src"); + if (!sourceFolder.exists()) { + final boolean force = true; + final boolean local = true; + sourceFolder.create(force, local, new NullProgressMonitor()); + } + final IPackageFragmentRoot packageFragment = javaProject.getPackageFragmentRoot(sourceFolder); + return packageFragment; + } + + private String getPathInProjectForSrcFile(final String srcFilePath) { + return "src/" + srcFilePath; + } + + protected T addClassInSecondPackage(final Class classOfCorrespondingObject) + throws Throwable { + final T createdEObject = this.addClassInPackage(this.secondPackage, classOfCorrespondingObject); + return createdEObject; + } + + protected T addClassInPackage(final Package packageForClass, + final Class classOfCorrespondingObject) throws Throwable { + final String implementingClassName = Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME; + return this.addClassInPackage(packageForClass, classOfCorrespondingObject, implementingClassName); + } + + protected T addClassInPackage(final Package packageForClass, + final Class classOfCorrespondingObject, final String implementingClassName) + throws CoreException, InterruptedException { + final Classifier jaMoPPClass = this.createClassInPackage(packageForClass, implementingClassName); + final Iterable eObjectsByType = getCorrespondingEObjects(jaMoPPClass, classOfCorrespondingObject); + return claimOne(eObjectsByType); + } + + protected Classifier createClassInPackage(final Package packageForClass, final String implementingClassName) + throws CoreException { + String packageName = packageForClass.getName(); + String packageNamespace = packageForClass.getNamespacesAsString() + packageName; + return createClassInPackage(implementingClassName, packageNamespace); + } + + protected Classifier createClassInPackage(final String implementingClassName, String packageNamespace) + throws CoreException { + final IPackageFragment packageFragment = this.getPackageFragment(packageNamespace); + createEmptyClass(packageFragment, implementingClassName); + final URI uri = this.getURIForElementInPackage(packageFragment, implementingClassName); + final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); + return jaMoPPClass; + } + + private URI getURIForElementInPackage(final IPackageFragment packageFragment, final String elementName) { + String uriString = packageFragment.getResource().getFullPath().toString() + "/" + elementName + ".java"; + return URI.createPlatformResourceURI(uriString, true); + } + + private IPackageFragment getPackageFragment(String packageNamespace) throws CoreException { + final IPackageFragmentRoot packageRoot = this.getIJavaProject(); + for (final IJavaElement javaElement : packageRoot.getChildren()) { + if (javaElement instanceof IPackageFragment && javaElement.getElementName().equals(packageNamespace)) { + return (IPackageFragment) javaElement; + } + } + throw new RuntimeException("No packageFragment found for JaMoPP package " + packageNamespace); + } + + protected ConcreteClassifier getJaMoPPClassifierForURI(final URI uri) { + final CompilationUnit cu = this.getJaMoPPRootForURI(uri); + final Classifier jaMoPPClassifier = cu.getClassifiers().get(0); + return (ConcreteClassifier) jaMoPPClassifier; + } + + private T getJaMoPPRootForURI(final URI uri) { + final Resource resource = new ResourceSetImpl().getResource(uri, true); + // unchecked is OK for the test. + @SuppressWarnings("unchecked") + final T javaRoot = (T) resource.getContents().get(0); + return javaRoot; + } + + protected CompositeComponent addSecondPackageCorrespondsToCompositeComponent() throws Throwable { + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + return this.createSecondPackage(CompositeComponent.class, Pcm2JavaTestUtils.REPOSITORY_NAME, + Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + } + + protected org.palladiosimulator.pcm.system.System addSecondPackageCorrespondsToSystem() throws Throwable { + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + return this.createSecondPackage(System.class, Pcm2JavaTestUtils.SYSTEM_NAME); + } + + protected void addSecondPackageCorrespondsWithoutCorrespondences() throws Throwable { + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + this.createSecondPackageWithoutCorrespondence(Pcm2JavaTestUtils.REPOSITORY_NAME, + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + } + + protected void assertRepositoryAndPCMName(final Repository repo, final RepositoryComponent repoComponent, + final String expectedName) throws Throwable { + + assertEquals(repo.getId(), repoComponent.getRepository__RepositoryComponent().getId(), + "Repository of compoennt is not the repository: " + repo); + + this.assertPCMNamedElement(repoComponent, expectedName); + } + + protected void assertResourceAndFileForEObjects(final EObject... eObjects) throws Throwable { + for (final EObject eObject : eObjects) { + final Resource eResource = eObject.eResource(); + assertNotNull(eResource, "Resource of eObject " + eObject + " is null"); + final IFile iFile = URIUtil.getIFileForEMFUri(eResource.getURI()); + assertTrue(iFile.exists(), "No IFile for eObject " + eObject + " in resource " + eResource + " found."); + } + } + + protected void assertFilesOnlyForEObjects(final EObject... eObjects) throws Throwable { + final Set fullFilePaths = new HashSet(); + for (final EObject eObject : eObjects) { + final IFile iFile = URIUtil.getIFileForEMFUri(eObject.eResource().getURI()); + fullFilePaths.add(iFile.getFullPath().toString()); + } + final IFolder folder = this.getCurrentTestProject().getFolder("model"); + final List foundAdditionalFiles = new ArrayList<>(); + for (final IResource iResource : folder.members()) { + final String iResourcePath = iResource.getFullPath().toString(); + if (!fullFilePaths.contains(iResourcePath)) { + foundAdditionalFiles.add(iResourcePath); + } + } + if (0 < foundAdditionalFiles.size()) { + final StringBuilder failMsg = new StringBuilder("Found addtional files in model folder: "); + foundAdditionalFiles.forEach(str -> failMsg.append(str).append(", ")); + fail(failMsg.toString()); + } + } + + protected void assertRepositoryAndPCMNameForDatatype(final Repository repo, final DataType dt, + final String expectedName) throws Throwable { + + assertEquals(repo.getId(), dt.getRepository__DataType().getId(), + "Repository of compoennt is not the repository: " + repo); + if (dt instanceof CompositeDataType) { + this.assertPCMNamedElement((CompositeDataType) dt, expectedName); + } else if (dt instanceof CollectionDataType) { + this.assertPCMNamedElement((CollectionDataType) dt, expectedName); + } else { + throw new RuntimeException("Primitive data types should not have a correspondence to classes"); + } + } + + protected void assertPCMNamedElement(final NamedElement pcmNamedElement, final String expectedName) + throws Throwable { + assertEquals(expectedName, pcmNamedElement.getEntityName(), + "The name of pcm named element is not " + expectedName); + this.assertResourceAndFileForEObjects(pcmNamedElement); + } + + protected void assertPcmParameter(final Parameter pcmParameter, final String expectedName) throws Throwable { + assertEquals(expectedName, pcmParameter.getParameterName(), "The name of pcm parameter is not " + expectedName); + this.assertResourceAndFileForEObjects(pcmParameter); + } + + protected OperationInterface addInterfaceInContractsPackage() throws Throwable { + return this.createInterfaceInPackage("contracts"); + } + + private OperationInterface createInterfaceInPackage(final String packageName) + throws Throwable, CoreException, InterruptedException { + final String interfaceName = Pcm2JavaTestUtils.INTERFACE_NAME; + return this.createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence(packageName, interfaceName); + } + + protected OperationInterface createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence( + final String packageName, final String interfaceName) throws CoreException { + Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); + return this.createInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), + interfaceName, true); + } + + protected ConcreteClassifier createInterfaceInPackageBasedOnJaMoPPPackageWithoutCorrespondence( + final String packageName, final String interfaceName) + throws Throwable, CoreException, InterruptedException { + Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); + return this.createJaMoPPInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), + interfaceName); + } + + protected OperationInterface createInterfaceInPackage(String packageNamespace, final String interfaceName, + boolean claimOne) throws CoreException { + final Classifier jaMoPPIf = createJaMoPPInterfaceInPackage(packageNamespace, interfaceName); + Iterable correspondingOpInterfaces = getCorrespondingEObjects(jaMoPPIf, + OperationInterface.class); + if (claimOne) { + return claimOne(correspondingOpInterfaces); + } + if (null == correspondingOpInterfaces || !correspondingOpInterfaces.iterator().hasNext()) { + return null; + } + logger.warn("More than one corresponding interfaces found for interface " + jaMoPPIf + ". Returning the first"); + return correspondingOpInterfaces.iterator().next(); + } + + protected ConcreteClassifier createJaMoPPInterfaceInPackage(String packageNamespace, final String interfaceName) + throws CoreException { + final IPackageFragment packageFragment = this.getPackageFragment(packageNamespace); + createEmptyInterface(packageFragment, interfaceName); + final URI uri = this.getURIForElementInPackage(packageFragment, interfaceName); + final ConcreteClassifier jaMoPPIf = this.getJaMoPPClassifierForURI(uri); + + return jaMoPPIf; + } + + private void createEmptyCompilationUnit(IPackageFragment packageFragment, String typeName, String cuName) + throws JavaModelException { + String lineDelimiter = null; + lineDelimiter = StubUtility.getLineDelimiterUsed(packageFragment.getJavaProject()); + ICompilationUnit compilationUnit = createCompilationUnit(packageFragment, cuName); + InsertEdit edit = new InsertEdit(0, "package " + packageFragment.getElementName() + ";" + lineDelimiter + + lineDelimiter + "public " + typeName + " " + cuName + " { }"); + editCompilationUnit(compilationUnit, edit); + } + + protected void createEmptyInterface(IPackageFragment packageFragment, String interfaceName) + throws JavaModelException { + createEmptyCompilationUnit(packageFragment, "interface", interfaceName); + } + + protected void createEmptyClass(IPackageFragment packageFragment, String className) throws JavaModelException { + createEmptyCompilationUnit(packageFragment, "class", className); + } + + protected OperationInterface addInterfaceInSecondPackageWithCorrespondence(final String packageName) + throws Throwable { + this.getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); + return this.createInterfaceInPackage(packageName); + } + + protected EObject addInterfaceInPackageWithoutCorrespondence(final String packageName) throws Throwable { + this.getUserInteraction().addNextSingleSelection( + Java2PcmUserSelection.SELECT_DONT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); + Package jaMoPPPackage = this.getPackageWithNameFromCorrespondenceModel(packageName); + return this.createInterfaceInPackage(jaMoPPPackage.getNamespacesAsString() + jaMoPPPackage.getName(), + "I" + packageName, false); + } + + protected Package getPackageWithNameFromCorrespondenceModel(final String name) throws CoreException { + final Iterable packages = getCorrespondingEObjects(ContainersPackage.Literals.PACKAGE, Package.class); + for (final Package currentPackage : packages) { + if (currentPackage.getName().equals(name)) { + return currentPackage; + } + } + throw new RuntimeException("Could not find package with name " + name); + } + + protected OperationSignature addMethodToInterfaceWithCorrespondence(final String interfaceName) throws Throwable { + final String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + return this.addMethodToInterfaceWithCorrespondence(interfaceName, methodName); + } + + protected OperationSignature addMethodToInterfaceWithCorrespondence(final String interfaceName, + final String methodName) throws Throwable, JavaModelException { + final String methodString = "\nvoid " + methodName + "();\n"; + final ICompilationUnit cu = addMethodToCompilationUnit(interfaceName, methodString, + this.getCurrentTestProject()); + return this.findOperationSignatureForJaMoPPMethodInCompilationUnit(methodName, interfaceName, cu); + } + + protected ResourceDemandingSEFF addClassMethodToClassThatOverridesInterfaceMethod(final String className, + final String methodName) throws Throwable { + final String methodString = "\n\tpublic void " + methodName + " () {\n\t}\n"; + final ICompilationUnit icu = addMethodToCompilationUnit(className, methodString, this.getCurrentTestProject()); + final Method jaMoPPMethod = this.findJaMoPPMethodInICU(icu, methodName); + final ClassMethod classMethod = (ClassMethod) jaMoPPMethod; + return claimOne(getCorrespondingEObjects(classMethod, ResourceDemandingSEFF.class)); + } + + protected OperationSignature findOperationSignatureForJaMoPPMethodInCompilationUnit(final String methodName, + final String interfaceName, final ICompilationUnit cu) throws Throwable { + final URI uri = createPlatformResourceURI(cu.getResource()); + final Classifier classifier = this.getJaMoPPClassifierForURI(uri); + final Interface jaMoPPInterface = (Interface) classifier; + for (final Method jaMoPPMethod : jaMoPPInterface.getMethods()) { + if (jaMoPPMethod.getName().equals(methodName)) { + return claimOne(getCorrespondingEObjects(jaMoPPMethod, OperationSignature.class)); + } + } + logger.warn("No JaMoPP method with name " + methodName + " found in " + interfaceName); + return null; + } + + protected OperationSignature renameMethodInClassWithName(final String className, final String methodName) + throws Throwable { + final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, + this.getCurrentTestProject()); + final IMethod iMethod = cu.getType(className).getMethod(methodName, null); + final int offset = iMethod.getNameRange().getOffset(); + final int length = iMethod.getNameRange().getLength(); + final String newMethodName = methodName + Pcm2JavaTestUtils.RENAME; + final ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, newMethodName); + editCompilationUnit(cu, replaceEdit); + return this.findOperationSignatureForJaMoPPMethodInCompilationUnit(newMethodName, className, cu); + } + + protected void assertDataTypeName(final TypeReference typeReference, final DataType pcmDataType) { + final String jaMoPPTypeName = getNameFromJaMoPPType(typeReference); + final String pcmTypeName = this.getNameFromPCMDataType(pcmDataType); + assertEquals(jaMoPPTypeName, pcmTypeName, "The name of the PCM datatype does not equal the JaMoPP type name"); + } + + protected String getNameFromPCMDataType(final DataType pcmDataType) { + if (null == pcmDataType) { + return "void"; + } else if (pcmDataType instanceof CollectionDataType) { + return ((CollectionDataType) pcmDataType).getEntityName(); + } else if (pcmDataType instanceof CompositeDataType) { + return ((CompositeDataType) pcmDataType).getEntityName(); + } else if (pcmDataType instanceof PrimitiveDataType) { + return this.getNameFromPrimitveDataType((PrimitiveDataType) pcmDataType); + } + throw new RuntimeException("getNameFromPCMDataType failed"); + } + + private String getNameFromPrimitveDataType(final PrimitiveDataType pcmDataType) { + switch (pcmDataType.getType()) { + case BOOL: + return "boolean"; + case CHAR: + return "char"; + case BYTE: + return "byte"; + case DOUBLE: + return "double"; + case INT: + return "int"; + case LONG: + return "long"; + case STRING: + return "String"; + } + throw new RuntimeException("getNameFromPrimitveDataType"); + } + + protected Parameter addParameterToSignature(final String interfaceName, final String methodName, + final String typeName, final String parameterName, final String[] parameterTypeSignatures) + throws Throwable { + final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, + this.getCurrentTestProject()); + final IMethod iMethod = icu.getType(interfaceName).getMethod(methodName, parameterTypeSignatures); + final String parameterStr = typeName + " " + parameterName; + return this.insertParameterIntoSignature(methodName, parameterName, icu, iMethod, parameterStr); + } + + protected Parameter insertParameterIntoSignature(final String methodName, final String parameterName, + final ICompilationUnit icu, final IMethod iMethod, final String parameterStr) + throws JavaModelException, Throwable { + final int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; + final InsertEdit insertEdit = new InsertEdit(offset, parameterStr); + editCompilationUnit(icu, insertEdit); + final ConcreteClassifier concreateClassifier = this + .getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); + final Method jaMoPPMethod = (Method) concreateClassifier.getMembersByName(methodName).get(0); + final org.emftext.language.java.parameters.Parameter jaMoPPParam = this + .getJaMoPPParameterFromJaMoPPMethod(jaMoPPMethod, parameterName); + return claimOne(getCorrespondingEObjects(jaMoPPParam, Parameter.class)); + } + + protected OperationSignature addReturnTypeToSignature(final String interfaceName, final String methodName, + final String typeName, String oldTypeName) throws Throwable { + if (null == oldTypeName) { + oldTypeName = "void"; + } + final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, + this.getCurrentTestProject()); + final IMethod iMethod = icu.getType(interfaceName).getMethod(methodName, null); + final String retTypeStr = typeName; + final int offset = iMethod.getSourceRange().getOffset() + + iMethod.getSourceRange().toString().indexOf(oldTypeName); + final ReplaceEdit replaceEdit = new ReplaceEdit(offset + 1, oldTypeName.length() + 1, retTypeStr); + editCompilationUnit(icu, replaceEdit); + final ConcreteClassifier concreateClassifier = this + .getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); + final Method jaMoPPMethod = (Method) concreateClassifier.getMembersByName(methodName).get(0); + return claimOne(getCorrespondingEObjects(jaMoPPMethod, OperationSignature.class)); + } + + protected org.emftext.language.java.parameters.Parameter getJaMoPPParameterFromJaMoPPMethod( + final Method jaMoPPMethod, final String parameterName) { + for (final org.emftext.language.java.parameters.Parameter jaMoPPParam : jaMoPPMethod.getParameters()) { + if (jaMoPPParam.getName().equals(parameterName)) { + return jaMoPPParam; + } + } + throw new RuntimeException( + "JaMoPP param with name " + parameterName + " not found in method " + jaMoPPMethod.getName()); + } + + protected org.emftext.language.java.parameters.Parameter findJaMoPPParameterInICU(final ICompilationUnit icu, + final String interfaceName, final String methodName, final String parameterName) { + final Method method = this.findJaMoPPMethodInICU(icu, methodName); + return this.getJaMoPPParameterFromJaMoPPMethod(method, parameterName); + } + + protected Method findJaMoPPMethodInICU(final ICompilationUnit icu, final String methodName) { + final ConcreteClassifier cc = this.getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); + final List jaMoPPMethods = cc.getMembersByName(methodName); + for (final Member member : jaMoPPMethods) { + if (member instanceof Method && member.getName().equals(methodName)) { + return (Method) member; + } + } + throw new RuntimeException("No method with name " + methodName + " found in " + icu); + } + + protected String getNameFromPCMPrimitiveDataType(final PrimitiveDataType primitiveDataType) { + return primitiveDataType.getType().getName(); + } + + protected Package getDatatypesPackage() throws Throwable { + return this.getPackageWithNameFromCorrespondenceModel("datatypes"); + } + + protected IMethod findIMethodByName(final String typeName, final String methodName, final ICompilationUnit icu) + throws JavaModelException { + final IType type = icu.getAllTypes()[0]; + logger.info(type.getMethods()); + for (final IMethod method : type.getMethods()) { + if (method.getElementName().equals(methodName)) { + return method; + } + } + throw new RuntimeException("Method not " + methodName + " not found in classifier " + typeName); + } + + protected IField findIFieldByName(final String className, final String fieldName, final ICompilationUnit icu) + throws JavaModelException { + final IType type = icu.getType(className); + return type.getField(fieldName); + } + + protected String addPackageAndImplementingClass(final String componentName) + throws CoreException, IOException, InterruptedException { + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + final Package mediaStorePackage = this.createPackageWithPackageInfo(Pcm2JavaTestUtils.REPOSITORY_NAME, + componentName); + this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + + final String implementingClassName = componentName + "Impl"; + this.addClassInPackage(mediaStorePackage, BasicComponent.class, implementingClassName); + return implementingClassName; + } + + protected OperationProvidedRole addImplementsCorrespondingToOperationProvidedRoleToClass(final String className, + final String implementingInterfaceName) throws CoreException { + final ICompilationUnit classCompilationUnit = CompilationUnitManipulatorHelper + .findICompilationUnitWithClassName(className, this.getCurrentTestProject()); + this.importCompilationUnitWithName(implementingInterfaceName, classCompilationUnit); + + final IType classType = classCompilationUnit.getType(className); + final String newSource = " implements " + implementingInterfaceName; + int offset = classType.getSourceRange().getOffset(); + final int firstBracket = classType.getSource().indexOf("{"); + offset = offset + firstBracket - 1; + final InsertEdit insertEdit = new InsertEdit(offset, newSource); + editCompilationUnit(classCompilationUnit, insertEdit); + final org.emftext.language.java.classifiers.Class jaMoPPClass = (org.emftext.language.java.classifiers.Class) this + .getJaMoPPClassifierForURI(createPlatformResourceURI(classCompilationUnit.getResource())); + final EList classImplements = jaMoPPClass.getImplements(); + logger.debug("Found implements: " + classImplements); + for (final TypeReference implementsReference : classImplements) { + logger.debug("Implements data: " + implementsReference.getTarget()); + final Iterable correspondingEObjects = getCorrespondingEObjects(implementsReference, + OperationProvidedRole.class); + logger.debug("Corresponding provided roles: " + correspondingEObjects); + if (null != correspondingEObjects && correspondingEObjects.iterator().hasNext()) { + return correspondingEObjects.iterator().next(); + } + } + throw new RuntimeException("Could not find an operation provided role for the newly created implements"); + } + + protected void importCompilationUnitWithName(final String implementingInterfaceName, + final ICompilationUnit classCompilationUnit) throws JavaModelException { + final ICompilationUnit interfaceCompilationUnit = CompilationUnitManipulatorHelper + .findICompilationUnitWithClassName(implementingInterfaceName, this.getCurrentTestProject()); + final String namespace = interfaceCompilationUnit.getType(implementingInterfaceName).getFullyQualifiedName(); + saveCurrentStateOfResourceAndRegisterForSynchronization( + URIUtil.createPlatformResourceURI(interfaceCompilationUnit.getResource())); + saveCurrentStateOfResourceAndRegisterForSynchronization( + URIUtil.createPlatformResourceURI(classCompilationUnit.getResource())); + classCompilationUnit.createImport(namespace, null, null); + } + + protected T addFieldToClassWithName(final String className, final String fieldType, + final String fieldName, final Class correspondingType) throws Throwable { + final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, + this.getCurrentTestProject()); + if (!fieldType.equals("String")) { + this.importCompilationUnitWithName(fieldType, icu); + } + final IType iClass = icu.getAllTypes()[0]; + final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); + final String fieldStr = "private " + fieldType + " " + fieldName + ";"; + final InsertEdit insertEdit = new InsertEdit(offset, fieldStr); + editCompilationUnit(icu, insertEdit); + final Field jaMoPPField = this.getJaMoPPFieldFromClass(icu, fieldName); + if (correspondingType == null) { + return null; + } + return claimOne(getCorrespondingEObjects(jaMoPPField, correspondingType)); + } + + protected Field getJaMoPPFieldFromClass(final ICompilationUnit icu, final String fieldName) { + final ConcreteClassifier cc = this.getJaMoPPClassifierForURI(createPlatformResourceURI(icu.getResource())); + final Field field = (Field) cc.getMembersByName(fieldName).get(0); + return field; + } + + // add Annotation via the framework + protected T addAnnotationToClassifier(final AnnotableAndModifiable annotable, + final String annotationName, final String annotationParameter, final Class classOfCorrespondingObject, + final String className) throws Throwable { + final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, + this.getCurrentTestProject()); + final IType type = cu.getType(className); + final int offset = CompilationUnitManipulatorHelper.getOffsetForAddingAnntationToClass(type); + final String composedName = "@" + annotationName + + (annotationParameter != null ? "(\"" + annotationParameter + "\")" : "") + + java.lang.System.lineSeparator(); + final InsertEdit insertEdit = new InsertEdit(offset, composedName); + editCompilationUnit(cu, insertEdit); + final Iterable eObjectsByType = getCorrespondingEObjects(annotable, classOfCorrespondingObject); + return claimOne(eObjectsByType); + } + + // add Annotation via the framework + protected T addAnnotationToField(final String fieldName, final String annotationName, + final String annotationParameter, final Class classOfCorrespondingObject, final String className) + throws Throwable { + final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, + this.getCurrentTestProject()); + final IType type = cu.getType(className); + final int offset = CompilationUnitManipulatorHelper.getOffsetForAddingAnntationToField(type, fieldName); + final String composedName = "@" + annotationName + + (annotationParameter != null ? "(\"" + annotationParameter + "\")" : "") + + java.lang.System.lineSeparator(); + final InsertEdit insertEdit = new InsertEdit(offset, composedName); + editCompilationUnit(cu, insertEdit); + final Field jaMoPPField = this.getJaMoPPFieldFromClass(cu, fieldName); + final Iterable eObjectsByType = getCorrespondingEObjects(jaMoPPField, classOfCorrespondingObject); + return claimOne(eObjectsByType); + } + + protected void assertCorrespondingSEFF(final ResourceDemandingSEFF correspondingSeff, String methodName) + throws Throwable { + final ClassMethod jaMoPPMethod = claimOne(getCorrespondingEObjects(correspondingSeff, ClassMethod.class)); + assertEquals(jaMoPPMethod.getName(), methodName); + } + + protected void assertOperationInterface(final Repository repo, final OperationInterface opIf, + final String expectedName) { + assertTrue(null != opIf, "The created operation interface is null"); + assertEquals(opIf.getEntityName(), expectedName, + "OperationInterface name does not equals the expected interface Name."); + assertEquals(repo.getId(), opIf.getRepository__Interface().getId(), + "The created operation interface is not in the repository"); + } + + public ICompilationUnit addMethodToCompilationUnit(final String compilationUnitName, final String methodString, + final IProject currentTestProject) throws JavaModelException { + final ICompilationUnit cu = CompilationUnitManipulatorHelper + .findICompilationUnitWithClassName(compilationUnitName, currentTestProject); + final IType firstType = cu.getAllTypes()[0]; + final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); + final InsertEdit insertEdit = new InsertEdit(offset, methodString); + editCompilationUnit(cu, insertEdit); + return cu; + } + +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java similarity index 98% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java index e64d23546..54f0e4dab 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IMethod; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java new file mode 100644 index 000000000..2eef8cd53 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java @@ -0,0 +1,98 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; + +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.CompositeComponent; +import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.system.System; + +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class PackageMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { + + /** + * first package is created --> should be mapped to a repository + * + * @throws Throwable + */ + @Test + public void testAddFirstPackage() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + assertEquals(Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED, repo.getEntityName(), + "Name of the repository is not the same as the name of the package"); + this.assertResourceAndFileForEObjects(repo); + this.assertFilesOnlyForEObjects(repo); + } + + /** + * second packages is added --> should be mapped to a basic component + * + * @throws Throwable + */ + @Test + public void testAddSecondPackage() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + + final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); + + this.assertRepositoryAndPCMName(repo, bc, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + this.assertFilesOnlyForEObjects(bc); + } + + @Test + public void testCreateCompositeComponent() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + + final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); + + this.assertRepositoryAndPCMName(repo, cc, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + this.assertFilesOnlyForEObjects(cc); + } + + @Test + public void testCreateSystem() throws Throwable { + final Repository repository = super.addRepoContractsAndDatatypesPackage(); + + final System system = super.addSecondPackageCorrespondsToSystem(); + + this.assertPCMNamedElement(system, Pcm2JavaTestUtils.SYSTEM_NAME); + this.assertFilesOnlyForEObjects(repository, system); + } + + @Test + public void testCreateNoCorrespondingObject() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + + super.addSecondPackageCorrespondsWithoutCorrespondences(); + assertNotNull(repo); + // TODO:what to check? + } + + @Test + public void testRenamePackage() throws Throwable { + final Repository repo = super.addRepoContractsAndDatatypesPackage(); + super.addSecondPackageCorrespondsToBasicComponent(); + + final String packageName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; + + final Package renamedPackage = super.renamePackage(this.secondPackage, packageName); + + final BasicComponent bc = claimOne(getCorrespondingEObjects(renamedPackage, BasicComponent.class)); + // repository of basic component has to be the repository + assertEquals(repo.getId(), bc.getRepository__RepositoryComponent().getId(), + "Repository of basic compoennt is not the repository: " + repo); + // name should be changed since there is no implementing class (yet) for the + // component + assertTrue(packageName.toLowerCase().contains(bc.getEntityName().toLowerCase()), + "The name of the basic component, which is '" + bc.getEntityName() + + "', is not contained in the name of the package: " + packageName + " "); + this.assertResourceAndFileForEObjects(bc); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java similarity index 96% rename from tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java rename to tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java index 6eaee7424..c56e06adf 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java @@ -1,4 +1,4 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/BlockingProgressMonitor.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/BlockingProgressMonitor.java new file mode 100644 index 000000000..4ef8b104e --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/BlockingProgressMonitor.java @@ -0,0 +1,84 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import java.util.function.Function; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; + +public class BlockingProgressMonitor extends NullProgressMonitor { + private static final int DEFAULT_TIMEOUT_MS = 500; + + private final Semaphore semaphore = new Semaphore(0); + + public BlockingProgressMonitor() { + super(); + } + + @Override + public void done() { + super.done(); + semaphore.release(); + } + + @Override + public void setCanceled(boolean cancelled) { + super.setCanceled(cancelled); + if (cancelled) { + semaphore.release(); + } + } + + public void waitForCompletion() { + try { + semaphore.tryAcquire(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void performSynchronous(Consumer action) { + BlockingProgressMonitor monitor = new BlockingProgressMonitor(); + action.accept(monitor); + monitor.waitForCompletion(); + } + + public static void acceptSynchronousThrowing(ThrowingConsumer action) throws E { + BlockingProgressMonitor monitor = new BlockingProgressMonitor(); + try { + action.accept(monitor); + } catch (Exception exception) { + throw exception; + } + monitor.waitForCompletion(); + } + + public static R performSynchronous(Function action) { + BlockingProgressMonitor monitor = new BlockingProgressMonitor(); + R result = action.apply(monitor); + monitor.waitForCompletion(); + return result; + } + + public static R applySynchronousThrowing(ThrowingFunction action) throws E { + BlockingProgressMonitor monitor = new BlockingProgressMonitor(); + R result = null; + try { + result = action.apply(monitor); + } catch (Exception e) { + throw e; + } + monitor.waitForCompletion(); + return result; + } + + public static interface ThrowingConsumer { + void accept(T t) throws E; + } + + public static interface ThrowingFunction { + R apply(T t) throws E; + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java new file mode 100644 index 000000000..48d96a058 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java @@ -0,0 +1,197 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createFileURI; +import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createPlatformResourceURI; +import static org.eclipse.emf.common.util.URI.createPlatformResourceURI; +import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.acceptSynchronousThrowing; +import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.applySynchronousThrowing; + +import java.nio.file.Path; + +import org.apache.log4j.Logger; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.jdt.core.IBuffer; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.refactoring.IJavaRefactorings; +import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; +import org.eclipse.jdt.internal.core.manipulation.StubUtility; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.Refactoring; +import org.eclipse.ltk.core.refactoring.RefactoringContribution; +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; +import org.emftext.language.java.classifiers.Classifier; +import org.emftext.language.java.classifiers.ConcreteClassifier; +import org.emftext.language.java.containers.CompilationUnit; +import org.emftext.language.java.containers.JavaRoot; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.function.ThrowingConsumer; + +import edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil; +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; + +public class JavaEditorManipulationUtil { + private static final Logger logger = Logger.getLogger(JavaEditorManipulationUtil.class); + private static final String SRC_FOLDER = "src"; + + private final JavaEditorView javaEditorView; + private final Path projectPath; + private IPackageFragmentRoot rootPackageFragment; + + JavaEditorManipulationUtil(JavaEditorView javaEditorView, Path projectPath) { + this.javaEditorView = javaEditorView; + this.projectPath = projectPath; + } + + public void createClass(String name, Package containingPackage, ThrowingConsumer initialization) throws CoreException { + IPackageFragment packageFragment = getPackageFragment(containingPackage); + ICompilationUnit compilationUnit = createEmptyCompilationUnit(packageFragment, "class", name); + if (initialization != null) { + try { + initialization.accept(compilationUnit); + } + catch (Throwable t) { + throw new IllegalStateException(t); + } + } + javaEditorView.reloadResource(getFileUri(compilationUnit)); + } + + public void editCompilationUnit(final ICompilationUnit cu, final TextEdit... edits) throws JavaModelException { + acceptSynchronousThrowing(monitor -> cu.becomeWorkingCopy(monitor)); + for (final TextEdit edit : edits) { + acceptSynchronousThrowing(monitor -> cu.applyTextEdit(edit, monitor)); + } + acceptSynchronousThrowing(monitor -> cu.reconcile(ICompilationUnit.NO_AST, false, null, monitor)); + acceptSynchronousThrowing(monitor -> cu.commitWorkingCopy(false, monitor)); + cu.discardWorkingCopy(); + acceptSynchronousThrowing(monitor -> cu.save(monitor, true)); + } + + public void renamePackage(final Package packageToRename, final String newName) throws CoreException { + final Resource resource = packageToRename.eResource(); + URI oldLocation = resource.getURI(); + final IFile iFile = getIFileForUri(resource.getURI(), projectPath); + IPath iPath = iFile.getProjectRelativePath(); + iPath = iPath.removeLastSegments(1); + final String newQualifiedName = packageToRename.getNamespacesAsString() + newName; + final IFolder iFolder = iFile.getProject().getFolder(iPath); + final IJavaElement javaPackage = JavaCore.create(iFolder); + String packageFile = String.join("/", packageToRename.getNamespaces()); + packageFile = packageFile + "/" + newName + "/package-info.java"; + refactorRenameJavaElement(newQualifiedName, javaPackage, IJavaRefactorings.RENAME_PACKAGE); + javaEditorView.moveResource(oldLocation, createFileURI(projectPath.resolve("src").resolve(packageFile).toFile())); + } + + public void renameClassifierWithName(IProject eclipseProject, final String entityName, final String newName) throws Throwable { + final ICompilationUnit cu = CompilationUnitManipulatorHelper + .findICompilationUnitWithClassName(entityName + ".java", eclipseProject); + final int offset = cu.getBuffer().getContents().indexOf(entityName); + if (cu.getBuffer() instanceof IBuffer.ITextEditCapability) { + logger.info(cu.getBuffer()); + } + final ReplaceEdit edit = new ReplaceEdit(offset, entityName.length(), newName); + editCompilationUnit(cu, edit); + final URI uri = createPlatformResourceURI(cu.getResource()); + final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); + } + + private void refactorRenameJavaElement(final String newName, final IJavaElement iJavaElement, + final String refactorRenameActionName) throws CoreException { + final RefactoringContribution refacContrib = RefactoringCore + .getRefactoringContribution(refactorRenameActionName); + final RefactoringStatus status = new RefactoringStatus(); + final RenameJavaElementDescriptor desc = (RenameJavaElementDescriptor) refacContrib.createDescriptor(); + desc.setUpdateReferences(true); + desc.setJavaElement(iJavaElement); + desc.setNewName(newName); + final Refactoring refactoring = desc.createRefactoring(status); + acceptSynchronousThrowing(monitor -> refactoring.checkInitialConditions(monitor)); + acceptSynchronousThrowing(monitor -> refactoring.checkFinalConditions(monitor)); + Change change = applySynchronousThrowing(monitor -> refactoring.createChange(monitor)); + acceptSynchronousThrowing(monitor -> change.perform(monitor)); + } + + private T getJaMoPPRootForURI(final URI uri) { + final Resource resource = new ResourceSetImpl().getResource(uri, true); + // unchecked is OK for the test. + @SuppressWarnings("unchecked") + final T javaRoot = (T) resource.getContents().get(0); + return javaRoot; + } + + protected ConcreteClassifier getJaMoPPClassifierForURI(final URI uri) { + final CompilationUnit cu = this.getJaMoPPRootForURI(uri); + final Classifier jaMoPPClassifier = cu.getClassifiers().get(0); + return (ConcreteClassifier) jaMoPPClassifier; + } + + private ICompilationUnit createEmptyCompilationUnit(IPackageFragment packageFragment, String typeName, String name) throws JavaModelException { + String lineDelimiter = StubUtility.getLineDelimiterUsed(packageFragment.getJavaProject()); + ICompilationUnit compilationUnit = applySynchronousThrowing(monitor -> packageFragment.createCompilationUnit(name + ".java", "", false, null)); + InsertEdit edit = new InsertEdit(0, "package " + packageFragment.getElementName() + ";" + lineDelimiter + + lineDelimiter + "public " + typeName + " " + name + " { }"); + editCompilationUnit(compilationUnit, edit); + return compilationUnit; + } + + private static IFile getIFileForUri(URI uri, Path projectPath) { + if (uri.isPlatform()) { + return URIUtil.getIFileForEMFUri(uri); + } + URI base = createFileURI(projectPath.toFile()); + String path = uri.deresolve(base).path(); + path = path.substring(path.indexOf("/") + 1); + URI platformUri = createPlatformResourceURI(projectPath.getFileName().resolve(path).normalize() + "/", + true); + return getIFileForUri(platformUri, projectPath); + } + + private URI getUriForElementInPackage(IPackageFragment packageFragment, String elementName) { + String uriString = packageFragment.getResource().getFullPath().toString() + "/" + elementName + ".java"; + return URI.createPlatformResourceURI(uriString, true); + } + + private URI getFileUri(ICompilationUnit compilationUnit) { + return URI.createFileURI(compilationUnit.getResource().getFullPath().toString()); + } + + private IPackageFragmentRoot getRootPackage() throws CoreException { + if (rootPackageFragment != null) { + return rootPackageFragment; + } + + IJavaProject javaProject = javaEditorView.getJavaProject(); + IFolder sourceFolder = javaEditorView.getEclipseProject().getFolder(SRC_FOLDER); + if (!sourceFolder.exists()) { + acceptSynchronousThrowing(monitor -> sourceFolder.create(true, true, monitor)); + } + return javaProject.getPackageFragmentRoot(sourceFolder); + } + + private IPackageFragment getPackageFragment(Package somePackage) throws CoreException { + String packageNamespace = somePackage.getNamespacesAsString() + somePackage.getName(); + for (IJavaElement javaElement : getRootPackage().getChildren()) { + if (javaElement instanceof IPackageFragment && javaElement.getElementName().equals(packageNamespace)) { + return (IPackageFragment)javaElement; + } + } + throw new IllegalStateException("No package fragment found for JaMoPP package " + somePackage); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java new file mode 100644 index 000000000..f9af5bf3d --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java @@ -0,0 +1,150 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.acceptSynchronousThrowing; + +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; + +import org.apache.log4j.Logger; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; + +import edu.kit.ipd.sdq.commons.util.org.eclipse.core.resources.IProjectUtil; +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.Java2PcmTransformationTest; +import tools.vitruv.change.composite.description.PropagatedChange; +import tools.vitruv.framework.views.CommittableView; +import tools.vitruv.framework.views.View; +import tools.vitruv.framework.views.ViewSelection; +import tools.vitruv.framework.views.ViewSelector; +import tools.vitruv.framework.views.ViewType; +import tools.vitruv.framework.views.changederivation.StateBasedChangeResolutionStrategy; + +public class JavaEditorView implements CommittableView { + private static final Logger logger = Logger.getLogger(Java2PcmTransformationTest.class); + + private final Path projectFolder; + private CommittableView view; + private IProject eclipseProject; + private IJavaProject javaProject; + + public JavaEditorView(Path projectFolder, View view) { + this.projectFolder = projectFolder; + this.view = view.withChangeDerivingTrait(); + createJavaProject(); + } + + public IProject getEclipseProject() { + return eclipseProject; + } + + public IJavaProject getJavaProject() { + return javaProject; + } + + public JavaEditorManipulationUtil getManipulationUtil() { + return new JavaEditorManipulationUtil(this, projectFolder); + } + + @Override + public Collection getRootObjects() { + return view.getRootObjects(); + } + + @Override + public boolean isModified() { + return view.isModified(); + } + + @Override + public boolean isOutdated() { + return view.isOutdated(); + } + + @Override + public void update() { + try { + closeJavaProject(); + } catch (CoreException e) { + logger.error("Failed to close Java project", e); + } + view.update(); + createJavaProject(); + } + + private void createJavaProject() { + String projectName = projectFolder.getFileName().toString(); + eclipseProject = IProjectUtil.createProjectAt(projectName, projectFolder); + IProjectUtil.configureAsJavaProject(eclipseProject); + javaProject = JavaCore.create(eclipseProject); + } + + @Override + public boolean isClosed() { + return view.isClosed(); + } + + @Override + public void registerRoot(EObject object, URI persistAt) { + view.registerRoot(object, persistAt); + } + + @Override + public void moveRoot(EObject object, URI newLocation) { + view.moveRoot(object, newLocation); + } + + @Override + public void reloadResource(URI location) { + view.reloadResource(location); + } + + @Override + public void moveResource(URI oldLocation, URI newLocation) { + view.moveResource(oldLocation, newLocation); + } + + @Override + public ViewSelection getSelection() { + return view.getSelection(); + } + + @Override + public ViewType getViewType() { + return view.getViewType(); + } + + @Override + public CommittableView withChangeRecordingTrait() { + return view.withChangeRecordingTrait(); + } + + @Override + public CommittableView withChangeDerivingTrait(StateBasedChangeResolutionStrategy changeResolutionStrategy) { + return view; + } + + @Override + public void close() throws Exception { + closeJavaProject(); + view.close(); + } + + private void closeJavaProject() throws CoreException { + if (eclipseProject == null) { + return; + } + acceptSynchronousThrowing(monitor -> eclipseProject.close(monitor)); + eclipseProject = null; + javaProject = null; + } + + @Override + public List commitChanges() { + return view.commitChanges(); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaPcmViewFactory.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaPcmViewFactory.java new file mode 100644 index 000000000..9df77d5dc --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaPcmViewFactory.java @@ -0,0 +1,73 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import java.nio.file.Path; +import java.util.List; +import java.util.function.Consumer; + +import org.eclipse.core.runtime.CoreException; +import org.emftext.language.java.containers.CompilationUnit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.function.ThrowingConsumer; +import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.system.System; + +import tools.vitruv.framework.views.CommittableView; +import tools.vitruv.framework.views.View; +import tools.vitruv.framework.views.ViewProvider; +import tools.vitruv.testutils.TestViewFactory; + +public class JavaPcmViewFactory extends TestViewFactory { + private final Path javaProjectFolder; + + public JavaPcmViewFactory(Path javaProjectFolder, ViewProvider viewProvider) { + super(viewProvider); + this.javaProjectFolder = javaProjectFolder; + } + + private View createJavaView() { + return createViewOfElements("Java packages and classes", List.of(Package.class, CompilationUnit.class)); + } + + private JavaEditorView createJavaEditorView() { + View javaView = createJavaView(); + return new JavaEditorView(javaProjectFolder, javaView); + } + + private View createPcmView() { + return createViewOfElements("PCM", List.of(Repository.class, System.class)); + } + + public void changeJavaView(Consumer modelModification) throws Exception { + changeViewRecordingChanges(createJavaView(), modelModification); + } + + /** + * Changes the Java view containing all Java packages and classes as root + * elements according to the given modification function. Derives the performed + * changes, commits the recorded changes, and closes the view afterwards. + */ + public void changeJavaEditorView(ThrowingConsumer modelModification) throws Exception { + JavaEditorView javaEditorView = createJavaEditorView(); + try { + modelModification.accept(javaEditorView); + } catch (CoreException e) { + throw e; + } catch (Throwable e) { + throw new IllegalStateException(e); + } + javaEditorView.commitChanges(); + javaEditorView.close(); + } + + /** + * Validates the Java view containing all packages and classes by applying the + * validation function and closes the view afterwards. + */ + public void validateJavaView(Consumer viewValidation) throws Exception { + validateView(createJavaView(), viewValidation); + } + + public void validatePcmView(Consumer viewValidation) throws Exception { + validateView(createPcmView(), viewValidation); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java new file mode 100644 index 000000000..3d384ce4a --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java @@ -0,0 +1,18 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; + +import java.util.stream.Collectors; + +import org.emftext.language.java.containers.Package; + +import tools.vitruv.framework.views.View; + +public class JavaQueryUtil { + private JavaQueryUtil() { + } + + public static Package claimPackage(View view, String name) { + return claimOne(view.getRootObjects(Package.class).stream().filter(it -> name.equals(it.getName())).collect(Collectors.toList())); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java new file mode 100644 index 000000000..e76d3b6b0 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java @@ -0,0 +1,81 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; + +import java.util.stream.Collectors; + +import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.repository.RepositoryComponent; +import org.palladiosimulator.pcm.system.System; + +import tools.vitruv.framework.views.View; + +public class PcmQueryUtil { + private PcmQueryUtil() { + } + + public static Repository claimSingleRepository(View view) { + return claimOne(view.getRootObjects(Repository.class)); + } + + public static System claimSingleSystem(View view) { + return claimOne(view.getRootObjects(System.class)); + } + + public static Repository claimPcmRepository(View view, String repositoryName) { + return claimOne(view.getRootObjects(Repository.class).stream() + .filter(it -> repositoryName.equals(it.getEntityName())).collect(Collectors.toList())); + } + + public static T claimComponent(Repository repository, String componentName, + Class componentType) { + return claimOne(repository.getComponents__Repository().stream().filter(componentType::isInstance) + .map(componentType::cast).filter(it -> componentName.equals(it.getEntityName())) + .collect(Collectors.toList())); + } +} + +//package tools.vitruv.applications.pcmjava.tests.pcm2java +// +//import edu.kit.ipd.sdq.activextendannotations.Utility +//import org.palladiosimulator.pcm.repository.CompositeDataType +//import org.palladiosimulator.pcm.repository.OperationInterface +//import org.palladiosimulator.pcm.repository.PrimitiveDataType +//import org.palladiosimulator.pcm.repository.PrimitiveTypeEnum +//import org.palladiosimulator.pcm.repository.Repository +//import org.palladiosimulator.pcm.repository.RepositoryComponent +//import org.palladiosimulator.pcm.system.System +//import tools.vitruv.framework.views.View +// +//import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne +// +//@Utility +//class PcmQueryUtil { +// static def Repository claimSinglePcmRepository(View view) { +// view.getRootObjects(Repository).claimOne +// } +// +// static def System claimSinglePcmSystem(View view) { +// view.getRootObjects(System).claimOne +// } +// +// static def Repository claimPcmRepository(View view, String repositoryName) { +// view.getRootObjects(Repository).filter[it.entityName == repositoryName].claimOne +// } +// +// static def RepositoryComponent claimComponent(Repository repository, String componentName) { +// repository.components__Repository.filter[it.entityName == componentName].claimOne +// } +// +// static def PrimitiveDataType claimPrimitiveDataType(Repository repository, PrimitiveTypeEnum primitiveType) { +// repository.dataTypes__Repository.filter(PrimitiveDataType).filter[it.type == primitiveType].claimOne +// } +// +// static def CompositeDataType claimCompositeDataType(Repository repository, String datatypeName) { +// repository.dataTypes__Repository.filter(CompositeDataType).filter[it.entityName == datatypeName].claimOne +// } +// +// static def OperationInterface claimOperationInterface(Repository repository, String interfaceName) { +// repository.interfaces__Repository.filter(OperationInterface).filter[it.entityName == interfaceName].claimOne +// } +//} \ No newline at end of file diff --git a/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java b/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java index f0033b437..8cb4f7656 100644 --- a/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java +++ b/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java @@ -25,6 +25,8 @@ public final class Pcm2JavaTestUtils { public static final String ASSEMBLY_CONTEXT_NAME = "assemblyContext"; public static final String COMPOSITE_COMPONENT_NAME = "TestCompositeComponent"; public static final String COLLECTION_DATA_TYPE_NAME = "TestCollectionDatatype"; + + public static final String IMPLEMENTING_CLASS_SUFFIX = "Impl"; // private ctor for util class private Pcm2JavaTestUtils() { From 8c4d1bba5caf270234d0521a8362a7d56c44e73a Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 15:11:48 +0100 Subject: [PATCH 02/11] fix javaeditor helpers and add more tests --- .../ClassMappingTransformationTest.java | 5 +- .../ClassMethodMappingTransformationTest.java | 37 ++++++++++++ .../FieldMappingTransformationTest.java | 60 +++++++++++++++++++ .../java2pcm/Java2PcmTransformationTest.java | 36 +++++++++++ .../util/JavaEditorManipulationUtil.java | 44 +++++++++----- .../javaeditor/util/JavaEditorView.java | 8 ++- .../pcmjava/javaeditor/util/PcmQueryUtil.java | 9 +++ 7 files changed, 177 insertions(+), 22 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index 8f8d4aa07..08d5ac201 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -1,6 +1,5 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; @@ -8,8 +7,6 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; -import java.util.stream.Collectors; - import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; @@ -93,7 +90,7 @@ void testAddClassInSecondPackageAsSystem() throws Exception { * Basic Component */ @Test - void addClassInSecondPackageAsNone() throws Exception { + void testAddClassInSecondPackageAsNone() throws Exception { createRepositoryPackage(); getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java new file mode 100644 index 000000000..d49a77d7e --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java @@ -0,0 +1,37 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class ClassMethodMappingTransformationTest extends Java2PcmTransformationTest { + @Test + void testCreateClassMethod() throws Exception { + //TODO: adapt + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "TODO", BasicComponent.class); + }); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java new file mode 100644 index 000000000..559df7d0a --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java @@ -0,0 +1,60 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimDataType; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IType; +import org.eclipse.text.edits.InsertEdit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.CompositeDataType; +import org.palladiosimulator.pcm.repository.InnerDeclaration; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class FieldMappingTransformationTest extends Java2PcmTransformationTest { + private static final String FIELD_TYPE = "String"; + private static final String FIELD_IDENTIFIER = "stringField"; + + private static final String DATATYPES_PACKAGE = "datatypes"; + + @Test + void testAddFieldToClassCorrespondingToCompositeComponent() throws Exception { + createRepositoryPackage(); + + String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); + changeJavaEditorView(view -> { + Package dataTypesPackage = claimPackage(view, DATATYPES_PACKAGE); + view.getManipulationUtil().createClass(compilationUnitName, dataTypesPackage, null); + }); + + changeJavaEditorView(view -> { + Package datatypesPackage = JavaQueryUtil.claimPackage(view, DATATYPES_PACKAGE); + ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); + + final IType iClass = compilationUnit.getAllTypes()[0]; + final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); + final String fieldStr = "private " + FIELD_TYPE + " " + FIELD_IDENTIFIER + ";"; + final InsertEdit insertEdit = new InsertEdit(offset, fieldStr); + view.getManipulationUtil().editCompilationUnit(compilationUnit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + CompositeDataType dataType = claimDataType(repository, compilationUnitName, CompositeDataType.class); + InnerDeclaration innerDeclaration = claimOne(dataType.getInnerDeclaration_CompositeDataType()); + assertEquals(FIELD_IDENTIFIER, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); + assertEquals(FIELD_TYPE, getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration())); + }); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java index c5769c422..71c2d04e7 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java @@ -11,6 +11,9 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.function.ThrowingConsumer; +import org.palladiosimulator.pcm.core.entity.NamedElement; +import org.palladiosimulator.pcm.repository.DataType; +import org.palladiosimulator.pcm.repository.PrimitiveDataType; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmChangePropagationSpecification; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaEditorView; @@ -78,6 +81,39 @@ protected Package createPackageWithPackageInfo(View view, final String... namesp return jaMoPPPackage; } + protected String getTypeNameOfPcmDataType(DataType dataType) { + if (dataType == null) { + return "void"; + } + else if (dataType instanceof PrimitiveDataType) { + return getTypeNameFromPrimitveDataType((PrimitiveDataType)dataType); + } + else if (dataType instanceof NamedElement) { + return ((NamedElement)dataType).getEntityName(); + } + throw new RuntimeException("Unknown data type " + dataType); + } + + private String getTypeNameFromPrimitveDataType(final PrimitiveDataType dataType) { + switch (dataType.getType()) { + case BOOL: + return "boolean"; + case CHAR: + return "char"; + case BYTE: + return "byte"; + case DOUBLE: + return "double"; + case INT: + return "int"; + case LONG: + return "long"; + case STRING: + return "String"; + } + throw new RuntimeException("Unknown primitive data type " + dataType); + } + // === Shortcuts === protected void changeJavaView(Consumer modelModification) throws Exception { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java index 48d96a058..5d4e5ecd9 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java @@ -1,6 +1,5 @@ package tools.vitruv.applications.pcmjava.javaeditor.util; -import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createFileURI; import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createPlatformResourceURI; import static org.eclipse.emf.common.util.URI.createPlatformResourceURI; import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.acceptSynchronousThrowing; @@ -20,7 +19,6 @@ import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; @@ -73,15 +71,26 @@ public void createClass(String name, Package containingPackage, ThrowingConsumer javaEditorView.reloadResource(getFileUri(compilationUnit)); } - public void editCompilationUnit(final ICompilationUnit cu, final TextEdit... edits) throws JavaModelException { - acceptSynchronousThrowing(monitor -> cu.becomeWorkingCopy(monitor)); + public ICompilationUnit claimCompilationUnit(String name, Package containingPackage) throws CoreException { + IPackageFragment packageFragment = getPackageFragment(containingPackage); + for (IJavaElement javaElement : packageFragment.getChildren()) { + if (javaElement instanceof ICompilationUnit && javaElement.getElementName().equals(name + ".java")) { + return (ICompilationUnit)javaElement; + } + } + throw new IllegalStateException("Could not find class " + name + " in package " + containingPackage); + } + + public void editCompilationUnit(final ICompilationUnit compilationUnit, final TextEdit... edits) throws JavaModelException { + acceptSynchronousThrowing(monitor -> compilationUnit.becomeWorkingCopy(monitor)); for (final TextEdit edit : edits) { - acceptSynchronousThrowing(monitor -> cu.applyTextEdit(edit, monitor)); + acceptSynchronousThrowing(monitor -> compilationUnit.applyTextEdit(edit, monitor)); } - acceptSynchronousThrowing(monitor -> cu.reconcile(ICompilationUnit.NO_AST, false, null, monitor)); - acceptSynchronousThrowing(monitor -> cu.commitWorkingCopy(false, monitor)); - cu.discardWorkingCopy(); - acceptSynchronousThrowing(monitor -> cu.save(monitor, true)); + acceptSynchronousThrowing(monitor -> compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, monitor)); + acceptSynchronousThrowing(monitor -> compilationUnit.commitWorkingCopy(true, monitor)); + compilationUnit.discardWorkingCopy(); + acceptSynchronousThrowing(monitor -> compilationUnit.save(monitor, true)); + javaEditorView.reloadResource(getFileUri(compilationUnit)); } public void renamePackage(final Package packageToRename, final String newName) throws CoreException { @@ -96,7 +105,7 @@ public void renamePackage(final Package packageToRename, final String newName) t String packageFile = String.join("/", packageToRename.getNamespaces()); packageFile = packageFile + "/" + newName + "/package-info.java"; refactorRenameJavaElement(newQualifiedName, javaPackage, IJavaRefactorings.RENAME_PACKAGE); - javaEditorView.moveResource(oldLocation, createFileURI(projectPath.resolve("src").resolve(packageFile).toFile())); + javaEditorView.moveResource(oldLocation, URIUtil.createFileURI(projectPath.resolve(SRC_FOLDER).resolve(packageFile).toFile())); } public void renameClassifierWithName(IProject eclipseProject, final String entityName, final String newName) throws Throwable { @@ -155,7 +164,7 @@ private static IFile getIFileForUri(URI uri, Path projectPath) { if (uri.isPlatform()) { return URIUtil.getIFileForEMFUri(uri); } - URI base = createFileURI(projectPath.toFile()); + URI base = URIUtil.createFileURI(projectPath.toFile()); String path = uri.deresolve(base).path(); path = path.substring(path.indexOf("/") + 1); URI platformUri = createPlatformResourceURI(projectPath.getFileName().resolve(path).normalize() + "/", @@ -168,8 +177,13 @@ private URI getUriForElementInPackage(IPackageFragment packageFragment, String e return URI.createPlatformResourceURI(uriString, true); } - private URI getFileUri(ICompilationUnit compilationUnit) { - return URI.createFileURI(compilationUnit.getResource().getFullPath().toString()); + private URI getFileUri(ICompilationUnit compilationUnit) throws JavaModelException { + String[] namespace = compilationUnit.getPackageDeclarations()[0].getElementName().split("\\."); + Path path = projectPath.resolve(SRC_FOLDER); + for (String partialNamespace : namespace) { + path = path.resolve(partialNamespace); + } + return URIUtil.createFileURI(path.resolve(compilationUnit.getElementName()).toFile()); } private IPackageFragmentRoot getRootPackage() throws CoreException { @@ -177,12 +191,12 @@ private IPackageFragmentRoot getRootPackage() throws CoreException { return rootPackageFragment; } - IJavaProject javaProject = javaEditorView.getJavaProject(); IFolder sourceFolder = javaEditorView.getEclipseProject().getFolder(SRC_FOLDER); if (!sourceFolder.exists()) { acceptSynchronousThrowing(monitor -> sourceFolder.create(true, true, monitor)); } - return javaProject.getPackageFragmentRoot(sourceFolder); + rootPackageFragment = javaEditorView.getJavaProject().getPackageFragmentRoot(sourceFolder); + return rootPackageFragment; } private IPackageFragment getPackageFragment(Package somePackage) throws CoreException { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java index f9af5bf3d..ba581a563 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java @@ -78,9 +78,11 @@ public void update() { private void createJavaProject() { String projectName = projectFolder.getFileName().toString(); - eclipseProject = IProjectUtil.createProjectAt(projectName, projectFolder); - IProjectUtil.configureAsJavaProject(eclipseProject); - javaProject = JavaCore.create(eclipseProject); + eclipseProject = IProjectUtil.getWorkspaceProject(projectName); + if (!eclipseProject.exists()) { + eclipseProject = IProjectUtil.createProjectAt(projectName, projectFolder); + } + javaProject = IProjectUtil.configureAsJavaProject(eclipseProject); } @Override diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java index e76d3b6b0..e3b4ccc9c 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java @@ -4,6 +4,8 @@ import java.util.stream.Collectors; +import org.palladiosimulator.pcm.core.entity.NamedElement; +import org.palladiosimulator.pcm.repository.DataType; import org.palladiosimulator.pcm.repository.Repository; import org.palladiosimulator.pcm.repository.RepositoryComponent; import org.palladiosimulator.pcm.system.System; @@ -33,6 +35,13 @@ public static T claimComponent(Repository reposi .map(componentType::cast).filter(it -> componentName.equals(it.getEntityName())) .collect(Collectors.toList())); } + + public static T claimDataType(Repository repository, String dataTypeName, + Class dataTypeType) { + return claimOne(repository.getDataTypes__Repository().stream().filter(dataTypeType::isInstance) + .map(dataTypeType::cast).filter(it -> dataTypeName.equals(it.getEntityName())) + .collect(Collectors.toList())); + } } //package tools.vitruv.applications.pcmjava.tests.pcm2java From 8990446a775734939a1570024b7f1f1e576c081c Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 16:10:04 +0100 Subject: [PATCH 03/11] more tests in javeditor --- .../FieldMappingTransformationTest.java | 135 +++++++++++++++--- .../InterfaceMappingTransformationTest.java | 116 +++++++++++++++ .../legacy/Java2PcmTransformationTest.java | 17 ++- .../PackageMappingTransformationTest.java | 98 ------------- .../util/JavaEditorManipulationUtil.java | 44 +++--- .../javaeditor/util/JavaEditorView.java | 1 - .../javaeditor/util/JavaQueryUtil.java | 3 + .../pcmjava/javaeditor/util/PcmQueryUtil.java | 67 ++------- 8 files changed, 281 insertions(+), 200 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java index 559df7d0a..91d5f6fa7 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java @@ -7,9 +7,12 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IType; +import org.eclipse.text.edits.DeleteEdit; import org.eclipse.text.edits.InsertEdit; import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.CompositeDataType; import org.palladiosimulator.pcm.repository.InnerDeclaration; @@ -22,39 +25,133 @@ class FieldMappingTransformationTest extends Java2PcmTransformationTest { private static final String FIELD_TYPE = "String"; - private static final String FIELD_IDENTIFIER = "stringField"; - - private static final String DATATYPES_PACKAGE = "datatypes"; - + private static final String FIELD_NAME = "stringField"; + @Test - void testAddFieldToClassCorrespondingToCompositeComponent() throws Exception { + void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { createRepositoryPackage(); - - String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; - + + String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); changeJavaEditorView(view -> { - Package dataTypesPackage = claimPackage(view, DATATYPES_PACKAGE); + Package dataTypesPackage = claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); view.getManipulationUtil().createClass(compilationUnitName, dataTypesPackage, null); }); - + changeJavaEditorView(view -> { - Package datatypesPackage = JavaQueryUtil.claimPackage(view, DATATYPES_PACKAGE); - ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); - - final IType iClass = compilationUnit.getAllTypes()[0]; - final int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); - final String fieldStr = "private " + FIELD_TYPE + " " + FIELD_IDENTIFIER + ";"; - final InsertEdit insertEdit = new InsertEdit(offset, fieldStr); + Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); + ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, + datatypesPackage); + + IType iClass = compilationUnit.getAllTypes()[0]; + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); + String fieldStr = "private " + FIELD_TYPE + " " + FIELD_NAME + ";"; + InsertEdit insertEdit = new InsertEdit(offset, fieldStr); view.getManipulationUtil().editCompilationUnit(compilationUnit, insertEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); CompositeDataType dataType = claimDataType(repository, compilationUnitName, CompositeDataType.class); InnerDeclaration innerDeclaration = claimOne(dataType.getInnerDeclaration_CompositeDataType()); - assertEquals(FIELD_IDENTIFIER, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); + assertEquals(FIELD_NAME, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); assertEquals(FIELD_TYPE, getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration())); }); } + + @Test + void testRenameFieldInClassCorrespondingToCompositeDataType() throws Exception { + testAddFieldToClassCorrespondingToCompositeDataType(); + + String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + String changedFieldName = FIELD_NAME + Pcm2JavaTestUtils.RENAME; + + changeJavaEditorView(view -> { + Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); + ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, + datatypesPackage); + + IType type = compilationUnit.getType(compilationUnitName); + IField fieldToRename = type.getField(FIELD_NAME); + String fieldToRenameStr = fieldToRename.getSource(); + String fieldToRenameType = fieldToRenameStr.split(" ")[1]; + String fieldToRenameName = fieldToRenameStr.split(" ")[2]; + int offset = fieldToRename.getSourceRange().getOffset() + fieldToRenameStr.indexOf(fieldToRenameType) + + fieldToRenameType.length() + 1; + int lengthToDelete = fieldToRenameName.length(); + DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); + InsertEdit insertEdit = new InsertEdit(offset, changedFieldName + ";"); + view.getManipulationUtil().editCompilationUnit(compilationUnit, deleteEdit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + CompositeDataType dataType = claimDataType(repository, compilationUnitName, CompositeDataType.class); + InnerDeclaration innerDeclaration = claimOne(dataType.getInnerDeclaration_CompositeDataType()); + assertEquals(changedFieldName, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); + assertEquals(FIELD_TYPE, getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration())); + }); + } + + @Test + void testChangeTypeOfFieldInClassCorrespondingToCompositeDataType() throws Exception { + testAddFieldToClassCorrespondingToCompositeDataType(); + + String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + String changedFieldTypeName = "int"; + + changeJavaEditorView(view -> { + Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); + ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, + datatypesPackage); + + IType type = compilationUnit.getType(compilationUnitName); + IField fieldToRename = type.getField(FIELD_NAME); + String fieldSrc = fieldToRename.getSource(); + String fieldType = fieldSrc.split(" ")[1]; + int offset = fieldToRename.getSourceRange().getOffset() + fieldSrc.indexOf(fieldType); + int lengthToDelete = fieldType.length(); + DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); + InsertEdit insertEdit = new InsertEdit(offset, changedFieldTypeName); + view.getManipulationUtil().editCompilationUnit(compilationUnit, deleteEdit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + CompositeDataType dataType = claimDataType(repository, compilationUnitName, CompositeDataType.class); + InnerDeclaration innerDeclaration = claimOne(dataType.getInnerDeclaration_CompositeDataType()); + assertEquals(FIELD_NAME, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); + assertEquals(changedFieldTypeName, + getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration())); + }); + } + + @Disabled("Not yet implemented") + @Test + void testRemoveFieldInClassCorrespondingToBasicComponent() { + } + + @Disabled("Not yet implemented") + @Test + void testAddFieldToClassCorrespondingToBasicComponent() { + } + + @Disabled("Not yet implemented") + @Test + void testAddFieldInClassWithoutCorrespondence() { + } + + @Test + void testAddFieldWithTypeOfInterface() { + //TODO: + } + + @Test + void testAddFieldWithTypeOfBasicComponent() { + //TODO: + } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java new file mode 100644 index 000000000..182ac4331 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java @@ -0,0 +1,116 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.eclipse.jdt.core.IBuffer; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.text.edits.ReplaceEdit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class InterfaceMappingTransformationTest extends Java2PcmTransformationTest { + /** + * interface in contracts package --> should automatically be mapped to + * operation interface + */ + @Test + void testAddInterfaceInContractsPackage() throws Exception { + createRepositoryPackage(); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + }); + } + + /** + * interface in non-repository package --> "user" should be asked and decide to + * add it + */ + @Test + void testAddInterfaceInNonRepositoryPackageAsArchitecturalInterface() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); + changeJavaEditorView(view -> { + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, componentPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + }); + } + + /** + * interface in non-repository package --> "user" should be asked and decide to + * not add it + */ + @Test + void testAddInterfaceInNonRepositoryPackageAsTechnicalInterface() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection( + Java2PcmUserSelection.SELECT_DONT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); + changeJavaEditorView(view -> { + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, componentPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertEquals(0, repository.getInterfaces__Repository().size(), "there must not exist an interface"); + }); + } + + @Test + void testRenameInterface() throws Exception { + createRepositoryPackage(); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + }); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + int offset = anInterface.getBuffer().getContents().indexOf(Pcm2JavaTestUtils.INTERFACE_NAME); + ReplaceEdit renameEdit = new ReplaceEdit(offset, Pcm2JavaTestUtils.INTERFACE_NAME.length(), Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME); + view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); + //TODO: resource URI is not adjusted to new name + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME, OperationInterface.class); + assertEquals(1, repository.getInterfaces__Repository().size(), "too many interfaces in repository"); + }); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java index 1fc223353..03927f32f 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java @@ -244,15 +244,14 @@ public void propagateChanges() { } refreshProject(); StateBasedChangeResolutionStrategy changeResolutionStrategy = new DefaultStateBasedChangeResolutionStrategy(); -// for (Entry modifiedResourceURI : oldToNewURIsOfModifiedResources.entrySet()) { -// Resource currentResource = loadResourceIndependentFromView(modifiedResourceURI.getValue()); -// Resource virtualModelResource = resourceAt(modifiedResourceURI.getKey()); -// VitruviusChange change = changeResolutionStrategy.getChangeSequenceBetween(currentResource, -// virtualModelResource); -// VitruviusChange unresolvedChange = change.unresolve(); -// record(resourceAt(modifiedResourceURI.getKey()).getResourceSet(), -// resourceSet -> unresolvedChange.resolveAndApply(localUuidResolver)); -// } + for (Entry modifiedResourceURI : oldToNewURIsOfModifiedResources.entrySet()) { + Resource currentResource = loadResourceIndependentFromView(modifiedResourceURI.getValue()); + VitruviusChange change = changeResolutionStrategy.getChangeSequenceBetween(currentResource, + resourceAt(modifiedResourceURI.getKey())); + VitruviusChange unresolvedChange = change.unresolve(); + record(resourceAt(modifiedResourceURI.getKey()).getResourceSet(), + resourceSet -> unresolvedChange.resolveAndApply(resourceSet)); + } oldToNewURIsOfModifiedResources.clear(); propagate(); disposeViewResources(); diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java deleted file mode 100644 index 2eef8cd53..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/PackageMappingTransformationTest.java +++ /dev/null @@ -1,98 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.emftext.language.java.containers.Package; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.CompositeComponent; -import org.palladiosimulator.pcm.repository.Repository; -import org.palladiosimulator.pcm.system.System; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class PackageMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - /** - * first package is created --> should be mapped to a repository - * - * @throws Throwable - */ - @Test - public void testAddFirstPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - assertEquals(Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED, repo.getEntityName(), - "Name of the repository is not the same as the name of the package"); - this.assertResourceAndFileForEObjects(repo); - this.assertFilesOnlyForEObjects(repo); - } - - /** - * second packages is added --> should be mapped to a basic component - * - * @throws Throwable - */ - @Test - public void testAddSecondPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - this.assertRepositoryAndPCMName(repo, bc, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - this.assertFilesOnlyForEObjects(bc); - } - - @Test - public void testCreateCompositeComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); - - this.assertRepositoryAndPCMName(repo, cc, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); - this.assertFilesOnlyForEObjects(cc); - } - - @Test - public void testCreateSystem() throws Throwable { - final Repository repository = super.addRepoContractsAndDatatypesPackage(); - - final System system = super.addSecondPackageCorrespondsToSystem(); - - this.assertPCMNamedElement(system, Pcm2JavaTestUtils.SYSTEM_NAME); - this.assertFilesOnlyForEObjects(repository, system); - } - - @Test - public void testCreateNoCorrespondingObject() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - super.addSecondPackageCorrespondsWithoutCorrespondences(); - assertNotNull(repo); - // TODO:what to check? - } - - @Test - public void testRenamePackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsToBasicComponent(); - - final String packageName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; - - final Package renamedPackage = super.renamePackage(this.secondPackage, packageName); - - final BasicComponent bc = claimOne(getCorrespondingEObjects(renamedPackage, BasicComponent.class)); - // repository of basic component has to be the repository - assertEquals(repo.getId(), bc.getRepository__RepositoryComponent().getId(), - "Repository of basic compoennt is not the repository: " + repo); - // name should be changed since there is no implementing class (yet) for the - // component - assertTrue(packageName.toLowerCase().contains(bc.getEntityName().toLowerCase()), - "The name of the basic component, which is '" + bc.getEntityName() - + "', is not contained in the name of the package: " + packageName + " "); - this.assertResourceAndFileForEObjects(bc); - } -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java index 5d4e5ecd9..f1a16efd4 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java @@ -1,6 +1,5 @@ package tools.vitruv.applications.pcmjava.javaeditor.util; -import static edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil.createPlatformResourceURI; import static org.eclipse.emf.common.util.URI.createPlatformResourceURI; import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.acceptSynchronousThrowing; import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.applySynchronousThrowing; @@ -10,13 +9,11 @@ import org.apache.log4j.Logger; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IPackageFragment; @@ -32,7 +29,6 @@ import org.eclipse.ltk.core.refactoring.RefactoringCore; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.ReplaceEdit; import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.classifiers.Classifier; import org.emftext.language.java.classifiers.ConcreteClassifier; @@ -42,7 +38,6 @@ import org.junit.jupiter.api.function.ThrowingConsumer; import edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil; -import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; public class JavaEditorManipulationUtil { private static final Logger logger = Logger.getLogger(JavaEditorManipulationUtil.class); @@ -58,8 +53,16 @@ public class JavaEditorManipulationUtil { } public void createClass(String name, Package containingPackage, ThrowingConsumer initialization) throws CoreException { + createCompilationUnit(name, "class", containingPackage, initialization); + } + + public void createInterface(String name, Package containingPackage, ThrowingConsumer initialization) throws CoreException { + createCompilationUnit(name, "interface", containingPackage, initialization); + } + + private void createCompilationUnit(String name, String type, Package containingPackage, ThrowingConsumer initialization) throws CoreException { IPackageFragment packageFragment = getPackageFragment(containingPackage); - ICompilationUnit compilationUnit = createEmptyCompilationUnit(packageFragment, "class", name); + ICompilationUnit compilationUnit = createEmptyCompilationUnit(packageFragment, type, name); if (initialization != null) { try { initialization.accept(compilationUnit); @@ -82,6 +85,19 @@ public ICompilationUnit claimCompilationUnit(String name, Package containingPack } public void editCompilationUnit(final ICompilationUnit compilationUnit, final TextEdit... edits) throws JavaModelException { + URI originalUri = getFileUri(compilationUnit); + editCompilationUnitInternal(compilationUnit, edits); + URI changedUri = getFileUri(compilationUnit); + if (originalUri != changedUri) { + javaEditorView.moveResource(originalUri, changedUri); + } + javaEditorView.reloadResource(changedUri); + } + + /** + * Modifies the compilation unit without notifying the java editor view about resource changes. + */ + private void editCompilationUnitInternal(final ICompilationUnit compilationUnit, final TextEdit... edits) throws JavaModelException { acceptSynchronousThrowing(monitor -> compilationUnit.becomeWorkingCopy(monitor)); for (final TextEdit edit : edits) { acceptSynchronousThrowing(monitor -> compilationUnit.applyTextEdit(edit, monitor)); @@ -90,7 +106,6 @@ public void editCompilationUnit(final ICompilationUnit compilationUnit, final Te acceptSynchronousThrowing(monitor -> compilationUnit.commitWorkingCopy(true, monitor)); compilationUnit.discardWorkingCopy(); acceptSynchronousThrowing(monitor -> compilationUnit.save(monitor, true)); - javaEditorView.reloadResource(getFileUri(compilationUnit)); } public void renamePackage(final Package packageToRename, final String newName) throws CoreException { @@ -108,19 +123,6 @@ public void renamePackage(final Package packageToRename, final String newName) t javaEditorView.moveResource(oldLocation, URIUtil.createFileURI(projectPath.resolve(SRC_FOLDER).resolve(packageFile).toFile())); } - public void renameClassifierWithName(IProject eclipseProject, final String entityName, final String newName) throws Throwable { - final ICompilationUnit cu = CompilationUnitManipulatorHelper - .findICompilationUnitWithClassName(entityName + ".java", eclipseProject); - final int offset = cu.getBuffer().getContents().indexOf(entityName); - if (cu.getBuffer() instanceof IBuffer.ITextEditCapability) { - logger.info(cu.getBuffer()); - } - final ReplaceEdit edit = new ReplaceEdit(offset, entityName.length(), newName); - editCompilationUnit(cu, edit); - final URI uri = createPlatformResourceURI(cu.getResource()); - final Classifier jaMoPPClass = this.getJaMoPPClassifierForURI(uri); - } - private void refactorRenameJavaElement(final String newName, final IJavaElement iJavaElement, final String refactorRenameActionName) throws CoreException { final RefactoringContribution refacContrib = RefactoringCore @@ -156,7 +158,7 @@ private ICompilationUnit createEmptyCompilationUnit(IPackageFragment packageFrag ICompilationUnit compilationUnit = applySynchronousThrowing(monitor -> packageFragment.createCompilationUnit(name + ".java", "", false, null)); InsertEdit edit = new InsertEdit(0, "package " + packageFragment.getElementName() + ";" + lineDelimiter + lineDelimiter + "public " + typeName + " " + name + " { }"); - editCompilationUnit(compilationUnit, edit); + editCompilationUnitInternal(compilationUnit, edit); return compilationUnit; } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java index ba581a563..1dce3b81b 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java @@ -12,7 +12,6 @@ import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaCore; import edu.kit.ipd.sdq.commons.util.org.eclipse.core.resources.IProjectUtil; import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.Java2PcmTransformationTest; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java index 3d384ce4a..404073e3d 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaQueryUtil.java @@ -9,6 +9,9 @@ import tools.vitruv.framework.views.View; public class JavaQueryUtil { + public static final String DATATYPES_PACKAGE = "datatypes"; + public static final String CONTRACTS_PACKAGE = "contracts"; + private JavaQueryUtil() { } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java index e3b4ccc9c..42bb810af 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java @@ -2,10 +2,12 @@ import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import java.util.Collection; import java.util.stream.Collectors; import org.palladiosimulator.pcm.core.entity.NamedElement; import org.palladiosimulator.pcm.repository.DataType; +import org.palladiosimulator.pcm.repository.Interface; import org.palladiosimulator.pcm.repository.Repository; import org.palladiosimulator.pcm.repository.RepositoryComponent; import org.palladiosimulator.pcm.system.System; @@ -31,60 +33,21 @@ public static Repository claimPcmRepository(View view, String repositoryName) { public static T claimComponent(Repository repository, String componentName, Class componentType) { - return claimOne(repository.getComponents__Repository().stream().filter(componentType::isInstance) - .map(componentType::cast).filter(it -> componentName.equals(it.getEntityName())) - .collect(Collectors.toList())); + return claimNamedElement(repository.getComponents__Repository(), componentName, componentType); } - + + public static T claimInterface(Repository repository, String interfaceName, + Class interfaceType) { + return claimNamedElement(repository.getInterfaces__Repository(), interfaceName, interfaceType); + } + public static T claimDataType(Repository repository, String dataTypeName, Class dataTypeType) { - return claimOne(repository.getDataTypes__Repository().stream().filter(dataTypeType::isInstance) - .map(dataTypeType::cast).filter(it -> dataTypeName.equals(it.getEntityName())) - .collect(Collectors.toList())); + return claimNamedElement(repository.getDataTypes__Repository(), dataTypeName, dataTypeType); } -} -//package tools.vitruv.applications.pcmjava.tests.pcm2java -// -//import edu.kit.ipd.sdq.activextendannotations.Utility -//import org.palladiosimulator.pcm.repository.CompositeDataType -//import org.palladiosimulator.pcm.repository.OperationInterface -//import org.palladiosimulator.pcm.repository.PrimitiveDataType -//import org.palladiosimulator.pcm.repository.PrimitiveTypeEnum -//import org.palladiosimulator.pcm.repository.Repository -//import org.palladiosimulator.pcm.repository.RepositoryComponent -//import org.palladiosimulator.pcm.system.System -//import tools.vitruv.framework.views.View -// -//import static extension edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne -// -//@Utility -//class PcmQueryUtil { -// static def Repository claimSinglePcmRepository(View view) { -// view.getRootObjects(Repository).claimOne -// } -// -// static def System claimSinglePcmSystem(View view) { -// view.getRootObjects(System).claimOne -// } -// -// static def Repository claimPcmRepository(View view, String repositoryName) { -// view.getRootObjects(Repository).filter[it.entityName == repositoryName].claimOne -// } -// -// static def RepositoryComponent claimComponent(Repository repository, String componentName) { -// repository.components__Repository.filter[it.entityName == componentName].claimOne -// } -// -// static def PrimitiveDataType claimPrimitiveDataType(Repository repository, PrimitiveTypeEnum primitiveType) { -// repository.dataTypes__Repository.filter(PrimitiveDataType).filter[it.type == primitiveType].claimOne -// } -// -// static def CompositeDataType claimCompositeDataType(Repository repository, String datatypeName) { -// repository.dataTypes__Repository.filter(CompositeDataType).filter[it.entityName == datatypeName].claimOne -// } -// -// static def OperationInterface claimOperationInterface(Repository repository, String interfaceName) { -// repository.interfaces__Repository.filter(OperationInterface).filter[it.entityName == interfaceName].claimOne -// } -//} \ No newline at end of file + private static T claimNamedElement(Collection elements, String name, Class type) { + return claimOne(elements.stream().filter(type::isInstance).map(type::cast) + .filter(it -> name.equals(it.getEntityName())).collect(Collectors.toList())); + } +} From 58fb4ff71a4d2eeee96b52989b4d80828c73f66c Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 16:31:42 +0100 Subject: [PATCH 04/11] refactor MethodMappingTransformationTest to view-based --- .../InterfaceMappingTransformationTest.java | 1 - .../MethodMappingTransformationTest.java | 110 ++++++++++++++++++ .../InterfaceMappingTransformationTest.java | 74 ------------ .../MethodMappingTransformationTest.java | 81 ------------- .../pcmjava/javaeditor/util/PcmQueryUtil.java | 4 + 5 files changed, 114 insertions(+), 156 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java index 182ac4331..79b23ea94 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java @@ -5,7 +5,6 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; -import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.text.edits.ReplaceEdit; import org.emftext.language.java.containers.Package; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java new file mode 100644 index 000000000..165cbd009 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java @@ -0,0 +1,110 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.text.edits.DeleteEdit; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationSignature; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class MethodMappingTransformationTest extends Java2PcmTransformationTest { + @Test + void testAddMethod() throws Exception { + createRepositoryPackage(); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + }); + + String returnType = "void"; + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + String methodSignature = "\n" + returnType + " " + methodName + "();\n"; + + IType firstType = anInterface.getAllTypes()[0]; + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); + InsertEdit insertEdit = new InsertEdit(offset, methodSignature); + view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + assertEquals(0, operationSignature.getParameters__OperationSignature().size()); + assertEquals(returnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); + }); + } + + @Test + void testRenameMethod() throws Exception { + testAddMethod(); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); + int offset = iMethod.getNameRange().getOffset(); + int length = iMethod.getNameRange().getLength(); + String newMethodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + Pcm2JavaTestUtils.RENAME; + ReplaceEdit renameEdit = new ReplaceEdit(offset, length, newMethodName); + view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + Pcm2JavaTestUtils.RENAME); + assertEquals(1, operationInterface.getSignatures__OperationInterface().size(), "too many operation signatures"); + }); + } + + @Test + void testAddReturnType() throws Exception { + testAddMethod(); + + String changedReturnType = "String"; + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); + int returnTypeOffset = iMethod.getSourceRange().getOffset(); + String retTypeName = iMethod.getSource().split(" ")[0]; + int returnTypeLength = retTypeName.length(); + DeleteEdit deleteEdit = new DeleteEdit(returnTypeOffset, returnTypeLength); + InsertEdit insertEdit = new InsertEdit(returnTypeOffset, changedReturnType); + view.getManipulationUtil().editCompilationUnit(anInterface, deleteEdit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + assertEquals(0, operationSignature.getParameters__OperationSignature().size()); + assertEquals(changedReturnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); + }); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java deleted file mode 100644 index 6e9d28f02..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/InterfaceMappingTransformationTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.eclipse.emf.ecore.EObject; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.OperationInterface; -import org.palladiosimulator.pcm.repository.Repository; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class InterfaceMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - /** - * interface in contracts package --> should automatically be mapped to - * operation interface - * - * @throws Throwable - */ - @Test - public void testAddInterfaceInContractsPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - - final OperationInterface opIf = super.addInterfaceInContractsPackage(); - - this.assertOperationInterface(repo, opIf, Pcm2JavaTestUtils.INTERFACE_NAME); - } - - /** - * interface in non-repository package --> "user" should be asked and decide to - * add it - * - * @throws Exception - */ - @Test - public void testAddArchitecturalInterfaceInNonRepositoryPackage() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - final OperationInterface opInterface = super.addInterfaceInSecondPackageWithCorrespondence(bc.getEntityName()); - - this.assertOperationInterface(repo, opInterface, Pcm2JavaTestUtils.INTERFACE_NAME); - } - - /** - * interface in non-repository package --> "user" should be asked and decide to - * not add it - * - * @throws Exception - */ - @Test - public void testAddTechnicalInterfaceInNonRepositoryPackage() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - final EObject eObject = super.addInterfaceInPackageWithoutCorrespondence(bc.getEntityName()); - - assertTrue(null == eObject, - "Corresponding object for interface that is created in non main package is not null: " + eObject); - } - - @Test - public void testRenameInterfaceWithCorrespondence() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsToBasicComponent(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - - final OperationInterface newOpInterface = this.renameClassifierWithName(opInterface.getEntityName(), - Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME, OperationInterface.class); - - this.assertOperationInterface(repo, newOpInterface, - Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME); - } -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java deleted file mode 100644 index 54f0e4dab..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/MethodMappingTransformationTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IMethod; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; -import org.emftext.language.java.members.Method; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.OperationInterface; -import org.palladiosimulator.pcm.repository.OperationSignature; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class MethodMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - @Test - public void testAddMethod() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - - this.assertOperationSignature(opSig, opInterface, Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); - } - - @Test - public void testRenameMethod() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - - final OperationSignature newOpSig = super.renameMethodInClassWithName(opInterface.getEntityName(), - opSig.getEntityName()); - - this.assertOperationSignature(newOpSig, opInterface, - Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + Pcm2JavaTestUtils.RENAME); - } - - @Test - public void testAddReturnType() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - - final OperationSignature newOpSig = this.changeReturnType(opSig); - - this.assertOperationSignature(newOpSig, opInterface, Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); - } - - private OperationSignature changeReturnType(final OperationSignature opSig) throws Throwable { - final String className = opSig.getInterface__OperationSignature().getEntityName(); - final String methodName = opSig.getEntityName(); - final ICompilationUnit cu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IMethod iMethod = cu.getType(className).getMethod(methodName, null); - final int returnTypeOffset = iMethod.getSourceRange().getOffset(); - final String retTypeName = iMethod.getSource().split(" ")[0]; - final int returnTypeLength = retTypeName.length(); - final String newReturnTypeName = "String "; - final DeleteEdit deleteEdit = new DeleteEdit(returnTypeOffset, returnTypeLength); - final InsertEdit insertEdit = new InsertEdit(returnTypeOffset, newReturnTypeName); - editCompilationUnit(cu, deleteEdit, insertEdit); - return super.findOperationSignatureForJaMoPPMethodInCompilationUnit(methodName, className, cu); - } - - private void assertOperationSignature(final OperationSignature opSig, final OperationInterface opInterface, - final String expectedName) throws Throwable { - assertEquals(opSig.getInterface__OperationSignature().getId(), opInterface.getId(), - "OperationSignature " + opSig + " is not in OperationInterface " + opInterface); - this.assertPCMNamedElement(opSig, expectedName); - - Method jaMoPPMethod = claimOne(getCorrespondingEObjects(opSig, Method.class)); - MethodMappingTransformationTest.this.assertDataTypeName(jaMoPPMethod.getTypeReference(), - opSig.getReturnType__OperationSignature()); - } -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java index 42bb810af..97f4173ab 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java @@ -45,6 +45,10 @@ public static T claimDataType(Repository rep Class dataTypeType) { return claimNamedElement(repository.getDataTypes__Repository(), dataTypeName, dataTypeType); } + + public static T claimNamedElement(Collection elements, String name) { + return claimOne(elements.stream().filter(it -> name.equals(it.getEntityName())).collect(Collectors.toList())); + } private static T claimNamedElement(Collection elements, String name, Class type) { return claimOne(elements.stream().filter(type::isInstance).map(type::cast) From c441aab1a1ddda6c097094e07f0bf5c0c004a8f7 Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 17:09:05 +0100 Subject: [PATCH 05/11] refactor TypeReferenceMappingTransformationTest to view-based --- .../ClassMappingTransformationTest.java | 16 ++-- ...ypeReferenceMappingTransformationTest.java | 83 +++++++++++++++++++ ...ypeReferenceMappingTransformationTest.java | 37 --------- .../util/JavaEditorManipulationUtil.java | 11 +++ .../pcmjava/javaeditor/util/PcmQueryUtil.java | 2 +- .../pcmjava/pcm2java/Pcm2JavaTestUtils.java | 6 ++ 6 files changed, 109 insertions(+), 46 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index 08d5ac201..288f80352 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -33,8 +33,8 @@ void testAddClassInSecondPackageAsBasicComponent() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); }); validatePcmView(view -> { @@ -54,8 +54,8 @@ void testAddClassInSecondPackageAsCompositeComponent() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); }); validatePcmView(view -> { @@ -75,8 +75,8 @@ void testAddClassInSecondPackageAsSystem() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.SYSTEM_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.SYSTEM_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + Package systemPackage = claimPackage(view, Pcm2JavaTestUtils.SYSTEM_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.SYSTEM_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, systemPackage, null); }); validatePcmView(view -> { @@ -100,8 +100,8 @@ void testAddClassInSecondPackageAsNone() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); }); validatePcmView(view -> { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java new file mode 100644 index 000000000..fe542f018 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java @@ -0,0 +1,83 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IType; +import org.eclipse.text.edits.InsertEdit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationProvidedRole; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class TypeReferenceMappingTransformationTest extends Java2PcmTransformationTest { + @Test + void testAddImplementsToClassCorrespondingToBasicComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass( + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, + componentPackage, null); + + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + }); + + changeJavaEditorView(view -> { + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, + componentPackage); + + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil() + .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, + Pcm2JavaTestUtils.INTERFACE_NAME); + + IType classType = componentClass + .getType(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + int offset = classType.getSourceRange().getOffset(); + int firstBracket = classType.getSource().indexOf("{"); + offset = offset + firstBracket - 1; + final InsertEdit insertEdit = new InsertEdit(offset, newSource); + view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + BasicComponent component = claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, + BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationProvidedRole providedRole = claimNamedElement( + component.getProvidedRoles_InterfaceProvidingEntity(), + Pcm2JavaTestUtils.providesInterfaceName(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, + Pcm2JavaTestUtils.INTERFACE_NAME), + OperationProvidedRole.class); + assertEquals(anInterface, providedRole.getProvidedInterface__OperationProvidedRole(), + "incorrect provided interface"); + }); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java deleted file mode 100644 index c56e06adf..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/TypeReferenceMappingTransformationTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.OperationInterface; -import org.palladiosimulator.pcm.repository.OperationProvidedRole; - -import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class TypeReferenceMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - @Test - public void testAddImplementsToClassWithCorrespondingComponent() throws Throwable { - // create repo - this.addRepoContractsAndDatatypesPackage(); - // create class - this.addSecondPackageCorrespondsWithoutCorrespondences(); - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent basicComponent = this.addClassInSecondPackage(BasicComponent.class); - // create interface - final OperationInterface opInterface = this.createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence( - "contracts", Pcm2JavaTestUtils.INTERFACE_NAME); - - // add the implement relation - final OperationProvidedRole opr = super.addImplementsCorrespondingToOperationProvidedRoleToClass( - basicComponent.getEntityName() + "Impl", opInterface.getEntityName()); - - assertEquals(opr.getProvidedInterface__OperationProvidedRole().getId(), opInterface.getId(), - "The interface proivieded by the provided role is not the expected interface"); - assertEquals(opr.getProvidingEntity_ProvidedRole().getId(), basicComponent.getId(), - "The component that provides the interface is not the expected component"); - } - -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java index f1a16efd4..f423812bf 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java @@ -102,6 +102,10 @@ private void editCompilationUnitInternal(final ICompilationUnit compilationUnit, for (final TextEdit edit : edits) { acceptSynchronousThrowing(monitor -> compilationUnit.applyTextEdit(edit, monitor)); } + saveCompilationUnit(compilationUnit); + } + + private void saveCompilationUnit(ICompilationUnit compilationUnit) throws JavaModelException { acceptSynchronousThrowing(monitor -> compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, monitor)); acceptSynchronousThrowing(monitor -> compilationUnit.commitWorkingCopy(true, monitor)); compilationUnit.discardWorkingCopy(); @@ -123,6 +127,13 @@ public void renamePackage(final Package packageToRename, final String newName) t javaEditorView.moveResource(oldLocation, URIUtil.createFileURI(projectPath.resolve(SRC_FOLDER).resolve(packageFile).toFile())); } + public void addImportToCompilationUnit(ICompilationUnit compilationUnit, ICompilationUnit compilationUnitToImport, String classifierToImport) throws JavaModelException { + acceptSynchronousThrowing(monitor -> compilationUnit.becomeWorkingCopy(monitor)); + final String namespace = compilationUnitToImport.getType(classifierToImport).getFullyQualifiedName(); + acceptSynchronousThrowing(monitor -> compilationUnit.createImport(namespace, null, monitor)); + saveCompilationUnit(compilationUnit); + } + private void refactorRenameJavaElement(final String newName, final IJavaElement iJavaElement, final String refactorRenameActionName) throws CoreException { final RefactoringContribution refacContrib = RefactoringCore diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java index 97f4173ab..d348e1a86 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/PcmQueryUtil.java @@ -50,7 +50,7 @@ public static T claimNamedElement(Collection element return claimOne(elements.stream().filter(it -> name.equals(it.getEntityName())).collect(Collectors.toList())); } - private static T claimNamedElement(Collection elements, String name, Class type) { + public static T claimNamedElement(Collection elements, String name, Class type) { return claimOne(elements.stream().filter(type::isInstance).map(type::cast) .filter(it -> name.equals(it.getEntityName())).collect(Collectors.toList())); } diff --git a/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java b/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java index 8cb4f7656..9c5859c41 100644 --- a/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java +++ b/tests/tools.vitruv.applications.pcmjava.tests/src/tools/vitruv/applications/pcmjava/pcm2java/Pcm2JavaTestUtils.java @@ -27,6 +27,8 @@ public final class Pcm2JavaTestUtils { public static final String COLLECTION_DATA_TYPE_NAME = "TestCollectionDatatype"; public static final String IMPLEMENTING_CLASS_SUFFIX = "Impl"; + + private static final String PROVIDES_INTERFACE = "_provides_"; // private ctor for util class private Pcm2JavaTestUtils() { @@ -57,4 +59,8 @@ public static System createSystem(final String systemName) throws Throwable { system.setEntityName(systemName); return system; } + + public static String providesInterfaceName(String componentName, String interfaceName) { + return componentName + PROVIDES_INTERFACE + interfaceName; + } } From 97a5a5892a5c0ad68622e60e968a445f9b352a6f Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 17:40:02 +0100 Subject: [PATCH 06/11] refactor ClassMethodMappingTransformationTest to view-based --- .../ClassMethodMappingTransformationTest.java | 88 +++++++++++++++++-- .../ClassMethodMappingTransformationTest.java | 36 -------- .../util/JavaEditorManipulationUtil.java | 2 - 3 files changed, 80 insertions(+), 46 deletions(-) delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java index d49a77d7e..fb40ff7ce 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java @@ -1,37 +1,109 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import static org.junit.jupiter.api.Assertions.assertEquals; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IType; +import org.eclipse.text.edits.InsertEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationProvidedRole; import org.palladiosimulator.pcm.repository.Repository; +import org.palladiosimulator.pcm.repository.Signature; +import org.palladiosimulator.pcm.seff.ServiceEffectSpecification; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class ClassMethodMappingTransformationTest extends Java2PcmTransformationTest { @Test void testCreateClassMethod() throws Exception { - //TODO: adapt createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); }); - + + // setup interface with one method + // setup class corresponding to basic component, implementing interface getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, innerPackage, null); + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass( + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, + componentPackage, null); + ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, + componentPackage); + + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + ICompilationUnit anInterface = view.getManipulationUtil() + .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + { + String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + String methodSignature = "\nvoid " + methodName + "();\n"; + IType firstType = anInterface.getAllTypes()[0]; + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); + InsertEdit insertEdit = new InsertEdit(offset, methodSignature); + view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + } + + { + view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, + Pcm2JavaTestUtils.INTERFACE_NAME); + + IType classType = componentClass + .getType(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + int offset = classType.getSourceRange().getOffset(); + int firstBracket = classType.getSource().indexOf("{"); + offset = offset + firstBracket - 1; + final InsertEdit insertEdit = new InsertEdit(offset, newSource); + view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); + } }); - + + // add implementation of interface method to class + changeJavaEditorView(view -> { + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( + Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, + componentPackage); + + final String methodBlock = "\n\tpublic void " + Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + + " () {\n\t}\n"; + IType firstType = componentClass.getAllTypes()[0]; + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); + InsertEdit insertEdit = new InsertEdit(offset, methodBlock); + view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); + }); + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "TODO", BasicComponent.class); + BasicComponent component = claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, + BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + Signature signature = claimOne(anInterface.getSignatures__OperationInterface()); + claimNamedElement(component.getProvidedRoles_InterfaceProvidingEntity(), Pcm2JavaTestUtils + .providesInterfaceName(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, Pcm2JavaTestUtils.INTERFACE_NAME), + OperationProvidedRole.class); + ServiceEffectSpecification seff = claimOne(component.getServiceEffectSpecifications__BasicComponent()); + assertEquals(signature, seff.getDescribedService__SEFF(), "SEFF is describing wrong service"); }); } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java deleted file mode 100644 index 09bfd39cd..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMethodMappingTransformationTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.seff.ResourceDemandingSEFF; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -public class ClassMethodMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - @Test - public void testAddClassMethodWithCorrespondence() throws Throwable { - // create repo - super.addRepoContractsAndDatatypesPackage(); - // this.getUserInteractor().addNextSelections(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - // create component implementing class - super.addPackageAndImplementingClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - // create interface - super.createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence("contracts", - Pcm2JavaTestUtils.INTERFACE_NAME); - // create interface method - super.addMethodToInterfaceWithCorrespondence(Pcm2JavaTestUtils.INTERFACE_NAME, - Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); - // add implements/provided role - super.addImplementsCorrespondingToOperationProvidedRoleToClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Impl", - Pcm2JavaTestUtils.INTERFACE_NAME); - - // actual test: add class method to implementing class that overrides the - // interface - final ResourceDemandingSEFF correspondingSeff = super.addClassMethodToClassThatOverridesInterfaceMethod( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Impl", Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); - - // assert the correspondingSEFF - assertCorrespondingSEFF(correspondingSeff, Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); - } - -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java index f423812bf..844968f90 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorManipulationUtil.java @@ -6,7 +6,6 @@ import java.nio.file.Path; -import org.apache.log4j.Logger; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.CoreException; @@ -40,7 +39,6 @@ import edu.kit.ipd.sdq.commons.util.org.eclipse.emf.common.util.URIUtil; public class JavaEditorManipulationUtil { - private static final Logger logger = Logger.getLogger(JavaEditorManipulationUtil.class); private static final String SRC_FOLDER = "src"; private final JavaEditorView javaEditorView; From a837075a3962486cde90f297d73e81b906730cc1 Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Mon, 21 Nov 2022 18:10:11 +0100 Subject: [PATCH 07/11] refactor ClassMappingTransformationTest to view-based --- .../META-INF/MANIFEST.MF | 1 + .../ClassMappingTransformationTest.java | 87 ++++++++-- .../ClassMappingTransformationTest.java | 164 ------------------ 3 files changed, 77 insertions(+), 175 deletions(-) delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF index 031144c58..69d1a8e94 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/META-INF/MANIFEST.MF @@ -13,6 +13,7 @@ Require-Bundle: org.apache.log4j, org.eclipse.text, org.eclipse.core.resources, org.junit.jupiter.api, + org.junit.jupiter.params, edu.kit.ipd.sdq.commons.util.eclipse, edu.kit.ipd.sdq.commons.util.emf, edu.kit.ipd.sdq.commons.util.java, diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index 288f80352..f08926c9a 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -4,29 +4,37 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimDataType; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.text.edits.ReplaceEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.palladiosimulator.pcm.repository.BasicComponent; import org.palladiosimulator.pcm.repository.CompositeComponent; +import org.palladiosimulator.pcm.repository.CompositeDataType; import org.palladiosimulator.pcm.repository.Repository; import org.palladiosimulator.pcm.system.System; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; -public class ClassMappingTransformationTest extends Java2PcmTransformationTest { +class ClassMappingTransformationTest extends Java2PcmTransformationTest { /** * Class that in mapped package and same name as component + impl--> should be * the new implementing class for the component */ - @Test - void testAddClassInSecondPackageAsBasicComponent() throws Exception { + @ParameterizedTest() + @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_BASIC_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + void testAddClassInSecondPackageAsBasicComponent(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); }); @@ -43,11 +51,12 @@ void testAddClassInSecondPackageAsBasicComponent() throws Exception { }); } - @Test - void testAddClassInSecondPackageAsCompositeComponent() throws Exception { + @ParameterizedTest() + @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_COMPOSITE_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + void testAddClassInSecondPackageAsCompositeComponent(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); }); @@ -64,11 +73,12 @@ void testAddClassInSecondPackageAsCompositeComponent() throws Exception { }); } - @Test - void testAddClassInSecondPackageAsSystem() throws Exception { + @ParameterizedTest() + @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_SYSTEM", "SELECT_NOTHING_DECIDE_LATER" }) + void testAddClassInSecondPackageAsSystem(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.SYSTEM_NAME); }); @@ -111,5 +121,60 @@ void testAddClassInSecondPackageAsNone() throws Exception { }); } - //TODO: add more + @Test + void testAddClassInDataTypesPackageAsCompositeDataType() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); + changeJavaEditorView(view -> { + Package dataTypesPackage = claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimDataType(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, CompositeDataType.class); + }); + } + + @Test + void testAddClassInDataTypesPackageAsNone() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); + changeJavaEditorView(view -> { + Package dataTypesPackage = claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); + view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, null); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + assertEquals(0, repository.getDataTypes__Repository().size(), "repository must not have any data type"); + }); + } + + @ParameterizedTest() + @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_BASIC_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + void testRenameClassCorrespondingToBasicComponent(Java2PcmUserSelection packageSelection) throws Exception { + testAddClassInSecondPackageAsBasicComponent(packageSelection); + + String changedName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; + + changeJavaEditorView(view -> { + String componentClassName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit(componentClassName, componentPackage); + + int offset = componentClass.getBuffer().getContents().indexOf(componentClassName); + ReplaceEdit renameEdit = new ReplaceEdit(offset, componentClassName.length(), changedName + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + view.getManipulationUtil().editCompilationUnit(componentClass, renameEdit); + //TODO: resource URI is not adjusted to new name + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + claimComponent(repository, changedName, BasicComponent.class); + assertEquals(1, repository.getComponents__Repository().size(), "too many components in repository"); + }); + } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java deleted file mode 100644 index cfee60edf..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/ClassMappingTransformationTest.java +++ /dev/null @@ -1,164 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import static org.junit.jupiter.api.Assertions.fail; - -import org.eclipse.emf.ecore.EObject; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.BasicComponent; -import org.palladiosimulator.pcm.repository.CollectionDataType; -import org.palladiosimulator.pcm.repository.CompositeComponent; -import org.palladiosimulator.pcm.repository.CompositeDataType; -import org.palladiosimulator.pcm.repository.Repository; -import org.palladiosimulator.pcm.system.System; - -import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -public class ClassMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - /** - * Class that in mapped package and same name as component + impl--> should be - * the new implementing class for the component - * - * @throws Exception - */ - @Test - public void testAddComponentClassInPackageWithCorrespondingComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - final BasicComponent bc = super.addSecondPackageCorrespondsToBasicComponent(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent bcForClass = super.addClassInSecondPackage(BasicComponent.class); - - super.assertRepositoryAndPCMName(repo, bcForClass, bc.getEntityName()); - } - - @Test - public void testAddCompositeComponentClassInPackageWithCorrespondingCompositeComponent() throws Throwable { - final Repository repo = super.addRepoContractsAndDatatypesPackage(); - final CompositeComponent cc = super.addSecondPackageCorrespondsToCompositeComponent(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CompositeComponent ccForClass = this.addClassInSecondPackage(CompositeComponent.class); - - super.assertRepositoryAndPCMName(repo, ccForClass, cc.getEntityName()); - } - - @Test - public void testAddSystemClassInPackageWithCorrespondingSystem() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final System pcmSystem = super.addSecondPackageCorrespondsToSystem(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); - final System systemForClass = super.addClassInSecondPackage(System.class); - - super.assertPCMNamedElement(systemForClass, pcmSystem.getEntityName()); - } - - /** - * Test ii) class in non corresponding package --> should not be mapped to a - * Basic Component - * - * @throws Throwable - */ - @Test - public void testAddClassInPackageWithoutCorrespondingComponent() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); - try { - final EObject eObject = super.addClassInPackage(this.secondPackage, EObject.class); - fail("The class should not have any correspondences, but it has a correspondence to eObject: " + eObject); - } catch (final RuntimeException re) { - // expected exception - } - } - - @Test - public void testAddBasicComponentClassInPackageWithoutCorrespondence() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent newBc = super.addClassInSecondPackage(BasicComponent.class); - - super.assertRepositoryAndPCMName(repo, newBc, newBc.getEntityName()); - } - - @Test - public void testAddCompositeComponentClassInPackageWithoutCorrespondence() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CompositeComponent cc = super.addClassInSecondPackage(CompositeComponent.class); - - super.assertRepositoryAndPCMName(repo, cc, cc.getEntityName()); - } - - @Test - public void testAddSystemClassInPackageWithoutCorrespondence() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - super.addSecondPackageCorrespondsWithoutCorrespondences(); - - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); - final System pcmSystem = super.addClassInSecondPackage(System.class); - - this.assertPCMNamedElement(pcmSystem, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); - } - - @Test - public void testAddCompositeDatatypeClassInDatatypePackage() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - - final CompositeDataType cdt = this.addClassThatCorrespondsToCompositeDatatype(); - - this.assertRepositoryAndPCMNameForDatatype(repo, cdt, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); - } - - @Test - public void testAddCollectionDatatypeClassInDatatypePackage() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); - final CollectionDataType collection = super.addClassInPackage(this.getDatatypesPackage(), - CollectionDataType.class); - - this.assertRepositoryAndPCMNameForDatatype(repo, collection, Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME); - } - - @Test - public void testAddClassInDatatypePackage() throws Throwable { - this.addRepoContractsAndDatatypesPackage(); - try { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); - final EObject eObject = super.addClassInPackage(this.getDatatypesPackage(), EObject.class); - fail("The class should not have any datatype correspondences, but it has a correspondence to eObject: " - + eObject); - } catch (final RuntimeException re) { - // expected Exception - } - } - - @Test - public void testRenameBasicComponentClass() throws Throwable { - final Repository repo = this.addRepoContractsAndDatatypesPackage(); - this.addSecondPackageCorrespondsWithoutCorrespondences(); - this.getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); - final BasicComponent basicComponent = this.addClassInSecondPackage(BasicComponent.class); - - final BasicComponent newBasicComponent = super.renameClassifierWithName(basicComponent.getEntityName() + "Impl", - Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME, BasicComponent.class); - - this.assertRepositoryAndPCMName(repo, newBasicComponent, - Pcm2JavaTestUtils.IMPLEMENTING_CLASS_NAME + Pcm2JavaTestUtils.RENAME); - this.assertFilesOnlyForEObjects(newBasicComponent); - } - -} From f3259a10f6bca53eff42a4dd011b0e64753b4613 Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Tue, 22 Nov 2022 11:35:24 +0100 Subject: [PATCH 08/11] implement workarounds for missing View API functionality in JavaEditorView --- .../javaeditor/util/JavaEditorView.java | 66 +++++++++++++++---- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java index 1dce3b81b..c644e63f5 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaEditorView.java @@ -2,15 +2,19 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.BlockingProgressMonitor.acceptSynchronousThrowing; +import java.io.IOException; import java.nio.file.Path; import java.util.Collection; import java.util.List; +import java.util.Optional; import org.apache.log4j.Logger; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.jdt.core.IJavaProject; import edu.kit.ipd.sdq.commons.util.org.eclipse.core.resources.IProjectUtil; @@ -25,26 +29,26 @@ public class JavaEditorView implements CommittableView { private static final Logger logger = Logger.getLogger(Java2PcmTransformationTest.class); - + private final Path projectFolder; private CommittableView view; private IProject eclipseProject; private IJavaProject javaProject; - + public JavaEditorView(Path projectFolder, View view) { this.projectFolder = projectFolder; this.view = view.withChangeDerivingTrait(); createJavaProject(); } - + public IProject getEclipseProject() { return eclipseProject; } - + public IJavaProject getJavaProject() { return javaProject; } - + public JavaEditorManipulationUtil getManipulationUtil() { return new JavaEditorManipulationUtil(this, projectFolder); } @@ -74,7 +78,7 @@ public void update() { view.update(); createJavaProject(); } - + private void createJavaProject() { String projectName = projectFolder.getFileName().toString(); eclipseProject = IProjectUtil.getWorkspaceProject(projectName); @@ -98,15 +102,51 @@ public void registerRoot(EObject object, URI persistAt) { public void moveRoot(EObject object, URI newLocation) { view.moveRoot(object, newLocation); } - - @Override + + /** + * Reloads a resource at the given location. + * + * @param location is the location of the resource. + */ public void reloadResource(URI location) { - view.reloadResource(location); + // TODO: this is just a hack to circumvent missing support from the View + // interface. The resources of the element should not be accessed directly, so + // for the long-term we need to evaluate whether we want to add the here + // implemented functionality to the View interface. + Resource oldResource = getRootObjects().stream().map(it -> it.eResource()) + .filter(r -> r.getURI().equals(location)).findAny().orElse(null); + if (oldResource == null) { + Resource resource = new ResourceSetImpl().getResource(location, true); + for (EObject root : resource.getContents()) { + view.registerRoot(root, location); + } + } else { + oldResource.unload(); + try { + oldResource.load(null); + } catch (IOException e) { + logger.error("Failed to load resource at " + location, e); + } + } } - - @Override + + /** + * Moves a resource from the old to the new location and reloads its content. + * + * @param oldLocation is the old location. + * @param newLocation is the new location. + */ public void moveResource(URI oldLocation, URI newLocation) { - view.moveResource(oldLocation, newLocation); + // TODO: this is just a hack to circumvent missing support from the View + // interface. The resources of the element should not be accessed directly, so + // for the long-term we need to evaluate whether we want to add the here + // implemented functionality to the View interface. + Optional existingResource = getRootObjects().stream().map(it -> it.eResource()) + .filter(r -> r.getURI().equals(oldLocation)).findAny(); + existingResource.ifPresent(resource -> { + resource.setURI(newLocation); + reloadResource(newLocation); + }); } @Override @@ -134,7 +174,7 @@ public void close() throws Exception { closeJavaProject(); view.close(); } - + private void closeJavaProject() throws CoreException { if (eclipseProject == null) { return; From 1745717e851219afa214e60884cd9d5e9cc84741 Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Tue, 22 Nov 2022 11:35:41 +0100 Subject: [PATCH 09/11] refactor missing javaeditor tests to view-based --- .../FieldMappingTransformationTest.java | 171 ++++++++++++++-- .../ParameterMappingTransformationTest.java | 148 ++++++++++++++ .../FieldMappingTransformationTest.java | 186 ------------------ ...oPPParameterMappingTransformationTest.java | 127 ------------ ...a2PcmPackageMappingTransformationTest.java | 26 --- 5 files changed, 307 insertions(+), 351 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java delete mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java index 91d5f6fa7..6fa2c7f4a 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java @@ -3,19 +3,26 @@ import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; import static org.junit.jupiter.api.Assertions.assertEquals; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimDataType; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.text.edits.DeleteEdit; import org.eclipse.text.edits.InsertEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.BasicComponent; import org.palladiosimulator.pcm.repository.CompositeDataType; import org.palladiosimulator.pcm.repository.InnerDeclaration; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationRequiredRole; import org.palladiosimulator.pcm.repository.Repository; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; @@ -27,6 +34,9 @@ class FieldMappingTransformationTest extends Java2PcmTransformationTest { private static final String FIELD_TYPE = "String"; private static final String FIELD_NAME = "stringField"; + private static final String PROVIDING_COMPONENT_NAME = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing"; + private static final String REQUIRING_COMPONENT_NAME = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring"; + @Test void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { createRepositoryPackage(); @@ -45,10 +55,7 @@ void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); - IType iClass = compilationUnit.getAllTypes()[0]; - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); - String fieldStr = "private " + FIELD_TYPE + " " + FIELD_NAME + ";"; - InsertEdit insertEdit = new InsertEdit(offset, fieldStr); + InsertEdit insertEdit = editForAddingField(FIELD_NAME, FIELD_TYPE, compilationUnit, compilationUnitName); view.getManipulationUtil().editCompilationUnit(compilationUnit, insertEdit); }); @@ -126,32 +133,172 @@ void testChangeTypeOfFieldInClassCorrespondingToCompositeDataType() throws Excep InnerDeclaration innerDeclaration = claimOne(dataType.getInnerDeclaration_CompositeDataType()); assertEquals(FIELD_NAME, innerDeclaration.getEntityName(), "incorrect name for InnerDeclaration"); assertEquals(changedFieldTypeName, - getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration())); + getTypeNameOfPcmDataType(innerDeclaration.getDatatype_InnerDeclaration()), + "incorrect type for InnerDeclaration"); }); } @Disabled("Not yet implemented") @Test - void testRemoveFieldInClassCorrespondingToBasicComponent() { + void testRemoveFieldInClassCorrespondingToBasicComponent() throws Exception { } @Disabled("Not yet implemented") @Test - void testAddFieldToClassCorrespondingToBasicComponent() { + void testAddFieldToClassCorrespondingToBasicComponent() throws Exception { } @Disabled("Not yet implemented") @Test - void testAddFieldInClassWithoutCorrespondence() { + void testAddFieldInClassWithoutCorrespondence() throws Exception { } @Test - void testAddFieldWithTypeOfInterface() { - //TODO: + void testAddFieldWithTypeOfInterface() throws Exception { + setupExampleWithProvidingAndRequiringComponent(); + + String fieldType = Pcm2JavaTestUtils.INTERFACE_NAME; + String fieldName = "i" + Pcm2JavaTestUtils.INTERFACE_NAME; + + changeJavaEditorView(view -> { + Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); + + Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil() + .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, + Pcm2JavaTestUtils.INTERFACE_NAME); + + InsertEdit insertEdit = editForAddingField(fieldName, fieldType, providingCompilationUnit, + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + BasicComponent providingComponent = claimComponent(repository, PROVIDING_COMPONENT_NAME, + BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationRequiredRole requiredRole = claimNamedElement( + providingComponent.getRequiredRoles_InterfaceRequiringEntity(), fieldName, + OperationRequiredRole.class); + assertEquals(fieldName, requiredRole.getEntityName(), "incorrect name for RequiredRole"); + assertEquals(anInterface, requiredRole.getRequiredInterface__OperationRequiredRole(), + "incorrect interface for RequiredRole"); + }); } @Test - void testAddFieldWithTypeOfBasicComponent() { - //TODO: + void testAddFieldWithTypeOfBasicComponent() throws Exception { + setupExampleWithProvidingAndRequiringComponent(); + + String fieldName = PROVIDING_COMPONENT_NAME.substring(0, 1).toLowerCase() + + PROVIDING_COMPONENT_NAME.substring(1); + String fieldType = PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package requiringPackage = JavaQueryUtil.claimPackage(view, REQUIRING_COMPONENT_NAME); + ICompilationUnit requiringCompilationUnit = view.getManipulationUtil().claimCompilationUnit( + REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, requiringPackage); + + Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); + view.getManipulationUtil().addImportToCompilationUnit(requiringCompilationUnit, providingCompilationUnit, + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + + InsertEdit insertEdit = editForAddingField(fieldName, fieldType, requiringCompilationUnit, + REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + view.getManipulationUtil().editCompilationUnit(requiringCompilationUnit, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + BasicComponent requiringComponent = claimComponent(repository, REQUIRING_COMPONENT_NAME, + BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationRequiredRole requiredRole = claimNamedElement( + requiringComponent.getRequiredRoles_InterfaceRequiringEntity(), fieldName, + OperationRequiredRole.class); + assertEquals(fieldName, requiredRole.getEntityName(), "incorrect name for RequiredRole"); + assertEquals(anInterface, requiredRole.getRequiredInterface__OperationRequiredRole(), + "incorrect interface for RequiredRole"); + }); + } + + /** + * Sets up an example scenario with the root Repository and two packages named + * {@link REQUIRING_COMPONENT_NAME} and {@link PROVIDING_COMPONENT_NAME} with + * each one class with suffix {@Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX} as + * basic components. Sets up an interface named + * {@link Pcm2JavaTestUtils.INTERFACE_NAME} in the contracts package. Sets up an + * {@code implements} relation between the providing component class and the + * interface. + */ + private void setupExampleWithProvidingAndRequiringComponent() throws Exception { + createRepositoryPackage(); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaView(view -> { + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, REQUIRING_COMPONENT_NAME); + createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, PROVIDING_COMPONENT_NAME); + }); + + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); + changeJavaEditorView(view -> { + Package requiringPackage = JavaQueryUtil.claimPackage(view, REQUIRING_COMPONENT_NAME); + view.getManipulationUtil().createClass( + REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, requiringPackage, null); + + Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); + view.getManipulationUtil().createClass( + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage, null); + + Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + + { + ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( + PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); + ICompilationUnit anInterface = view.getManipulationUtil() + .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, + Pcm2JavaTestUtils.INTERFACE_NAME); + + IType classType = providingCompilationUnit + .getType(PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + int offset = classType.getSourceRange().getOffset(); + int firstBracket = classType.getSource().indexOf("{"); + offset = offset + firstBracket - 1; + final InsertEdit insertEdit = new InsertEdit(offset, newSource); + view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, insertEdit); + } + }); + } + + /** + * Creates an edit action that inserts a private field into the class + * + * @param fieldName is the field name + * @param fieldType is the field type + * @param compilationUnit is the compilation unit to edit + * @param typeName is the type within the compilation unit to edit + * @return an edit action that can be applied to the compilation unit to add a + * field + */ + private InsertEdit editForAddingField(String fieldName, String fieldType, ICompilationUnit compilationUnit, + String typeName) throws JavaModelException { + IType iClass = compilationUnit.getType(typeName); + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); + String fieldStr = "private " + fieldType + " " + fieldName + ";"; + return new InsertEdit(offset, fieldStr); } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java new file mode 100644 index 000000000..d4ba510a5 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java @@ -0,0 +1,148 @@ +package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; + +import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; +import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.ILocalVariable; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.emftext.language.java.containers.Package; +import org.junit.jupiter.api.Test; +import org.palladiosimulator.pcm.repository.OperationInterface; +import org.palladiosimulator.pcm.repository.OperationSignature; +import org.palladiosimulator.pcm.repository.Parameter; +import org.palladiosimulator.pcm.repository.Repository; + +import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; + +class ParameterMappingTransformationTest extends Java2PcmTransformationTest { + private static final String PARAMETER_TYPE = "String"; + + @Test + void testAddParameter() throws Exception { + createRepositoryPackage(); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + + String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + String returnType = "void"; + String methodSignature = "\n" + returnType + " " + methodName + "();\n"; + + IType firstType = anInterface.getAllTypes()[0]; + int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); + InsertEdit insertEdit = new InsertEdit(offset, methodSignature); + view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + }); + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); + String parameterString = PARAMETER_TYPE + " " + Pcm2JavaTestUtils.PARAMETER_NAME; + int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; + InsertEdit insertEdit = new InsertEdit(offset, parameterString); + view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); + assertEquals(Pcm2JavaTestUtils.PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); + assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + }); + } + + @Test + void testRenameParameter() throws Exception { + testAddParameter(); + + String changedParameterName = Pcm2JavaTestUtils.PARAMETER_NAME + Pcm2JavaTestUtils.RENAME; + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + IMethod iMethod = findIMethodByName(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME)); + ILocalVariable parameter = this.findParameterInIMethod(iMethod, Pcm2JavaTestUtils.PARAMETER_NAME); + String typeName = parameter.getSource().split(" ")[0]; + String paramName = parameter.getSource().split(" ")[1]; + int offset = parameter.getSourceRange().getOffset() + typeName.length() + 1; + int length = paramName.length(); + ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterName); + view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); + assertEquals(changedParameterName, parameter.getParameterName(), "invalid parameter name"); + assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + }); + } + + @Test + void testChangeParameterType() throws Exception { + testAddParameter(); + + String changedParameterType = "int"; + + changeJavaEditorView(view -> { + Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + IMethod iMethod = findIMethodByName(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME)); + ILocalVariable parameter = this.findParameterInIMethod(iMethod, Pcm2JavaTestUtils.PARAMETER_NAME); + int offset = parameter.getSourceRange().getOffset(); + int length = parameter.getSource().split(" ")[0].length(); + ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterType); + view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); + }); + + validatePcmView(view -> { + Repository repository = claimSingleRepository(view); + OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, + OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); + assertEquals(Pcm2JavaTestUtils.PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); + assertEquals(changedParameterType, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + }); + } + + protected IMethod findIMethodByName(final String methodName, final IType type) + throws JavaModelException { + for (final IMethod method : type.getMethods()) { + if (method.getElementName().equals(methodName)) { + return method; + } + } + throw new RuntimeException("Method " + methodName + " not found in type " + type); + } + + private ILocalVariable findParameterInIMethod(final IMethod iMethod, final String parameterName) + throws JavaModelException { + for (final ILocalVariable localVariable : iMethod.getParameters()) { + if (localVariable.getElementName().equals(parameterName)) { + return localVariable; + } + } + throw new RuntimeException("Old parameter with name " + parameterName + " not found"); + } +} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java deleted file mode 100644 index e93123807..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/FieldMappingTransformationTest.java +++ /dev/null @@ -1,186 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IField; -import org.eclipse.jdt.core.IType; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; -import org.emftext.language.java.members.Field; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.repository.CompositeDataType; -import org.palladiosimulator.pcm.repository.InnerDeclaration; -import org.palladiosimulator.pcm.repository.OperationRequiredRole; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class FieldMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - @Test - public void testAddFieldToClassThatCorrespondsToCompositeDatatype() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final CompositeDataType cdt = super.addClassThatCorrespondsToCompositeDatatype(); - final String fieldType = "String"; - final String fieldName = "stringField"; - - final InnerDeclaration innerDeclaration = this.addFieldToClassWithName(cdt.getEntityName(), fieldType, - fieldName, InnerDeclaration.class); - - this.assertInnerDeclaration(innerDeclaration, fieldType, fieldName); - } - - @Test - public void testRenameFieldInClassThatCorrespondsToCompositeDatatype() throws Throwable { - final String fieldTypeName = "String"; - final String fieldName = "stringField"; - super.addRepoContractsAndDatatypesPackage(); - final CompositeDataType cdt = super.addClassThatCorrespondsToCompositeDatatype(); - this.addFieldToClassWithName(cdt.getEntityName(), fieldTypeName, fieldName, InnerDeclaration.class); - - final String newFieldName = fieldName + Pcm2JavaTestUtils.RENAME; - final InnerDeclaration newInnerDeclaration = this.renameFieldInClass(cdt.getEntityName(), fieldName, - newFieldName); - - this.assertInnerDeclaration(newInnerDeclaration, fieldTypeName, newFieldName); - } - - @Test - public void testChangeTypeOfFieldInClassThatCorrespondsToCompositeDatatype() throws Throwable { - final String fieldTypeName = "String"; - final String fieldName = "stringField"; - super.addRepoContractsAndDatatypesPackage(); - final CompositeDataType cdt = super.addClassThatCorrespondsToCompositeDatatype(); - this.addFieldToClassWithName(cdt.getEntityName(), fieldTypeName, fieldName, InnerDeclaration.class); - - final String newFieldTypeName = "int"; - final InnerDeclaration newInnerDeclaration = this.changeFieldTypeInClass(cdt.getEntityName(), fieldName, - newFieldTypeName); - - this.assertInnerDeclaration(newInnerDeclaration, newFieldTypeName, fieldName); - } - - @Disabled("Not yet implemented") - @Test - public void testRemoveFieldInClassThatCorrespondsToBasicComponent() { - } - - @Disabled("Not yet implemented") - @Test - public void testAddFieldToClassThatCorrespondsToBasicComponent() { - } - - @Disabled("Not yet implemented") - @Test - public void testAddFieldInClassWithoutCorrespondence() { - } - - @Test - public void testAddFieldWithTypeOfInterface() throws Throwable { - this.createRepoBasicComponentAndInterface(); - - // create required role from Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring" to Interface - final OperationRequiredRole orrToInterface = this.addFieldToClassWithName( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring" + "Impl", Pcm2JavaTestUtils.INTERFACE_NAME, - "i" + Pcm2JavaTestUtils.INTERFACE_NAME, OperationRequiredRole.class); - - this.assertOperationRequiredRole(orrToInterface); - } - - @Test - public void testAddFieldWithTypeOfBasicComponentToClass() throws Throwable { - this.createRepoBasicComponentAndInterface(); - - // create required role from Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring" to - // Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing" - final OperationRequiredRole orrToInterface = this.addFieldToClassWithName( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring" + "Impl", - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing" + "Impl", - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME.toLowerCase() + "Providing", OperationRequiredRole.class); - - this.assertOperationRequiredRole(orrToInterface); - } - - private void createRepoBasicComponentAndInterface() throws CoreException, IOException, InterruptedException { - // create main package - super.addRepoContractsAndDatatypesPackage(); - // create package and classes - this.addPackageAndImplementingClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing"); - this.addPackageAndImplementingClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring"); - // create interface - super.createInterfaceInPackageBasedOnJaMoPPPackageWithCorrespondence("contracts", - Pcm2JavaTestUtils.INTERFACE_NAME); - // create provided role from providing component to interface - super.addImplementsCorrespondingToOperationProvidedRoleToClass( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing" + "Impl", Pcm2JavaTestUtils.INTERFACE_NAME); - } - - private void assertOperationRequiredRole(final OperationRequiredRole operationRequiredRole) throws Throwable { - Iterable correspondingEObjects = getCorrespondingEObjects(operationRequiredRole, EObject.class); - - boolean fieldFound = false; - for (final EObject correspondingEObject : correspondingEObjects) { - if (correspondingEObject instanceof Field) { - fieldFound = true; - } else { - fail("OperationRequiredRole should correspond to field only, but corresonds also to: " - + correspondingEObject); - } - } - assertTrue(fieldFound, "OperationRequiredRole does not correspond to a field"); - } - - private InnerDeclaration renameFieldInClass(final String className, final String fieldName, - final String newFieldName) throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IType type = icu.getType(className); - final IField fieldToRename = type.getField(fieldName); - final String fieldToRenameStr = fieldToRename.getSource(); - final String fieldToRenameType = fieldToRenameStr.split(" ")[1]; - final String fieldToRenameName = fieldToRenameStr.split(" ")[2]; - final int offset = fieldToRename.getSourceRange().getOffset() + fieldToRenameStr.indexOf(fieldToRenameType) - + fieldToRenameType.length() + 1; - final int lengthToDelete = fieldToRenameName.length(); - final DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); - final InsertEdit insertEdit = new InsertEdit(offset, newFieldName + ";"); - editCompilationUnit(icu, deleteEdit, insertEdit); - final Field newJaMoPPField = this.getJaMoPPFieldFromClass(icu, newFieldName); - return claimOne(getCorrespondingEObjects(newJaMoPPField, InnerDeclaration.class)); - } - - private InnerDeclaration changeFieldTypeInClass(final String className, final String fieldName, - final String newFieldTypeName) throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(className, - this.getCurrentTestProject()); - final IType type = icu.getType(className); - final IField fieldToRename = type.getField(fieldName); - final String fieldSrc = fieldToRename.getSource(); - final String fieldType = fieldSrc.split(" ")[1]; - final int offset = fieldToRename.getSourceRange().getOffset() + fieldSrc.indexOf(fieldType); - final int lengthToDelete = fieldType.length(); - final DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); - final InsertEdit insertEdit = new InsertEdit(offset, newFieldTypeName); - editCompilationUnit(icu, deleteEdit, insertEdit); - final Field newJaMoPPField = this.getJaMoPPFieldFromClass(icu, fieldName); - return claimOne(getCorrespondingEObjects(newJaMoPPField, InnerDeclaration.class)); - } - - private void assertInnerDeclaration(final InnerDeclaration innerDeclaration, final String fieldType, - final String fieldName) throws Throwable { - super.assertPCMNamedElement(innerDeclaration, fieldName); - final String pcmDataTypeName = super.getNameFromPCMDataType(innerDeclaration.getDatatype_InnerDeclaration()); - assertEquals(pcmDataTypeName, fieldType, "The name of the PCM datatype does not equal the JaMoPP type name"); - } - -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java deleted file mode 100644 index e3239566d..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/JaMoPPParameterMappingTransformationTest.java +++ /dev/null @@ -1,127 +0,0 @@ -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; - -import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.ILocalVariable; -import org.eclipse.jdt.core.IMethod; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.text.edits.ReplaceEdit; -import org.junit.jupiter.api.Test; -import org.palladiosimulator.pcm.core.entity.NamedElement; -import org.palladiosimulator.pcm.repository.CollectionDataType; -import org.palladiosimulator.pcm.repository.CompositeDataType; -import org.palladiosimulator.pcm.repository.OperationInterface; -import org.palladiosimulator.pcm.repository.OperationSignature; -import org.palladiosimulator.pcm.repository.Parameter; -import org.palladiosimulator.pcm.repository.PrimitiveDataType; - -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; - -import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.*; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class JaMoPPParameterMappingTransformationTest extends Java2PcmPackageMappingTransformationTest { - - @Test - public void testAddParameter() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - final Parameter parameter = super.addParameterToSignature(opInterface.getEntityName(), opSig.getEntityName(), - "String", Pcm2JavaTestUtils.PARAMETER_NAME, null); - - this.assertParameter(opSig, parameter, "String", Pcm2JavaTestUtils.PARAMETER_NAME); - } - - @Test - public void testRenameParameter() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - final Parameter parameter = super.addParameterToSignature(opInterface.getEntityName(), opSig.getEntityName(), - "String", Pcm2JavaTestUtils.PARAMETER_NAME, null); - - final Parameter newParameter = this.renameParameterInSignature(opInterface.getEntityName(), - opSig.getEntityName(), parameter.getParameterName(), - Pcm2JavaTestUtils.PARAMETER_NAME + Pcm2JavaTestUtils.RENAME); - - this.assertParameter(opSig, newParameter, "String", - Pcm2JavaTestUtils.PARAMETER_NAME + Pcm2JavaTestUtils.RENAME); - } - - @Test - public void testChangeParameterType() throws Throwable { - super.addRepoContractsAndDatatypesPackage(); - final OperationInterface opInterface = super.addInterfaceInContractsPackage(); - final OperationSignature opSig = super.addMethodToInterfaceWithCorrespondence(opInterface.getEntityName()); - final Parameter parameter = super.addParameterToSignature(opInterface.getEntityName(), opSig.getEntityName(), - "String", Pcm2JavaTestUtils.PARAMETER_NAME, null); - final String expectedParamType = "int"; - - final Parameter changedParameter = this.changeParameterType(opInterface.getEntityName(), opSig.getEntityName(), - parameter.getParameterName(), expectedParamType); - - this.assertParameter(opSig, changedParameter, expectedParamType, changedParameter.getParameterName()); - } - - private Parameter renameParameterInSignature(final String interfaceName, final String methodName, - final String oldParameterName, final String newParameterName) throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, - this.getCurrentTestProject()); - final IMethod iMethod = this.findIMethodByName(interfaceName, methodName, icu); - final ILocalVariable localVariable = this.findParameterInIMethod(iMethod, oldParameterName); - final String typeName = localVariable.getSource().split(" ")[0]; - final String paramName = localVariable.getSource().split(" ")[1]; - final int offset = localVariable.getSourceRange().getOffset() + typeName.length() + 1; - final int length = paramName.length(); - final ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, newParameterName); - editCompilationUnit(icu, replaceEdit); - final org.emftext.language.java.parameters.Parameter newJaMoPPParameter = super.findJaMoPPParameterInICU(icu, - interfaceName, methodName, newParameterName); - return claimOne(getCorrespondingEObjects(newJaMoPPParameter, Parameter.class)); - } - - private Parameter changeParameterType(final String interfaceName, final String methodName, final String paramName, - final String newTypeName) throws Throwable { - final ICompilationUnit icu = CompilationUnitManipulatorHelper.findICompilationUnitWithClassName(interfaceName, - this.getCurrentTestProject()); - final IMethod iMethod = this.findIMethodByName(interfaceName, methodName, icu); - final ILocalVariable parameter = this.findParameterInIMethod(iMethod, paramName); - final int offset = parameter.getSourceRange().getOffset(); - final int length = parameter.getSource().split(" ")[0].length(); - final ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, newTypeName); - editCompilationUnit(icu, replaceEdit); - final org.emftext.language.java.parameters.Parameter newJaMoPPParameter = super.findJaMoPPParameterInICU(icu, - interfaceName, methodName, paramName); - Iterable correspondingEObjectsByType = getCorrespondingEObjects(newJaMoPPParameter, Parameter.class); - return claimOne(correspondingEObjectsByType); - } - - private ILocalVariable findParameterInIMethod(final IMethod iMethod, final String parameterName) - throws JavaModelException { - for (final ILocalVariable localVariable : iMethod.getParameters()) { - if (localVariable.getElementName().equals(parameterName)) { - return localVariable; - } - } - throw new RuntimeException("Old parameter with name " + parameterName + " not found"); - } - - private void assertParameter(final OperationSignature opSig, final Parameter parameter, - final String expectedTypeName, final String expectedName) throws Throwable { - OperationSignature operationSignature__Parameter = parameter.getOperationSignature__Parameter(); - assertEquals(opSig.getId(), operationSignature__Parameter.getId(), - "The parameter is not contained in the expected operation signature"); - this.assertPcmParameter(parameter, expectedName); - if (parameter.getDataType__Parameter() instanceof CollectionDataType - || parameter.getDataType__Parameter() instanceof CompositeDataType) { - this.assertPCMNamedElement((NamedElement) parameter.getDataType__Parameter(), expectedTypeName); - } else { - final String primitiveTypeName = this - .getNameFromPCMPrimitiveDataType((PrimitiveDataType) parameter.getDataType__Parameter()); - assertTrue(expectedTypeName.equalsIgnoreCase(primitiveTypeName), - "The primitve type parameter has the wrong name"); - } - } -} diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java deleted file mode 100644 index 17a44e81e..000000000 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmPackageMappingTransformationTest.java +++ /dev/null @@ -1,26 +0,0 @@ - -package tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy; -import java.util.Collections; -import java.util.List; - -import org.palladiosimulator.pcm.repository.CompositeDataType; - -import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmChangePropagationSpecification; -import tools.vitruv.change.propagation.ChangePropagationSpecification; - -public class Java2PcmPackageMappingTransformationTest extends Java2PcmTransformationTest { - - @Override - protected List getChangePropagationSpecifications() { - return Collections.singletonList(new Java2PcmChangePropagationSpecification()); - } - - protected CompositeDataType addClassThatCorrespondsToCompositeDatatype() throws Throwable { - this.getUserInteraction() - .addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); - final CompositeDataType cdt = this.addClassInPackage(this.getDatatypesPackage(), CompositeDataType.class); - return cdt; - } - -} From 62fc3872e63e781397f17df292634f8e5b527e9b Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Tue, 22 Nov 2022 13:05:35 +0100 Subject: [PATCH 10/11] statically import all Pcm2JavaTestUtils constants formatting --- .../ClassMappingTransformationTest.java | 128 ++++++++++-------- .../ClassMethodMappingTransformationTest.java | 56 ++++---- .../FieldMappingTransformationTest.java | 95 +++++++------ .../InterfaceMappingTransformationTest.java | 69 +++++----- .../java2pcm/Java2PcmTransformationTest.java | 53 +++----- .../MethodMappingTransformationTest.java | 77 ++++++----- .../PackageMappingTransformationTest.java | 70 +++++----- .../ParameterMappingTransformationTest.java | 101 +++++++------- ...ypeReferenceMappingTransformationTest.java | 44 +++--- 9 files changed, 362 insertions(+), 331 deletions(-) diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index f08926c9a..743424fa7 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -7,6 +7,12 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimDataType; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.SYSTEM_NAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.text.edits.ReplaceEdit; @@ -22,7 +28,6 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class ClassMappingTransformationTest extends Java2PcmTransformationTest { /** @@ -30,71 +35,75 @@ class ClassMappingTransformationTest extends Java2PcmTransformationTest { * the new implementing class for the component */ @ParameterizedTest() - @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_BASIC_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + @EnumSource(value = Java2PcmUserSelection.class, names = { "SELECT_BASIC_COMPONENT", + "SELECT_NOTHING_DECIDE_LATER" }) void testAddClassInSecondPackageAsBasicComponent(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage, + null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, BasicComponent.class); + claimComponent(repository, BASIC_COMPONENT_NAME, BasicComponent.class); }); } - + @ParameterizedTest() - @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_COMPOSITE_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + @EnumSource(value = Java2PcmUserSelection.class, names = { "SELECT_COMPOSITE_COMPONENT", + "SELECT_NOTHING_DECIDE_LATER" }) void testAddClassInSecondPackageAsCompositeComponent(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, COMPOSITE_COMPONENT_NAME); }); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); + Package componentPackage = claimPackage(view, COMPOSITE_COMPONENT_NAME); + view.getManipulationUtil().createClass(COMPOSITE_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, + componentPackage, null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimComponent(repository, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME, CompositeComponent.class); + claimComponent(repository, COMPOSITE_COMPONENT_NAME, CompositeComponent.class); }); } - + @ParameterizedTest() - @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_SYSTEM", "SELECT_NOTHING_DECIDE_LATER" }) + @EnumSource(value = Java2PcmUserSelection.class, names = { "SELECT_SYSTEM", "SELECT_NOTHING_DECIDE_LATER" }) void testAddClassInSecondPackageAsSystem(Java2PcmUserSelection packageSelection) throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(packageSelection.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.SYSTEM_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, SYSTEM_NAME); }); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); changeJavaEditorView(view -> { - Package systemPackage = claimPackage(view, Pcm2JavaTestUtils.SYSTEM_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.SYSTEM_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, systemPackage, null); + Package systemPackage = claimPackage(view, SYSTEM_NAME); + view.getManipulationUtil().createClass(SYSTEM_NAME + IMPLEMENTING_CLASS_SUFFIX, systemPackage, null); }); - + validatePcmView(view -> { System system = claimSingleSystem(view); - assertEquals(Pcm2JavaTestUtils.SYSTEM_NAME, system.getEntityName(), "system has wrong name"); + assertEquals(SYSTEM_NAME, system.getEntityName(), "system has wrong name"); }); } - + /** * Test ii) class in non corresponding package --> should not be mapped to a * Basic Component @@ -102,75 +111,82 @@ void testAddClassInSecondPackageAsSystem(Java2PcmUserSelection packageSelection) @Test void testAddClassInSecondPackageAsNone() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, componentPackage, null); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage, + null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - assertTrue(repository.getComponents__Repository().isEmpty(), "there must not exist any contained Component"); + assertTrue(repository.getComponents__Repository().isEmpty(), + "there must not exist any contained Component"); assertTrue(view.getRootObjects(System.class).isEmpty(), "there must not exist any System"); }); } - + @Test void testAddClassInDataTypesPackageAsCompositeDataType() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); changeJavaEditorView(view -> { Package dataTypesPackage = claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, null); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, + null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimDataType(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, CompositeDataType.class); + claimDataType(repository, BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, CompositeDataType.class); }); } - + @Test void testAddClassInDataTypesPackageAsNone() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); changeJavaEditorView(view -> { Package dataTypesPackage = claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); - view.getManipulationUtil().createClass(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, null); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage, + null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); assertEquals(0, repository.getDataTypes__Repository().size(), "repository must not have any data type"); }); } - + @ParameterizedTest() - @EnumSource(value = Java2PcmUserSelection.class, names = {"SELECT_BASIC_COMPONENT", "SELECT_NOTHING_DECIDE_LATER" }) + @EnumSource(value = Java2PcmUserSelection.class, names = { "SELECT_BASIC_COMPONENT", + "SELECT_NOTHING_DECIDE_LATER" }) void testRenameClassCorrespondingToBasicComponent(Java2PcmUserSelection packageSelection) throws Exception { testAddClassInSecondPackageAsBasicComponent(packageSelection); - - String changedName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; - + + String changedName = BASIC_COMPONENT_NAME + RENAME; + changeJavaEditorView(view -> { - String componentClassName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit(componentClassName, componentPackage); - + String componentClassName = BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX; + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit(componentClassName, + componentPackage); + int offset = componentClass.getBuffer().getContents().indexOf(componentClassName); - ReplaceEdit renameEdit = new ReplaceEdit(offset, componentClassName.length(), changedName + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + ReplaceEdit renameEdit = new ReplaceEdit(offset, componentClassName.length(), + changedName + IMPLEMENTING_CLASS_SUFFIX); view.getManipulationUtil().editCompilationUnit(componentClass, renameEdit); - //TODO: resource URI is not adjusted to new name + // TODO: resource URI is not adjusted to new name }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); claimComponent(repository, changedName, BasicComponent.class); diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java index fb40ff7ce..455e3c0f5 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java @@ -7,6 +7,11 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; @@ -32,29 +37,26 @@ void testCreateClassMethod() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); // setup interface with one method // setup class corresponding to basic component, implementing interface getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, - componentPackage, null); - ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, - componentPackage); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage, + null); + ICompilationUnit componentClass = view.getManipulationUtil() + .claimCompilationUnit(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage); Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); - ICompilationUnit anInterface = view.getManipulationUtil() - .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); { - String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + String methodName = OPERATION_SIGNATURE_1_NAME; String methodSignature = "\nvoid " + methodName + "();\n"; IType firstType = anInterface.getAllTypes()[0]; int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); @@ -63,12 +65,10 @@ void testCreateClassMethod() throws Exception { } { - view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, - Pcm2JavaTestUtils.INTERFACE_NAME); + view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, INTERFACE_NAME); - IType classType = componentClass - .getType(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + IType classType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + INTERFACE_NAME; int offset = classType.getSourceRange().getOffset(); int firstBracket = classType.getSource().indexOf("{"); offset = offset + firstBracket - 1; @@ -79,13 +79,11 @@ void testCreateClassMethod() throws Exception { // add implementation of interface method to class changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, - componentPackage); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil() + .claimCompilationUnit(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage); - final String methodBlock = "\n\tpublic void " + Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME - + " () {\n\t}\n"; + final String methodBlock = "\n\tpublic void " + OPERATION_SIGNATURE_1_NAME + " () {\n\t}\n"; IType firstType = componentClass.getAllTypes()[0]; int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); InsertEdit insertEdit = new InsertEdit(offset, methodBlock); @@ -94,13 +92,11 @@ void testCreateClassMethod() throws Exception { validatePcmView(view -> { Repository repository = claimSingleRepository(view); - BasicComponent component = claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, - BasicComponent.class); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); + BasicComponent component = claimComponent(repository, BASIC_COMPONENT_NAME, BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); Signature signature = claimOne(anInterface.getSignatures__OperationInterface()); - claimNamedElement(component.getProvidedRoles_InterfaceProvidingEntity(), Pcm2JavaTestUtils - .providesInterfaceName(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, Pcm2JavaTestUtils.INTERFACE_NAME), + claimNamedElement(component.getProvidedRoles_InterfaceProvidingEntity(), + Pcm2JavaTestUtils.providesInterfaceName(BASIC_COMPONENT_NAME, INTERFACE_NAME), OperationProvidedRole.class); ServiceEffectSpecification seff = claimOne(component.getServiceEffectSpecifications__BasicComponent()); assertEquals(signature, seff.getDescribedService__SEFF(), "SEFF is describing wrong service"); diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java index 6fa2c7f4a..59d4479e4 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java @@ -8,6 +8,12 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; @@ -28,21 +34,19 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class FieldMappingTransformationTest extends Java2PcmTransformationTest { private static final String FIELD_TYPE = "String"; private static final String FIELD_NAME = "stringField"; - private static final String PROVIDING_COMPONENT_NAME = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Providing"; - private static final String REQUIRING_COMPONENT_NAME = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + "Requiring"; + private static final String PROVIDING_COMPONENT_NAME = BASIC_COMPONENT_NAME + "Providing"; + private static final String REQUIRING_COMPONENT_NAME = BASIC_COMPONENT_NAME + "Requiring"; @Test void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { createRepositoryPackage(); - String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME - + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + String compilationUnitName = COMPOSITE_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX; getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_DATA_TYPE.getSelection()); changeJavaEditorView(view -> { @@ -72,9 +76,8 @@ void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { void testRenameFieldInClassCorrespondingToCompositeDataType() throws Exception { testAddFieldToClassCorrespondingToCompositeDataType(); - String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME - + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; - String changedFieldName = FIELD_NAME + Pcm2JavaTestUtils.RENAME; + String compilationUnitName = COMPOSITE_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX; + String changedFieldName = FIELD_NAME + RENAME; changeJavaEditorView(view -> { Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); @@ -107,8 +110,7 @@ void testRenameFieldInClassCorrespondingToCompositeDataType() throws Exception { void testChangeTypeOfFieldInClassCorrespondingToCompositeDataType() throws Exception { testAddFieldToClassCorrespondingToCompositeDataType(); - String compilationUnitName = Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME - + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + String compilationUnitName = COMPOSITE_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX; String changedFieldTypeName = "int"; changeJavaEditorView(view -> { @@ -157,22 +159,22 @@ void testAddFieldInClassWithoutCorrespondence() throws Exception { void testAddFieldWithTypeOfInterface() throws Exception { setupExampleWithProvidingAndRequiringComponent(); - String fieldType = Pcm2JavaTestUtils.INTERFACE_NAME; - String fieldName = "i" + Pcm2JavaTestUtils.INTERFACE_NAME; + String fieldType = INTERFACE_NAME; + String fieldName = "i" + INTERFACE_NAME; changeJavaEditorView(view -> { Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); - ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil() + .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil() - .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, - Pcm2JavaTestUtils.INTERFACE_NAME); + INTERFACE_NAME); InsertEdit insertEdit = editForAddingField(fieldName, fieldType, providingCompilationUnit, - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, insertEdit); }); @@ -180,8 +182,7 @@ void testAddFieldWithTypeOfInterface() throws Exception { Repository repository = claimSingleRepository(view); BasicComponent providingComponent = claimComponent(repository, PROVIDING_COMPONENT_NAME, BasicComponent.class); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); OperationRequiredRole requiredRole = claimNamedElement( providingComponent.getRequiredRoles_InterfaceRequiringEntity(), fieldName, OperationRequiredRole.class); @@ -197,22 +198,22 @@ void testAddFieldWithTypeOfBasicComponent() throws Exception { String fieldName = PROVIDING_COMPONENT_NAME.substring(0, 1).toLowerCase() + PROVIDING_COMPONENT_NAME.substring(1); - String fieldType = PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; + String fieldType = PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX; getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { Package requiringPackage = JavaQueryUtil.claimPackage(view, REQUIRING_COMPONENT_NAME); - ICompilationUnit requiringCompilationUnit = view.getManipulationUtil().claimCompilationUnit( - REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, requiringPackage); + ICompilationUnit requiringCompilationUnit = view.getManipulationUtil() + .claimCompilationUnit(REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, requiringPackage); Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); - ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil() + .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); view.getManipulationUtil().addImportToCompilationUnit(requiringCompilationUnit, providingCompilationUnit, - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); InsertEdit insertEdit = editForAddingField(fieldName, fieldType, requiringCompilationUnit, - REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); + REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); view.getManipulationUtil().editCompilationUnit(requiringCompilationUnit, insertEdit); }); @@ -220,8 +221,7 @@ void testAddFieldWithTypeOfBasicComponent() throws Exception { Repository repository = claimSingleRepository(view); BasicComponent requiringComponent = claimComponent(repository, REQUIRING_COMPONENT_NAME, BasicComponent.class); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); OperationRequiredRole requiredRole = claimNamedElement( requiringComponent.getRequiredRoles_InterfaceRequiringEntity(), fieldName, OperationRequiredRole.class); @@ -234,11 +234,10 @@ void testAddFieldWithTypeOfBasicComponent() throws Exception { /** * Sets up an example scenario with the root Repository and two packages named * {@link REQUIRING_COMPONENT_NAME} and {@link PROVIDING_COMPONENT_NAME} with - * each one class with suffix {@Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX} as - * basic components. Sets up an interface named - * {@link Pcm2JavaTestUtils.INTERFACE_NAME} in the contracts package. Sets up an - * {@code implements} relation between the providing component class and the - * interface. + * each one class with suffix {IMPLEMENTING_CLASS_SUFFIX} as basic components. + * Sets up an interface named {@link INTERFACE_NAME} in the contracts package. + * Sets up an {@code implements} relation between the providing component class + * and the interface. */ private void setupExampleWithProvidingAndRequiringComponent() throws Exception { createRepositoryPackage(); @@ -246,35 +245,35 @@ private void setupExampleWithProvidingAndRequiringComponent() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, REQUIRING_COMPONENT_NAME); - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, PROVIDING_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, REQUIRING_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, PROVIDING_COMPONENT_NAME); }); getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { Package requiringPackage = JavaQueryUtil.claimPackage(view, REQUIRING_COMPONENT_NAME); - view.getManipulationUtil().createClass( - REQUIRING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, requiringPackage, null); + view.getManipulationUtil().createClass(REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, + requiringPackage, null); Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); - view.getManipulationUtil().createClass( - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage, null); + view.getManipulationUtil().createClass(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, + providingPackage, null); Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); { - ICompilationUnit providingCompilationUnit = view.getManipulationUtil().claimCompilationUnit( - PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, providingPackage); - ICompilationUnit anInterface = view.getManipulationUtil() - .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil() + .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, - Pcm2JavaTestUtils.INTERFACE_NAME); + INTERFACE_NAME); IType classType = providingCompilationUnit - .getType(PROVIDING_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + .getType(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + INTERFACE_NAME; int offset = classType.getSourceRange().getOffset(); int firstBracket = classType.getSource().indexOf("{"); offset = offset + firstBracket - 1; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java index 79b23ea94..a4b56a3b7 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java @@ -4,6 +4,10 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.text.edits.ReplaceEdit; @@ -14,7 +18,6 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class InterfaceMappingTransformationTest extends Java2PcmTransformationTest { /** @@ -24,18 +27,18 @@ class InterfaceMappingTransformationTest extends Java2PcmTransformationTest { @Test void testAddInterfaceInContractsPackage() throws Exception { createRepositoryPackage(); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + claimInterface(repository, INTERFACE_NAME, OperationInterface.class); }); } - + /** * interface in non-repository package --> "user" should be asked and decide to * add it @@ -43,24 +46,25 @@ void testAddInterfaceInContractsPackage() throws Exception { @Test void testAddInterfaceInNonRepositoryPackageAsArchitecturalInterface() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - - getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); + + getUserInteraction() + .addNextSingleSelection(Java2PcmUserSelection.SELECT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, componentPackage, null); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createInterface(INTERFACE_NAME, componentPackage, null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); + claimInterface(repository, INTERFACE_NAME, OperationInterface.class); }); } - + /** * interface in non-repository package --> "user" should be asked and decide to * not add it @@ -68,47 +72,48 @@ void testAddInterfaceInNonRepositoryPackageAsArchitecturalInterface() throws Exc @Test void testAddInterfaceInNonRepositoryPackageAsTechnicalInterface() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - + getUserInteraction().addNextSingleSelection( Java2PcmUserSelection.SELECT_DONT_CREATE_INTERFACE_NOT_IN_CONTRACTS.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, componentPackage, null); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createInterface(INTERFACE_NAME, componentPackage, null); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); assertEquals(0, repository.getInterfaces__Repository().size(), "there must not exist an interface"); }); } - + @Test void testRenameInterface() throws Exception { createRepositoryPackage(); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); }); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - - int offset = anInterface.getBuffer().getContents().indexOf(Pcm2JavaTestUtils.INTERFACE_NAME); - ReplaceEdit renameEdit = new ReplaceEdit(offset, Pcm2JavaTestUtils.INTERFACE_NAME.length(), Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + + int offset = anInterface.getBuffer().getContents().indexOf(INTERFACE_NAME); + ReplaceEdit renameEdit = new ReplaceEdit(offset, INTERFACE_NAME.length(), INTERFACE_NAME + RENAME); view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); - //TODO: resource URI is not adjusted to new name + // TODO: resource URI is not adjusted to new name }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME + Pcm2JavaTestUtils.RENAME, OperationInterface.class); + claimInterface(repository, INTERFACE_NAME + RENAME, OperationInterface.class); assertEquals(1, repository.getInterfaces__Repository().size(), "too many interfaces in repository"); }); } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java index 71c2d04e7..a188e2b1e 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/Java2PcmTransformationTest.java @@ -1,5 +1,7 @@ package tools.vitruv.applications.pcmjava.javaeditor.java2pcm; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; + import java.nio.file.Path; import java.util.Arrays; import java.util.List; @@ -19,7 +21,6 @@ import tools.vitruv.applications.pcmjava.javaeditor.util.JavaEditorView; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaPcmViewFactory; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaChangePropagationSpecification; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; import tools.vitruv.applications.util.temporary.java.JavaPersistenceHelper; import tools.vitruv.applications.util.temporary.java.JavaSetup; import tools.vitruv.change.propagation.ChangePropagationSpecification; @@ -30,7 +31,7 @@ public class Java2PcmTransformationTest extends ViewBasedVitruvApplicationTest { protected JavaPcmViewFactory viewFactory; - + @BeforeAll public static final void setupJavaFactories() { JavaSetup.prepareFactories(); @@ -40,60 +41,50 @@ public static final void setupJavaFactories() { public final void setupJavaClasspath() { JavaSetup.resetClasspathAndRegisterStandardLibrary(); } - + @BeforeEach public final void setupViewFactory(@TestProject Path testProjectFolder) { viewFactory = new JavaPcmViewFactory(testProjectFolder, getVirtualModel()); } - + @Override protected boolean enableTransitiveCyclicChangePropagation() { return false; } - + @Override protected Iterable getChangePropagationSpecifications() { return List.of(new Java2PcmChangePropagationSpecification(), new Pcm2JavaChangePropagationSpecification()); } - - /* - * JDT functionality requires platform URIs. - */ -// @Override -// protected UriMode getUriMode() { -// return UriMode.PLATFORM_URIS; -// } - + protected void createRepositoryPackage() throws Exception { changeJavaView(view -> { - createPackageWithPackageInfo(view, new String[] { Pcm2JavaTestUtils.REPOSITORY_NAME }); + createPackageWithPackageInfo(view, new String[] { REPOSITORY_NAME }); }); } - + protected Package createPackageWithPackageInfo(View view, final String... namespace) { final Package jaMoPPPackage = ContainersFactory.eINSTANCE.createPackage(); final List namespaceList = Arrays.asList(namespace); jaMoPPPackage.setName(namespaceList.get(namespaceList.size() - 1)); jaMoPPPackage.getNamespaces().addAll(namespaceList.subList(0, namespaceList.size() - 1)); - + URI createPackageURI = getUri(Path.of(JavaPersistenceHelper.buildJavaFilePath(jaMoPPPackage))); view.registerRoot(jaMoPPPackage, createPackageURI); return jaMoPPPackage; } - + protected String getTypeNameOfPcmDataType(DataType dataType) { if (dataType == null) { return "void"; - } - else if (dataType instanceof PrimitiveDataType) { - return getTypeNameFromPrimitveDataType((PrimitiveDataType)dataType); - } - else if (dataType instanceof NamedElement) { - return ((NamedElement)dataType).getEntityName(); + } else if (dataType instanceof PrimitiveDataType) { + return getTypeNameFromPrimitveDataType((PrimitiveDataType) dataType); + } else if (dataType instanceof NamedElement) { + return ((NamedElement) dataType).getEntityName(); } throw new RuntimeException("Unknown data type " + dataType); } - + private String getTypeNameFromPrimitveDataType(final PrimitiveDataType dataType) { switch (dataType.getType()) { case BOOL: @@ -113,21 +104,21 @@ private String getTypeNameFromPrimitveDataType(final PrimitiveDataType dataType) } throw new RuntimeException("Unknown primitive data type " + dataType); } - + // === Shortcuts === - + protected void changeJavaView(Consumer modelModification) throws Exception { viewFactory.changeJavaView(modelModification); } - + protected void changeJavaEditorView(ThrowingConsumer modelModification) throws Exception { viewFactory.changeJavaEditorView(modelModification); } - + protected void changeJavaEditorViewThrowing(ThrowingConsumer modelModification) throws Exception { - + } - + protected void validatePcmView(Consumer viewValidation) throws Exception { viewFactory.validatePcmView(viewValidation); } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java index 165cbd009..01142a9db 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java @@ -5,6 +5,9 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IMethod; @@ -20,77 +23,84 @@ import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class MethodMappingTransformationTest extends Java2PcmTransformationTest { @Test void testAddMethod() throws Exception { createRepositoryPackage(); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); }); - + String returnType = "void"; - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - - String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + + String methodName = OPERATION_SIGNATURE_1_NAME; String methodSignature = "\n" + returnType + " " + methodName + "();\n"; - + IType firstType = anInterface.getAllTypes()[0]; int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); InsertEdit insertEdit = new InsertEdit(offset, methodSignature); view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); - OperationSignature operationSignature = claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + OperationInterface operationInterface = claimInterface(repository, INTERFACE_NAME, + OperationInterface.class); + OperationSignature operationSignature = claimNamedElement( + operationInterface.getSignatures__OperationInterface(), OPERATION_SIGNATURE_1_NAME); assertEquals(0, operationSignature.getParameters__OperationSignature().size()); assertEquals(returnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); }); } - + @Test void testRenameMethod() throws Exception { testAddMethod(); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - - IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + + IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); int offset = iMethod.getNameRange().getOffset(); int length = iMethod.getNameRange().getLength(); - String newMethodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + Pcm2JavaTestUtils.RENAME; + String newMethodName = OPERATION_SIGNATURE_1_NAME + RENAME; ReplaceEdit renameEdit = new ReplaceEdit(offset, length, newMethodName); view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); - claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME + Pcm2JavaTestUtils.RENAME); - assertEquals(1, operationInterface.getSignatures__OperationInterface().size(), "too many operation signatures"); + OperationInterface operationInterface = claimInterface(repository, INTERFACE_NAME, + OperationInterface.class); + claimNamedElement(operationInterface.getSignatures__OperationInterface(), + OPERATION_SIGNATURE_1_NAME + RENAME); + assertEquals(1, operationInterface.getSignatures__OperationInterface().size(), + "too many operation signatures"); }); } - + @Test void testAddReturnType() throws Exception { testAddMethod(); - + String changedReturnType = "String"; - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - - IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + + IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); int returnTypeOffset = iMethod.getSourceRange().getOffset(); String retTypeName = iMethod.getSource().split(" ")[0]; int returnTypeLength = retTypeName.length(); @@ -98,13 +108,16 @@ void testAddReturnType() throws Exception { InsertEdit insertEdit = new InsertEdit(returnTypeOffset, changedReturnType); view.getManipulationUtil().editCompilationUnit(anInterface, deleteEdit, insertEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface operationInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, OperationInterface.class); - OperationSignature operationSignature = claimNamedElement(operationInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + OperationInterface operationInterface = claimInterface(repository, INTERFACE_NAME, + OperationInterface.class); + OperationSignature operationSignature = claimNamedElement( + operationInterface.getSignatures__OperationInterface(), OPERATION_SIGNATURE_1_NAME); assertEquals(0, operationSignature.getParameters__OperationSignature().size()); - assertEquals(changedReturnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); + assertEquals(changedReturnType, + getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); }); } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java index e47287ff8..bece2b5dc 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/PackageMappingTransformationTest.java @@ -6,6 +6,12 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleSystem; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.SYSTEM_NAME; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; @@ -15,7 +21,6 @@ import org.palladiosimulator.pcm.system.System; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class PackageMappingTransformationTest extends Java2PcmTransformationTest { /** @@ -24,102 +29,103 @@ class PackageMappingTransformationTest extends Java2PcmTransformationTest { @Test void testAddFirstPackage() throws Exception { createRepositoryPackage(); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - assertEquals(Pcm2JavaTestUtils.REPOSITORY_NAME_EXPECTED, repository.getEntityName(), "repository has wrong name"); + assertEquals(REPOSITORY_NAME_EXPECTED, repository.getEntityName(), "repository has wrong name"); }); } - + /** * second package is added --> should be mapped to a basic component */ @Test void testAddSecondPackageAsBasicComponent() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, BasicComponent.class); + claimComponent(repository, BASIC_COMPONENT_NAME, BasicComponent.class); }); } - + /** * second package is added --> should be mapped to a composite component */ @Test void testAddSecondPackageAsCompositeComponent() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, COMPOSITE_COMPONENT_NAME); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - claimComponent(repository, Pcm2JavaTestUtils.COMPOSITE_COMPONENT_NAME, CompositeComponent.class); + claimComponent(repository, COMPOSITE_COMPONENT_NAME, CompositeComponent.class); }); } - + /** * second package is added --> should be mapped to a system */ @Test - void testAddSecondPackageAsSystem() throws Exception { + void testAddSecondPackageAsSystem() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.SYSTEM_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, SYSTEM_NAME); }); - + validatePcmView(view -> { System system = claimSingleSystem(view); - assertEquals(Pcm2JavaTestUtils.SYSTEM_NAME, system.getEntityName(), "system has wrong name"); + assertEquals(SYSTEM_NAME, system.getEntityName(), "system has wrong name"); }); } - + /** * second package is added --> should be mapped to nothing */ @Test void testAddSecondPackageAsNone() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - assertTrue(repository.getComponents__Repository().isEmpty(), "there must not exist any contained Component"); + assertTrue(repository.getComponents__Repository().isEmpty(), + "there must not exist any contained Component"); assertTrue(view.getRootObjects(System.class).isEmpty(), "there must not exist any System"); }); } - + @Test void testRenameRepository() throws Exception { createRepositoryPackage(); - + getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); - - String changedPackageName = Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.RENAME; - + + String changedPackageName = BASIC_COMPONENT_NAME + RENAME; + changeJavaEditorView(view -> { - Package innerPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + Package innerPackage = claimPackage(view, BASIC_COMPONENT_NAME); view.getManipulationUtil().renamePackage(innerPackage, changedPackageName); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); assertEquals(1, repository.getComponents__Repository().size(), "wrong number of components in repository"); diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java index d4ba510a5..73c95c08f 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java @@ -6,6 +6,10 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.PARAMETER_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.ILocalVariable; @@ -23,62 +27,65 @@ import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; -import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class ParameterMappingTransformationTest extends Java2PcmTransformationTest { private static final String PARAMETER_TYPE = "String"; - + @Test void testAddParameter() throws Exception { createRepositoryPackage(); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - - String methodName = Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME; + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + + String methodName = OPERATION_SIGNATURE_1_NAME; String returnType = "void"; String methodSignature = "\n" + returnType + " " + methodName + "();\n"; - + IType firstType = anInterface.getAllTypes()[0]; int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); InsertEdit insertEdit = new InsertEdit(offset, methodSignature); view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); }); - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - IMethod iMethod = anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME).getMethod(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, null); - String parameterString = PARAMETER_TYPE + " " + Pcm2JavaTestUtils.PARAMETER_NAME; + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); + String parameterString = PARAMETER_TYPE + " " + PARAMETER_NAME; int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; InsertEdit insertEdit = new InsertEdit(offset, parameterString); view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); - OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), + OPERATION_SIGNATURE_1_NAME); Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); - assertEquals(Pcm2JavaTestUtils.PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); - assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + assertEquals(PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); + assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), + "invalid parameter type"); }); } - + @Test void testRenameParameter() throws Exception { testAddParameter(); - - String changedParameterName = Pcm2JavaTestUtils.PARAMETER_NAME + Pcm2JavaTestUtils.RENAME; - + + String changedParameterName = PARAMETER_NAME + RENAME; + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - IMethod iMethod = findIMethodByName(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME)); - ILocalVariable parameter = this.findParameterInIMethod(iMethod, Pcm2JavaTestUtils.PARAMETER_NAME); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + IMethod iMethod = findIMethodByName(OPERATION_SIGNATURE_1_NAME, anInterface.getType(INTERFACE_NAME)); + ILocalVariable parameter = this.findParameterInIMethod(iMethod, PARAMETER_NAME); String typeName = parameter.getSource().split(" ")[0]; String paramName = parameter.getSource().split(" ")[1]; int offset = parameter.getSourceRange().getOffset() + typeName.length() + 1; @@ -86,48 +93,50 @@ void testRenameParameter() throws Exception { ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterName); view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); - OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), + OPERATION_SIGNATURE_1_NAME); Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); assertEquals(changedParameterName, parameter.getParameterName(), "invalid parameter name"); - assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + assertEquals(PARAMETER_TYPE, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), + "invalid parameter type"); }); } - + @Test void testChangeParameterType() throws Exception { testAddParameter(); - + String changedParameterType = "int"; - + changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - IMethod iMethod = findIMethodByName(Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME, anInterface.getType(Pcm2JavaTestUtils.INTERFACE_NAME)); - ILocalVariable parameter = this.findParameterInIMethod(iMethod, Pcm2JavaTestUtils.PARAMETER_NAME); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + IMethod iMethod = findIMethodByName(OPERATION_SIGNATURE_1_NAME, anInterface.getType(INTERFACE_NAME)); + ILocalVariable parameter = this.findParameterInIMethod(iMethod, PARAMETER_NAME); int offset = parameter.getSourceRange().getOffset(); int length = parameter.getSource().split(" ")[0].length(); ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterType); view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); }); - + validatePcmView(view -> { Repository repository = claimSingleRepository(view); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); - OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), Pcm2JavaTestUtils.OPERATION_SIGNATURE_1_NAME); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); + OperationSignature operationSignature = claimNamedElement(anInterface.getSignatures__OperationInterface(), + OPERATION_SIGNATURE_1_NAME); Parameter parameter = claimOne(operationSignature.getParameters__OperationSignature()); - assertEquals(Pcm2JavaTestUtils.PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); - assertEquals(changedParameterType, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), "invalid parameter type"); + assertEquals(PARAMETER_NAME, parameter.getParameterName(), "invalid parameter name"); + assertEquals(changedParameterType, getTypeNameOfPcmDataType(parameter.getDataType__Parameter()), + "invalid parameter type"); }); } - - protected IMethod findIMethodByName(final String methodName, final IType type) - throws JavaModelException { + + protected IMethod findIMethodByName(final String methodName, final IType type) throws JavaModelException { for (final IMethod method : type.getMethods()) { if (method.getElementName().equals(methodName)) { return method; @@ -135,7 +144,7 @@ protected IMethod findIMethodByName(final String methodName, final IType type) } throw new RuntimeException("Method " + methodName + " not found in type " + type); } - + private ILocalVariable findParameterInIMethod(final IMethod iMethod, final String parameterName) throws JavaModelException { for (final ILocalVariable localVariable : iMethod.getParameters()) { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java index fe542f018..edb39ab34 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java @@ -6,6 +6,10 @@ import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimSingleRepository; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.BASIC_COMPONENT_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.INTERFACE_NAME; +import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; @@ -28,36 +32,31 @@ void testAddImplementsToClassCorrespondingToBasicComponent() throws Exception { getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaView(view -> { - createPackageWithPackageInfo(view, Pcm2JavaTestUtils.REPOSITORY_NAME, - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); + createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME); }); getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection()); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - view.getManipulationUtil().createClass( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, - componentPackage, null); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + view.getManipulationUtil().createClass(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage, + null); Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - view.getManipulationUtil().createInterface(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage, null); + view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); }); changeJavaEditorView(view -> { - Package componentPackage = claimPackage(view, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME); - ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit( - Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX, - componentPackage); + Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); + ICompilationUnit componentClass = view.getManipulationUtil() + .claimCompilationUnit(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage); Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); - ICompilationUnit anInterface = view.getManipulationUtil() - .claimCompilationUnit(Pcm2JavaTestUtils.INTERFACE_NAME, contractsPackage); - view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, - Pcm2JavaTestUtils.INTERFACE_NAME); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); + view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, INTERFACE_NAME); - IType classType = componentClass - .getType(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME + Pcm2JavaTestUtils.IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + Pcm2JavaTestUtils.INTERFACE_NAME; + IType classType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); + String newSource = " implements " + INTERFACE_NAME; int offset = classType.getSourceRange().getOffset(); int firstBracket = classType.getSource().indexOf("{"); offset = offset + firstBracket - 1; @@ -67,14 +66,11 @@ void testAddImplementsToClassCorrespondingToBasicComponent() throws Exception { validatePcmView(view -> { Repository repository = claimSingleRepository(view); - BasicComponent component = claimComponent(repository, Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, - BasicComponent.class); - OperationInterface anInterface = claimInterface(repository, Pcm2JavaTestUtils.INTERFACE_NAME, - OperationInterface.class); + BasicComponent component = claimComponent(repository, BASIC_COMPONENT_NAME, BasicComponent.class); + OperationInterface anInterface = claimInterface(repository, INTERFACE_NAME, OperationInterface.class); OperationProvidedRole providedRole = claimNamedElement( component.getProvidedRoles_InterfaceProvidingEntity(), - Pcm2JavaTestUtils.providesInterfaceName(Pcm2JavaTestUtils.BASIC_COMPONENT_NAME, - Pcm2JavaTestUtils.INTERFACE_NAME), + Pcm2JavaTestUtils.providesInterfaceName(BASIC_COMPONENT_NAME, INTERFACE_NAME), OperationProvidedRole.class); assertEquals(anInterface, providedRole.getProvidedInterface__OperationProvidedRole(), "incorrect provided interface"); From 5e545b5992dceaa0d257428cb6e81bbab35383e9 Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Tue, 22 Nov 2022 15:46:57 +0100 Subject: [PATCH 11/11] move all java editor functionality to dedicated class --- .../ClassMappingTransformationTest.java | 8 +- .../ClassMethodMappingTransformationTest.java | 43 ++--- .../FieldMappingTransformationTest.java | 99 +++------- .../InterfaceMappingTransformationTest.java | 9 +- .../MethodMappingTransformationTest.java | 50 +++--- .../ParameterMappingTransformationTest.java | 73 +++----- ...ypeReferenceMappingTransformationTest.java | 11 +- .../legacy/Java2PcmTransformationTest.java | 1 - .../javaeditor/util/JavaTextEditFactory.java | 169 ++++++++++++++++++ 9 files changed, 265 insertions(+), 198 deletions(-) create mode 100644 tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaTextEditFactory.java diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java index 743424fa7..413b23c6c 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMappingTransformationTest.java @@ -15,7 +15,7 @@ import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.SYSTEM_NAME; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -28,6 +28,7 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; class ClassMappingTransformationTest extends Java2PcmTransformationTest { /** @@ -179,12 +180,9 @@ void testRenameClassCorrespondingToBasicComponent(Java2PcmUserSelection packageS Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit(componentClassName, componentPackage); - - int offset = componentClass.getBuffer().getContents().indexOf(componentClassName); - ReplaceEdit renameEdit = new ReplaceEdit(offset, componentClassName.length(), + TextEdit renameEdit = JavaTextEditFactory.renameCompilationUnit(componentClass, componentClassName, changedName + IMPLEMENTING_CLASS_SUFFIX); view.getManipulationUtil().editCompilationUnit(componentClass, renameEdit); - // TODO: resource URI is not adjusted to new name }); validatePcmView(view -> { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java index 455e3c0f5..102d09c49 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ClassMethodMappingTransformationTest.java @@ -3,6 +3,7 @@ import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; import static org.junit.jupiter.api.Assertions.assertEquals; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory.Visibility.PUBLIC; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimNamedElement; @@ -15,7 +16,7 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; -import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; @@ -26,8 +27,8 @@ import org.palladiosimulator.pcm.seff.ServiceEffectSpecification; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class ClassMethodMappingTransformationTest extends Java2PcmTransformationTest { @@ -49,32 +50,20 @@ void testCreateClassMethod() throws Exception { null); ICompilationUnit componentClass = view.getManipulationUtil() .claimCompilationUnit(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage); + IType basicComponentType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); + IType interfaceType = anInterface.getType(INTERFACE_NAME); + + TextEdit addMethodEdit = JavaTextEditFactory.addMethodSignature(interfaceType, "void", OPERATION_SIGNATURE_1_NAME); + view.getManipulationUtil().editCompilationUnit(anInterface, addMethodEdit); - { - String methodName = OPERATION_SIGNATURE_1_NAME; - String methodSignature = "\nvoid " + methodName + "();\n"; - IType firstType = anInterface.getAllTypes()[0]; - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); - InsertEdit insertEdit = new InsertEdit(offset, methodSignature); - view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); - } - - { - view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, INTERFACE_NAME); - - IType classType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + INTERFACE_NAME; - int offset = classType.getSourceRange().getOffset(); - int firstBracket = classType.getSource().indexOf("{"); - offset = offset + firstBracket - 1; - final InsertEdit insertEdit = new InsertEdit(offset, newSource); - view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); - } + view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, INTERFACE_NAME); + TextEdit implementsEdit = JavaTextEditFactory.addImplementsRelation(basicComponentType, INTERFACE_NAME); + view.getManipulationUtil().editCompilationUnit(componentClass, implementsEdit); }); // add implementation of interface method to class @@ -82,12 +71,10 @@ void testCreateClassMethod() throws Exception { Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME); ICompilationUnit componentClass = view.getManipulationUtil() .claimCompilationUnit(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, componentPackage); - - final String methodBlock = "\n\tpublic void " + OPERATION_SIGNATURE_1_NAME + " () {\n\t}\n"; - IType firstType = componentClass.getAllTypes()[0]; - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); - InsertEdit insertEdit = new InsertEdit(offset, methodBlock); - view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); + IType componentClassType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); + + TextEdit addMethodEdit = JavaTextEditFactory.addMethodWithEmptyBody(componentClassType, PUBLIC, "void", OPERATION_SIGNATURE_1_NAME); + view.getManipulationUtil().editCompilationUnit(componentClass, addMethodEdit); }); validatePcmView(view -> { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java index 59d4479e4..498f1d63e 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/FieldMappingTransformationTest.java @@ -3,6 +3,7 @@ import static edu.kit.ipd.sdq.commons.util.java.lang.IterableUtil.claimOne; import static org.junit.jupiter.api.Assertions.assertEquals; import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil.claimPackage; +import static tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory.Visibility.PRIVATE; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimComponent; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimDataType; import static tools.vitruv.applications.pcmjava.javaeditor.util.PcmQueryUtil.claimInterface; @@ -16,11 +17,8 @@ import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -32,8 +30,8 @@ import org.palladiosimulator.pcm.repository.Repository; import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; -import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; class FieldMappingTransformationTest extends Java2PcmTransformationTest { private static final String FIELD_TYPE = "String"; @@ -58,9 +56,10 @@ void testAddFieldToClassCorrespondingToCompositeDataType() throws Exception { Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); + IType compilationUnitType = compilationUnit.getType(compilationUnitName); - InsertEdit insertEdit = editForAddingField(FIELD_NAME, FIELD_TYPE, compilationUnit, compilationUnitName); - view.getManipulationUtil().editCompilationUnit(compilationUnit, insertEdit); + TextEdit addFieldEdit = JavaTextEditFactory.addField(compilationUnitType, PRIVATE, FIELD_NAME, FIELD_TYPE); + view.getManipulationUtil().editCompilationUnit(compilationUnit, addFieldEdit); }); validatePcmView(view -> { @@ -83,18 +82,10 @@ void testRenameFieldInClassCorrespondingToCompositeDataType() throws Exception { Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); - IType type = compilationUnit.getType(compilationUnitName); - IField fieldToRename = type.getField(FIELD_NAME); - String fieldToRenameStr = fieldToRename.getSource(); - String fieldToRenameType = fieldToRenameStr.split(" ")[1]; - String fieldToRenameName = fieldToRenameStr.split(" ")[2]; - int offset = fieldToRename.getSourceRange().getOffset() + fieldToRenameStr.indexOf(fieldToRenameType) - + fieldToRenameType.length() + 1; - int lengthToDelete = fieldToRenameName.length(); - DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); - InsertEdit insertEdit = new InsertEdit(offset, changedFieldName + ";"); - view.getManipulationUtil().editCompilationUnit(compilationUnit, deleteEdit, insertEdit); + + TextEdit renameFieldEdit = JavaTextEditFactory.renameField(type, FIELD_NAME, changedFieldName); + view.getManipulationUtil().editCompilationUnit(compilationUnit, renameFieldEdit); }); validatePcmView(view -> { @@ -117,16 +108,10 @@ void testChangeTypeOfFieldInClassCorrespondingToCompositeDataType() throws Excep Package datatypesPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.DATATYPES_PACKAGE); ICompilationUnit compilationUnit = view.getManipulationUtil().claimCompilationUnit(compilationUnitName, datatypesPackage); - IType type = compilationUnit.getType(compilationUnitName); - IField fieldToRename = type.getField(FIELD_NAME); - String fieldSrc = fieldToRename.getSource(); - String fieldType = fieldSrc.split(" ")[1]; - int offset = fieldToRename.getSourceRange().getOffset() + fieldSrc.indexOf(fieldType); - int lengthToDelete = fieldType.length(); - DeleteEdit deleteEdit = new DeleteEdit(offset, lengthToDelete); - InsertEdit insertEdit = new InsertEdit(offset, changedFieldTypeName); - view.getManipulationUtil().editCompilationUnit(compilationUnit, deleteEdit, insertEdit); + + TextEdit changeTypeEdit = JavaTextEditFactory.changeTypeOfField(type, FIELD_NAME, changedFieldTypeName); + view.getManipulationUtil().editCompilationUnit(compilationUnit, changeTypeEdit); }); validatePcmView(view -> { @@ -166,16 +151,16 @@ void testAddFieldWithTypeOfInterface() throws Exception { Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); ICompilationUnit providingCompilationUnit = view.getManipulationUtil() .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); + IType providingCompilationUnitType = providingCompilationUnit.getType(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); + view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, INTERFACE_NAME); - - InsertEdit insertEdit = editForAddingField(fieldName, fieldType, providingCompilationUnit, - PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, insertEdit); + TextEdit addFieldEdit = JavaTextEditFactory.addField(providingCompilationUnitType, PRIVATE, fieldName, fieldType); + view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, addFieldEdit); }); validatePcmView(view -> { @@ -205,16 +190,16 @@ void testAddFieldWithTypeOfBasicComponent() throws Exception { Package requiringPackage = JavaQueryUtil.claimPackage(view, REQUIRING_COMPONENT_NAME); ICompilationUnit requiringCompilationUnit = view.getManipulationUtil() .claimCompilationUnit(REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, requiringPackage); + IType requiringCompilationUnitType = requiringCompilationUnit.getType(REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); ICompilationUnit providingCompilationUnit = view.getManipulationUtil() .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); + view.getManipulationUtil().addImportToCompilationUnit(requiringCompilationUnit, providingCompilationUnit, PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - - InsertEdit insertEdit = editForAddingField(fieldName, fieldType, requiringCompilationUnit, - REQUIRING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - view.getManipulationUtil().editCompilationUnit(requiringCompilationUnit, insertEdit); + TextEdit addFieldEdit = JavaTextEditFactory.addField(requiringCompilationUnitType, PRIVATE, fieldName, fieldType); + view.getManipulationUtil().editCompilationUnit(requiringCompilationUnit, addFieldEdit); }); validatePcmView(view -> { @@ -259,45 +244,19 @@ private void setupExampleWithProvidingAndRequiringComponent() throws Exception { Package providingPackage = JavaQueryUtil.claimPackage(view, PROVIDING_COMPONENT_NAME); view.getManipulationUtil().createClass(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage, null); + ICompilationUnit providingCompilationUnit = view.getManipulationUtil() + .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); Package contractsPackage = JavaQueryUtil.claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); + ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, + contractsPackage); - { - ICompilationUnit providingCompilationUnit = view.getManipulationUtil() - .claimCompilationUnit(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, providingPackage); - ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, - contractsPackage); - view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, - INTERFACE_NAME); - - IType classType = providingCompilationUnit - .getType(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + INTERFACE_NAME; - int offset = classType.getSourceRange().getOffset(); - int firstBracket = classType.getSource().indexOf("{"); - offset = offset + firstBracket - 1; - final InsertEdit insertEdit = new InsertEdit(offset, newSource); - view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, insertEdit); - } + view.getManipulationUtil().addImportToCompilationUnit(providingCompilationUnit, anInterface, + INTERFACE_NAME); + IType classType = providingCompilationUnit.getType(PROVIDING_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); + TextEdit implementsEdit = JavaTextEditFactory.addImplementsRelation(classType, INTERFACE_NAME); + view.getManipulationUtil().editCompilationUnit(providingCompilationUnit, implementsEdit); }); } - - /** - * Creates an edit action that inserts a private field into the class - * - * @param fieldName is the field name - * @param fieldType is the field type - * @param compilationUnit is the compilation unit to edit - * @param typeName is the type within the compilation unit to edit - * @return an edit action that can be applied to the compilation unit to add a - * field - */ - private InsertEdit editForAddingField(String fieldName, String fieldType, ICompilationUnit compilationUnit, - String typeName) throws JavaModelException { - IType iClass = compilationUnit.getType(typeName); - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(iClass); - String fieldStr = "private " + fieldType + " " + fieldName + ";"; - return new InsertEdit(offset, fieldStr); - } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java index a4b56a3b7..14f98fe5d 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/InterfaceMappingTransformationTest.java @@ -10,7 +10,7 @@ import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.REPOSITORY_NAME; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.OperationInterface; @@ -18,6 +18,7 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; class InterfaceMappingTransformationTest extends Java2PcmTransformationTest { /** @@ -104,11 +105,9 @@ void testRenameInterface() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); - - int offset = anInterface.getBuffer().getContents().indexOf(INTERFACE_NAME); - ReplaceEdit renameEdit = new ReplaceEdit(offset, INTERFACE_NAME.length(), INTERFACE_NAME + RENAME); + TextEdit renameEdit = JavaTextEditFactory.renameCompilationUnit(anInterface, INTERFACE_NAME, + INTERFACE_NAME + RENAME); view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); - // TODO: resource URI is not adjusted to new name }); validatePcmView(view -> { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java index 01142a9db..420a762e7 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/MethodMappingTransformationTest.java @@ -10,19 +10,16 @@ import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.OperationInterface; import org.palladiosimulator.pcm.repository.OperationSignature; import org.palladiosimulator.pcm.repository.Repository; -import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; class MethodMappingTransformationTest extends Java2PcmTransformationTest { @Test @@ -40,14 +37,11 @@ void testAddMethod() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); + IType interfaceType = anInterface.getType(INTERFACE_NAME); - String methodName = OPERATION_SIGNATURE_1_NAME; - String methodSignature = "\n" + returnType + " " + methodName + "();\n"; - - IType firstType = anInterface.getAllTypes()[0]; - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); - InsertEdit insertEdit = new InsertEdit(offset, methodSignature); - view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + TextEdit addMethodEdit = JavaTextEditFactory.addMethodSignature(interfaceType, returnType, + OPERATION_SIGNATURE_1_NAME); + view.getManipulationUtil().editCompilationUnit(anInterface, addMethodEdit); }); validatePcmView(view -> { @@ -56,8 +50,10 @@ void testAddMethod() throws Exception { OperationInterface.class); OperationSignature operationSignature = claimNamedElement( operationInterface.getSignatures__OperationInterface(), OPERATION_SIGNATURE_1_NAME); - assertEquals(0, operationSignature.getParameters__OperationSignature().size()); - assertEquals(returnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); + assertEquals(0, operationSignature.getParameters__OperationSignature().size(), + "signature must not have any parameters"); + assertEquals(returnType, getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature()), + "signature has wrong return type"); }); } @@ -69,13 +65,11 @@ void testRenameMethod() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); + IType interfaceType = anInterface.getType(INTERFACE_NAME); - IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); - int offset = iMethod.getNameRange().getOffset(); - int length = iMethod.getNameRange().getLength(); - String newMethodName = OPERATION_SIGNATURE_1_NAME + RENAME; - ReplaceEdit renameEdit = new ReplaceEdit(offset, length, newMethodName); - view.getManipulationUtil().editCompilationUnit(anInterface, renameEdit); + TextEdit renameMethodEdit = JavaTextEditFactory.renameMethod(interfaceType, OPERATION_SIGNATURE_1_NAME, + OPERATION_SIGNATURE_1_NAME + RENAME); + view.getManipulationUtil().editCompilationUnit(anInterface, renameMethodEdit); }); validatePcmView(view -> { @@ -99,14 +93,10 @@ void testAddReturnType() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); - - IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); - int returnTypeOffset = iMethod.getSourceRange().getOffset(); - String retTypeName = iMethod.getSource().split(" ")[0]; - int returnTypeLength = retTypeName.length(); - DeleteEdit deleteEdit = new DeleteEdit(returnTypeOffset, returnTypeLength); - InsertEdit insertEdit = new InsertEdit(returnTypeOffset, changedReturnType); - view.getManipulationUtil().editCompilationUnit(anInterface, deleteEdit, insertEdit); + IType interfaceType = anInterface.getType(INTERFACE_NAME); + + TextEdit changeReturnTypeEdit = JavaTextEditFactory.changeReturnTypeOfMethod(interfaceType, OPERATION_SIGNATURE_1_NAME, changedReturnType); + view.getManipulationUtil().editCompilationUnit(anInterface, changeReturnTypeEdit); }); validatePcmView(view -> { @@ -115,9 +105,9 @@ void testAddReturnType() throws Exception { OperationInterface.class); OperationSignature operationSignature = claimNamedElement( operationInterface.getSignatures__OperationInterface(), OPERATION_SIGNATURE_1_NAME); - assertEquals(0, operationSignature.getParameters__OperationSignature().size()); + assertEquals(0, operationSignature.getParameters__OperationSignature().size(), "signature must not have any parameters"); assertEquals(changedReturnType, - getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature())); + getTypeNameOfPcmDataType(operationSignature.getReturnType__OperationSignature()), "signature has wrong return type"); }); } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java index 73c95c08f..4232e0752 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/ParameterMappingTransformationTest.java @@ -12,12 +12,8 @@ import static tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils.RENAME; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.ILocalVariable; -import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.OperationInterface; @@ -25,8 +21,8 @@ import org.palladiosimulator.pcm.repository.Parameter; import org.palladiosimulator.pcm.repository.Repository; -import tools.vitruv.applications.pcmjava.javaeditor.java2pcm.legacy.CompilationUnitManipulatorHelper; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; class ParameterMappingTransformationTest extends Java2PcmTransformationTest { private static final String PARAMETER_TYPE = "String"; @@ -40,26 +36,22 @@ void testAddParameter() throws Exception { view.getManipulationUtil().createInterface(INTERFACE_NAME, contractsPackage, null); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); + IType interfaceType = anInterface.getType(INTERFACE_NAME); - String methodName = OPERATION_SIGNATURE_1_NAME; - String returnType = "void"; - String methodSignature = "\n" + returnType + " " + methodName + "();\n"; - - IType firstType = anInterface.getAllTypes()[0]; - int offset = CompilationUnitManipulatorHelper.getOffsetForClassifierManipulation(firstType); - InsertEdit insertEdit = new InsertEdit(offset, methodSignature); - view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + TextEdit addMethodEdit = JavaTextEditFactory.addMethodSignature(interfaceType, "void", + OPERATION_SIGNATURE_1_NAME); + view.getManipulationUtil().editCompilationUnit(anInterface, addMethodEdit); }); changeJavaEditorView(view -> { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); - IMethod iMethod = anInterface.getType(INTERFACE_NAME).getMethod(OPERATION_SIGNATURE_1_NAME, null); - String parameterString = PARAMETER_TYPE + " " + PARAMETER_NAME; - int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; - InsertEdit insertEdit = new InsertEdit(offset, parameterString); - view.getManipulationUtil().editCompilationUnit(anInterface, insertEdit); + IType interfaceType = anInterface.getType(INTERFACE_NAME); + + TextEdit addParameterEdit = JavaTextEditFactory.addMethodParameter(interfaceType, + OPERATION_SIGNATURE_1_NAME, PARAMETER_NAME, PARAMETER_TYPE); + view.getManipulationUtil().editCompilationUnit(anInterface, addParameterEdit); }); validatePcmView(view -> { @@ -84,14 +76,11 @@ void testRenameParameter() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); - IMethod iMethod = findIMethodByName(OPERATION_SIGNATURE_1_NAME, anInterface.getType(INTERFACE_NAME)); - ILocalVariable parameter = this.findParameterInIMethod(iMethod, PARAMETER_NAME); - String typeName = parameter.getSource().split(" ")[0]; - String paramName = parameter.getSource().split(" ")[1]; - int offset = parameter.getSourceRange().getOffset() + typeName.length() + 1; - int length = paramName.length(); - ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterName); - view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); + IType interfaceType = anInterface.getType(INTERFACE_NAME); + + TextEdit renameParameterEdit = JavaTextEditFactory.renameMethodParameter(interfaceType, + OPERATION_SIGNATURE_1_NAME, PARAMETER_NAME, changedParameterName); + view.getManipulationUtil().editCompilationUnit(anInterface, renameParameterEdit); }); validatePcmView(view -> { @@ -116,12 +105,11 @@ void testChangeParameterType() throws Exception { Package contractsPackage = claimPackage(view, JavaQueryUtil.CONTRACTS_PACKAGE); ICompilationUnit anInterface = view.getManipulationUtil().claimCompilationUnit(INTERFACE_NAME, contractsPackage); - IMethod iMethod = findIMethodByName(OPERATION_SIGNATURE_1_NAME, anInterface.getType(INTERFACE_NAME)); - ILocalVariable parameter = this.findParameterInIMethod(iMethod, PARAMETER_NAME); - int offset = parameter.getSourceRange().getOffset(); - int length = parameter.getSource().split(" ")[0].length(); - ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, changedParameterType); - view.getManipulationUtil().editCompilationUnit(anInterface, replaceEdit); + IType interfaceType = anInterface.getType(INTERFACE_NAME); + + TextEdit changeParameterTypeEdit = JavaTextEditFactory.changeParameterType(interfaceType, + OPERATION_SIGNATURE_1_NAME, PARAMETER_NAME, changedParameterType); + view.getManipulationUtil().editCompilationUnit(anInterface, changeParameterTypeEdit); }); validatePcmView(view -> { @@ -135,23 +123,4 @@ void testChangeParameterType() throws Exception { "invalid parameter type"); }); } - - protected IMethod findIMethodByName(final String methodName, final IType type) throws JavaModelException { - for (final IMethod method : type.getMethods()) { - if (method.getElementName().equals(methodName)) { - return method; - } - } - throw new RuntimeException("Method " + methodName + " not found in type " + type); - } - - private ILocalVariable findParameterInIMethod(final IMethod iMethod, final String parameterName) - throws JavaModelException { - for (final ILocalVariable localVariable : iMethod.getParameters()) { - if (localVariable.getElementName().equals(parameterName)) { - return localVariable; - } - } - throw new RuntimeException("Old parameter with name " + parameterName + " not found"); - } } diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java index edb39ab34..4fda9b41e 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/TypeReferenceMappingTransformationTest.java @@ -13,7 +13,7 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; -import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.TextEdit; import org.emftext.language.java.containers.Package; import org.junit.jupiter.api.Test; import org.palladiosimulator.pcm.repository.BasicComponent; @@ -23,6 +23,7 @@ import tools.vitruv.applications.pcmjava.java2pcm.Java2PcmUserSelection; import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil; +import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory; import tools.vitruv.applications.pcmjava.pcm2java.Pcm2JavaTestUtils; class TypeReferenceMappingTransformationTest extends Java2PcmTransformationTest { @@ -56,12 +57,8 @@ void testAddImplementsToClassCorrespondingToBasicComponent() throws Exception { view.getManipulationUtil().addImportToCompilationUnit(componentClass, anInterface, INTERFACE_NAME); IType classType = componentClass.getType(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX); - String newSource = " implements " + INTERFACE_NAME; - int offset = classType.getSourceRange().getOffset(); - int firstBracket = classType.getSource().indexOf("{"); - offset = offset + firstBracket - 1; - final InsertEdit insertEdit = new InsertEdit(offset, newSource); - view.getManipulationUtil().editCompilationUnit(componentClass, insertEdit); + TextEdit implementsEdit = JavaTextEditFactory.addImplementsRelation(classType, INTERFACE_NAME); + view.getManipulationUtil().editCompilationUnit(componentClass, implementsEdit); }); validatePcmView(view -> { diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java index 03927f32f..cb544b115 100644 --- a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/java2pcm/legacy/Java2PcmTransformationTest.java @@ -98,7 +98,6 @@ import tools.vitruv.change.propagation.ChangePropagationMode; import tools.vitruv.framework.views.changederivation.DefaultStateBasedChangeResolutionStrategy; import tools.vitruv.framework.views.changederivation.StateBasedChangeResolutionStrategy; -import tools.vitruv.framework.vsum.internal.InternalVirtualModel; import tools.vitruv.testutils.LegacyVitruvApplicationTest; import tools.vitruv.testutils.TestProject; import tools.vitruv.testutils.views.UriMode; diff --git a/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaTextEditFactory.java b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaTextEditFactory.java new file mode 100644 index 000000000..0d3ba3d37 --- /dev/null +++ b/tests/tools.vitruv.applications.pcmjava.javaeditor.tests/src/tools/vitruv/applications/pcmjava/javaeditor/util/JavaTextEditFactory.java @@ -0,0 +1,169 @@ +package tools.vitruv.applications.pcmjava.javaeditor.util; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.ILocalVariable; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; + +/** + * Factory method to encapsulate all kinds of editing operations directly on the + * Java source code. + */ +public class JavaTextEditFactory { + public enum Visibility { + PUBLIC("public "), PRIVATE("private "), PROTECTED(""); + + private String codeRepresentation; + + public String getCodeRepresentation() { + return this.codeRepresentation; + } + + private Visibility(String codeRepresentation) { + this.codeRepresentation = codeRepresentation; + } + } + + private JavaTextEditFactory() { + } + + // === COMPILATION UNIT === + + public static TextEdit renameCompilationUnit(ICompilationUnit compilationUnit, String previousName, String newName) + throws JavaModelException { + int offset = compilationUnit.getBuffer().getContents().indexOf(previousName); + return new ReplaceEdit(offset, previousName.length(), newName); + // TODO: resource URI is not adjusted to new name + } + + public static TextEdit addImplementsRelation(IType implementingType, String interfaceName) + throws JavaModelException { + String newSource = " implements " + interfaceName; + int offset = implementingType.getSourceRange().getOffset(); + int firstBracket = implementingType.getSource().indexOf("{"); + offset = offset + firstBracket - 1; + return new InsertEdit(offset, newSource); + } + + // === METHOD === + + public static TextEdit addMethodWithEmptyBody(IType type, Visibility visibility, String returnType, + String methodName) throws JavaModelException { + String methodBlock = "\n\t" + visibility.getCodeRepresentation() + returnType + " " + methodName + "() {\n\t}\n"; + int offset = getOffsetForClassifierManipulation(type); + return new InsertEdit(offset, methodBlock); + } + + public static TextEdit addMethodSignature(IType type, String returnType, String methodName) + throws JavaModelException { + String methodSignature = "\n" + returnType + " " + methodName + "();\n"; + int offset = getOffsetForClassifierManipulation(type); + return new InsertEdit(offset, methodSignature); + } + + public static TextEdit renameMethod(IType type, String oldMethodName, String newMethodName) + throws JavaModelException { + IMethod iMethod = getMethodByName(type, oldMethodName); + int offset = iMethod.getNameRange().getOffset(); + int length = iMethod.getNameRange().getLength(); + return new ReplaceEdit(offset, length, newMethodName); + } + + public static TextEdit changeReturnTypeOfMethod(IType type, String methodName, String newReturnType) + throws JavaModelException { + IMethod iMethod = getMethodByName(type, methodName); + int offset = iMethod.getSourceRange().getOffset(); + String oldReturnType = iMethod.getSource().split(" ")[0]; + int returnTypeLength = oldReturnType.length(); + return new ReplaceEdit(offset, returnTypeLength, newReturnType); + } + + // === METHOD PARAMETER === + + public static TextEdit addMethodParameter(IType type, String methodName, String parameterName, String parameterType) + throws JavaModelException { + IMethod iMethod = getMethodByName(type, methodName); + String parameterString = parameterType + " " + parameterName; + int offset = iMethod.getSourceRange().getOffset() + iMethod.getSourceRange().getLength() - 2; + return new InsertEdit(offset, parameterString); + } + + public static TextEdit renameMethodParameter(IType type, String methodName, String oldParameterName, + String newParameterName) throws JavaModelException { + IMethod iMethod = getMethodByName(type, methodName); + ILocalVariable parameter = getParameterByName(iMethod, oldParameterName); + String typeName = parameter.getSource().split(" ")[0]; + String paramName = parameter.getSource().split(" ")[1]; + int offset = parameter.getSourceRange().getOffset() + typeName.length() + 1; + int length = paramName.length(); + return new ReplaceEdit(offset, length, newParameterName); + } + + public static TextEdit changeParameterType(IType type, String methodName, String parameterName, String newType) + throws JavaModelException { + IMethod iMethod = getMethodByName(type, methodName); + ILocalVariable parameter = getParameterByName(iMethod, parameterName); + int offset = parameter.getSourceRange().getOffset(); + int length = parameter.getSource().split(" ")[0].length(); + return new ReplaceEdit(offset, length, newType); + } + + // === FIELDS === + + public static TextEdit addField(IType type, Visibility visibility, String fieldName, String fieldType) + throws JavaModelException { + int offset = getOffsetForClassifierManipulation(type); + String fieldStr = visibility.getCodeRepresentation() + fieldType + " " + fieldName + ";"; + return new InsertEdit(offset, fieldStr); + } + + public static TextEdit renameField(IType type, String previousName, String newName) throws JavaModelException { + IField fieldToRename = type.getField(previousName); + String fieldToRenameString = fieldToRename.getSource(); + String fieldToRenameType = fieldToRenameString.split(" ")[1]; + String fieldToRenameName = fieldToRenameString.split(" ")[2]; + int offset = fieldToRename.getSourceRange().getOffset() + fieldToRenameString.indexOf(fieldToRenameType) + + fieldToRenameType.length() + 1; + int lengthToDelete = fieldToRenameName.length(); + return new ReplaceEdit(offset, lengthToDelete, newName + ";"); + } + + public static TextEdit changeTypeOfField(IType type, String fieldName, String newType) throws JavaModelException { + IField fieldToRename = type.getField(fieldName); + String fieldSrc = fieldToRename.getSource(); + String fieldType = fieldSrc.split(" ")[1]; + int offset = fieldToRename.getSourceRange().getOffset() + fieldSrc.indexOf(fieldType); + int lengthToDelete = fieldType.length(); + return new ReplaceEdit(offset, lengthToDelete, newType); + } + + // === Helpers === + + private static int getOffsetForClassifierManipulation(IType firstType) throws JavaModelException { + int posOfFirstBracket = firstType.getCompilationUnit().getSource().indexOf("{"); + return posOfFirstBracket + 1; + } + + private static IMethod getMethodByName(IType type, String methodName) throws JavaModelException { + for (IMethod method : type.getMethods()) { + if (method.getElementName().equals(methodName)) { + return method; + } + } + throw new IllegalStateException("Method " + methodName + " not found in type " + type); + } + + private static ILocalVariable getParameterByName(IMethod iMethod, String parameterName) throws JavaModelException { + for (ILocalVariable localVariable : iMethod.getParameters()) { + if (localVariable.getElementName().equals(parameterName)) { + return localVariable; + } + } + throw new IllegalStateException("Old parameter with name " + parameterName + " not found"); + } +}