1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-04 15:28:20 +08:00

[v0.3.0] 添加测试项目,并对新内容进行测试

This commit is contained in:
2022-01-26 02:26:30 +08:00
parent e98a3357ab
commit 8a07759b87
23 changed files with 484 additions and 69 deletions
+130
View File
@@ -0,0 +1,130 @@
<?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">
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.3.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<artifactId>easysql-test</artifactId>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easysql-hikaricp</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.29</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<!-- when downloading via Maven we can pull depends individually -->
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptRef>jar-with-dependencies</descriptRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cc.carm.lib.easysql.testrunner.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,36 @@
package cc.carm.lib.easysql.testrunner;
import cc.carm.lib.easysql.api.SQLManager;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
public abstract class EasySQLTest {
@ApiStatus.OverrideOnly
public abstract void onTest(SQLManager sqlManager) throws SQLException;
public boolean executeTest(int index, SQLManager sqlManager) {
String testName = getClass().getSimpleName();
print(" #%s 测试 @%s 开始", index, testName);
long start = System.currentTimeMillis();
try {
onTest(sqlManager);
print(" #%s 测试 @%s 成功,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
return true;
} catch (Exception exception) {
print(" #%s 测试 @%s 失败,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
exception.printStackTrace();
return false;
}
}
protected static void print(@NotNull String format, Object... params) {
Main.print(format, params);
}
}
@@ -0,0 +1,93 @@
package cc.carm.lib.easysql.testrunner;
import cc.carm.lib.easysql.EasySQL;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.testrunner.tests.TableAlterTest;
import cc.carm.lib.easysql.testrunner.tests.TableCreateTest;
import cc.carm.lib.easysql.testrunner.tests.TableRenameTest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
if (args.length < 4) {
print("请提供以下参数: 数据库地址 数据库用户名 数据库密码");
return;
}
SQLManager sqlManager;
try {
print("初始化 SQLManager...");
print("数据库驱动 %s", args[0]);
print("数据库地址 %s", args[1]);
print("数据库用户 %s", args[2]);
print("数据库密码 %s", args[3]);
sqlManager = EasySQL.createManager(args[0], args[1], args[2], args[3]);
sqlManager.setDebugMode(args.length > 4);
print("SQLManager 初始化完成!");
} catch (Exception exception) {
print("SQLManager 初始化失败,请检查数据库配置。");
exception.printStackTrace();
return;
}
print("加载测试类...");
Set<EasySQLTest> tests = new LinkedHashSet<>();
tests.add(new TableCreateTest());
tests.add(new TableAlterTest());
tests.add(new TableRenameTest());
print("准备进行测试...");
int index = 1;
int success = 0;
Iterator<EasySQLTest> testIterator = tests.iterator();
print("准备完成,请按回车键开始执行测试。");
while (testIterator.hasNext()) {
Scanner scanner = new Scanner(System.in);
if (scanner.nextLine() != null) {
EasySQLTest currentTest = testIterator.next();
if (currentTest.executeTest(index, sqlManager)) {
success++;
}
index++;
}
}
print(" ");
print("全部测试执行完毕,成功 %s 个,失败 %s 个。",
success, (tests.size() - success)
);
}
public static void print(@NotNull String format, Object... params) {
System.out.printf((format) + "%n", params);
}
public static @Nullable EasySQLTest cast(@NotNull Class<?> value) {
if (!EasySQLTest.class.isAssignableFrom(value)) return null;
try {
return (EasySQLTest) value.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return null;
}
}
}
@@ -0,0 +1,35 @@
package cc.carm.lib.easysql.testrunner.tests;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.enums.NumberType;
import cc.carm.lib.easysql.testrunner.EasySQLTest;
import java.sql.SQLException;
public class TableAlterTest extends EasySQLTest {
@Override
public void onTest(SQLManager sqlManager) throws SQLException {
print(" 修改 test_user_table");
sqlManager.alterTable("test_user_table")
.addColumn("test", "VARCHAR(16) NOT NULL")
.execute();
sqlManager.alterTable("test_user_table")
.addColumn("test2", "VARCHAR(16) NOT NULL", "username")
.execute();
print(" 修改 test_user_info");
sqlManager.alterTable("test_user_info")
.addAutoIncrementColumn("num", NumberType.BIGINT, false, true)
.execute();
sqlManager.alterTable("test_user_info")
.addColumn("a", "VARCHAR(16) NOT NULL", "")
.execute();
}
}
@@ -0,0 +1,38 @@
package cc.carm.lib.easysql.testrunner.tests;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
import cc.carm.lib.easysql.api.enums.IndexType;
import cc.carm.lib.easysql.testrunner.EasySQLTest;
import java.sql.SQLException;
public class TableCreateTest extends EasySQLTest {
@Override
public void onTest(SQLManager sqlManager) throws SQLException {
print(" 创建 test_user_table");
sqlManager.createTable("test_user_table")
.addAutoIncrementColumn("id")
.addColumn("uuid", "VARCHAR(36) NOT NULL", "用户UUID")
.addColumn("username", "VARCHAR(16) NOT NULL", "用户名")
.addColumn("age", "TINYINT DEFAULT 0 NOT NULL", "年龄")
.setIndex("uuid", IndexType.UNIQUE_KEY)
.build().execute();
print(" 创建 test_user_info");
sqlManager.createTable("test_user_info")
.addColumn("uid", "INT UNSIGNED NOT NULL")
.addColumn("info", "TEXT", "相关信息")
.addForeignKey("uid", "user",
"test_user_table", "id",
ForeignKeyRule.CASCADE, ForeignKeyRule.CASCADE
)
.setIndex(IndexType.FULLTEXT_INDEX, "sign", "info")
.build().execute();
}
}
@@ -0,0 +1,16 @@
package cc.carm.lib.easysql.testrunner.tests;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.testrunner.EasySQLTest;
import java.sql.SQLException;
public class TableRenameTest extends EasySQLTest {
@Override
public void onTest(SQLManager sqlManager) throws SQLException {
print(" 重命名 test_user_table");
sqlManager.alterTable("test_user_table")
.renameTo("test_user_table2")
.execute();
}
}