From 078c6a1c950eb353656c020595e98a127e7c7196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=8B=87?= Date: Mon, 4 Mar 2024 12:10:26 +0800 Subject: [PATCH] Docean supports JUnit Extension. (#801) * update * Docean supports JUnit Extension. --- .../docean-plugin/docean-plugin-junit/pom.xml | 40 +++++++++++++ .../run/mone/junit/DoceanConfiguration.java | 16 +++++ .../java/run/mone/junit/DoceanExtension.java | 52 +++++++++++++++++ .../java/run/mone/junit/test/ServiceTest.java | 27 +++++++++ .../java/run/mone/junit/test/TestService.java | 16 +++++ jcommon/docean-plugin/pom.xml | 1 + jcommon/pom.xml | 21 +++++-- .../run/mone/struct/test/GraphContext.java | 22 +++++++ .../java/run/mone/struct/test/GraphTest.java | 48 +++++++++++++++ .../java/run/mone/struct/test/VertexData.java | 58 +++++++++++++++++++ 10 files changed, 297 insertions(+), 4 deletions(-) create mode 100644 jcommon/docean-plugin/docean-plugin-junit/pom.xml create mode 100644 jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanConfiguration.java create mode 100644 jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanExtension.java create mode 100644 jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/ServiceTest.java create mode 100644 jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/TestService.java create mode 100644 jcommon/struct/src/test/java/run/mone/struct/test/GraphContext.java create mode 100644 jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java create mode 100644 jcommon/struct/src/test/java/run/mone/struct/test/VertexData.java diff --git a/jcommon/docean-plugin/docean-plugin-junit/pom.xml b/jcommon/docean-plugin/docean-plugin-junit/pom.xml new file mode 100644 index 000000000..01c552f66 --- /dev/null +++ b/jcommon/docean-plugin/docean-plugin-junit/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + run.mone + docean-plugin + 1.5.0-jdk21 + + + docean-plugin-junit + 1.5.0-jdk21-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + + + run.mone + docean + + + + org.junit.jupiter + junit-jupiter-api + 5.10.2 + + + + + + + + \ No newline at end of file diff --git a/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanConfiguration.java b/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanConfiguration.java new file mode 100644 index 000000000..ab3f62d73 --- /dev/null +++ b/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanConfiguration.java @@ -0,0 +1,16 @@ +package run.mone.junit; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author goodjava@qq.com + * @date 2024/3/3 09:19 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) // 应用于类 +public @interface DoceanConfiguration { + String[] basePackage(); +} diff --git a/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanExtension.java b/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanExtension.java new file mode 100644 index 000000000..159b05d4e --- /dev/null +++ b/jcommon/docean-plugin/docean-plugin-junit/src/main/java/run/mone/junit/DoceanExtension.java @@ -0,0 +1,52 @@ +package run.mone.junit; + +import com.xiaomi.youpin.docean.Ioc; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import javax.annotation.Resource; +import java.util.Arrays; + + +/** + * @author goodjava@qq.com + * @date 2024/3/3 09:14 + */ +public class DoceanExtension implements BeforeAllCallback,BeforeEachCallback { + + private Ioc container; + + + @Override + public void beforeAll(ExtensionContext extensionContext) throws Exception { + // 获取测试类 + Class testClass = extensionContext.getRequiredTestClass(); + DoceanConfiguration iocConfig = testClass.getAnnotation(DoceanConfiguration.class); + if (iocConfig == null) { + throw new IllegalStateException("Missing @IocConfiguration on test class " + testClass.getName()); + } + // 从注解中获取包名 + String[] basePackage = iocConfig.basePackage(); + // 初始化IoC容器 + this.container = Ioc.ins().init(basePackage); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + Object testInstance = extensionContext.getRequiredTestInstance(); + Arrays.stream(testInstance.getClass().getDeclaredFields()) + .filter(field -> field.isAnnotationPresent(Resource.class)) + .forEach(field -> { + Object bean = container.getBean(field.getType()); + if (bean != null) { + field.setAccessible(true); + try { + field.set(testInstance, bean); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + }); + } +} diff --git a/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/ServiceTest.java b/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/ServiceTest.java new file mode 100644 index 000000000..ecbaed385 --- /dev/null +++ b/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/ServiceTest.java @@ -0,0 +1,27 @@ +package run.mone.junit.test; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import run.mone.junit.DoceanConfiguration; +import run.mone.junit.DoceanExtension; + +import javax.annotation.Resource; + +/** + * @author goodjava@qq.com + * @date 2024/3/3 17:37 + */ +@ExtendWith(DoceanExtension.class) +@DoceanConfiguration(basePackage = {"run.mone.junit.test"}) +public class ServiceTest { + + @Resource + private TestService ts; + + + @Test + public void test1() { + System.out.println(ts.hi()); + } + +} diff --git a/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/TestService.java b/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/TestService.java new file mode 100644 index 000000000..a25afeead --- /dev/null +++ b/jcommon/docean-plugin/docean-plugin-junit/src/test/java/run/mone/junit/test/TestService.java @@ -0,0 +1,16 @@ +package run.mone.junit.test; + +import com.xiaomi.youpin.docean.anno.Service; + +/** + * @author goodjava@qq.com + * @date 2024/3/3 17:36 + */ +@Service +public class TestService { + + public String hi() { + return "hello"; + } + +} diff --git a/jcommon/docean-plugin/pom.xml b/jcommon/docean-plugin/pom.xml index 979ed3b02..e51c2668e 100644 --- a/jcommon/docean-plugin/pom.xml +++ b/jcommon/docean-plugin/pom.xml @@ -54,6 +54,7 @@ docean-plugin-mesh-dubbo docean-plugin-es-antlr4 docean-plugin-storage + docean-plugin-junit diff --git a/jcommon/pom.xml b/jcommon/pom.xml index dc9e51ef3..5cdfcac09 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -88,14 +88,27 @@ docean-spring-starter + + + + + + + + + + + - ossrh - https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + central + maven-release-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots + snapshots + maven-snapshot-virtual + https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual diff --git a/jcommon/struct/src/test/java/run/mone/struct/test/GraphContext.java b/jcommon/struct/src/test/java/run/mone/struct/test/GraphContext.java new file mode 100644 index 000000000..bbc244177 --- /dev/null +++ b/jcommon/struct/src/test/java/run/mone/struct/test/GraphContext.java @@ -0,0 +1,22 @@ +package run.mone.struct.test; + +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author goodjava@qq.com + * @date 2024/3/1 14:01 + */ +@Data +@Builder +public class GraphContext { + + + @Builder.Default + private Map> input = new HashMap<>(); + + +} diff --git a/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java b/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java new file mode 100644 index 000000000..92fd594ca --- /dev/null +++ b/jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java @@ -0,0 +1,48 @@ +package run.mone.struct.test; + +import com.xiaomi.data.push.graph.Graph; +import com.xiaomi.data.push.graph.Vertex; +import org.junit.Test; + +import java.util.List; + +/** + * @author goodjava@qq.com + * @date 2024/3/1 10:53 + */ +public class GraphTest { + + @Test + public void initializeAndTopologicallySortGraph() { + Graph graph = new Graph<>(5); + graph.addVertex(new Vertex<>(0, VertexData.builder().data("开始").id(0).build())); + graph.addVertex(new Vertex<>(1, VertexData.builder().data("代码").id(1).build())); + graph.addVertex(new Vertex<>(2, VertexData.builder().data("大模型").id(2).build())); + graph.addVertex(new Vertex<>(3, VertexData.builder().data("选择器").id(3).param("abc").build())); + graph.addVertex(new Vertex<>(4, VertexData.builder().data("结束").id(4).build())); + + graph.addEdge(0, 1); + graph.addEdge(1, 3); + graph.addEdge(3, 2); + graph.addEdge(3, 4); + + List list = graph.topologicalSort(); + System.out.println(list); + + GraphContext context = GraphContext.builder().build(); + list.stream().forEach(it -> { + VertexData data = graph.getVertexData(it); + if (!data.isFinish()) { + data.execute(graph); + context.getInput().put(data.getId(),data.getOutput()); + System.out.println(data.getData()); + data.setFinish(true); + } + }); + + System.out.println(context); + + + } + +} diff --git a/jcommon/struct/src/test/java/run/mone/struct/test/VertexData.java b/jcommon/struct/src/test/java/run/mone/struct/test/VertexData.java new file mode 100644 index 000000000..d8797f896 --- /dev/null +++ b/jcommon/struct/src/test/java/run/mone/struct/test/VertexData.java @@ -0,0 +1,58 @@ +package run.mone.struct.test; + +import com.xiaomi.data.push.graph.Graph; +import lombok.Builder; +import lombok.Data; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author goodjava@qq.com + * @date 2024/3/1 10:55 + */ +@Data +@Builder +public class VertexData implements Serializable { + + private String data; + + boolean finish; + + private int id; + + private String param; + + @Builder.Default + private Map input = new HashMap<>(); + + @Builder.Default + private Map output = new HashMap<>(); + + + public void execute(Graph graph) { + if (data.equals("选择器")) { + List[] listArray = graph.getAdj(); + List list = listArray[id]; + + if (param.equals("abc")) { + graph.getVertexData(2).setFinish(true); + } + + System.out.println(list); + + } + + if (data.equals("开始")) { + this.output.put("name", "zzy"); + } + + if (data.equals("代码")) { + this.output.put("name1","aaaa"); + } + } + + +}