Repository: skiptests/astraea
Pull request #13 (2021-09-08 14:43:30)

Make App to show the executable classes
1. 鎖定JDK11
2. 入口App能顯示可執行的classes


Patch:
diff --git a/app/build.gradle b/app/build.gradle
index d048cac7..3e14b024 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -29,6 +29,11 @@ application {
     mainClass = 'org.astraea.App'
 }
 
+java {
+    sourceCompatibility = 11
+    targetCompatibility = 11
+}
+
 tasks.named('test') {
     // Use JUnit Platform for unit tests.
     useJUnitPlatform()
diff --git a/app/src/main/java/org/astraea/App.java b/app/src/main/java/org/astraea/App.java
index 3f9c4380..eb8dd5d6 100644
--- a/app/src/main/java/org/astraea/App.java
+++ b/app/src/main/java/org/astraea/App.java
@@ -1,14 +1,46 @@
-/*
- * This Java source file was generated by the Gradle 'init' task.
- */
 package org.astraea;
 
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
 public class App {
-  public String getGreeting() {
-    return "Hello World!";
+  private static final List<Class<?>> MAIN_CLASSES =
+      Arrays.asList(
+          // add the classes having main function
+          );
+
+  private static String toString(List<Class<?>> mains) {
+    return mains.stream().map(Class::getName).collect(Collectors.joining(","));
+  }
+
+  private static String usage(List<Class<?>> mains) {
+    return "Usage: [" + toString(mains) + "] [args ...]";
+  }
+
+  static void execute(List<Class<?>> mains, List<String> args) throws Throwable {
+    if (args.size() < 1) {
+      throw new IllegalArgumentException(usage(mains));
+    }
+
+    var className = args.get(0);
+
+    var targetClass =
+        mains.stream()
+            .filter(clz -> clz.getName().contains(className))
+            .findFirst()
+            .orElseThrow(
+                (Supplier<Throwable>)
+                    () ->
+                        new IllegalArgumentException(
+                            "className: " + className + " is not matched to " + toString(mains)));
+
+    var method = targetClass.getDeclaredMethod("main", String[].class);
+    method.invoke(null, (Object) args.subList(1, args.size()).toArray(String[]::new));
   }
 
-  public static void main(String[] args) {
-    System.out.println(new App().getGreeting());
+  public static void main(String[] args) throws Throwable {
+    execute(MAIN_CLASSES, Arrays.asList(args));
   }
 }


Test patch:
diff --git a/app/src/test/java/org/astraea/AppTest.java b/app/src/test/java/org/astraea/AppTest.java
index 987687bc..18df3aab 100644
--- a/app/src/test/java/org/astraea/AppTest.java
+++ b/app/src/test/java/org/astraea/AppTest.java
@@ -1,16 +1,23 @@
-/*
- * This Java source file was generated by the Gradle 'init' task.
- */
 package org.astraea;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import java.util.Arrays;
+import java.util.Collections;
 import org.junit.jupiter.api.Test;
 
 class AppTest {
+  private static String[] ARGS;
+
+  public static void main(String[] args) {
+    ARGS = args;
+  }
+
   @Test
-  void appHasAGreeting() {
-    App classUnderTest = new App();
-    assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
+  void testExecute() throws Throwable {
+    App.execute(Collections.singletonList(AppTest.class), Arrays.asList("AppTest", "1", "2"));
+    assertEquals(2, ARGS.length);
+    assertEquals("1", ARGS[0]);
+    assertEquals("2", ARGS[1]);
   }
 }