Commit cfbf0086 authored by Alexander Diewald's avatar Alexander Diewald Committed by Simon Barner
Browse files

RCP: Fix Java code gen test case.

Issue-Ref: 3620
Issue-Url: https://af3-developer.fortiss.org/issues/3620

Signed-off-by: Alexander Diewald's avatarAlexander Diewald <diewald@fortiss.org>
parent 2fe6b946
......@@ -18,6 +18,7 @@ package test.org.fortiss.af3.rcp.application.semantic.alignment.java;
import static java.io.File.separator;
import static org.conqat.lib.commons.filesystem.FileSystemUtils.deleteRecursively;
import static org.conqat.lib.commons.filesystem.FileSystemUtils.ensureDirectoryExists;
import static org.fortiss.af3.generator.common.utils.JavaLanguageModelElementFactory.SRC_GEN_SOURCE_PACKAGE;
import static org.fortiss.tooling.base.utils.SystemUtils.getTempDirPath;
import static org.junit.Assert.assertTrue;
......@@ -26,6 +27,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
......@@ -44,7 +46,6 @@ import org.fortiss.af3.expression.language.evaluation.NoVal;
import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.expression.utils.ExpressionModelElementFactory;
import org.fortiss.af3.generator.common.model.java.JavaSourcePackage;
import org.fortiss.af3.generator.common.utils.JavaLanguageModelElementFactory;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.utils.ProjectUtils;
......@@ -128,13 +129,18 @@ public class JavaEquivalenceTest_SimpleTrafficLights {
/** File representing the target forlder for Java code generation. */
File targetLocation;
/** Path to the root folder of the generated Java code. */
public String javaSourcePath;
public File javaSourcePath;
/** Name of Java class representing the root component of the model. */
public String rootCompClassName;
/** Set up. */
/**
* Set up.
*
* @throws MalformedURLException
* if the temporary user directory for code generation cannot be found.
*/
@Before
public void setup() {
public void setup() throws MalformedURLException {
setupProject();
setupJavaPaths();
generateJavaCode();
......@@ -159,15 +165,17 @@ public class JavaEquivalenceTest_SimpleTrafficLights {
/**
* Sets up the paths to generated Java code.
*
* @throws MalformedURLException
* if the temporary user directory for code generation cannot be found.
*
* @see #javaSourcePath
* @see #rootCompClassName
*/
protected void setupJavaPaths() {
protected void setupJavaPaths() throws MalformedURLException {
rootCompClassName = IdentifierUtils.getUniqueIdentifier(rootComp);
targetLocation = new File(getTempDirPath(false, getClass().getName()));
javaSourcePath = targetLocation.getAbsolutePath() + separator +
JavaLanguageModelElementFactory.SRC_GEN_SOURCE_PACKAGE + separator;
javaSourcePath = new File(targetLocation, SRC_GEN_SOURCE_PACKAGE);
}
/**
......@@ -195,18 +203,21 @@ public class JavaEquivalenceTest_SimpleTrafficLights {
}
/** Sets up the generated Java code. */
protected void setupGeneratedJavaCode(String sourcePath, String rootCompClassName) {
protected void setupGeneratedJavaCode(File sourcePath, String rootCompClassName) {
// Compile the generated Java code
String mainJavaFilePath = sourcePath + "af3" + separator + "javagen" + separator +
"component" + separator + rootCompClassName + ".java";
String[] args = new String[] {"-sourcepath", sourcePath, mainJavaFilePath};
String mainJavaFilePath = sourcePath + separator + "af3" + separator + "javagen" +
separator + "component" + separator + rootCompClassName + ".java";
String[] args = new String[] {"-sourcepath", sourcePath.toString(), mainJavaFilePath};
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, args);
URL url;
ClassLoader classLoader = null;
try {
url = new URL("file", null, -1, sourcePath);
ClassLoader classLoader = URLClassLoader.newInstance(new URL[] {url});
// Due to a bug in the URLClassPath (see
// https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/999dbd4192d0f819cb5224f26e9e7fa75ca6f289/src/java.base/share/classes/jdk/internal/loader/URLClassPath.java#L471)
// generated classes are not found on Windows. Hence, we must "unify" the source path.
URL sourcePathURL = new URL(sourcePath.toURI().toString().replace("\\", "/"));
classLoader = URLClassLoader.newInstance(new URL[] {sourcePathURL});
// Load the classes for root component and data dictionary
gen_rootComp = Class.forName(
ComponentProgramToJavaSourcePackageTransformation.COMPONENT_JAVA_PACKAGE + "." +
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment