Skip to content

Commit

Permalink
#265 Add junit to test additional image dependency merge
Browse files Browse the repository at this point in the history
scenarios

Change-Id: Idcfe511aba1a2719640c3ac2b05d8443f09fef8e
Signed-off-by: Anita Matei Gabriel <matei.anita@thalesgroup.com>
  • Loading branch information
AnitaMateiGabriel authored and pdulth committed Apr 3, 2023
1 parent fa2bce9 commit b6493d8
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*********************************************************************
* Copyright (c) 2023 Thales Global Services S.A.S.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales Global Services S.A.S. - initial API and implementation
**********************************************************************/
package org.eclipse.emf.diffmerge.tests.ju;

import static org.junit.Assert.assertEquals;

import org.eclipse.emf.diffmerge.diffdata.impl.EElementPresenceImpl;
import org.eclipse.emf.diffmerge.generic.api.diff.IDifference;
import org.eclipse.emf.diffmerge.tests.elements.Elements.ElementsFactory;
import org.eclipse.emf.diffmerge.tests.elements.Elements.Root;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.business.internal.image.ImageDependenciesAnnotationHelper;
import org.eclipse.sirius.viewpoint.description.DAnnotationEntry;
import org.eclipse.sirius.viewpoint.description.DescriptionFactory;

