Jelajahi Sumber

Merge pull request #487 from Yakindu/issue_482

Issue 482
Thomas Kutz 9 tahun lalu
induk
melakukan
7f29ca4f24

+ 14 - 1
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/gtest/GTest.java

@@ -19,8 +19,21 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.TYPE)
 public @interface GTest {
-	
+
 	String sourceFile();
 	String program();
 	String model();
+
+	/**
+	 * If no test bundle provided, source files (cc and sgen) are expected to be
+	 * in the Junit test's bundle and the model file (sct) in
+	 * org.yakindu.sct.test.models.
+	 * 
+	 * If test bundle is provided, all test files (cc, sgen, sct) are exepcted
+	 * to be located in the test bundle. Also a project of the same name is
+	 * created in Junit workspace.
+	 * 
+	 */
+	String testBundle() default "";
+
 }

+ 42 - 7
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/gtest/GTestHelper.java

@@ -32,6 +32,7 @@ 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;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
@@ -127,18 +128,21 @@ public class GTestHelper {
 
 		// copy model to JUnit workspace
 		copyFileFromBundleToFolder(getModelBundle(), getModelPath(), targetPath);
-
+		
 		String sgenFileName = getTestProgram() + ".sgen";
 		copyFileFromBundleToFolder(getTestBundle(), sgenFileName, targetPath);
 
 		IPath path = new Path(sgenFileName);
-		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
-		Resource sgenResource = loadResource(file);
+		Resource sgenResource = loadResource(getWorkspaceFileFor(path));
 		GeneratorModel model = (GeneratorModel) sgenResource.getContents().get(
 				0);
 		model.getEntries().get(0).setElementRef(getStatechart());
 		new GeneratorExecutor().executeGenerator(model);
 	}
+	
+	protected IFile getWorkspaceFileFor(IPath filePath) {
+		return ResourcesPlugin.getWorkspace().getRoot().getFile(getTargetProjectPath().append(filePath));
+	}
 
 	protected Statechart getStatechart() {
 		IPath path = new Path(getTargetPath().toString() + "/"
@@ -157,7 +161,11 @@ public class GTestHelper {
 	}
 
 	protected Bundle getModelBundle() {
-		return FrameworkUtil.getBundle(SCTUnitTestModels.class);
+		Bundle bundle = getAnnotatedTestBundle();
+		if (bundle == null) {
+			return FrameworkUtil.getBundle(SCTUnitTestModels.class);
+		}
+		return bundle;
 	}
 
 	private void copyFilesFromBundleToFolder() {
@@ -236,7 +244,7 @@ public class GTestHelper {
 	}
 
 	protected IPath getTargetPath() {
-		return new Path(getTestProgram()).removeLastSegments(1);
+		return getTargetProjectPath().append(new Path(getTestProgram()).removeLastSegments(1));
 	}
 
 	protected IPath getModelPath() {
@@ -265,6 +273,14 @@ public class GTestHelper {
 	protected String getModelAnnotation() {
 		return owner.getClass().getAnnotation(GTest.class).model();
 	}
+	
+	protected String getTestBundleAnnotation() {
+		return owner.getClass().getAnnotation(GTest.class).testBundle();
+	}
+	
+	protected IPath getTargetProjectPath() {
+		return new Path(getTestBundleAnnotation());
+	}
 
 	protected void copyFileFromBundleToFolder(Bundle bundle, String sourcePath,
 			String targetPath) {
@@ -304,7 +320,22 @@ public class GTestHelper {
 	}
 
 	protected Bundle getTestBundle() {
-		return FrameworkUtil.getBundle(owner.getClass());
+		Bundle bundle = getAnnotatedTestBundle();
+		if (bundle == null) {
+			return FrameworkUtil.getBundle(owner.getClass());
+		}
+		return bundle;
+	}
+
+	protected Bundle getAnnotatedTestBundle() {
+		String testProject = getTestBundleAnnotation();
+		if (!testProject.isEmpty()) {
+			Bundle testBundle = Platform.getBundle(testProject);
+			if (testBundle != null) {
+				return testBundle;
+			}
+		}
+		return null;
 	}
 
 	protected void copyFileFromBundle(String sourcePath, IFile targetFile) {
@@ -363,7 +394,7 @@ public class GTestHelper {
 			}
 		} else {
 			try {
-				project.create(monitor);
+				createTestProject(project, monitor);
 				project.open(monitor);
 			} catch (CoreException e) {
 				throw new RuntimeException(e);
@@ -375,6 +406,10 @@ public class GTestHelper {
 		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()

+ 16 - 2
test-plugins/org.yakindu.sct.generator.c.test/src/org/yakindu/sct/generator/c/gtest/GTestRunner.java

@@ -33,6 +33,7 @@ import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
 import org.junit.After;
@@ -96,7 +97,7 @@ public class GTestRunner extends Runner {
 	}
 
 	private CharSequence readSourceFile(String sourceFile) throws IOException {
-		Bundle bundle = FrameworkUtil.getBundle(testClass);
+		Bundle bundle = getTestBundle();
 		InputStream is = FileLocator.openStream(bundle, new Path(sourceFile),
 				false);
 		Reader reader = new InputStreamReader(is);
@@ -110,6 +111,17 @@ public class GTestRunner extends Runner {
 		is.close();
 		return sb;
 	}
+	
+	protected Bundle getTestBundle() {
+		String testProject = testClass.getAnnotation(GTest.class).testBundle();
+		if (!testProject.isEmpty()) {
+			Bundle testBundle = Platform.getBundle(testProject);
+			if (testBundle != null) {
+				return testBundle;
+			}
+		}
+		return FrameworkUtil.getBundle(testClass);
+	}
 
 	/*
 	 * (non-Javadoc)
@@ -177,7 +189,9 @@ public class GTestRunner extends Runner {
 		if (Platform.getOS().equalsIgnoreCase(Platform.OS_WIN32)) {
 			program += ".exe";
 		}
-		IResource programFile = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(program));
+		String targetProject = testClass.getAnnotation(GTest.class).testBundle();
+		IPath programPath = new Path(targetProject).append(program);
+		IResource programFile = ResourcesPlugin.getWorkspace().getRoot().findMember(programPath);
 		IContainer programContainer = programFile.getParent();
 		if (!programContainer.isAccessible()) {
 			throw new RuntimeException("Test program container " + programContainer.getLocation().toOSString() + " inaccessible");