blob: 3c4898bfd22dacdd40b1a2a2b0f708a9b821217e [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;
rootf35409f2025-05-19 04:41:57 +00007import java.util.UUID;
rootff0769a2025-05-18 17:24:41 +00008import java.util.stream.Collectors;
rootf35409f2025-05-19 04:41:57 +00009import java.util.stream.IntStream;
rootff0769a2025-05-18 17:24:41 +000010
11import javax.persistence.EntityManager;
12import javax.persistence.EntityManagerFactory;
rootf35409f2025-05-19 04:41:57 +000013import javax.persistence.EntityTransaction;
rootff0769a2025-05-18 17:24:41 +000014import javax.persistence.Persistence;
15
16import org.junit.jupiter.api.AfterAll;
17import org.junit.jupiter.api.Assertions;
18import org.junit.jupiter.api.BeforeAll;
19import org.junit.jupiter.api.DynamicTest;
20import org.junit.jupiter.api.TestFactory;
21
rootf35409f2025-05-19 04:41:57 +000022import entity.TransRecord;
23import entity.TransportId;
rootff0769a2025-05-18 17:24:41 +000024import entity.config;
25import tracker.Tracker;
26
27public class TrackerTest {
28 private static EntityManagerFactory emf;
29 private static EntityManager em;
30 private static List<String> userIds;
31 private static Map<String, Long> originalUploads;
32 private static Tracker tracker;
33
34 @BeforeAll
35 static void setup() throws Exception {
36 // 强制加载 MySQL 驱动,否则无法建立连接
37 Class.forName("com.mysql.cj.jdbc.Driver");
38 config cfg = new config();
39 Map<String,Object> props = new HashMap<>();
40 // 添加时区和 SSL 参数
41 String jdbcUrl = String.format(
42 "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
43 cfg.SqlURL, cfg.TestDatabase);
44 props.put("javax.persistence.jdbc.url", jdbcUrl);
45 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
46 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
47 props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
48 emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
49 em = emf.createEntityManager();
50 // 使用简单实体名而非带包名前缀
51 userIds = em.createQuery(
52 "select u.userid from UserPT u", String.class
53 ).getResultList();
54
55 // 保存初始 upload 值
56 originalUploads = new HashMap<>();
57 for (String uid : userIds) {
58 Long up = em.createQuery(
59 "select u.upload from UserPT u where u.userid = :uid", Long.class
60 ).setParameter("uid", uid)
61 .getSingleResult();
62 originalUploads.put(uid, up != null ? up : 0L);
63 }
64 tracker = new Tracker(emf);
65 }
66
67 @AfterAll
68 static void teardown() {
69 if (em != null && em.isOpen()) em.close();
70 if (emf != null && emf.isOpen()) emf.close();
71 }
72
73 @TestFactory
74 Collection<DynamicTest> testAddUpLoad() {
75 Random rnd = new Random();
76 return userIds.stream()
77 .map(uid -> DynamicTest.dynamicTest("AddUpLoad for user " + uid, () -> {
78 int delta = rnd.nextInt(1000) + 1; // Ensure non-zero value
79 long before = originalUploads.get(uid);
80
rootf35409f2025-05-19 04:41:57 +000081 // AddUpLoad 前打印
82 System.out.println("Running AddUpLoad assert for user=" + uid + ", delta=" + delta);
rootff0769a2025-05-18 17:24:41 +000083 Assertions.assertFalse(tracker.AddUpLoad(uid, delta),
84 "AddUpLoad should return false on successful operation");
rootf35409f2025-05-19 04:41:57 +000085 System.out.println("AddUpLoad assert passed for user=" + uid);
86
rootff0769a2025-05-18 17:24:41 +000087 // Clear the persistence context to ensure fresh data is fetched
88 em.clear();
89
90 // Fetch updated value
91 Long after = em.createQuery(
92 "select u.upload from UserPT u where u.userid = :uid", Long.class
93 ).setParameter("uid", uid)
94 .getSingleResult();
rootf35409f2025-05-19 04:41:57 +000095
96 // upload 值断言前打印
97 System.out.println("Running upload-value assert for user=" + uid);
rootff0769a2025-05-18 17:24:41 +000098 Assertions.assertEquals(before + delta, after,
99 "Upload value should be increased by " + delta);
rootf35409f2025-05-19 04:41:57 +0000100 System.out.println("Upload-value assert passed for user=" + uid);
rootff0769a2025-05-18 17:24:41 +0000101
rootf35409f2025-05-19 04:41:57 +0000102 // ReduceUpLoad 前打印
103 System.out.println("Running ReduceUpLoad assert for user=" + uid + ", delta=" + delta);
rootff0769a2025-05-18 17:24:41 +0000104 Assertions.assertFalse(tracker.ReduceUpLoad(uid, delta),
105 "ReduceUpLoad should return false on successful operation");
rootf35409f2025-05-19 04:41:57 +0000106 System.out.println("ReduceUpLoad assert passed for user=" + uid);
rootff0769a2025-05-18 17:24:41 +0000107 }))
108 .collect(Collectors.toList());
109 }
110
111 @TestFactory
112 Collection<DynamicTest> testReduceUpLoad() {
113 Random rnd = new Random();
114 return userIds.stream()
115 .map(uid -> DynamicTest.dynamicTest("ReduceUpLoad for user " + uid, () -> {
116 long before = originalUploads.get(uid);
117 int max = (int)Math.min(before, 1000);
118 int delta = max > 0 ? rnd.nextInt(max) + 1 : 0;
119 if (delta == 0) return; // 无可减量时跳过
rootf35409f2025-05-19 04:41:57 +0000120
121 // ReduceUpLoad 前打印
122 System.out.println("Running ReduceUpLoad assert for user=" + uid + ", delta=" + delta);
rootff0769a2025-05-18 17:24:41 +0000123 Assertions.assertFalse(tracker.ReduceUpLoad(uid, delta));
rootf35409f2025-05-19 04:41:57 +0000124 System.out.println("ReduceUpLoad assert passed for user=" + uid);
125
rootff0769a2025-05-18 17:24:41 +0000126 Long after = em.createQuery(
127 "select u.upload from UserPT u where u.userid = :uid", Long.class
128 ).setParameter("uid", uid)
129 .getSingleResult();
rootf35409f2025-05-19 04:41:57 +0000130
131 // 减少后值断言前打印
132 System.out.println("Running post-reduce-value assert for user=" + uid);
rootff0769a2025-05-18 17:24:41 +0000133 Assertions.assertEquals(before - delta, after);
rootf35409f2025-05-19 04:41:57 +0000134 System.out.println("Post-reduce-value assert passed for user=" + uid);
135
136 // 回滚 AddUpLoad 前打印
137 System.out.println("Running rollback AddUpLoad assert for user=" + uid + ", delta=" + delta);
rootff0769a2025-05-18 17:24:41 +0000138 Assertions.assertFalse(tracker.AddUpLoad(uid, delta));
rootf35409f2025-05-19 04:41:57 +0000139 System.out.println("Rollback AddUpLoad assert passed for user=" + uid);
140 }))
141 .collect(Collectors.toList());
142 }
143
144 @TestFactory
145 Collection<DynamicTest> testAddDownload() {
146 Random rnd = new Random();
147 return userIds.stream()
148 .map(uid -> DynamicTest.dynamicTest("AddDownload for user " + uid, () -> {
149 int delta = rnd.nextInt(1000) + 1;
150 Long before = em.createQuery(
151 "select u.download from UserPT u where u.userid = :uid", Long.class
152 ).setParameter("uid", uid).getSingleResult();
153 before = before != null ? before : 0L;
154
155 System.out.println("Running AddDownload assert for user=" + uid + ", delta=" + delta);
156 Assertions.assertFalse(tracker.AddDownload(uid, delta),
157 "AddDownload should return false on successful operation");
158 em.clear();
159
160 Long after = em.createQuery(
161 "select u.download from UserPT u where u.userid = :uid", Long.class
162 ).setParameter("uid", uid).getSingleResult();
163 System.out.println("Running download-value assert for user=" + uid);
164 Assertions.assertEquals(before + delta, after,
165 "Download value should be increased by " + delta);
166
167 System.out.println("Running rollback ReduceDownload assert for user=" + uid + ", delta=" + delta);
168 Assertions.assertFalse(tracker.ReduceDownload(uid, delta),
169 "Rollback ReduceDownload should return false");
170 }))
171 .collect(Collectors.toList());
172 }
173
174 @TestFactory
175 Collection<DynamicTest> testReduceDownload() {
176 Random rnd = new Random();
177 return userIds.stream()
178 .map(uid -> DynamicTest.dynamicTest("ReduceDownload for user " + uid, () -> {
179 Long before = em.createQuery(
180 "select u.download from UserPT u where u.userid = :uid", Long.class
181 ).setParameter("uid", uid).getSingleResult();
182 before = before != null ? before : 0L;
183 int max = before.intValue();
184 int delta = max > 0 ? rnd.nextInt(max) + 1 : 0;
185 if (delta == 0) return;
186
187 System.out.println("Running ReduceDownload assert for user=" + uid + ", delta=" + delta);
188 Assertions.assertFalse(tracker.ReduceDownload(uid, delta));
189 Long after = em.createQuery(
190 "select u.download from UserPT u where u.userid = :uid", Long.class
191 ).setParameter("uid", uid).getSingleResult();
192 System.out.println("Running post-reduce-download-value assert for user=" + uid);
193 Assertions.assertEquals(before - delta, after);
194
195 System.out.println("Running rollback AddDownload assert for user=" + uid + ", delta=" + delta);
196 Assertions.assertFalse(tracker.AddDownload(uid, delta));
197 }))
198 .collect(Collectors.toList());
199 }
200
201 @TestFactory
202 Collection<DynamicTest> testAddMagic() {
203 Random rnd = new Random();
204 return userIds.stream()
205 .map(uid -> DynamicTest.dynamicTest("AddMagic for user " + uid, () -> {
206 int delta = rnd.nextInt(1000) + 1;
207 Integer before = em.createQuery(
208 "select u.magic from UserPT u where u.userid = :uid", Integer.class
209 ).setParameter("uid", uid).getSingleResult();
210 before = before != null ? before : 0;
211
212 System.out.println("Running AddMagic assert for user=" + uid + ", delta=" + delta);
213 Assertions.assertFalse(tracker.AddMagic(uid, delta));
214 em.clear();
215
216 Integer after = em.createQuery(
217 "select u.magic from UserPT u where u.userid = :uid", Integer.class
218 ).setParameter("uid", uid).getSingleResult();
219 System.out.println("Running magic-value assert for user=" + uid);
220 Assertions.assertEquals(before + delta, after.intValue());
221
222 System.out.println("Running rollback ReduceMagic assert for user=" + uid + ", delta=" + delta);
223 Assertions.assertFalse(tracker.ReduceMagic(uid, delta));
224 }))
225 .collect(Collectors.toList());
226 }
227
228 @TestFactory
229 Collection<DynamicTest> testReduceMagic() {
230 Random rnd = new Random();
231 return userIds.stream()
232 .map(uid -> DynamicTest.dynamicTest("ReduceMagic for user " + uid, () -> {
233 Integer before = em.createQuery(
234 "select u.magic from UserPT u where u.userid = :uid", Integer.class
235 ).setParameter("uid", uid).getSingleResult();
236 before = before != null ? before : 0;
237 int max = before.intValue();
238 int delta = max > 0 ? rnd.nextInt(max) + 1 : 0;
239 if (delta == 0) return;
240
241 System.out.println("Running ReduceMagic assert for user=" + uid + ", delta=" + delta);
242 Assertions.assertFalse(tracker.ReduceMagic(uid, delta));
243 Integer after = em.createQuery(
244 "select u.magic from UserPT u where u.userid = :uid", Integer.class
245 ).setParameter("uid", uid).getSingleResult();
246 System.out.println("Running post-reduce-magic-value assert for user=" + uid);
247 Assertions.assertEquals(before - delta, after.intValue());
248
249 System.out.println("Running rollback AddMagic assert for user=" + uid + ", delta=" + delta);
250 Assertions.assertFalse(tracker.AddMagic(uid, delta));
251 }))
252 .collect(Collectors.toList());
253 }
254
255 @TestFactory
256 Collection<DynamicTest> testAddRecord() {
257 Random rnd = new Random();
258 // 取所有 seed_id 用于外键测试
259 List<String> seedIds = em.createQuery(
260 "select s.seedid from Seed s", String.class
261 ).getResultList();
262 // 若无可用 seedId 则跳过此组测试,避免 rnd.nextInt(0) 抛错
263 // if (seedIds.isEmpty()) {
264 // return Collections.emptyList();
265 // }
266 return IntStream.range(0, 10)
267 .mapToObj(i -> DynamicTest.dynamicTest("AddRecord test #" + i, () -> {
268 // 随机构造 TransRecord
269 String uploaderId = userIds.get(rnd.nextInt(userIds.size()));
270 String downloaderId = userIds.get(rnd.nextInt(userIds.size()));
271 String seedId = seedIds.get(rnd.nextInt(seedIds.size()));
272 String taskId = UUID.randomUUID().toString();
273
274 TransRecord rd = new TransRecord();
275 rd.taskid = taskId;
276 rd.uploaduserid = uploaderId;
277 rd.downloaduserid = downloaderId;
278 rd.seedid = seedId;
279 rd.upload = rnd.nextInt(10000);
280 rd.download = rnd.nextInt(10000);
281 rd.maxupload = rd.upload + rnd.nextInt(10000);
282 rd.maxdownload = rd.download + rnd.nextInt(10000);
283
284 // 调用待测方法
285 int ret = tracker.AddRecord(rd);
286 Assertions.assertTrue(ret > 0, "返回值应为新记录主键");
287
288 // 验证已插入
289 TransportId pk = new TransportId(taskId, uploaderId, downloaderId);
290 TransRecord fetched = em.find(TransRecord.class, pk);
291 Assertions.assertNotNull(fetched, "应查询到新增的 TransRecord");
292
293 // 清理:删除测试记录
294 EntityTransaction tx = em.getTransaction();
295 tx.begin();
296 em.remove(fetched);
297 tx.commit();
rootff0769a2025-05-18 17:24:41 +0000298 }))
299 .collect(Collectors.toList());
300 }
301}