public class ImageDependencyAnnotationDiffMergeTest extends ImageDependencyDiffMergeTest {

@Override
protected int getRequiredDifferences() {
return 0;
}

@Override
protected void assertDifferences(IDifference diff) {
assertNotImageDepsAnnotation(diff);

}

@Override
protected EObject getSourceRoot() {
DAnnotationEntry annotation = DescriptionFactory.eINSTANCE.createDAnnotationEntry();
annotation.setSource(ImageDependenciesAnnotationHelper.IMAGES_DEPENDENCIES_ANNOTATION_SOURCE_NAME);

return annotation;
}

@Override
protected EObject getTargetRoot() {
Root root = ElementsFactory.eINSTANCE.createRoot();

return root;
}

private void assertNotImageDepsAnnotation(IDifference diff) {
if (diff instanceof EElementPresenceImpl) {
EElementPresenceImpl castDiff = (EElementPresenceImpl) diff;
if (castDiff.getElementMatch().getReference() instanceof DAnnotationEntry) {
assertEquals("Image dependency annotations should not be proposed for merging.", false,
((DAnnotationEntry) castDiff.getElementMatch().getReference()).getSource()
.equals(ImageDependenciesAnnotationHelper.IMAGES_DEPENDENCIES_ANNOTATION_SOURCE_NAME));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*********************************************************************
* Copyright (c) 2023 Thales Global Services S.A.S.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales Global Services S.A.S. - initial API and implementation
**********************************************************************/
package org.eclipse.emf.diffmerge.tests.ju;

import static org.junit.Assert.assertEquals;

import java.util.Collection;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.diffmerge.diffdata.EComparison;
import org.eclipse.emf.diffmerge.diffdata.impl.EComparisonImpl;
import org.eclipse.emf.diffmerge.generic.api.Role;
import org.eclipse.emf.diffmerge.generic.api.diff.IDifference;
import org.eclipse.emf.diffmerge.sirius.SiriusDiffPolicy;
import org.eclipse.emf.diffmerge.sirius.SiriusMatchPolicy;
import org.eclipse.emf.diffmerge.sirius.SiriusMergePolicy;
import org.eclipse.emf.diffmerge.sirius.SiriusScope;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.sirius.business.api.session.Session;
import org.junit.Before;
import org.junit.Test;

public abstract class ImageDependencyDiffMergeTest {
private SiriusScope sourceScope;
private SiriusScope destinationScope;
private EComparison comparison;

private int passedAsserts = 0;

@Before
public void setupProjects() {
URI semanticDestination = URI.createPlatformResourceURI("target project/target project.elements", false);
ProjectHelper.saveSemanticResource(semanticDestination, getTargetRoot());
Session destinationSession = ProjectHelper.createSessionOn(semanticDestination);
destinationScope = new SiriusScope(destinationSession.getSessionResource().getURI(),
destinationSession.getTransactionalEditingDomain(), true);

URI semanticSource = URI.createPlatformResourceURI("source project/source project.elements", false);
ProjectHelper.saveSemanticResource(semanticSource, getSourceRoot());
Session sourceSession = ProjectHelper.createSessionOn(semanticSource);
sourceScope = new SiriusScope(sourceSession.getSessionResource().getURI(),
sourceSession.getTransactionalEditingDomain(), true);

comparison = new EComparisonImpl(destinationScope, sourceScope);
destinationScope.setComparison(comparison);
sourceScope.setComparison(comparison);
}

@Test
public void runTest() {
TransactionalEditingDomain domain = (TransactionalEditingDomain) destinationScope.getEditingDomain();
domain.getCommandStack().execute(new RecordingCommand(domain) {

@Override
protected void doExecute() {
comparison.compute(new SiriusMatchPolicy(), new SiriusDiffPolicy(), new SiriusMergePolicy(),
new NullProgressMonitor());
Collection<IDifference<EObject>> diffs = comparison.getDifferences(Role.REFERENCE);

comparison.merge(Role.TARGET, true, new NullProgressMonitor());

diffs.stream().forEach(diff -> {
assertDifferences(diff);
});

assertEquals("More or less differences have been found than required.", getRequiredDifferences(),
passedAsserts);
}

});
}

protected void increasePassedAsserts() {
passedAsserts++;
}

protected abstract int getRequiredDifferences();

protected abstract void assertDifferences(IDifference diff);

protected abstract EObject getSourceRoot();

protected abstract EObject getTargetRoot();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*********************************************************************
* Copyright (c) 2023 Thales Global Services S.A.S.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales Global Services S.A.S. - initial API and implementation
**********************************************************************/
package org.eclipse.emf.diffmerge.tests.ju;

import static org.junit.Assert.assertEquals;

import org.eclipse.emf.diffmerge.diffdata.impl.EAttributeValuePresenceImpl;
import org.eclipse.emf.diffmerge.generic.api.diff.IDifference;
import org.eclipse.emf.diffmerge.generic.gdiffdata.impl.GElementRelativePresenceImpl;
import org.eclipse.emf.diffmerge.tests.elements.Elements.Element;
import org.eclipse.emf.diffmerge.tests.elements.Elements.ElementsFactory;
import org.eclipse.emf.diffmerge.tests.elements.Elements.ElementsPackage;
import org.eclipse.emf.diffmerge.tests.elements.Elements.Root;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.business.api.image.RichTextAttributeRegistry;
import org.junit.Before;

public class ImageDependencyRichTextDiffMergeTest extends ImageDependencyDiffMergeTest {

private final String ROOT_ID = "root";
private final String EXISTING_ELEMENT_ID = "existingElement";
private final String NEW_ELEMENT_ID = "newElement";
private final String NEW_ELEMENT_CHILD_ID = "newElementChild";

@Override
protected int getRequiredDifferences() {
return 4;
}

@Override
protected void assertDifferences(IDifference diff) {
assertHardcodedPathElement(diff, EXISTING_ELEMENT_ID);
assertHardcodedPathElement(diff, NEW_ELEMENT_ID);
assertHardcodedPathElement(diff, NEW_ELEMENT_CHILD_ID);
assertRelativePathElement(diff, EXISTING_ELEMENT_ID);
}

@Override
protected EObject getSourceRoot() {
Root root = ElementsFactory.eINSTANCE.createRoot();
Element existingElement = ElementsFactory.eINSTANCE.createElement();
Element newElement = ElementsFactory.eINSTANCE.createElement();
Element newElementChild = ElementsFactory.eINSTANCE.createElement();

root.setId(ROOT_ID);
existingElement.setId(EXISTING_ELEMENT_ID);
newElement.setId(NEW_ELEMENT_ID);
newElementChild.setId(NEW_ELEMENT_CHILD_ID);

existingElement.setName("<p><img src=\"source project/image.png\" /></p>\n");
newElement.setName("<p><img src=\"source project/image.png\" /></p>\n");
newElementChild.setName("<p><img src=\"source project/image.png\" /></p>\n");

root.getContent().add(existingElement);
newElement.getManyContent().add(newElementChild);
root.getContent().add(newElement);

return root;

}

@Override
protected EObject getTargetRoot() {
Root root = ElementsFactory.eINSTANCE.createRoot();
Element existingElement = ElementsFactory.eINSTANCE.createElement();
root.setId(ROOT_ID);
existingElement.setId(EXISTING_ELEMENT_ID);

root.getContent().add(existingElement);
return root;
}

@Before
public void initializeElements() {
RichTextAttributeRegistry.INSTANCE.add(ElementsPackage.Literals.NAMED_ELEMENT__NAME);
}

private void assertHardcodedPathElement(IDifference diff, String name) {
if (diff instanceof GElementRelativePresenceImpl) {
GElementRelativePresenceImpl castDiff = (GElementRelativePresenceImpl) diff;
if (castDiff.getElementMatch().getTarget() instanceof Element
&& ((Element) castDiff.getElementMatch().getTarget()).getId().equals(name)) {
Element element = ((Element) castDiff.getElementMatch().getTarget());
assertEquals(String.format("Image path for %s should be hardcoded to target project.", name),
"<p><img src=\"target project/image.png\" /></p>\n", element.getName());
increasePassedAsserts();
}
}
}

private void assertRelativePathElement(IDifference diff, String name) {
if (diff instanceof EAttributeValuePresenceImpl) {
EAttributeValuePresenceImpl castDiff = (EAttributeValuePresenceImpl) diff;
if (castDiff.getElementMatch().getTarget() instanceof Element
&& ((Element) castDiff.getElementMatch().getTarget()).getId().equals(name)) {
assertEquals("Image path of modified element should be made relative.",
"<p><img src=\"./image.png\" /></p>\n", castDiff.getValue());
increasePassedAsserts();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*********************************************************************
* Copyright (c) 2023 Thales Global Services S.A.S.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales Global Services S.A.S. - initial API and implementation
**********************************************************************/
package org.eclipse.emf.diffmerge.tests.ju;

import static org.junit.Assert.assertEquals;

import org.eclipse.emf.diffmerge.generic.api.diff.IDifference;
import org.eclipse.emf.diffmerge.generic.gdiffdata.impl.GElementRelativePresenceImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.diagram.DNodeContainer;
import org.eclipse.sirius.diagram.DiagramFactory;
import org.eclipse.sirius.diagram.WorkspaceImage;

public class ImageDependencyWorkspaceImageDiffMergeTest extends ImageDependencyDiffMergeTest {
private final String NODE_ID = "node";

@Override
protected int getRequiredDifferences() {
return 1;
}

@Override
protected void assertDifferences(IDifference diff) {
assertPathWorkspaceImage(diff);

}

@Override
protected EObject getSourceRoot() {
DNodeContainer node = DiagramFactory.eINSTANCE.createDNodeContainer();
node.setUid(NODE_ID);
WorkspaceImage workspaceImage = DiagramFactory.eINSTANCE.createWorkspaceImage();
workspaceImage.setWorkspacePath("source project/test.PNG");
node.setOwnedStyle(workspaceImage);

return node;
}

@Override
protected EObject getTargetRoot() {
DNodeContainer node = DiagramFactory.eINSTANCE.createDNodeContainer();
node.setUid(NODE_ID);
return node;
}

private void assertPathWorkspaceImage(IDifference diff) {
if (diff instanceof GElementRelativePresenceImpl) {
GElementRelativePresenceImpl castDiff = (GElementRelativePresenceImpl) diff;
if (castDiff.getElementMatch().getTarget() instanceof WorkspaceImage) {
WorkspaceImage workspaceImage = ((WorkspaceImage) castDiff.getElementMatch().getTarget());
assertEquals("Image path should be hardcoded to target project.", "target project/test.PNG",
workspaceImage.getWorkspacePath());
increasePassedAsserts();
}
}
}
}
Loading

0 comments on commit b6493d8

Please sign in to comment.