ailiujiarui commented on code in PR #16542:
URL:
https://github.com/apache/dolphinscheduler/pull/16542#discussion_r1828915108
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
Review Comment:
Has been modified
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
+
+ try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
+
+ File sourceFile = new File(absolutePath);
+ if (!sourceFile.exists()) {
+ log.error("java file not exist", new
IllegalArgumentException());
+ }
+
+ Iterable<? extends JavaFileObject> compilationUnits =
+
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile));
+
+ List<String> options = Arrays.asList(
+ "--release", "8",
+ "-d", outputDirPath);
+
+ JavaCompiler.CompilationTask task = compiler.getTask(
+ null,
+ fileManager,
+ null,
+ options,
+ null,
+ compilationUnits);
+
+ boolean success = task.call();
+
+ if (!success) {
+ throw new RuntimeException("Compilation failed.");
+ }
+ } catch (IOException e) {
+ log.error("compile java file failed:", e);
Review Comment:
Has been modified
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
+
+ try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
+
+ File sourceFile = new File(absolutePath);
+ if (!sourceFile.exists()) {
+ log.error("java file not exist", new
IllegalArgumentException());
+ }
+
+ Iterable<? extends JavaFileObject> compilationUnits =
+
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile));
+
+ List<String> options = Arrays.asList(
+ "--release", "8",
+ "-d", outputDirPath);
+
+ JavaCompiler.CompilationTask task = compiler.getTask(
+ null,
+ fileManager,
+ null,
+ options,
+ null,
+ compilationUnits);
+
+ boolean success = task.call();
+
+ if (!success) {
+ throw new RuntimeException("Compilation failed.");
Review Comment:
Has been modified
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]