Skip to content

Commit

Permalink
impl, added junit test
Browse files Browse the repository at this point in the history
Issue #236
  • Loading branch information
rsoika committed Apr 20, 2023
1 parent 81796da commit 02fbdfe
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1199,13 +1199,13 @@ public BPMNElementNode cloneBPMNElementNode(BPMNElementNode _bpmnElementNode) th
// cleanup invalid flow references
result.updateSequenceFlowReferences();

// update alls id of bpmn2:documentation childs
NodeList documentationList = this.getElementNode()
.getElementsByTagName(this.getModel().getPrefix(BPMNNS.BPMN2) + ":documentation");
for (int i = 0; i < documentationList.getLength(); i++) {
Element item = (Element) documentationList.item(i);
// update alls id of bpmn2: childs
NodeList childList = result.getElementNode().getChildNodes();
for (int i = 0; i < childList.getLength(); i++) {
Element child = (Element) childList.item(i);
String tag = child.getLocalName();
// update id....
item.setAttribute("id", BPMNModel.generateShortID("documentation"));
child.setAttribute("id", BPMNModel.generateShortID(tag));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.junit.jupiter.api.Test;
Expand All @@ -28,10 +30,11 @@ public class TestCloneFlowElements {
private static Logger logger = Logger.getLogger(TestCloneFlowElements.class.getName());

/**
* This test parses a bpmn file
* This test if cloning a task is updating the id of a documentation and
* updating the sequenceFlow references
*/
@Test
public void testReadEmptyModel() {
public void testCloneDocumentationAndUpdateSequenceFlowRefs() {

logger.info("...read model");
try {
Expand Down Expand Up @@ -91,4 +94,90 @@ public void testReadEmptyModel() {
logger.info("...task cloned successful");
}

/**
* This test clones events and verifies if all child elements got new IDs
*/
@Test
public void testCloneEventsWithChilds() {

logger.info("...read model");
try {
String out = "src/test/resources/output/clone-refmodel-14.bpmn";
BPMNModel model = BPMNModelFactory.read("/refmodel-14.bpmn");

// copy all events
// The expectation here is that all childs of the element gets a new ids
List<String> uniqueIDList = new ArrayList<String>();

List<BPMNElementNode> originNodes = new ArrayList<BPMNElementNode>();
List<BPMNElementNode> clonedNodes = new ArrayList<BPMNElementNode>();

BPMNProcess process = model.openProcess(null);

assertNotNull(process);

originNodes.add(process.findElementNodeById("IntermediateCatchEvent_1"));
originNodes.add(process.findElementNodeById("IntermediateCatchEvent_2"));
originNodes.add(process.findElementNodeById("IntermediateCatchEvent_3"));
originNodes.add(process.findElementNodeById("IntermediateCatchEvent_4"));
originNodes.add(process.findElementNodeById("IntermediateCatchEvent_5"));

// clone....
for (BPMNElementNode origin : originNodes) {
clonedNodes.add(process.cloneBPMNElementNode(origin));
}

assertEquals(5, originNodes.size());
assertNotNull(clonedNodes);
assertEquals(5, clonedNodes.size());

// now we simply expect that all IDs are still unique...
for (BPMNElementNode bpmnnode : originNodes) {
addID(uniqueIDList, bpmnnode.getId());
NodeList childElements = bpmnnode.getElementNode().getChildNodes();
for (int i = 0; i < childElements.getLength(); i++) {
Element child = (Element) childElements.item(i);
if (child.hasAttribute("id")) {
addID(uniqueIDList, child.getAttribute("id"));
}
}
}
System.out.println("------------------");
for (BPMNElementNode bpmnnode : clonedNodes) {
addID(uniqueIDList, bpmnnode.getId());
NodeList childElements = bpmnnode.getElementNode().getChildNodes();
for (int i = 0; i < childElements.getLength(); i++) {
Element child = (Element) childElements.item(i);
if (child.hasAttribute("id")) {
addID(uniqueIDList, child.getAttribute("id"));
}
}
}

// finally write the output...
// we expect that this new model can be opened with other modellers like
// Eclipse-BPMN2
model.save(out);

} catch (BPMNModelException e) {
e.printStackTrace();
fail();
} catch (Exception e) {
System.out.println(e.getMessage());
fail();
}
logger.info("...cloned successful");
}

/*
* This helper method throws an exception if the given id is already in a given
* list
*/
private void addID(List<String> originIDs, String id) throws Exception {
System.out.println("..verify uniqueness of ID " + id);
if (originIDs.contains(id)) {
throw new Exception("ID " + id + " is not unique!");
}
originIDs.add(id);
}
}
60 changes: 60 additions & 0 deletions open-bpmn.metamodel/src/test/resources/refmodel-14.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- origin at X=0.0 Y=0.0 -->
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="Definitions_1" exporter="org.eclipse.bpmn2.modeler.core" exporterVersion="1.5.4.RC1-v20220528-0836-B1">
<bpmn2:process id="Process_1" name="Process 1" isExecutable="false">
<bpmn2:intermediateCatchEvent id="IntermediateCatchEvent_1" name="Intermediate Catch Event 1">
<bpmn2:documentation id="Documentation_3"><![CDATA[d1]]></bpmn2:documentation>
<bpmn2:messageEventDefinition id="MessageEventDefinition_1"/>
</bpmn2:intermediateCatchEvent>
<bpmn2:intermediateCatchEvent id="IntermediateCatchEvent_2" name="Intermediate Catch Event 2">
<bpmn2:documentation id="Documentation_4"><![CDATA[d2]]></bpmn2:documentation>
<bpmn2:linkEventDefinition id="LinkEventDefinition_1" name="Link Event Definition 1"/>
</bpmn2:intermediateCatchEvent>
<bpmn2:intermediateCatchEvent id="IntermediateCatchEvent_3" name="Intermediate Catch Event 3">
<bpmn2:signalEventDefinition id="SignalEventDefinition_1"/>
</bpmn2:intermediateCatchEvent>
<bpmn2:intermediateCatchEvent id="IntermediateCatchEvent_4" name="Intermediate Catch Event 4">
<bpmn2:timerEventDefinition id="TimerEventDefinition_1"/>
</bpmn2:intermediateCatchEvent>
<bpmn2:intermediateCatchEvent id="IntermediateCatchEvent_5" name="Intermediate Catch Event 5">
<bpmn2:conditionalEventDefinition id="ConditionalEventDefinition_1"/>
</bpmn2:intermediateCatchEvent>
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="BPMNShape_IntermediateCatchEvent_1" bpmnElement="IntermediateCatchEvent_1">
<dc:Bounds height="36.0" width="36.0" x="142.0" y="82.0"/>
<bpmndi:BPMNLabel id="BPMNLabel_1" labelStyle="BPMNLabelStyle_1">
<dc:Bounds height="28.0" width="79.0" x="121.0" y="118.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_IntermediateCatchEvent_2" bpmnElement="IntermediateCatchEvent_2">
<dc:Bounds height="36.0" width="36.0" x="273.0" y="82.0"/>
<bpmndi:BPMNLabel id="BPMNLabel_2" labelStyle="BPMNLabelStyle_1">
<dc:Bounds height="28.0" width="79.0" x="252.0" y="118.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_IntermediateCatchEvent_3" bpmnElement="IntermediateCatchEvent_3">
<dc:Bounds height="36.0" width="36.0" x="400.0" y="82.0"/>
<bpmndi:BPMNLabel id="BPMNLabel_3" labelStyle="BPMNLabelStyle_1">
<dc:Bounds height="28.0" width="79.0" x="379.0" y="118.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_IntermediateCatchEvent_4" bpmnElement="IntermediateCatchEvent_4">
<dc:Bounds height="36.0" width="36.0" x="540.0" y="82.0"/>
<bpmndi:BPMNLabel id="BPMNLabel_4" labelStyle="BPMNLabelStyle_1">
<dc:Bounds height="28.0" width="79.0" x="519.0" y="118.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_IntermediateCatchEvent_5" bpmnElement="IntermediateCatchEvent_5">
<dc:Bounds height="36.0" width="36.0" x="673.0" y="82.0"/>
<bpmndi:BPMNLabel id="BPMNLabel_5" labelStyle="BPMNLabelStyle_1">
<dc:Bounds height="28.0" width="79.0" x="652.0" y="118.0"/>
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
<bpmndi:BPMNLabelStyle id="BPMNLabelStyle_1">
<dc:Font name="arial" size="9.0"/>
</bpmndi:BPMNLabelStyle>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>

0 comments on commit 02fbdfe

Please sign in to comment.