package org.eclipse.bpmn2.modeler.core.test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Collections;
import java.util.List;

import org.eclipse.bpmn2.Definitions;
import org.eclipse.bpmn2.di.BPMNDiagram;
import org.eclipse.bpmn2.impl.DocumentRootImpl;
import org.eclipse.bpmn2.modeler.core.ModelHandlerLocator;
import org.eclipse.bpmn2.util.Bpmn2ResourceImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.ui.services.GraphitiUi;

public abstract class AbstractTestCommand extends RecordingCommand {
	
	protected AbstractBpmnEditorTest testCase;
	protected String diagramName;
	
	protected Diagram diagram;	
	protected IDiagramTypeProvider diagramTypeProvider;
	protected Resource diagramResource;

	protected Throwable recordedException;
	
	public AbstractTestCommand(AbstractBpmnEditorTest importBpmnModelTest, String diagramName) {
		super(importBpmnModelTest.getEditingDomain());
		
		this.testCase = importBpmnModelTest;
		this.diagramName = diagramName;
	}

	@Override
	protected void doExecute() {

		try {
			diagram = Graphiti.getPeCreateService().createDiagram("BPMN2", diagramName, true);

			String fileName = diagramName.replaceAll("/", ".").replaceAll(".bpmn", ".diagram");
			URI uri = URI.createFileURI(testCase.getTempFolder().newFile(fileName).getAbsolutePath());
			
			ModelHandlerLocator.put(uri, testCase.getModelHandler());
	
			diagramResource = testCase.getEditingDomain().getResourceSet().createResource(uri);
			diagramResource.getContents().add(diagram);
			
			diagramTypeProvider = GraphitiUi.getExtensionManager().createDiagramTypeProvider(
					diagram,
					"org.eclipse.bpmn2.modeler.ui.diagram.MainBPMNDiagramType");

			diagramTypeProvider.getDiagramEditor().getResourceSet();
			
			Bpmn2ResourceImpl resource = testCase.getResource();
			
			List<EObject> contents = resource.getContents();
			if (!contents.isEmpty()) {
				DocumentRootImpl rootImpl = (DocumentRootImpl) resource.getContents().get(0);
				Definitions definitions = rootImpl.getDefinitions();
				if (definitions != null) {
					List<BPMNDiagram> diagrams = definitions.getDiagrams();
					
					if (!diagrams.isEmpty()) {
						BPMNDiagram bpmnDiagram = diagrams.get(0);
						diagramTypeProvider.getFeatureProvider().link(diagram, bpmnDiagram);
					}
				}
			}
			
			FileOutputStream outBefore = new FileOutputStream(new File("testResource_before.bpmn"));
			diagram.getChildren();
			resource.save(outBefore, Collections.emptyMap());
			outBefore.close();
			
			test(diagramTypeProvider, diagram);
			
			FileOutputStream outAfter = new FileOutputStream(new File("testResource_after.bpmn"));
			resource.save(outAfter, Collections.emptyMap());
			outAfter.close();
			
			
		} catch (RuntimeException e) {
			this.recordedException = e;
			throw e;
			
		} catch (Throwable e) {
			this.recordedException = e;
			throw new RuntimeException(e);
		}
	}

	public abstract void test(IDiagramTypeProvider diagramTypeProvider, Diagram diagram) throws Throwable;

	public Throwable getRecordedException() {
		return recordedException;
	}
}

@@ -1107,16 +1107,75 @@
 on;%0D%0A%09%0D%0A
+%09private static final String TEST_DIR = %22target/test%22;%0D%0A%09%0D%0A
 %09public 
@@ -2741,131 +2741,223 @@
 %09%7D%0D%0A
-%09%09%09%0D%0A%09%09%09FileOutputStream outBefore = new FileOutputStream(new File(%22testResource_before.bpmn%22));%0D%0A%09%09%09diagram.getChildren();
+%0D%0A%09%09%09new File(TEST_DIR).mkdir();%0D%0A%09%09%09File diagramFile = new File (diagramName);%0D%0A%09%09%09%0D%0A%09%09%09FileOutputStream outBefore = new FileOutputStream(new File(TEST_DIR+File.separatorChar+%22before.%22+diagramFile.getName()));%0D%0A%09%09%09
 %0D%0A%09%09
@@ -3146,33 +3146,66 @@
 ile(
-%22testResource_after.bpmn%22
+TEST_DIR+File.separatorChar+%22after.%22+diagramFile.getName()
 ));%0D
@@ -3285,21 +3285,16 @@
 ;%0D%0A%09%09%09%0D%0A
-%09%09%09%0D%0A
 %09%09%7D catc

write before and after results in to target folder