blob: 1ef5f44e18b3eb3e9b5480d283099a640734f3a5 [file] [log] [blame]
rootff0769a2025-05-18 17:24:41 +00001package trackertest;
2import java.util.Collection;
3import java.util.HashMap;
4import java.util.List;
5import java.util.Map;
6import java.util.Random;
7import java.util.stream.Collectors;
8
9import javax.persistence.EntityManager;
10import javax.persistence.EntityManagerFactory;
11import javax.persistence.Persistence;
12
13import org.junit.jupiter.api.AfterAll;
14import org.junit.jupiter.api.Assertions;
15import org.junit.jupiter.api.BeforeAll;
16import org.junit.jupiter.api.DynamicTest;
17import org.junit.jupiter.api.TestFactory;
18
19import entity.config;
20import tracker.Tracker;
21
22public class TrackerTest {
23 private static EntityManagerFactory emf;
24 private static EntityManager em;
25 private static List<String> userIds;
26 private static Map<String, Long> originalUploads;
27 private static Tracker tracker;
28
29 @BeforeAll
30 static void setup() throws Exception {
31 // 强制加载 MySQL 驱动,否则无法建立连接
32 Class.forName("com.mysql.cj.jdbc.Driver");
33 config cfg = new config();
34 Map<String,Object> props = new HashMap<>();
35 // 添加时区和 SSL 参数
36 String jdbcUrl = String.format(
37 "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
38 cfg.SqlURL, cfg.TestDatabase);
39 props.put("javax.persistence.jdbc.url", jdbcUrl);
40 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
41 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
42 props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
43 emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
44 em = emf.createEntityManager();
45 // 使用简单实体名而非带包名前缀
46 userIds = em.createQuery(
47 "select u.userid from UserPT u", String.class
48 ).getResultList();
49
50 // 保存初始 upload 值
51 originalUploads = new HashMap<>();
52 for (String uid : userIds) {
53 Long up = em.createQuery(
54 "select u.upload from UserPT u where u.userid = :uid", Long.class
55 ).setParameter("uid", uid)
56 .getSingleResult();
57 originalUploads.put(uid, up != null ? up : 0L);
58 }
59 tracker = new Tracker(emf);
60 }
61
62 @AfterAll
63 static void teardown() {
64 if (em != null && em.isOpen()) em.close();
65 if (emf != null && emf.isOpen()) emf.close();
66 }
67
68 @TestFactory
69 Collection<DynamicTest> testAddUpLoad() {
70 Random rnd = new Random();
71 return userIds.stream()
72 .map(uid -> DynamicTest.dynamicTest("AddUpLoad for user " + uid, () -> {
73 int delta = rnd.nextInt(1000) + 1; // Ensure non-zero value
74 long before = originalUploads.get(uid);
75
76 // 操作成功时返回 false
77 Assertions.assertFalse(tracker.AddUpLoad(uid, delta),
78 "AddUpLoad should return false on successful operation");
79
80 // Clear the persistence context to ensure fresh data is fetched
81 em.clear();
82
83 // Fetch updated value
84 Long after = em.createQuery(
85 "select u.upload from UserPT u where u.userid = :uid", Long.class
86 ).setParameter("uid", uid)
87 .getSingleResult();
88
89 Assertions.assertEquals(before + delta, after,
90 "Upload value should be increased by " + delta);
91
92 // 操作成功时返回 false
93 Assertions.assertFalse(tracker.ReduceUpLoad(uid, delta),
94 "ReduceUpLoad should return false on successful operation");
95 }))
96 .collect(Collectors.toList());
97 }
98
99 @TestFactory
100 Collection<DynamicTest> testReduceUpLoad() {
101 Random rnd = new Random();
102 return userIds.stream()
103 .map(uid -> DynamicTest.dynamicTest("ReduceUpLoad for user " + uid, () -> {
104 long before = originalUploads.get(uid);
105 int max = (int)Math.min(before, 1000);
106 int delta = max > 0 ? rnd.nextInt(max) + 1 : 0;
107 if (delta == 0) return; // 无可减量时跳过
108 Assertions.assertFalse(tracker.ReduceUpLoad(uid, delta));
109 Long after = em.createQuery(
110 "select u.upload from UserPT u where u.userid = :uid", Long.class
111 ).setParameter("uid", uid)
112 .getSingleResult();
113 Assertions.assertEquals(before - delta, after);
114 // 回滚到初始值
115 Assertions.assertFalse(tracker.AddUpLoad(uid, delta));
116 }))
117 .collect(Collectors.toList());
118 }
119}