DB2接口和单元测试
Change-Id: I62312b94f2e5d8ad8bc2f22b96ceecae32de4bf4
diff --git a/src/test/java/databasetest/database2Test.java b/src/test/java/databasetest/database2Test.java
index 196db26..55e09e2 100644
--- a/src/test/java/databasetest/database2Test.java
+++ b/src/test/java/databasetest/database2Test.java
@@ -1,864 +1,1078 @@
-// package databasetest;
+package databasetest;
-// import database.Database2;
-// import entity.*;
-// import org.junit.jupiter.api.*;
+import database.Database2;
+import entity.*;
+import org.junit.jupiter.api.*;
-// import javax.persistence.*;
-// import java.util.*;
-// import java.util.stream.Collectors;
+import javax.persistence.*;
+import java.util.*;
-// public class database2Test {
-// private static EntityManagerFactory emf;
-// private static EntityManager em;
-// private static Database2 db;
+public class database2Test {
+ private static EntityManagerFactory emf;
+ private static EntityManager em;
+ private static Database2 db;
-// @BeforeAll
-// static void setup() throws Exception {
-// // 强制加载 MySQL 驱动
-// Class.forName("com.mysql.cj.jdbc.Driver");
-// entity.config cfg = new entity.config();
-// Map<String, Object> props = new HashMap<>();
-// String jdbcUrl = String.format(
-// "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
-// cfg.SqlURL, cfg.TestDatabase);
-// props.put("javax.persistence.jdbc.url", jdbcUrl);
-// props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
-// props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
-// props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
-// emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
-// em = emf.createEntityManager();
-// // 修改此处:传入配置好的EntityManagerFactory给Database2
-// db = new Database2(emf);
-// }
+ @BeforeAll
+ static void setup() throws Exception {
+ // 强制加载 MySQL 驱动
+ Class.forName("com.mysql.cj.jdbc.Driver");
+ entity.config cfg = new entity.config();
+ Map<String, Object> props = new HashMap<>();
+ String jdbcUrl = String.format(
+ "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
+ cfg.SqlURL, cfg.TestDatabase);
+ props.put("javax.persistence.jdbc.url", jdbcUrl);
+ props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
+ props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
+ props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
+ emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
+ em = emf.createEntityManager();
+ db = new Database2(emf);
+ }
-// @AfterAll
-// static void teardown() {
-// if (em != null && em.isOpen())
-// em.close();
-// if (emf != null && emf.isOpen())
-// emf.close();
-// }
+ @AfterAll
+ static void teardown() {
+ if (em != null && em.isOpen())
+ em.close();
+ if (emf != null && emf.isOpen())
+ emf.close();
+ }
-// @TestFactory
-// Collection<DynamicTest> testAddBegSeed() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ @TestFactory
+ Collection<DynamicTest> testAddBegSeed() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
+ String uid = userIds.get(0);
-// String begId = "test_beg_" + UUID.randomUUID().toString();
+ return List.of(
+ DynamicTest.dynamicTest("AddBegSeed success", () -> {
+ String begId = "test_beg_" + System.currentTimeMillis();
+ BegInfo beg = new BegInfo();
+ beg.begid = begId;
+ beg.begnumbers = 1;
+ beg.magic = 100;
+ beg.endtime = new Date();
+ beg.hasseed = 0;
+ try {
+ int ret = db.AddBegSeed(beg);
+ Assertions.assertEquals(0, ret, "AddBegSeed应返回0");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ BegInfo toDelete = em.find(BegInfo.class, begId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// beg.endtime = new Date();
-// beg.hasseed = false;
+ DynamicTest.dynamicTest("AddBegSeed duplicate", () -> {
+ String begId = "test_beg_" + (System.currentTimeMillis() + 1);
+ BegInfo beg = new BegInfo();
+ beg.begid = begId;
+ beg.begnumbers = 1;
+ beg.magic = 100;
+ beg.endtime = new Date();
+ beg.hasseed = 0;
+ try {
+ db.AddBegSeed(beg);
+ int ret = db.AddBegSeed(beg);
+ Assertions.assertEquals(1, ret, "重复插入应返回1");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ BegInfo toDelete = em.find(BegInfo.class, begId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("AddBegSeed success", () -> {
-// int ret = db.AddBegSeed(beg);
-// Assertions.assertEquals(0, ret, "AddBegSeed应返回0");
-// BegInfo inserted = em.find(BegInfo.class, begId);
-// Assertions.assertNotNull(inserted, "求种信息应已插入");
-// Assertions.assertFalse(inserted.hasseed, "新求种默认未完成");
-// }),
+ DynamicTest.dynamicTest("AddBegSeed invalid param", () -> {
+ Assertions.assertEquals(2, db.AddBegSeed(null));
+ BegInfo invalidBeg = new BegInfo();
+ invalidBeg.begid = "";
+ Assertions.assertEquals(2, db.AddBegSeed(invalidBeg));
+ }));
+ }
-// DynamicTest.dynamicTest("AddBegSeed duplicate", () -> {
-// int ret = db.AddBegSeed(beg);
-// Assertions.assertEquals(1, ret, "重复插入应返回1");
-// }),
+ @TestFactory
+ Collection<DynamicTest> testUpdateBegSeed() {
+ List<BegInfo> existingBegs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingBegs.isEmpty()) {
+ return Collections.emptyList();
+ }
-// DynamicTest.dynamicTest("AddBegSeed invalid param", () -> {
-// Assertions.assertEquals(2, db.AddBegSeed(null));
-// BegInfo invalidBeg = new BegInfo();
-// invalidBeg.begid = "";
-// Assertions.assertEquals(2, db.AddBegSeed(invalidBeg));
-// }));
-// }
+ BegInfo originalBeg = existingBegs.get(0);
+ String begId = originalBeg.begid;
+ int originalMagic = originalBeg.magic;
+ int originalBegNumbers = originalBeg.begnumbers;
+ int originalHasSeed = originalBeg.hasseed;
+ Date originalEndTime = originalBeg.endtime;
-// @TestFactory
-// Collection<DynamicTest> testUpdateBegSeed() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ return List.of(
+ DynamicTest.dynamicTest("UpdateBegSeed success", () -> {
+ try {
+ BegInfo update = new BegInfo();
+ update.begid = begId;
+ update.begnumbers = originalBegNumbers + 1;
+ update.magic = originalMagic + 100;
+ update.endtime = originalEndTime;
+ update.hasseed = originalHasSeed;
-// String begId = "test_beg_update_" + UUID.randomUUID().toString();
+ int ret = db.UpdateBegSeed(update);
+ Assertions.assertEquals(0, ret, "UpdateBegSeed应返回0");
-// // 先插入一条求种记录
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// beg.endtime = new Date();
-// db.AddBegSeed(beg);
+ em.clear();
+ BegInfo updated = em.find(BegInfo.class, begId);
+ Assertions.assertEquals(originalMagic + 100, updated.magic, "魔力值应已更新");
+ Assertions.assertEquals(originalBegNumbers + 1, updated.begnumbers, "求种人数应已更新");
+ } finally {
+ // 恢复原始数据
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ BegInfo toRestore = em.find(BegInfo.class, begId);
+ if (toRestore != null) {
+ toRestore.magic = originalMagic;
+ toRestore.begnumbers = originalBegNumbers;
+ toRestore.hasseed = originalHasSeed;
+ toRestore.endtime = originalEndTime;
+ em.merge(toRestore);
+ }
+ tx.commit();
+ }
+ }),
+ DynamicTest.dynamicTest("UpdateBegSeed not exist", () -> {
+ BegInfo notExist = new BegInfo();
+ notExist.begid = "not_exist_beg";
+ notExist.begnumbers = 1;
+ notExist.magic = 100;
+ notExist.endtime = new Date();
+ notExist.hasseed = 0;
-// return List.of(
-// DynamicTest.dynamicTest("UpdateBegSeed success", () -> {
-// BegInfo update = new BegInfo();
-// update.begid = begId;
-// update.begnumbers = 2;
-// update.magic = 200;
+ int ret = db.UpdateBegSeed(notExist);
+ Assertions.assertEquals(1, ret, "不存在的求种应返回1");
+ }),
+ DynamicTest.dynamicTest("UpdateBegSeed invalid param", () -> {
+ Assertions.assertEquals(2, db.UpdateBegSeed(null));
+ BegInfo invalid = new BegInfo();
+ invalid.begid = "";
+ Assertions.assertEquals(2, db.UpdateBegSeed(invalid));
+ }));
+ }
-// int ret = db.UpdateBegSeed(update);
-// Assertions.assertEquals(0, ret, "UpdateBegSeed应返回0");
+ @TestFactory
+ Collection<DynamicTest> testDeleteBegSeed() {
+ List<BegInfo> existingBegs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingBegs.isEmpty()) {
+ return Collections.emptyList();
+ }
-// BegInfo updated = em.find(BegInfo.class, begId);
-// Assertions.assertEquals(200, updated.magic, "魔力值应已更新");
-// Assertions.assertEquals(2, updated.begnumbers, "求种人数应已更新");
-// }),
+ BegInfo begToDelete = existingBegs.get(0);
+ String begId = begToDelete.begid;
-// DynamicTest.dynamicTest("UpdateBegSeed not exist", () -> {
-// BegInfo notExist = new BegInfo();
-// notExist.begid = "not_exist_beg";
-// notExist.begnumbers = 1;
-// int ret = db.UpdateBegSeed(notExist);
-// Assertions.assertEquals(1, ret, "不存在的求种应返回1");
-// }),
+ return List.of(
+ DynamicTest.dynamicTest("DeleteBegSeed success", () -> {
+ EntityTransaction tx = em.getTransaction();
+ try {
+ tx.begin();
+ int ret = db.DeleteBegSeed(begId);
+ Assertions.assertEquals(0, ret, "DeleteBegSeed 应返回0");
+ } finally {
+ tx.rollback();
+ }
+ }),
-// DynamicTest.dynamicTest("UpdateBegSeed invalid param", () -> {
-// Assertions.assertEquals(2, db.UpdateBegSeed(null));
-// BegInfo invalid = new BegInfo();
-// invalid.begid = "";
-// Assertions.assertEquals(2, db.UpdateBegSeed(invalid));
-// }));
-// }
+ DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> {
+ int ret = db.DeleteBegSeed("not_exist_beg");
+ Assertions.assertEquals(1, ret, "不存在的求种任务应返回1");
+ }),
-// @TestFactory
-// Collection<DynamicTest> testDeleteBegSeed() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> {
+ Assertions.assertEquals(2, db.DeleteBegSeed(null));
+ Assertions.assertEquals(2, db.DeleteBegSeed(""));
+ }));
+ }
-// String begId = "test_beg_delete_" + UUID.randomUUID().toString();
+ @TestFactory
+ Collection<DynamicTest> testVoteSeed() {
+ // 获取现有用户ID
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
+ String uid = userIds.get(0);
-// // 先插入一条求种记录
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// beg.endtime = new Date();
-// beg.hasseed = false;
-// db.AddBegSeed(beg);
+ // 获取现有的求种信息
+ List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b", BegInfo.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (begs.isEmpty())
+ return Collections.emptyList();
+ String begId = begs.get(0).begid;
-// return List.of(
-// DynamicTest.dynamicTest("DeleteBegSeed success", () -> {
-// int ret = db.DeleteBegSeed(begId);
-// Assertions.assertEquals(0, ret, "DeleteBegSeed应返回0");
-// BegInfo deleted = em.find(BegInfo.class, begId);
-// Assertions.assertNull(deleted, "求种信息应已删除");
-// }),
+ // 获取现有的种子信息
+ List<Seed> seeds = em.createQuery("SELECT s FROM Seed s", Seed.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (seeds.isEmpty())
+ return Collections.emptyList();
+ String seedId = seeds.get(0).seedid;
-// DynamicTest.dynamicTest("DeleteBegSeed not exist", () -> {
-// int ret = db.DeleteBegSeed("not_exist_beg");
-// Assertions.assertEquals(1, ret, "不存在的求种应返回1");
-// }),
+ try {
+ return List.of(
+ DynamicTest.dynamicTest("VoteSeed success", () -> {
+ int ret = db.VoteSeed(begId, seedId, uid);
+ Assertions.assertEquals(0, ret, "VoteSeed应返回0");
+ }),
-// DynamicTest.dynamicTest("DeleteBegSeed invalid param", () -> {
-// Assertions.assertEquals(2, db.DeleteBegSeed(null));
-// Assertions.assertEquals(2, db.DeleteBegSeed(""));
-// }));
-// }
+ DynamicTest.dynamicTest("VoteSeed duplicate", () -> {
+ int ret = db.VoteSeed(begId, seedId, uid);
+ Assertions.assertEquals(1, ret, "重复投票应返回1");
+ }),
-// @TestFactory
-// Collection<DynamicTest> testVoteSeed() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ DynamicTest.dynamicTest("VoteSeed invalid param", () -> {
+ Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid));
+ Assertions.assertEquals(2, db.VoteSeed(begId, null, uid));
+ Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null));
+ }));
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ try {
+ em.createQuery(
+ "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
+ .setParameter("begid", begId)
+ .setParameter("seedid", seedId)
+ .setParameter("uid", uid)
+ .executeUpdate();
+ } catch (Exception ignored) {
+ }
+ tx.commit();
+ }
+ }
-// String uid = userIds.get(0);
-// String begId = "test_beg_vote_" + UUID.randomUUID().toString();
-// String seedId = "test_seed_" + UUID.randomUUID().toString();
+ @TestFactory
+ Collection<DynamicTest> testSubmitSeed() {
+ // 获取现有的求种信息
+ List<BegInfo> begs = em.createQuery("SELECT b FROM BegInfo b WHERE b.hasseed = 0", BegInfo.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (begs.isEmpty())
+ return Collections.emptyList();
+ String begId = begs.get(0).begid;
-// // 插入求种记录
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// beg.endtime = new Date();
-// beg.hasseed = false;
-// db.AddBegSeed(beg);
+ // 获取现有的可用种子信息
+ List<Seed> existingSeeds = em.createQuery(
+ "SELECT s FROM Seed s WHERE s.seedid NOT IN " +
+ "(SELECT ss.seed.seedid FROM SubmitSeed ss)",
+ Seed.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingSeeds.isEmpty())
+ return Collections.emptyList();
+ Seed seed = existingSeeds.get(0);
-// // 插入种子记录
-// Seed seed = new Seed();
-// seed.seedid = seedId;
-// seed.seeduserid = uid;
-// seed.title = "测试种子";
-// em.getTransaction().begin();
-// em.persist(seed);
-// em.getTransaction().commit();
+ try {
+ return List.of(
+ DynamicTest.dynamicTest("SubmitSeed success", () -> {
+ int ret = db.SubmitSeed(begId, seed);
+ Assertions.assertEquals(0, ret, "SubmitSeed应返回0");
+ }),
-// // 提交种子
-// db.SubmitSeed(begId, seed);
+ DynamicTest.dynamicTest("SubmitSeed duplicate", () -> {
+ int ret = db.SubmitSeed(begId, seed);
+ Assertions.assertEquals(1, ret, "重复提交应返回1");
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("VoteSeed success", () -> {
-// int ret = db.VoteSeed(begId, seedId, uid);
-// Assertions.assertEquals(0, ret, "VoteSeed应返回0");
-// }),
+ DynamicTest.dynamicTest("SubmitSeed invalid param", () -> {
+ Assertions.assertEquals(2, db.SubmitSeed(null, seed));
+ Assertions.assertEquals(2, db.SubmitSeed(begId, null));
+ }));
+ } finally {
+ // 清理测试数据
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ try {
+ // 恢复求种状态
+ BegInfo toRestore = em.find(BegInfo.class, begId);
+ if (toRestore != null) {
+ toRestore.hasseed = 0;
+ em.merge(toRestore);
+ }
-// DynamicTest.dynamicTest("VoteSeed duplicate", () -> {
-// int ret = db.VoteSeed(begId, seedId, uid);
-// Assertions.assertEquals(1, ret, "重复投票应返回1");
-// }),
+ // 清理投票记录
+ em.createQuery("DELETE FROM Vote v WHERE v.begid = :begid AND v.seedid = :seedid")
+ .setParameter("begid", begId)
+ .setParameter("seedid", seed.seedid)
+ .executeUpdate();
+ } catch (Exception ignored) {
+ }
+ tx.commit();
+ }
+ }
-// DynamicTest.dynamicTest("VoteSeed invalid param", () -> {
-// Assertions.assertEquals(2, db.VoteSeed(null, seedId, uid));
-// Assertions.assertEquals(2, db.VoteSeed(begId, null, uid));
-// Assertions.assertEquals(2, db.VoteSeed(begId, seedId, null));
-// }));
-// }
+ @Test
+ void testSettleBeg() {
+ String uid = em.createQuery("select u.userid from User u", String.class)
+ .setMaxResults(1)
+ .getSingleResult();
-// @TestFactory
-// Collection<DynamicTest> testSubmitSeed() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ String begId = "test_beg_settle_" + System.currentTimeMillis();
+ String seedId = "test_seed_settle_" + System.currentTimeMillis();
-// String uid = userIds.get(0);
-// String begId = "test_beg_submit_" + UUID.randomUUID().toString();
-// String seedId = "test_seed_submit_" + UUID.randomUUID().toString();
+ BegInfo beg = new BegInfo();
+ beg.begid = begId;
+ beg.begnumbers = 1;
+ beg.magic = 100;
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前
+ beg.endtime = cal.getTime();
+ beg.hasseed = 0;
-// // 插入求种记录
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// beg.endtime = new Date();
-// beg.hasseed = false;
-// db.AddBegSeed(beg);
+ Seed seed = new Seed();
+ seed.seedid = seedId;
+ seed.seeduserid = uid;
+ seed.title = "测试种子";
+ seed.seedsize = "1";
-// Seed seed = new Seed();
-// seed.seedid = seedId;
-// seed.seeduserid = uid;
-// seed.title = "测试种子";
+ try {
+ db.AddBegSeed(beg);
+ db.SubmitSeed(begId, seed);
+ db.VoteSeed(begId, seedId, uid);
-// return List.of(
-// DynamicTest.dynamicTest("SubmitSeed success", () -> {
-// int ret = db.SubmitSeed(begId, seed);
-// Assertions.assertEquals(0, ret, "SubmitSeed应返回0");
-// }),
+ db.SettleBeg();
+ em.clear();
-// DynamicTest.dynamicTest("SubmitSeed duplicate", () -> {
-// int ret = db.SubmitSeed(begId, seed);
-// Assertions.assertEquals(1, ret, "重复提交应返回1");
-// }),
+ BegInfo settled = em.find(BegInfo.class, begId);
+ Assertions.assertEquals(settled.hasseed, 1, "求种应已完成");
-// DynamicTest.dynamicTest("SubmitSeed invalid param", () -> {
-// Assertions.assertEquals(2, db.SubmitSeed(null, seed));
-// Assertions.assertEquals(2, db.SubmitSeed(begId, null));
-// }));
-// }
+ UserPT userPT = em.find(UserPT.class, uid);
+ Assertions.assertTrue(userPT.magic >= 0, "用户应获得魔力值奖励");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ try {
+ // 删除投票记录
+ em.createQuery(
+ "DELETE FROM UserVotes v WHERE v.begid = :begid AND v.seedid = :seedid AND v.userid = :uid")
+ .setParameter("begid", begId)
+ .setParameter("seedid", seedId)
+ .setParameter("uid", uid)
+ .executeUpdate();
+ // 删除提交记录
+ em.createQuery("DELETE FROM SubmitSeed s WHERE s.begid = :begid AND s.seedid = :seedid")
+ .setParameter("begid", begId)
+ .setParameter("seedid", seedId)
+ .executeUpdate();
+ } catch (Exception ignored) {
+ }
+ // 删除种子和求种任务
+ Seed toDeleteSeed = em.find(Seed.class, seedId);
+ if (toDeleteSeed != null)
+ em.remove(toDeleteSeed);
+ BegInfo toDeleteBeg = em.find(BegInfo.class, begId);
+ if (toDeleteBeg != null)
+ em.remove(toDeleteBeg);
+ tx.commit();
+ }
+ }
-// @Test
-// void testSettleBeg() {
-// // 准备测试数据
-// String uid = em.createQuery("select u.userid from User u", String.class)
-// .setMaxResults(1)
-// .getSingleResult();
+ @TestFactory
+ Collection<DynamicTest> testAddPost() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
+ String uid = userIds.get(0);
+ String postid = "test_post_" + System.currentTimeMillis();
+ Post post = new Post();
+ post.postid = postid;
+ post.postuserid = uid;
+ post.posttitle = "单元测试帖子";
+ post.postcontent = "内容";
+ post.posttime = new Date();
-// String begId = "test_beg_settle_" + UUID.randomUUID().toString();
-// String seedId = "test_seed_settle_" + UUID.randomUUID().toString();
+ return List.of(
+ DynamicTest.dynamicTest("AddPost success", () -> {
+ try {
+ int ret = db.AddPost(post);
+ Assertions.assertEquals(0, ret, "AddPost 应返回0");
+ Post inserted = em.find(Post.class, postid);
+ Assertions.assertNotNull(inserted, "帖子应已插入");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ Post toDelete = em.find(Post.class, postid);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
+ DynamicTest.dynamicTest("AddPost duplicate", () -> {
+ try {
+ db.AddPost(post);
+ int ret = db.AddPost(post);
+ Assertions.assertEquals(1, ret, "重复插入应返回1");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ Post toDelete = em.find(Post.class, postid);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
+ DynamicTest.dynamicTest("AddPost user not exist", () -> {
+ Post p2 = new Post();
+ p2.postid = "test_post_" + (System.currentTimeMillis() + 1);
+ p2.postuserid = "not_exist_user";
+ p2.posttitle = "无效用户";
+ p2.postcontent = "内容";
+ p2.posttime = new Date();
+ int ret = db.AddPost(p2);
+ Assertions.assertEquals(2, ret, "用户不存在应返回2");
+ }),
+ DynamicTest.dynamicTest("AddPost invalid param", () -> {
+ Assertions.assertEquals(2, db.AddPost(null));
+ Post p3 = new Post();
+ p3.postid = null;
+ p3.postuserid = uid;
+ Assertions.assertEquals(2, db.AddPost(p3));
+ Post p4 = new Post();
+ p4.postid = "";
+ p4.postuserid = uid;
+ Assertions.assertEquals(2, db.AddPost(p4));
+ }));
+ }
-// // 创建求种记录(已过期)
-// BegInfo beg = new BegInfo();
-// beg.begid = begId;
-// beg.begnumbers = 1;
-// beg.magic = 100;
-// Calendar cal = Calendar.getInstance();
-// cal.add(Calendar.DAY_OF_MONTH, -15); // 15天前
-// beg.endtime = cal.getTime();
-// beg.hasseed = false;
-// db.AddBegSeed(beg);
+ @TestFactory
+ Collection<DynamicTest> testUpdatePost() {
+ List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingPosts.isEmpty()) {
+ return Collections.emptyList();
+ }
-// // 创建种子并提交
-// Seed seed = new Seed();
-// seed.seedid = seedId;
-// seed.seeduserid = uid;
-// seed.title = "测试种子";
-// db.SubmitSeed(begId, seed);
+ Post originalPost = existingPosts.get(0);
+ String postId = originalPost.postid;
+ String originalTitle = originalPost.posttitle;
+ String originalContent = originalPost.postcontent;
+ String originalUserId = originalPost.postuserid;
+ Date originalTime = originalPost.posttime;
-// // 投票
-// db.VoteSeed(begId, seedId, uid);
+ return List.of(
+ DynamicTest.dynamicTest("UpdatePost success", () -> {
+ try {
+ Post update = new Post();
+ update.postid = postId;
+ update.postuserid = originalUserId;
+ update.posttitle = originalTitle + "_updated";
+ update.postcontent = originalContent + "_updated";
+ update.posttime = originalTime;
-// // 执行结算
-// db.SettleBeg();
+ int ret = db.UpdatePost(update);
+ Assertions.assertEquals(0, ret, "UpdatePost应返回0");
-// // 验证结算结果
-// BegInfo settled = em.find(BegInfo.class, begId);
-// Assertions.assertTrue(settled.hasseed, "求种应已完成");
+ em.clear();
+ Post updated = em.find(Post.class, postId);
+ Assertions.assertEquals(originalTitle + "_updated", updated.posttitle, "标题应已更新");
+ Assertions.assertEquals(originalContent + "_updated", updated.postcontent, "内容应已更新");
+ } finally {
+ // 恢复原始数据
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ Post toRestore = em.find(Post.class, postId);
+ if (toRestore != null) {
+ toRestore.posttitle = originalTitle;
+ toRestore.postcontent = originalContent;
+ toRestore.posttime = originalTime;
+ em.merge(toRestore);
+ }
+ tx.commit();
+ }
+ }),
+ DynamicTest.dynamicTest("UpdatePost not exist", () -> {
+ Post notExist = new Post();
+ notExist.postid = "not_exist_post";
+ notExist.postuserid = originalUserId;
+ notExist.posttitle = "不存在的帖子";
+ notExist.postcontent = "测试内容";
+ notExist.posttime = new Date();
-// UserPT userPT = em.find(UserPT.class, uid);
-// Assertions.assertTrue(userPT.magic >= 100, "用户应获得魔力值奖励");
-// }
+ int ret = db.UpdatePost(notExist);
+ Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
+ }),
+ DynamicTest.dynamicTest("UpdatePost invalid param", () -> {
+ Assertions.assertEquals(2, db.UpdatePost(null));
+ Post invalid = new Post();
+ invalid.postid = "";
+ Assertions.assertEquals(2, db.UpdatePost(invalid));
-// @TestFactory
-// Collection<DynamicTest> testAddPost() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
-// String uid = userIds.get(0);
-// String postid = "test_post_" + UUID.randomUUID();
-// Post post = new Post();
-// post.postid = postid;
-// post.postuserid = uid;
-// post.posttitle = "单元测试帖子";
-// post.postcontent = "内容";
-// post.posttime = new Date();
+ Post invalid2 = new Post();
+ invalid2.postid = null;
+ Assertions.assertEquals(2, db.UpdatePost(invalid2));
+ }));
+ }
-// return List.of(
-// DynamicTest.dynamicTest("AddPost success", () -> {
-// int ret = db.AddPost(post);
-// Assertions.assertEquals(0, ret, "AddPost 应返回0");
-// Post inserted = em.find(Post.class, postid);
-// Assertions.assertNotNull(inserted, "帖子应已插入");
-// }),
-// DynamicTest.dynamicTest("AddPost duplicate", () -> {
-// int ret = db.AddPost(post);
-// Assertions.assertEquals(1, ret, "重复插入应返回1");
-// }),
-// DynamicTest.dynamicTest("AddPost user not exist", () -> {
-// Post p2 = new Post();
-// p2.postid = "test_post_" + UUID.randomUUID();
-// p2.postuserid = "not_exist_user";
-// p2.posttitle = "无效用户";
-// p2.postcontent = "内容";
-// p2.posttime = new Date();
-// int ret = db.AddPost(p2);
-// Assertions.assertEquals(2, ret, "用户不存在应返回2");
-// }),
-// DynamicTest.dynamicTest("AddPost invalid param", () -> {
-// Assertions.assertEquals(2, db.AddPost(null));
-// Post p3 = new Post();
-// p3.postid = null;
-// p3.postuserid = uid;
-// Assertions.assertEquals(2, db.AddPost(p3));
-// Post p4 = new Post();
-// p4.postid = "";
-// p4.postuserid = uid;
-// Assertions.assertEquals(2, db.AddPost(p4));
-// }));
-// }
+ @TestFactory
+ Collection<DynamicTest> testDeletePost() {
+ List<Post> existingPosts = em.createQuery("SELECT p FROM Post p", Post.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingPosts.isEmpty()) {
+ return Collections.emptyList();
+ }
-// @TestFactory
-// Collection<DynamicTest> testUpdatePost() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
-// String uid = userIds.get(0);
-// String postid = "test_post_update_" + UUID.randomUUID();
-// Post post = new Post();
-// post.postid = postid;
-// post.postuserid = uid;
-// post.posttitle = "原始标题";
-// post.postcontent = "原始内容";
-// post.posttime = new Date();
-// db.AddPost(post);
+ Post postToDelete = existingPosts.get(0);
+ String postId = postToDelete.postid;
-// return List.of(
-// DynamicTest.dynamicTest("UpdatePost success", () -> {
-// Post update = new Post();
-// update.postid = postid;
-// update.postuserid = uid; // 实际实现会忽略此字段
-// update.posttitle = "新标题";
-// update.postcontent = "新内容";
-// update.posttime = post.posttime;
-// int ret = db.UpdatePost(update);
-// Assertions.assertEquals(0, ret, "UpdatePost 应返回0");
-// Post updated = em.find(Post.class, postid);
-// Assertions.assertEquals("新标题", updated.posttitle);
-// Assertions.assertEquals("新内容", updated.postcontent);
-// }),
-// DynamicTest.dynamicTest("UpdatePost not exist", () -> {
-// Post notExist = new Post();
-// notExist.postid = "not_exist_post";
-// notExist.postuserid = uid;
-// notExist.posttitle = "xxx";
-// notExist.postcontent = "xxx";
-// int ret = db.UpdatePost(notExist);
-// Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
-// }),
-// DynamicTest.dynamicTest("UpdatePost invalid param", () -> {
-// Assertions.assertEquals(2, db.UpdatePost(null));
-// Post p2 = new Post();
-// p2.postid = null;
-// Assertions.assertEquals(2, db.UpdatePost(p2));
-// Post p3 = new Post();
-// p3.postid = "";
-// Assertions.assertEquals(2, db.UpdatePost(p3));
-// }));
-// }
+ return List.of(
+ DynamicTest.dynamicTest("DeletePost success", () -> {
+ EntityTransaction tx = em.getTransaction();
+ try {
+ tx.begin();
+ int ret = db.DeletePost(postId);
+ Assertions.assertEquals(0, ret, "DeletePost 应返回0");
+ } finally {
+ tx.rollback();
+ }
+ }),
-// @TestFactory
-// Collection<DynamicTest> testDeletePost() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
-// String uid = userIds.get(0);
-// String postid = "test_post_delete_" + UUID.randomUUID();
-// Post post = new Post();
-// post.postid = postid;
-// post.postuserid = uid;
-// post.posttitle = "待删除帖子";
-// post.postcontent = "内容";
-// post.posttime = new Date();
-// db.AddPost(post);
+ DynamicTest.dynamicTest("DeletePost not exist", () -> {
+ int ret = db.DeletePost("not_exist_post");
+ Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("DeletePost success", () -> {
-// int ret = db.DeletePost(postid);
-// Assertions.assertEquals(0, ret, "DeletePost 应返回0");
-// Post deleted = em.find(Post.class, postid);
-// Assertions.assertNull(deleted, "帖子应已删除");
-// }),
-// DynamicTest.dynamicTest("DeletePost not exist", () -> {
-// int ret = db.DeletePost("not_exist_post");
-// Assertions.assertEquals(1, ret, "不存在的帖子应返回1");
-// }),
-// DynamicTest.dynamicTest("DeletePost invalid param", () -> {
-// Assertions.assertEquals(2, db.DeletePost(null));
-// Assertions.assertEquals(2, db.DeletePost(""));
-// }));
-// }
-
-// @TestFactory
-// Collection<DynamicTest> testAddComment() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// List<String> postIds = em.createQuery("select p.postid from Post p",
-// String.class).getResultList();
-// if (userIds.isEmpty() || postIds.isEmpty()) {
-// return Collections.emptyList();
-// }
-// String comment = "单元测试评论";
-// return userIds.stream().flatMap(uid -> postIds.stream()
-// .map(pid -> DynamicTest.dynamicTest("AddComment for user=" + uid + ", post="
-// + pid, () -> {
-// int ret = db.AddComment(pid, uid, comment);
-// Assertions.assertEquals(0, ret, "AddComment 应返回0");
-// // 验证评论已插入
-// List<PostReply> replies = em.createQuery(
-// "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND
-// r.content = :c",
-// PostReply.class)
-// .setParameter("pid", pid)
-// .setParameter("uid", uid)
-// .setParameter("c", comment)
-// .getResultList();
-// Assertions.assertFalse(replies.isEmpty(), "评论应已插入");
-// }))).collect(Collectors.toList());
-// }
+ DynamicTest.dynamicTest("DeletePost invalid param", () -> {
+ Assertions.assertEquals(2, db.DeletePost(null));
+ Assertions.assertEquals(2, db.DeletePost(""));
+ }));
+ }
-// @TestFactory
-// Collection<DynamicTest> testDeleteComment() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// List<String> postIds = em.createQuery("select p.postid from Post p",
-// String.class).getResultList();
-// if (userIds.isEmpty() || postIds.isEmpty()) {
-// return Collections.emptyList();
-// }
-// String comment = "待删除评论";
-// List<DynamicTest> tests = new ArrayList<>();
-// for (String uid : userIds) {
-// for (String pid : postIds) {
-// // 插入评论
-// db.AddComment(pid, uid, comment);
-// // 查询评论id
-// List<PostReply> replies = em.createQuery(
-// "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND
-// r.content = :c",
-// PostReply.class)
-// .setParameter("pid", pid)
-// .setParameter("uid", uid)
-// .setParameter("c", comment)
-// .getResultList();
-// if (replies.isEmpty())
-// continue;
-// PostReply reply = replies.get(0);
-// String replyid = reply.replyid;
+ @TestFactory
+ Collection<DynamicTest> testAddComment() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
+ if (userIds.isEmpty() || postIds.isEmpty()) {
+ return Collections.emptyList();
+ }
+ String uid = userIds.get(0);
+ String pid = postIds.get(0);
+ String comment = "单元测试评论";
+ return List.of(
+ DynamicTest.dynamicTest("AddComment", () -> {
+ String replyid = null;
+ try {
+ int ret = db.AddComment(pid, uid, comment);
+ Assertions.assertEquals(0, ret, "AddComment 应返回0");
+ List<PostReply> replies = em.createQuery(
+ "SELECT r FROM PostReply r WHERE r.postid = :pid AND r.authorid = :uid AND r.content = :c",
+ PostReply.class)
+ .setParameter("pid", pid)
+ .setParameter("uid", uid)
+ .setParameter("c", comment)
+ .getResultList();
+ Assertions.assertFalse(replies.isEmpty(), "评论应已插入");
+ replyid = replies.get(0).replyid;
+ } finally {
+ if (replyid != null) {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ PostReply toDelete = em.find(PostReply.class, replyid);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }
+ }));
+ }
-// tests.add(DynamicTest.dynamicTest("DeleteComment for post=" + pid + ",
-// replyid=" + replyid, () -> {
-// int ret = db.DeleteComment(pid, replyid);
-// Assertions.assertEquals(0, ret, "DeleteComment 应返回0");
-// PostReply deleted = em.find(PostReply.class, replyid);
-// Assertions.assertNull(deleted, "评论应已删除");
-// }));
-// }
-// }
-// return tests;
-// }
+ @TestFactory
+ Collection<DynamicTest> testDeleteComment() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ List<String> postIds = em.createQuery("select p.postid from Post p", String.class).getResultList();
+ if (userIds.isEmpty() || postIds.isEmpty()) {
+ return Collections.emptyList();
+ }
+ String uid = userIds.get(0);
+ String pid = postIds.get(0);
+ String comment = "待删除评论";
-// @TestFactory
-// Collection<DynamicTest> testExchangeMagicToUpload() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ return List.of(
+ DynamicTest.dynamicTest("DeleteComment", () -> {
+ String replyid = null;
+ try {
+ // 先确保评论存在
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ PostReply reply = new PostReply();
+ reply.replyid = "reply_" + System.currentTimeMillis();
+ reply.postid = pid;
+ reply.authorid = uid;
+ reply.content = comment;
+ reply.createdAt = new Date();
+ em.persist(reply);
+ tx.commit();
+ replyid = reply.replyid;
-// String uid = userIds.get(0);
-// final UserPT testUserPT;
+ // 执行删除测试
+ int ret = db.DeleteComment(pid, replyid);
+ Assertions.assertEquals(0, ret, "DeleteComment 应返回0");
-// // 确保用户有足够的魔力值用于测试
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// UserPT tempUserPT = em.find(UserPT.class, uid);
-// if (tempUserPT == null) {
-// tempUserPT = new UserPT();
-// tempUserPT.userid = uid;
-// tempUserPT.magic = 1000;
-// tempUserPT.upload = 1000;
-// tempUserPT.download = 1000;
-// em.persist(tempUserPT);
-// } else {
-// tempUserPT.magic = 1000;
-// em.merge(tempUserPT);
-// }
-// testUserPT = tempUserPT;
-// tx.commit();
+ em.clear();
+ PostReply deleted = em.find(PostReply.class, replyid);
+ Assertions.assertNull(deleted, "评论应已删除");
+ } finally {
+ if (replyid != null) {
+ try {
+ EntityTransaction tx = em.getTransaction();
+ if (!tx.isActive()) {
+ tx.begin();
+ PostReply toDelete = em.find(PostReply.class, replyid);
+ if (toDelete != null) {
+ em.remove(toDelete);
+ }
+ tx.commit();
+ }
+ } catch (Exception e) {
+ }
+ }
+ }
+ }));
+ }
-// return List.of(
-// DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> {
-// int magic = 100;
-// long beforeUpload = testUserPT.upload;
-// int beforeMagic = testUserPT.magic;
+ @TestFactory
+ Collection<DynamicTest> testExchangeMagicToUpload() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
-// boolean ret = db.ExchangeMagicToUpload(uid, magic);
-// Assertions.assertTrue(ret, "兑换上传量应成功");
+ String uid = userIds.get(0);
+ final UserPT testUserPT;
-// em.clear();
-// UserPT after = em.find(UserPT.class, uid);
-// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
-// Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加");
-// }),
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ UserPT tempUserPT = em.find(UserPT.class, uid);
-// DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> {
-// boolean ret = db.ExchangeMagicToUpload(uid, 2000);
-// Assertions.assertFalse(ret, "魔力值不足时应返回false");
-// }),
+ if (tempUserPT == null) {
+ tempUserPT = new UserPT();
+ tempUserPT.userid = uid;
+ tempUserPT.magic = 1000;
+ tempUserPT.upload = 1000;
+ tempUserPT.download = 1000;
+ em.persist(tempUserPT);
+ } else {
+ tempUserPT.magic = 1000;
+ tempUserPT.upload = 1000;
+ em.merge(tempUserPT);
+ }
+ testUserPT = tempUserPT;
+ tx.commit();
-// DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> {
-// Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100));
-// Assertions.assertFalse(db.ExchangeMagicToUpload("", 100));
-// Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0));
-// Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1));
-// }));
-// }
+ return List.of(
+ DynamicTest.dynamicTest("ExchangeMagicToUpload success", () -> {
+ try {
+ int magic = 100;
+ long beforeUpload = testUserPT.upload;
+ int beforeMagic = testUserPT.magic;
-// @TestFactory
-// Collection<DynamicTest> testExchangeMagicToDownload() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ boolean ret = db.ExchangeMagicToUpload(uid, magic);
+ Assertions.assertTrue(ret, "兑换上传量应成功");
-// String uid = userIds.get(0);
-// final UserPT testUserPT;
+ em.clear();
+ UserPT after = em.find(UserPT.class, uid);
+ Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
+ Assertions.assertEquals(beforeUpload + magic, after.upload, "上传量应增加");
+ } finally {
+ EntityTransaction tx2 = em.getTransaction();
+ tx2.begin();
+ UserPT user = em.find(UserPT.class, uid);
+ if (user != null) {
+ user.magic = 0;
+ user.upload = 0;
+ em.merge(user);
+ }
+ tx2.commit();
+ }
+ }),
-// // 确保用户有足够的魔力值和下载量用于测试
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// UserPT tempUserPT = em.find(UserPT.class, uid);
-// if (tempUserPT == null) {
-// tempUserPT = new UserPT();
-// tempUserPT.userid = uid;
-// tempUserPT.magic = 1000;
-// tempUserPT.upload = 1000;
-// tempUserPT.download = 1000;
-// em.persist(tempUserPT);
-// } else {
-// tempUserPT.magic = 1000;
-// em.merge(tempUserPT);
-// }
-// testUserPT = tempUserPT;
-// tx.commit();
+ DynamicTest.dynamicTest("ExchangeMagicToUpload insufficient magic", () -> {
+ boolean ret = db.ExchangeMagicToUpload(uid, 2000);
+ Assertions.assertFalse(ret, "魔力值不足时应返回false");
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> {
-// int magic = 100;
-// long beforeDownload = testUserPT.download;
-// int beforeMagic = testUserPT.magic;
+ DynamicTest.dynamicTest("ExchangeMagicToUpload invalid params", () -> {
+ Assertions.assertFalse(db.ExchangeMagicToUpload(null, 100));
+ Assertions.assertFalse(db.ExchangeMagicToUpload("", 100));
+ Assertions.assertFalse(db.ExchangeMagicToUpload(uid, 0));
+ Assertions.assertFalse(db.ExchangeMagicToUpload(uid, -1));
+ }));
+ }
-// boolean ret = db.ExchangeMagicToDownload(uid, magic);
-// Assertions.assertTrue(ret, "兑换下载量应成功");
+ @TestFactory
+ Collection<DynamicTest> testExchangeMagicToDownload() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
-// em.clear();
-// UserPT after = em.find(UserPT.class, uid);
-// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
-// Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少");
-// }),
+ String uid = userIds.get(0);
+ final UserPT testUserPT;
-// DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> {
-// boolean ret = db.ExchangeMagicToDownload(uid, 2000);
-// Assertions.assertFalse(ret, "魔力值不足时应返回false");
-// }),
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ UserPT tempUserPT = em.find(UserPT.class, uid);
+ if (tempUserPT == null) {
+ tempUserPT = new UserPT();
+ tempUserPT.userid = uid;
+ tempUserPT.magic = 1000;
+ tempUserPT.upload = 1000;
+ tempUserPT.download = 1000;
+ em.persist(tempUserPT);
+ } else {
+ tempUserPT.magic = 1000;
+ tempUserPT.download = 1000;
+ em.merge(tempUserPT);
+ }
+ testUserPT = tempUserPT;
+ tx.commit();
-// DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> {
-// Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100));
-// Assertions.assertFalse(db.ExchangeMagicToDownload("", 100));
-// Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0));
-// Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1));
-// }));
-// }
+ return List.of(
+ DynamicTest.dynamicTest("ExchangeMagicToDownload success", () -> {
+ try {
+ int magic = 100;
+ long beforeDownload = testUserPT.download;
+ int beforeMagic = testUserPT.magic;
-// @TestFactory
-// Collection<DynamicTest> testExchangeMagicToVip() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ boolean ret = db.ExchangeMagicToDownload(uid, magic);
+ Assertions.assertTrue(ret, "兑换下载量应成功");
-// String uid = userIds.get(0);
-// final UserPT testUserPT;
+ em.clear();
+ UserPT after = em.find(UserPT.class, uid);
+ Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
+ Assertions.assertEquals(beforeDownload - magic, after.download, "下载量应减少");
+ } finally {
+ EntityTransaction tx2 = em.getTransaction();
+ tx2.begin();
+ UserPT user = em.find(UserPT.class, uid);
+ if (user != null) {
+ user.magic = 0;
+ user.download = 0;
+ em.merge(user);
+ }
+ tx2.commit();
+ }
+ }),
-// // 确保用户有足够的魔力值用于测试
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// UserPT tempUserPT = em.find(UserPT.class, uid);
-// if (tempUserPT == null) {
-// tempUserPT = new UserPT();
-// tempUserPT.userid = uid;
-// tempUserPT.magic = 1000;
-// tempUserPT.upload = 1000;
-// tempUserPT.download = 1000;
-// em.persist(tempUserPT);
-// } else {
-// tempUserPT.magic = 1000;
-// em.merge(tempUserPT);
-// }
-// testUserPT = tempUserPT;
-// tx.commit();
+ DynamicTest.dynamicTest("ExchangeMagicToDownload insufficient magic", () -> {
+ boolean ret = db.ExchangeMagicToDownload(uid, 2000);
+ Assertions.assertFalse(ret, "魔力值不足时应返回false");
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> {
-// int magic = 100;
-// int beforeVip = testUserPT.viptime;
-// int beforeMagic = testUserPT.magic;
+ DynamicTest.dynamicTest("ExchangeMagicToDownload invalid params", () -> {
+ Assertions.assertFalse(db.ExchangeMagicToDownload(null, 100));
+ Assertions.assertFalse(db.ExchangeMagicToDownload("", 100));
+ Assertions.assertFalse(db.ExchangeMagicToDownload(uid, 0));
+ Assertions.assertFalse(db.ExchangeMagicToDownload(uid, -1));
+ }));
+ }
-// boolean ret = db.ExchangeMagicToVip(uid, magic);
-// Assertions.assertTrue(ret, "兑换VIP次数应成功");
+ @TestFactory
+ Collection<DynamicTest> testExchangeMagicToVip() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
-// em.clear();
-// UserPT after = em.find(UserPT.class, uid);
-// Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
-// Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加");
-// }),
+ String uid = userIds.get(0);
+ final UserPT testUserPT;
-// DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> {
-// boolean ret = db.ExchangeMagicToVip(uid, 2000);
-// Assertions.assertFalse(ret, "魔力值不足时应返回false");
-// }),
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ UserPT tempUserPT = em.find(UserPT.class, uid);
+ if (tempUserPT == null) {
+ tempUserPT = new UserPT();
+ tempUserPT.userid = uid;
+ tempUserPT.magic = 1000;
+ tempUserPT.upload = 1000;
+ tempUserPT.download = 1000;
+ tempUserPT.viptime = 0;
+ em.persist(tempUserPT);
+ } else {
+ tempUserPT.magic = 1000;
+ tempUserPT.viptime = 0;
+ em.merge(tempUserPT);
+ }
+ testUserPT = tempUserPT;
+ tx.commit();
-// DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> {
-// Assertions.assertFalse(db.ExchangeMagicToVip(null, 100));
-// Assertions.assertFalse(db.ExchangeMagicToVip("", 100));
-// Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0));
-// Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1));
-// }));
-// }
+ return List.of(
+ DynamicTest.dynamicTest("ExchangeMagicToVip success", () -> {
+ try {
+ int magic = 100;
+ int beforeVip = testUserPT.viptime;
+ int beforeMagic = testUserPT.magic;
-// @TestFactory
-// Collection<DynamicTest> testUploadTransmitProfile() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ boolean ret = db.ExchangeMagicToVip(uid, magic);
+ Assertions.assertTrue(ret, "兑换VIP次数应成功");
-// String uid = userIds.get(0);
-// String profileId = "test_profile_" + UUID.randomUUID();
-// Profile profile = new Profile();
-// profile.profileurl = profileId;
-// profile.userid = uid;
-// profile.magictogive = "100";
-// profile.uploadtogive = "1000";
-// profile.downloadtogive = "500";
+ em.clear();
+ UserPT after = em.find(UserPT.class, uid);
+ Assertions.assertEquals(beforeMagic - magic, after.magic, "魔力值应减少");
+ Assertions.assertEquals(beforeVip + magic, after.viptime, "VIP次数应增加");
+ } finally {
+ EntityTransaction tx2 = em.getTransaction();
+ tx2.begin();
+ UserPT user = em.find(UserPT.class, uid);
+ if (user != null) {
+ user.magic = 0;
+ user.viptime = 0;
+ em.merge(user);
+ }
+ tx2.commit();
+ }
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("UploadTransmitProfile success", () -> {
-// boolean ret = db.UploadTransmitProfile(profile);
-// Assertions.assertTrue(ret, "上传迁移信息应成功");
+ DynamicTest.dynamicTest("ExchangeMagicToVip insufficient magic", () -> {
+ boolean ret = db.ExchangeMagicToVip(uid, 2000);
+ Assertions.assertFalse(ret, "魔力值不足时应返回false");
+ }),
-// Profile inserted = em.find(Profile.class, profileId);
-// Assertions.assertNotNull(inserted, "迁移信息应已插入");
-// Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核");
-// Assertions.assertEquals("0", inserted.magicgived, "已发放魔力值应为0");
-// Assertions.assertEquals("0", inserted.uploadgived, "已发放上传量应为0");
-// }),
+ DynamicTest.dynamicTest("ExchangeMagicToVip invalid params", () -> {
+ Assertions.assertFalse(db.ExchangeMagicToVip(null, 100));
+ Assertions.assertFalse(db.ExchangeMagicToVip("", 100));
+ Assertions.assertFalse(db.ExchangeMagicToVip(uid, 0));
+ Assertions.assertFalse(db.ExchangeMagicToVip(uid, -1));
+ }));
+ }
-// DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> {
-// boolean ret = db.UploadTransmitProfile(profile);
-// Assertions.assertFalse(ret, "重复上传应返回false");
-// }),
+ @TestFactory
+ Collection<DynamicTest> testUploadTransmitProfile() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
-// DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> {
-// Assertions.assertFalse(db.UploadTransmitProfile(null));
-// Profile invalidProfile = new Profile();
-// Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile));
-// }));
-// }
+ String uid = userIds.get(0);
+ String profileId = "test_profile_" + System.currentTimeMillis();
+ Profile profile = new Profile();
+ profile.profileurl = profileId;
+ profile.userid = uid;
+ profile.magictogive = "100";
+ profile.uploadtogive = "1000";
+ profile.downloadtogive = "500";
+ profile.exampass = false;
+ profile.magicgived = "0";
+ profile.uploadgived = "0";
+ profile.applicationurl = "http://example.com/apply";
-// @TestFactory
-// Collection<DynamicTest> testGetTransmitProfile() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ return List.of(
+ DynamicTest.dynamicTest("UploadTransmitProfile success", () -> {
+ try {
+ boolean ret = db.UploadTransmitProfile(profile);
+ Assertions.assertTrue(ret, "上传迁移信息应成功");
-// String uid = userIds.get(0);
-// String profileId = "test_profile_get_" + UUID.randomUUID();
-// Profile profile = new Profile();
-// profile.profileurl = profileId;
-// profile.userid = uid;
-// profile.magictogive = "100";
-// profile.uploadtogive = "1000";
-// profile.exampass = false;
+ Profile inserted = em.find(Profile.class, profileId);
+ Assertions.assertNotNull(inserted, "迁移信息应已插入");
+ Assertions.assertFalse(inserted.exampass, "新迁移信息默认未审核");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ Profile toDelete = em.find(Profile.class, profileId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
-// // 先插入测试数据
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// em.persist(profile);
-// tx.commit();
+ DynamicTest.dynamicTest("UploadTransmitProfile duplicate", () -> {
+ try {
+ db.UploadTransmitProfile(profile);
+ boolean ret = db.UploadTransmitProfile(profile);
+ Assertions.assertFalse(ret, "重复上传应返回false");
+ } finally {
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ Profile toDelete = em.find(Profile.class, profileId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx.commit();
+ }
+ }),
-// return List.of(
-// DynamicTest.dynamicTest("GetTransmitProfile success", () -> {
-// Profile ret = db.GetTransmitProfile(profileId);
-// Assertions.assertNotNull(ret, "应成功获取迁移信息");
-// Assertions.assertEquals(uid, ret.userid, "用户ID应匹配");
-// Assertions.assertEquals("100", ret.magictogive, "待发放魔力值应匹配");
-// Assertions.assertEquals("1000", ret.uploadtogive, "待发放上传量应匹配");
-// }),
+ DynamicTest.dynamicTest("UploadTransmitProfile invalid params", () -> {
+ Assertions.assertFalse(db.UploadTransmitProfile(null));
+ Profile invalidProfile = new Profile();
+ Assertions.assertFalse(db.UploadTransmitProfile(invalidProfile));
+ }));
+ }
-// DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> {
-// Profile ret = db.GetTransmitProfile("not_exist_profile");
-// Assertions.assertNull(ret, "不存在的迁移信息应返回null");
-// }),
+ @TestFactory
+ Collection<DynamicTest> testGetTransmitProfile() {
+ // 获取现有的迁移信息记录
+ List<Profile> existingProfiles = em.createQuery(
+ "SELECT p FROM Profile p", Profile.class)
+ .setMaxResults(1)
+ .getResultList();
+ if (existingProfiles.isEmpty())
+ return Collections.emptyList();
-// DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> {
-// Assertions.assertNull(db.GetTransmitProfile(null));
-// Assertions.assertNull(db.GetTransmitProfile(""));
-// }));
-// }
+ Profile profile = existingProfiles.get(0);
+ String profileId = profile.profileurl;
-// @TestFactory
-// Collection<DynamicTest> testExamTransmitProfile() {
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class).getResultList();
-// if (userIds.isEmpty())
-// return Collections.emptyList();
+ return List.of(
+ DynamicTest.dynamicTest("GetTransmitProfile success", () -> {
+ Profile ret = db.GetTransmitProfile(profileId);
+ Assertions.assertNotNull(ret, "应成功获取迁移信息");
+ }),
-// String uid = userIds.get(0);
-// String profileId = "test_profile_exam_" + UUID.randomUUID();
-// Profile profile = new Profile();
-// profile.profileurl = profileId;
-// profile.userid = uid;
-// profile.magictogive = "100";
-// profile.uploadtogive = "1000";
-// profile.exampass = false;
+ DynamicTest.dynamicTest("GetTransmitProfile not exist", () -> {
+ Profile ret = db.GetTransmitProfile("not_exist_profile");
+ Assertions.assertNull(ret, "不存在的迁移信息应返回null");
+ }),
-// // 先插入测试数据
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// em.persist(profile);
-// UserPT userPT = em.find(UserPT.class, uid);
-// if (userPT == null) {
-// userPT = new UserPT();
-// userPT.userid = uid;
-// userPT.magic = 0;
-// userPT.upload = 0;
-// em.persist(userPT);
-// }
-// tx.commit();
+ DynamicTest.dynamicTest("GetTransmitProfile invalid params", () -> {
+ Assertions.assertNull(db.GetTransmitProfile(null));
+ Assertions.assertNull(db.GetTransmitProfile(""));
+ }));
+ }
-// return List.of(
-// DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> {
-// boolean ret = db.ExamTransmitProfile(profileId, true);
-// Assertions.assertTrue(ret, "审核通过应成功");
+ @TestFactory
+ Collection<DynamicTest> testExamTransmitProfile() {
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class).getResultList();
+ if (userIds.isEmpty())
+ return Collections.emptyList();
-// em.clear();
-// Profile examined = em.find(Profile.class, profileId);
-// Assertions.assertTrue(examined.exampass, "迁移信息应已审核通过");
-// Assertions.assertEquals("100", examined.magicgived, "应记录已发放魔力值");
-// Assertions.assertEquals("1000", examined.uploadgived, "应记录已发放上传量");
+ String uid = userIds.get(0);
+ String profileId = "test_profile_exam_" + System.currentTimeMillis();
+ Profile profile = new Profile();
+ profile.profileurl = profileId;
+ profile.userid = uid;
+ profile.magictogive = "100";
+ profile.uploadtogive = "1000";
+ profile.exampass = false;
+ profile.magicgived = "0";
+ profile.uploadgived = "0";
+ profile.applicationurl = "http://example.com/apply";
-// UserPT afterPT = em.find(UserPT.class, uid);
-// Assertions.assertEquals(100, afterPT.magic, "用户魔力值应增加");
-// Assertions.assertEquals(1000, afterPT.upload, "用户上传量应增加");
-// }),
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ em.persist(profile);
+ UserPT userPT = em.find(UserPT.class, uid);
+ if (userPT == null) {
+ userPT = new UserPT();
+ userPT.userid = uid;
+ userPT.magic = 0;
+ userPT.upload = 0;
+ em.persist(userPT);
+ } else {
+ userPT.magic = 0;
+ userPT.upload = 0;
+ em.merge(userPT);
+ }
+ tx.commit();
-// DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> {
-// String rejectId = "test_profile_reject_" + UUID.randomUUID();
-// Profile rejectProfile = new Profile();
-// rejectProfile.profileurl = rejectId;
-// rejectProfile.userid = uid;
-// rejectProfile.magictogive = "100";
-// rejectProfile.uploadtogive = "1000";
-// rejectProfile.exampass = false;
+ return List.of(
+ DynamicTest.dynamicTest("ExamTransmitProfile approve", () -> {
+ try {
+ boolean ret = db.ExamTransmitProfile(profileId, true);
+ Assertions.assertTrue(ret, "审核通过应成功");
+ } finally {
+ EntityTransaction tx2 = em.getTransaction();
+ tx2.begin();
+ UserPT user = em.find(UserPT.class, uid);
+ if (user != null) {
+ user.magic = 0;
+ user.upload = 0;
+ em.merge(user);
+ }
+ Profile toDelete = em.find(Profile.class, profileId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx2.commit();
+ }
+ }),
-// tx.begin();
-// em.persist(rejectProfile);
-// tx.commit();
+ DynamicTest.dynamicTest("ExamTransmitProfile reject", () -> {
+ String rejectId = "test_profile_reject_" + System.currentTimeMillis();
+ Profile rejectProfile = new Profile();
+ rejectProfile.profileurl = rejectId;
+ rejectProfile.userid = uid;
+ rejectProfile.magictogive = "100";
+ rejectProfile.uploadtogive = "1000";
+ rejectProfile.magicgived = "0";
+ rejectProfile.uploadgived = "0";
+ rejectProfile.exampass = false;
+ rejectProfile.applicationurl = "http://example.com/apply";
-// boolean ret = db.ExamTransmitProfile(rejectId, false);
-// Assertions.assertTrue(ret, "审核拒绝应成功");
+ EntityTransaction tx3 = em.getTransaction();
+ tx3.begin();
+ em.persist(rejectProfile);
+ tx3.commit();
-// em.clear();
-// Profile examined = em.find(Profile.class, rejectId);
-// Assertions.assertFalse(examined.exampass, "迁移信息应已审核拒绝");
-// Assertions.assertEquals("0", examined.magicgived, "不应发放魔力值");
-// Assertions.assertEquals("0", examined.uploadgived, "不应发放上传量");
-// }),
+ try {
+ boolean ret = db.ExamTransmitProfile(rejectId, false);
+ Assertions.assertTrue(ret, "审核拒绝应成功");
+ } finally {
+ EntityTransaction tx4 = em.getTransaction();
+ tx4.begin();
+ Profile toDelete = em.find(Profile.class, rejectId);
+ if (toDelete != null)
+ em.remove(toDelete);
+ tx4.commit();
+ }
+ }),
-// DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> {
-// Assertions.assertFalse(db.ExamTransmitProfile(null, true));
-// Assertions.assertFalse(db.ExamTransmitProfile("", true));
-// Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true));
-// }));
-// }
+ DynamicTest.dynamicTest("ExamTransmitProfile invalid params", () -> {
+ Assertions.assertFalse(db.ExamTransmitProfile(null, true));
+ Assertions.assertFalse(db.ExamTransmitProfile("", true));
+ Assertions.assertFalse(db.ExamTransmitProfile("not_exist_profile", true));
+ }));
+ }
-// @Test
-// void testGetTransmitProfileList() {
-// // 清空已有数据
-// em.getTransaction().begin();
-// em.createQuery("DELETE FROM Profile").executeUpdate();
-// em.getTransaction().commit();
+ @Test
+ void testGetTransmitProfileList() {
+ em.getTransaction().begin();
+ em.createQuery("DELETE FROM Profile").executeUpdate();
+ em.getTransaction().commit();
-// // 插入测试数据
-// List<String> userIds = em.createQuery("select u.userid from User u",
-// String.class)
-// .setMaxResults(3)
-// .getResultList();
-// if (userIds.isEmpty()) {
-// return;
-// }
+ List<String> userIds = em.createQuery("select u.userid from User u", String.class)
+ .setMaxResults(3)
+ .getResultList();
+ if (userIds.isEmpty()) {
+ return;
+ }
-// EntityTransaction tx = em.getTransaction();
-// tx.begin();
-// for (int i = 0; i < userIds.size(); i++) {
-// Profile profile = new Profile();
-// profile.profileurl = "test_profile_list_" + i;
-// profile.userid = userIds.get(i);
-// profile.magictogive = String.valueOf(100 * (i + 1));
-// profile.uploadtogive = String.valueOf(1000 * (i + 1));
-// profile.exampass = false;
-// em.persist(profile);
-// }
-// tx.commit();
+ EntityTransaction tx = em.getTransaction();
+ tx.begin();
+ for (int i = 0; i < userIds.size(); i++) {
+ Profile profile = new Profile();
+ profile.profileurl = "test_profile_list_" + i + "_" + System.currentTimeMillis();
+ profile.userid = userIds.get(i);
+ profile.magictogive = String.valueOf(100 * (i + 1));
+ profile.uploadtogive = String.valueOf(1000 * (i + 1));
+ profile.exampass = false;
+ profile.magicgived = "0";
+ profile.uploadgived = "0";
+ profile.applicationurl = "http://example.com/apply";
+ em.persist(profile);
+ }
+ tx.commit();
-// // 测试获取列表
-// Profile[] profiles = db.GetTransmitProfileList();
-// Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请");
-// Arrays.stream(profiles)
-// .forEach(p ->
-// Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"),
-// "应为测试数据"));
-// }
-// }
+ try {
+ Profile[] profiles = db.GetTransmitProfileList();
+ Assertions.assertEquals(userIds.size(), profiles.length, "应返回所有迁移申请");
+ Arrays.stream(profiles)
+ .forEach(p -> Assertions.assertTrue(p.profileurl.startsWith("test_profile_list_"), "应为测试数据"));
+ } finally {
+ EntityTransaction tx2 = em.getTransaction();
+ tx2.begin();
+ em.createQuery("DELETE FROM Profile p WHERE p.profileurl LIKE 'test_profile_list_%'").executeUpdate();
+ tx2.commit();
+ }
+ }
+}
\ No newline at end of file