Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactors JavaEditor -> PCM tests to view-based API #256

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ 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,
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
Original file line number Diff line number Diff line change
@@ -1,164 +1,194 @@
package tools.vitruv.applications.pcmjava.javaeditor.java2pcm;

import static org.junit.jupiter.api.Assertions.fail;

import org.eclipse.emf.ecore.EObject;
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.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.TextEdit;
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.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 {
import tools.vitruv.applications.pcmjava.javaeditor.util.JavaQueryUtil;
import tools.vitruv.applications.pcmjava.javaeditor.util.JavaTextEditFactory;

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());
@ParameterizedTest()
@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, REPOSITORY_NAME, BASIC_COMPONENT_NAME);
});

getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_BASIC_COMPONENT.getSelection());
changeJavaEditorView(view -> {
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, 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());
@ParameterizedTest()
@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, REPOSITORY_NAME, COMPOSITE_COMPONENT_NAME);
});

getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_COMPOSITE_COMPONENT.getSelection());
changeJavaEditorView(view -> {
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, 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());
@ParameterizedTest()
@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, REPOSITORY_NAME, SYSTEM_NAME);
});

getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_SYSTEM.getSelection());
changeJavaEditorView(view -> {
Package systemPackage = claimPackage(view, SYSTEM_NAME);
view.getManipulationUtil().createClass(SYSTEM_NAME + IMPLEMENTING_CLASS_SUFFIX, systemPackage, null);
});

validatePcmView(view -> {
System system = claimSingleSystem(view);
assertEquals(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
}
void testAddClassInSecondPackageAsNone() throws Exception {
createRepositoryPackage();

getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection());
changeJavaView(view -> {
createPackageWithPackageInfo(view, REPOSITORY_NAME, BASIC_COMPONENT_NAME);
});

getUserInteraction().addNextSingleSelection(Java2PcmUserSelection.SELECT_NOTHING_DECIDE_LATER.getSelection());
changeJavaEditorView(view -> {
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(view.getRootObjects(System.class).isEmpty(), "there must not exist any System");
});
}

@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());
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(BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, dataTypesPackage,
null);
});

validatePcmView(view -> {
Repository repository = claimSingleRepository(view);
claimDataType(repository, BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX, CompositeDataType.class);
});
}

@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());
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(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");
});
}

@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);
@ParameterizedTest()
@EnumSource(value = Java2PcmUserSelection.class, names = { "SELECT_BASIC_COMPONENT",
"SELECT_NOTHING_DECIDE_LATER" })
void testRenameClassCorrespondingToBasicComponent(Java2PcmUserSelection packageSelection) throws Exception {
testAddClassInSecondPackageAsBasicComponent(packageSelection);

String changedName = BASIC_COMPONENT_NAME + RENAME;

changeJavaEditorView(view -> {
String componentClassName = BASIC_COMPONENT_NAME + IMPLEMENTING_CLASS_SUFFIX;
Package componentPackage = claimPackage(view, BASIC_COMPONENT_NAME);
ICompilationUnit componentClass = view.getManipulationUtil().claimCompilationUnit(componentClassName,
componentPackage);
TextEdit renameEdit = JavaTextEditFactory.renameCompilationUnit(componentClass, componentClassName,
changedName + IMPLEMENTING_CLASS_SUFFIX);
view.getManipulationUtil().editCompilationUnit(componentClass, renameEdit);
});

validatePcmView(view -> {
Repository repository = claimSingleRepository(view);
claimComponent(repository, changedName, BasicComponent.class);
assertEquals(1, repository.getComponents__Repository().size(), "too many components in repository");
});
}

@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);
}

}
Loading