Browse Source

Extracted generic copy functions from GTestHelper (#2167)

Thomas Kutz 7 years ago
parent
commit
9cabe221d0

+ 2 - 1
test-plugins/org.yakindu.sct.generator.c.test/META-INF/MANIFEST.MF

@@ -21,4 +21,5 @@ Require-Bundle: org.junit,
  org.eclipse.xtext.ui.shared,
  org.yakindu.sct.domain.generic.resource
 Export-Package: org.yakindu.sct.generator.c.gtest,
- org.yakindu.sct.generator.c.test
+ org.yakindu.sct.generator.c.test,
+ org.yakindu.sct.generator.c.test.utils

+ 10 - 0
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/gtest/GCCCommandExecutor.java

@@ -1,3 +1,13 @@
+/**
+ * Copyright (c) 2018 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     committers of YAKINDU - initial API and implementation
+ */
 package org.yakindu.sct.generator.c.gtest;
 
 import java.io.File;

+ 12 - 121
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/gtest/GTestHelper.java

@@ -12,25 +12,17 @@
 package org.yakindu.sct.generator.c.gtest;
 
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
-import org.eclipse.core.resources.IContainer;
 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.resources.IncrementalProjectBuilder;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.emf.common.util.URI;
@@ -39,6 +31,7 @@ import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.FrameworkUtil;
 import org.yakindu.sct.generator.builder.EclipseContextGeneratorExecutorLookup;
+import org.yakindu.sct.generator.c.test.utils.TestFileCopier;
 import org.yakindu.sct.generator.core.execution.GeneratorExecutorLookup;
 import org.yakindu.sct.model.sgen.GeneratorModel;
 import org.yakindu.sct.model.sgraph.Statechart;
@@ -70,6 +63,7 @@ public class GTestHelper {
 
 	private final Object owner;
 	protected Compiler compiler;
+	protected TestFileCopier copier;
 
 	public GTestHelper(Object owner) {
 		this(owner, Compiler.GCC);
@@ -78,6 +72,11 @@ public class GTestHelper {
 	public GTestHelper(Object owner, Compiler compiler) {
 		this.owner = owner;
 		this.compiler = compiler;
+		
+		this.copier = new TestFileCopier((p, m) -> {
+			p.create(m);
+			return p;
+		});
 	}
 
 	public void compile() {
@@ -93,10 +92,10 @@ public class GTestHelper {
 		IPath targetPath = getTargetPath();
 
 		// copy model to JUnit workspace
-		copyFileFromBundleToFolder(getModelBundle(), getModelPath(), targetPath);
+		copier.copyFileFromBundleToFolder(getModelBundle(), getModelPath(), targetPath);
 
 		String sgenFileName = getSgenFileName(getTestProgram());
-		copyFileFromBundleToFolder(getTestBundle(), sgenFileName, targetPath);
+		copier.copyFileFromBundleToFolder(getTestBundle(), sgenFileName, targetPath);
 
 		GeneratorModel model = getGeneratorModel(sgenFileName);
 		model.getEntries().get(0).setElementRef(getStatechart());
@@ -171,7 +170,7 @@ public class GTestHelper {
 		List<String> testDataFiles = getFilesToCopy();
 		getTestDataFiles(testDataFiles);
 		for (String file : testDataFiles) {
-			copyFileFromBundleToFolder(getTestBundle(), file, targetPath);
+			copier.copyFileFromBundleToFolder(getTestBundle(), file, targetPath);
 		}
 	}
 
@@ -273,36 +272,7 @@ public class GTestHelper {
 	protected IPath getTargetProjectPath() {
 		return new Path(getTestBundleAnnotation());
 	}
-
-	protected void copyFileFromBundleToFolder(Bundle bundle, String sourcePath, String targetPath) {
-		copyFileFromBundleToFolder(bundle, new Path(sourcePath), new Path(targetPath));
-	}
-
-	protected void copyFileFromBundleToFolder(Bundle bundle, String sourcePath, IPath targetPath) {
-		copyFileFromBundleToFolder(bundle, new Path(sourcePath), targetPath);
-	}
-
-	protected void copyFileFromBundleToFolder(Bundle bundle, IPath sourcePath, IPath targetPath) {
-		String fileName = sourcePath.lastSegment();
-		copyFileFromBundle(bundle, sourcePath, targetPath.append(fileName));
-	}
-
-	protected void copyFileFromBundle(Bundle bundle, String sourcePath, String targetPath) {
-		copyFileFromBundle(bundle, sourcePath, new Path(targetPath));
-	}
-
-	protected void copyFileFromBundle(Bundle bundle, String sourcePath, IPath targetPath) {
-		copyFileFromBundle(bundle, new Path(sourcePath), targetPath);
-	}
-
-	protected void copyFileFromBundle(Bundle bundle, IPath sourcePath, IPath targetPath) {
-		try {
-			InputStream is = FileLocator.openStream(bundle, sourcePath, false);
-			createFile(targetPath, is);
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
+	
 
 	protected Bundle getTestBundle() {
 		Bundle bundle = getAnnotatedTestBundle();
@@ -323,85 +293,6 @@ public class GTestHelper {
 		return null;
 	}
 
-	protected void copyFileFromBundle(String sourcePath, IFile targetFile) {
-		copyFileFromBundle(new Path(sourcePath), targetFile);
-	}
-
-	protected void copyFileFromBundle(IPath sourcePath, IFile targetFile) {
-		try {
-			InputStream is = FileLocator.openStream(getTestBundle(), sourcePath, false);
-			createFile(targetFile, is);
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	protected void createFile(String path, InputStream source) {
-		createFile(new Path(path), source);
-	}
-
-	protected void createFile(IPath path, InputStream source) {
-		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
-		createFile(file, source);
-	}
-
-	protected void createFile(IFile file, InputStream source) {
-		ensureContainerExists(file.getParent());
-		try {
-			if (file.exists()) {
-				file.setContents(source, true, false, new NullProgressMonitor());
-			} else {
-				file.create(source, true, new NullProgressMonitor());
-			}
-		} catch (CoreException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	protected IFolder getFolder(String path) {
-		return ensureContainerExists(ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(path)));
-	}
-
-	protected IFolder getFolder(IPath path) {
-		return ensureContainerExists(ResourcesPlugin.getWorkspace().getRoot().getFolder(path));
-	}
-
-	protected <T extends IContainer> T ensureContainerExists(T container) {
-		IProgressMonitor monitor = new NullProgressMonitor();
-		IProject project = container.getProject();
-		if (project.exists()) {
-			if (!project.isOpen()) {
-				throw new RuntimeException("Project " + project.getName() + " closed");
-			}
-		} else {
-			try {
-				createTestProject(project, monitor);
-				project.open(monitor);
-			} catch (CoreException e) {
-				throw new RuntimeException(e);
-			}
-		}
-		if (container instanceof IFolder) {
-			doEnsureFolderExists((IFolder) container, monitor);
-		}
-		return container;
-	}
-
-	protected void createTestProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
-		projectHandle.create(monitor);
-	}
-
-	private void doEnsureFolderExists(IFolder folder, IProgressMonitor monitor) {
-		if (!folder.exists()) {
-			if (!folder.getParent().exists() && folder.getParent() instanceof IFolder) {
-				doEnsureFolderExists((IFolder) folder.getParent(), monitor);
-			}
-			try {
-				folder.create(true, true, monitor);
-			} catch (CoreException e) {
-				throw new RuntimeException(e);
-			}
-		}
-	}
+	
 
 }

+ 145 - 0
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/test/utils/TestFileCopier.java

@@ -0,0 +1,145 @@
+/**
+ * Copyright (c) 2018 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.c.test.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.osgi.framework.Bundle;
+
+public class TestFileCopier {
+
+	private IProject testProject;
+	private ITestProjectFactory projectFactory;
+
+	public interface ITestProjectFactory {
+		IProject createTestProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException;
+	}
+
+	public TestFileCopier(ITestProjectFactory projectFactory) {
+		this.projectFactory = projectFactory;
+	}
+
+	public void copyFileFromBundleToFolder(Bundle bundle, String sourcePath, String targetPath) {
+		copyFileFromBundleToFolder(bundle, new Path(sourcePath), new Path(targetPath));
+	}
+
+	public void copyFileFromBundleToFolder(Bundle bundle, String sourcePath, IPath targetPath) {
+		copyFileFromBundleToFolder(bundle, new Path(sourcePath), targetPath);
+	}
+
+	public void copyFileFromBundleToFolder(Bundle bundle, IPath sourcePath, IPath targetPath) {
+		String fileName = sourcePath.lastSegment();
+		copyFileFromBundle(bundle, sourcePath, targetPath.append(fileName));
+	}
+
+	public void copyFolderFromBundleToFolder(Bundle bundle, String sourcePath, String targetPath) {
+		try {
+			URL folder = bundle.getEntry(sourcePath);
+			File file = new File(FileLocator.resolve(folder).toURI());
+			if (file.exists() && file.isDirectory()) {
+				for (File f : file.listFiles()) {
+					copyFileFromBundleToFolder(bundle, new Path(sourcePath).append(f.getName()), new Path(targetPath));
+				}
+			}
+		} catch (URISyntaxException | IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	protected void copyFileFromBundle(Bundle bundle, IPath sourcePath, IPath targetPath) {
+		try {
+			InputStream is = FileLocator.openStream(bundle, sourcePath, false);
+			createFile(targetPath, is);
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	protected void createFile(IPath path, InputStream source) {
+		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+		createFile(file, source);
+	}
+
+	protected void createFile(IFile file, InputStream source) {
+		ensureContainerExists(file.getParent());
+		try {
+			if (file.exists()) {
+				file.setContents(source, true, false, new NullProgressMonitor());
+			} else {
+				file.create(source, true, new NullProgressMonitor());
+			}
+		} catch (CoreException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	protected void ensureContainerExists(IContainer container) {
+		ensureProjectExists(container);
+		if (container instanceof IFolder) {
+			ensureFolderExists((IFolder) container);
+		}
+	}
+	
+	protected void ensureProjectExists(IContainer container) {
+		IProject project = container.getProject();
+		if (project.exists()) {
+			if (!project.isOpen()) {
+				throw new RuntimeException("Project " + project.getName() + " closed");
+			}
+		} else {
+			try {
+				testProject = projectFactory.createTestProject(project, new NullProgressMonitor());
+				project.open(new NullProgressMonitor());
+			} catch (CoreException e) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
+
+	protected void ensureFolderExists(IFolder folder) {
+		if (!folder.exists()) {
+			if (!folder.getParent().exists() && folder.getParent() instanceof IFolder) {
+				ensureFolderExists((IFolder) folder.getParent());
+			}
+			try {
+				folder.create(true, true, new NullProgressMonitor());
+			} catch (CoreException e) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
+
+	public void cleanUp() {
+		try {
+			if (testProject != null) {
+				testProject.delete(true, new NullProgressMonitor());
+				testProject = null;
+			}
+		} catch (CoreException e) {
+			throw new RuntimeException(e);
+		}
+	}
+}