Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docean supports JUnit Extension. #801

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions jcommon/docean-plugin/docean-plugin-junit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>docean-plugin</artifactId>
<version>1.5.0-jdk21</version>
</parent>

<artifactId>docean-plugin-junit</artifactId>
<version>1.5.0-jdk21-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>

<dependency>
<groupId>run.mone</groupId>
<artifactId>docean</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
</dependency>



</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @date 2024/3/3 09:19
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) // 应用于类
public @interface DoceanConfiguration {
String[] basePackage();
}
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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);
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package run.mone.junit.test;

import com.xiaomi.youpin.docean.anno.Service;

/**
* @author [email protected]
* @date 2024/3/3 17:36
*/
@Service
public class TestService {

public String hi() {
return "hello";
}

}
1 change: 1 addition & 0 deletions jcommon/docean-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<module>docean-plugin-mesh-dubbo</module>
<module>docean-plugin-es-antlr4</module>
<module>docean-plugin-storage</module>
<module>docean-plugin-junit</module>
</modules>
<dependencies>
<dependency>
Expand Down
21 changes: 17 additions & 4 deletions jcommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,27 @@
<module>docean-spring-starter</module>
</modules>

<!-- <distributionManagement>-->
<!-- <repository>-->
<!-- <id>ossrh</id>-->
<!-- <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>-->
<!-- </repository>-->
<!-- <snapshotRepository>-->
<!-- <id>ossrh</id>-->
<!-- <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>-->
<!-- </snapshotRepository>-->
<!-- </distributionManagement>-->

<distributionManagement>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<id>central</id>
<name>maven-release-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<id>snapshots</id>
<name>maven-snapshot-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual</url>
</snapshotRepository>
</distributionManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.mone.struct.test;

import lombok.Builder;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
* @author [email protected]
* @date 2024/3/1 14:01
*/
@Data
@Builder
public class GraphContext {


@Builder.Default
private Map<Integer, Map<String, String>> input = new HashMap<>();


}
48 changes: 48 additions & 0 deletions jcommon/struct/src/test/java/run/mone/struct/test/GraphTest.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @date 2024/3/1 10:53
*/
public class GraphTest {

@Test
public void initializeAndTopologicallySortGraph() {
Graph<VertexData> 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<Integer> 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);


}

}
58 changes: 58 additions & 0 deletions jcommon/struct/src/test/java/run/mone/struct/test/VertexData.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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<String, String> input = new HashMap<>();

@Builder.Default
private Map<String, String> output = new HashMap<>();


public void execute(Graph<VertexData> graph) {
if (data.equals("选择器")) {
List[] listArray = graph.getAdj();
List<Integer> 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");
}
}


}
Loading