root | 0dbc981 | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 1 | package databasetest; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | import java.util.UUID; |
| 9 | import java.util.stream.Collectors; |
| 10 | import java.util.stream.IntStream; |
| 11 | |
| 12 | import javax.persistence.EntityManager; |
| 13 | import javax.persistence.EntityManagerFactory; |
| 14 | import javax.persistence.EntityTransaction; |
| 15 | import javax.persistence.Persistence; |
| 16 | |
| 17 | import org.junit.jupiter.api.AfterAll; |
| 18 | import org.junit.jupiter.api.Assertions; |
| 19 | import org.junit.jupiter.api.BeforeAll; |
| 20 | import org.junit.jupiter.api.DynamicTest; |
| 21 | import org.junit.jupiter.api.TestFactory; |
| 22 | |
| 23 | import database.Database1; |
| 24 | import entity.User; |
| 25 | import entity.config; |
| 26 | |
| 27 | public class databasesystest { |
| 28 | private static EntityManagerFactory emf; |
| 29 | private static EntityManager em; |
| 30 | private static Database1 db; |
| 31 | private static List<String> testUserIds; |
| 32 | private static List<String> testEmails; |
| 33 | |
| 34 | @BeforeAll |
| 35 | static void setup() throws Exception { |
| 36 | Class.forName("com.mysql.cj.jdbc.Driver"); |
| 37 | config cfg = new config(); |
| 38 | Map<String, Object> props = new HashMap<>(); |
| 39 | String jdbcUrl = String.format( |
| 40 | "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC", |
| 41 | cfg.SqlURL, cfg.TestDatabase); |
| 42 | props.put("javax.persistence.jdbc.url", jdbcUrl); |
| 43 | props.put("javax.persistence.jdbc.user", cfg.SqlUsername); |
| 44 | props.put("javax.persistence.jdbc.password", cfg.SqlPassword); |
| 45 | props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver"); |
| 46 | emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props); |
| 47 | em = emf.createEntityManager(); |
| 48 | db = new Database1(); |
| 49 | java.lang.reflect.Field f = Database1.class.getDeclaredField("entitymanager"); |
| 50 | f.setAccessible(true); |
| 51 | f.set(db, em); |
| 52 | testUserIds = new ArrayList<>(); |
| 53 | testEmails = new ArrayList<>(); |
| 54 | for (int i = 0; i < 10; i++) { |
| 55 | String id = "test_user_" + i + "_" + UUID.randomUUID(); |
| 56 | if (id.length() > 36) id = id.substring(0, 36); |
| 57 | testUserIds.add(id); |
| 58 | testEmails.add("test_email_" + i + "@example.com"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @AfterAll |
| 63 | static void teardown() { |
| 64 | EntityTransaction tx = em.getTransaction(); |
| 65 | tx.begin(); |
| 66 | for (String uid : testUserIds) { |
| 67 | User found = em.find(User.class, uid); |
| 68 | if (found != null) em.remove(found); |
| 69 | } |
| 70 | tx.commit(); |
| 71 | if (em != null && em.isOpen()) em.close(); |
| 72 | if (emf != null && emf.isOpen()) emf.close(); |
| 73 | } |
| 74 | |
| 75 | @TestFactory |
| 76 | Collection<DynamicTest> testRegisterUser() { |
| 77 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("RegisterUser test #" + i, () -> { |
| 78 | EntityTransaction tx = em.getTransaction(); |
| 79 | tx.begin(); |
| 80 | try { |
| 81 | User user = new User(); |
| 82 | String userId = testUserIds.get(i); |
| 83 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 84 | user.userid = userId; |
| 85 | user.email = testEmails.get(i); |
| 86 | user.username = "user" + i; |
| 87 | user.password = "pwd" + i; |
| 88 | user.sex = "m"; |
| 89 | user.school = "school" + i; |
| 90 | user.pictureurl = null; |
| 91 | user.profile = null; |
| 92 | user.accountstate = false; // 必填 |
| 93 | user.invitetimes = 5; // 必填 |
| 94 | // 关键:补充userPT字段,避免not-null property异常 |
| 95 | entity.UserPT userPT = new entity.UserPT(); |
| 96 | userPT.userid = user.userid; |
| 97 | userPT.magic = 0; |
| 98 | userPT.upload = 0L; |
| 99 | userPT.download = 0L; |
| 100 | userPT.share = 0.0; |
| 101 | userPT.farmurl = null; |
| 102 | userPT.viptime = 0; |
| 103 | userPT.user = user; |
| 104 | user.userPT = userPT; |
| 105 | // 正常注册 |
| 106 | int ret = db.RegisterUser(user); |
| 107 | Assertions.assertEquals(0, ret, "RegisterUser should return 0 for success"); |
| 108 | // 再次注册同邮箱用户应返回1 |
| 109 | User user2 = new User(); |
| 110 | String userId2 = userId + "_dup"; |
| 111 | if (userId2.length() > 36) userId2 = userId2.substring(0, 36); |
| 112 | user2.userid = userId2; |
| 113 | user2.email = testEmails.get(i); |
| 114 | user2.username = "user_dup" + i; |
| 115 | user2.password = "pwd_dup" + i; |
| 116 | user2.sex = "f"; |
| 117 | user2.school = "school_dup" + i; |
| 118 | user2.pictureurl = null; |
| 119 | user2.profile = null; |
| 120 | user2.accountstate = false; // 必填 |
| 121 | user2.invitetimes = 5; // 必填 |
| 122 | int ret2 = db.RegisterUser(user2); |
| 123 | Assertions.assertEquals(1, ret2, "RegisterUser should return 1 for duplicate email"); |
| 124 | } finally { |
| 125 | tx.rollback(); |
| 126 | } |
| 127 | })).collect(Collectors.toList()); |
| 128 | } |
| 129 | |
| 130 | @TestFactory |
| 131 | Collection<DynamicTest> testUpdateInformation() { |
| 132 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("UpdateInformation test #" + i, () -> { |
| 133 | EntityTransaction tx = em.getTransaction(); |
| 134 | tx.begin(); |
| 135 | try { |
| 136 | // 先插入一个用户 |
| 137 | User user = new User(); |
| 138 | String userId = testUserIds.get(i); |
| 139 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 140 | user.userid = userId; |
| 141 | user.email = testEmails.get(i); |
| 142 | user.username = "user" + i; |
| 143 | user.password = "pwd" + i; |
| 144 | user.sex = "m"; |
| 145 | user.school = "school" + i; |
| 146 | user.pictureurl = null; |
| 147 | user.profile = null; |
| 148 | user.accountstate = false; |
| 149 | user.invitetimes = 5; |
| 150 | entity.UserPT userPT = new entity.UserPT(); |
| 151 | userPT.userid = user.userid; |
| 152 | userPT.magic = 0; |
| 153 | userPT.upload = 0L; |
| 154 | userPT.download = 0L; |
| 155 | userPT.share = 0.0; |
| 156 | userPT.farmurl = null; |
| 157 | userPT.viptime = 0; |
| 158 | userPT.user = user; |
| 159 | user.userPT = userPT; |
| 160 | em.persist(user); |
| 161 | em.flush(); |
| 162 | // 更新信息 |
| 163 | User updated = new User(); |
| 164 | updated.userid = userId; |
| 165 | updated.email = testEmails.get(i); |
| 166 | updated.username = "user_updated" + i; |
| 167 | updated.password = "newpassword" + i; |
| 168 | updated.sex = "f"; |
| 169 | updated.school = "MIT" + i; |
| 170 | updated.pictureurl = "https://cdn.example.com/avatars/user_new" + i + ".jpg"; |
| 171 | updated.profile = "Updated profile " + i; |
| 172 | updated.accountstate = true; |
| 173 | updated.invitetimes = 10 + i; |
| 174 | int ret = db.UpdateInformation(updated); |
| 175 | Assertions.assertEquals(0, ret, "UpdateInformation should return 0 for success"); |
| 176 | em.flush(); |
| 177 | em.clear(); |
| 178 | User after = em.find(User.class, userId); |
| 179 | Assertions.assertEquals("user_updated" + i, after.username); |
| 180 | Assertions.assertEquals("newpassword" + i, after.password); |
| 181 | Assertions.assertEquals("f", after.sex); |
| 182 | Assertions.assertEquals("MIT" + i, after.school); |
| 183 | Assertions.assertEquals("https://cdn.example.com/avatars/user_new" + i + ".jpg", after.pictureurl); |
| 184 | Assertions.assertEquals("Updated profile " + i, after.profile); |
| 185 | Assertions.assertTrue(after.accountstate); |
| 186 | Assertions.assertEquals(10 + i, after.invitetimes); |
| 187 | // 测试不存在用户 |
| 188 | User notExist = new User(); |
| 189 | notExist.userid = "not_exist_user_" + i; |
| 190 | notExist.email = "not_exist" + i + "@example.com"; |
| 191 | notExist.username = "not_exist"; |
| 192 | notExist.password = "not_exist"; |
| 193 | notExist.sex = "m"; |
| 194 | notExist.school = "not_exist"; |
| 195 | notExist.pictureurl = null; |
| 196 | notExist.profile = null; |
| 197 | notExist.accountstate = false; |
| 198 | notExist.invitetimes = 0; |
| 199 | int ret2 = db.UpdateInformation(notExist); |
| 200 | Assertions.assertEquals(1, ret2, "UpdateInformation should return 1 for not found user"); |
| 201 | } finally { |
| 202 | tx.rollback(); |
| 203 | } |
| 204 | })).collect(Collectors.toList()); |
| 205 | } |
| 206 | |
| 207 | @TestFactory |
| 208 | Collection<DynamicTest> testGetInformation() { |
| 209 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("GetInformation test #" + i, () -> { |
| 210 | EntityTransaction tx = em.getTransaction(); |
| 211 | tx.begin(); |
| 212 | try { |
| 213 | // 先插入一个用户(补充userPT字段,避免not-null property异常) |
| 214 | User user = new User(); |
| 215 | user.userid = testUserIds.get(i); |
| 216 | if (user.userid.length() > 36) user.userid = user.userid.substring(0, 36); |
| 217 | user.email = testEmails.get(i); |
| 218 | user.username = "user" + i; |
| 219 | user.password = "pwd" + i; |
| 220 | user.sex = "m"; |
| 221 | user.school = "school" + i; |
| 222 | user.pictureurl = "pic" + i + ".jpg"; |
| 223 | user.profile = "profile" + i; |
| 224 | user.accountstate = false; |
| 225 | user.invitetimes = 5; |
| 226 | entity.UserPT userPT = new entity.UserPT(); |
| 227 | userPT.userid = user.userid; |
| 228 | userPT.magic = 0; |
| 229 | userPT.upload = 0L; |
| 230 | userPT.download = 0L; |
| 231 | userPT.share = 0.0; |
| 232 | userPT.farmurl = null; |
| 233 | userPT.viptime = 0; |
| 234 | userPT.user = user; |
| 235 | user.userPT = userPT; |
| 236 | em.persist(user); |
| 237 | em.flush(); |
| 238 | // 查询 |
| 239 | User got = db.GetInformation(testUserIds.get(i)); |
| 240 | Assertions.assertNotNull(got, "GetInformation should return user"); |
| 241 | Assertions.assertEquals(user.userid, got.userid); |
| 242 | Assertions.assertEquals(user.email, got.email); |
| 243 | Assertions.assertEquals(user.username, got.username); |
| 244 | Assertions.assertEquals(user.password, got.password); |
| 245 | Assertions.assertEquals(user.sex, got.sex); |
| 246 | Assertions.assertEquals(user.school, got.school); |
| 247 | Assertions.assertEquals(user.pictureurl, got.pictureurl); |
| 248 | Assertions.assertEquals(user.profile, got.profile); |
| 249 | Assertions.assertEquals(user.accountstate, got.accountstate); |
| 250 | Assertions.assertEquals(user.invitetimes, got.invitetimes); |
| 251 | // 查询不存在用户 |
| 252 | User notExist = db.GetInformation("not_exist_" + i); |
| 253 | Assertions.assertNull(notExist, "GetInformation should return null for not found user"); |
| 254 | } finally { |
| 255 | tx.rollback(); |
| 256 | } |
| 257 | })).collect(Collectors.toList()); |
| 258 | } |
| 259 | |
| 260 | @TestFactory |
| 261 | Collection<DynamicTest> testGetInformationPT() { |
| 262 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("GetInformationPT test #" + i, () -> { |
| 263 | EntityTransaction tx = em.getTransaction(); |
| 264 | tx.begin(); |
| 265 | try { |
| 266 | // 先插入User |
| 267 | User user = new User(); |
| 268 | String userId = testUserIds.get(i); |
| 269 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 270 | user.userid = userId; |
| 271 | user.email = testEmails.get(i); |
| 272 | user.username = "user" + i; |
| 273 | user.password = "pwd" + i; |
| 274 | user.sex = "m"; |
| 275 | user.school = "school" + i; |
| 276 | user.pictureurl = null; |
| 277 | user.profile = null; |
| 278 | user.accountstate = false; |
| 279 | user.invitetimes = 5; |
| 280 | // 同时插入UserPT,保证外键依赖 |
| 281 | entity.UserPT userPT = new entity.UserPT(); |
| 282 | userPT.userid = userId; |
| 283 | userPT.magic = 100 + i; |
| 284 | userPT.upload = 1000L + i; |
| 285 | userPT.download = 2000L + i; |
| 286 | userPT.share = 1.5 + i; |
| 287 | userPT.farmurl = "/data/seeds/" + userId; |
| 288 | userPT.viptime = 3 + i; |
| 289 | userPT.user = user; |
| 290 | user.userPT = userPT; |
| 291 | em.persist(user); |
| 292 | em.persist(userPT); |
| 293 | em.flush(); |
| 294 | // 查询 |
| 295 | entity.UserPT got = db.GetInformationPT(userId); |
| 296 | Assertions.assertNotNull(got, "GetInformationPT should return UserPT"); |
| 297 | Assertions.assertEquals(userId, got.userid); |
| 298 | Assertions.assertEquals(100 + i, got.magic); |
| 299 | Assertions.assertEquals(1000L + i, got.upload); |
| 300 | Assertions.assertEquals(2000L + i, got.download); |
| 301 | Assertions.assertEquals(1.5 + i, got.share); |
| 302 | Assertions.assertEquals("/data/seeds/" + userId, got.farmurl); |
| 303 | Assertions.assertEquals(3 + i, got.viptime); |
| 304 | // 查询不存在用户 |
| 305 | entity.UserPT notExist = db.GetInformationPT("not_exist_user_" + i); |
| 306 | Assertions.assertNull(notExist, "GetInformationPT should return null for not found user"); |
| 307 | } finally { |
| 308 | tx.rollback(); |
| 309 | } |
| 310 | })).collect(Collectors.toList()); |
| 311 | } |
| 312 | |
| 313 | @TestFactory |
| 314 | Collection<DynamicTest> testUpdateInformationPT() { |
| 315 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("UpdateInformationPT test #" + i, () -> { |
| 316 | EntityTransaction tx = em.getTransaction(); |
| 317 | tx.begin(); |
| 318 | try { |
| 319 | // 先插入User |
| 320 | User user = new User(); |
| 321 | String userId = testUserIds.get(i); |
| 322 | user.userid = userId; |
| 323 | user.email = testEmails.get(i); |
| 324 | user.username = "user" + i; |
| 325 | user.password = "pwd" + i; |
| 326 | user.sex = "m"; |
| 327 | user.school = "school" + i; |
| 328 | user.pictureurl = null; |
| 329 | user.profile = null; |
| 330 | user.accountstate = false; |
| 331 | user.invitetimes = 5; |
| 332 | // 自动补齐userPT,避免not-null property异常 |
| 333 | entity.UserPT userPT = new entity.UserPT(); |
| 334 | userPT.userid = userId; |
| 335 | userPT.magic = 0; |
| 336 | userPT.upload = 0L; |
| 337 | userPT.download = 0L; |
| 338 | userPT.share = 0.0; |
| 339 | userPT.farmurl = null; |
| 340 | userPT.viptime = 0; |
| 341 | userPT.user = user; |
| 342 | user.userPT = userPT; |
| 343 | em.persist(user); |
| 344 | em.persist(userPT); |
| 345 | em.flush(); |
| 346 | // 修改信息 |
| 347 | entity.UserPT updated = new entity.UserPT(); |
| 348 | updated.userid = userId; |
| 349 | updated.magic = 200 + i; |
| 350 | updated.upload = 3000L + i; |
| 351 | updated.download = 4000L + i; |
| 352 | updated.share = 2.5 + i; |
| 353 | updated.farmurl = "/new/path/seed" + i; |
| 354 | updated.viptime = 8 + i; |
| 355 | int ret = db.UpdateInformationPT(updated); |
| 356 | Assertions.assertEquals(0, ret, "UpdateInformationPT should return 0 for success"); |
| 357 | em.flush(); |
| 358 | em.clear(); |
| 359 | entity.UserPT after = em.find(entity.UserPT.class, userId); |
| 360 | Assertions.assertEquals(200 + i, after.magic); |
| 361 | Assertions.assertEquals(3000L + i, after.upload); |
| 362 | Assertions.assertEquals(4000L + i, after.download); |
| 363 | Assertions.assertEquals(2.5 + i, after.share); |
| 364 | Assertions.assertEquals("/new/path/seed" + i, after.farmurl); |
| 365 | Assertions.assertEquals(8 + i, after.viptime); |
| 366 | // 测试不存在用户 |
| 367 | entity.UserPT notExist = new entity.UserPT(); |
| 368 | notExist.userid = "not_exist_" + i; |
| 369 | notExist.magic = 0; |
| 370 | notExist.upload = 0L; |
| 371 | notExist.download = 0L; |
| 372 | notExist.share = 0.0; |
| 373 | notExist.farmurl = null; |
| 374 | notExist.viptime = 0; |
| 375 | int ret2 = db.UpdateInformationPT(notExist); |
| 376 | Assertions.assertEquals(1, ret2, "UpdateInformationPT should return 1 for not found user"); |
| 377 | } finally { |
| 378 | tx.rollback(); |
| 379 | } |
| 380 | })).collect(Collectors.toList()); |
| 381 | } |
| 382 | |
| 383 | @TestFactory |
| 384 | Collection<DynamicTest> testRegisterUserPT() { |
| 385 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("RegisterUserPT test #" + i, () -> { |
| 386 | EntityTransaction tx = em.getTransaction(); |
| 387 | tx.begin(); |
| 388 | try { |
| 389 | // 先插入User,保证UserPT外键约束 |
| 390 | User user = new User(); |
| 391 | String userId = testUserIds.get(i); |
| 392 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 393 | user.userid = userId; |
| 394 | user.email = testEmails.get(i); |
| 395 | user.username = "user" + i; |
| 396 | user.password = "pwd" + i; |
| 397 | user.sex = "m"; |
| 398 | user.school = "school" + i; |
| 399 | user.pictureurl = null; |
| 400 | user.profile = null; |
| 401 | user.accountstate = false; |
| 402 | user.invitetimes = 5; |
| 403 | // 自动补齐userPT,避免not-null property异常 |
| 404 | entity.UserPT userPT = new entity.UserPT(); |
| 405 | userPT.userid = userId; |
| 406 | userPT.magic = 100 + i; |
| 407 | userPT.upload = 1000L + i; |
| 408 | userPT.download = 2000L + i; |
| 409 | userPT.share = 1.5; |
| 410 | userPT.farmurl = "/path/to/seed" + i; |
| 411 | userPT.viptime = 3 + i; |
| 412 | userPT.user = user; |
| 413 | user.userPT = userPT; |
| 414 | // em.persist(user); |
| 415 | // em.persist(userPT); |
| 416 | // em.flush(); |
| 417 | // 正常注册 |
| 418 | int ret = db.RegisterUserPT(userPT); |
| 419 | Assertions.assertEquals(0, ret, "RegisterUserPT should return 0 for success"); |
| 420 | // 再次注册同ID应返回1 |
| 421 | entity.UserPT userPT2 = new entity.UserPT(); |
| 422 | userPT2.userid = userId; |
| 423 | userPT2.magic = 200 + i; |
| 424 | userPT2.upload = 3000L + i; |
| 425 | userPT2.download = 4000L + i; |
| 426 | userPT2.share = 2.5; |
| 427 | userPT2.farmurl = "/other/path/seed" + i; |
| 428 | userPT2.viptime = 8 + i; |
| 429 | int ret2 = db.RegisterUserPT(userPT2); |
| 430 | Assertions.assertEquals(1, ret2, "RegisterUserPT should return 1 for duplicate id"); |
| 431 | } finally { |
| 432 | tx.rollback(); |
| 433 | } |
| 434 | })).collect(Collectors.toList()); |
| 435 | } |
| 436 | |
| 437 | @TestFactory |
| 438 | Collection<DynamicTest> testGetSeedInformation() { |
| 439 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("GetSeedInformation test #" + i, () -> { |
| 440 | EntityTransaction tx = em.getTransaction(); |
| 441 | tx.begin(); |
| 442 | try { |
| 443 | // 先插入User,保证seed.seeduserid外键依赖 |
| 444 | User user = new User(); |
| 445 | String userId = testUserIds.get(i); |
| 446 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 447 | user.userid = userId; |
| 448 | user.email = testEmails.get(i); |
| 449 | user.username = "user" + i; |
| 450 | user.password = "pwd" + i; |
| 451 | user.sex = "m"; |
| 452 | user.school = "school" + i; |
| 453 | user.pictureurl = null; |
| 454 | user.profile = null; |
| 455 | user.accountstate = false; |
| 456 | user.invitetimes = 5; |
| 457 | // 自动补齐userPT,保证UserPT不为空 |
| 458 | entity.UserPT userPT = new entity.UserPT(); |
| 459 | userPT.userid = userId; |
| 460 | userPT.magic = 0; |
| 461 | userPT.upload = 0L; |
| 462 | userPT.download = 0L; |
| 463 | userPT.share = 0.0; |
| 464 | userPT.farmurl = null; |
| 465 | userPT.viptime = 0; |
| 466 | userPT.user = user; |
| 467 | user.userPT = userPT; |
| 468 | em.persist(user); |
| 469 | em.persist(userPT); |
| 470 | // 再插入Seed |
| 471 | entity.Seed seed = new entity.Seed(); |
| 472 | seed.seedid = "test_seed_" + i + "_" + UUID.randomUUID(); |
| 473 | seed.seeduserid = userId; |
| 474 | seed.faketime = i; |
| 475 | seed.lastfakecheck = new java.util.Date(); |
| 476 | seed.outurl = "http://example.com/seed" + i; |
| 477 | seed.title = "title" + i; |
| 478 | seed.subtitle = "subtitle" + i; |
| 479 | seed.seedsize = "100MB"; |
| 480 | seed.seedtag = "tag" + i; |
| 481 | seed.downloadtimes = 10 + i; |
| 482 | seed.url = "http://download.com/seed" + i; |
| 483 | em.persist(seed); |
| 484 | em.flush(); |
| 485 | // 查询 |
| 486 | entity.Seed got = db.GetSeedInformation(seed.seedid); |
| 487 | Assertions.assertNotNull(got, "GetSeedInformation should return seed"); |
| 488 | Assertions.assertEquals(seed.seedid, got.seedid); |
| 489 | Assertions.assertEquals(seed.seeduserid, got.seeduserid); |
| 490 | Assertions.assertEquals(seed.faketime, got.faketime); |
| 491 | Assertions.assertEquals(seed.outurl, got.outurl); |
| 492 | Assertions.assertEquals(seed.title, got.title); |
| 493 | Assertions.assertEquals(seed.subtitle, got.subtitle); |
| 494 | Assertions.assertEquals(seed.seedsize, got.seedsize); |
| 495 | Assertions.assertEquals(seed.seedtag, got.seedtag); |
| 496 | Assertions.assertEquals(seed.downloadtimes, got.downloadtimes); |
| 497 | Assertions.assertEquals(seed.url, got.url); |
| 498 | // 查询不存在种子 |
| 499 | entity.Seed notExist = db.GetSeedInformation("not_exist_seed_" + i); |
| 500 | Assertions.assertNull(notExist, "GetSeedInformation should return null for not found seed"); |
| 501 | } finally { |
| 502 | tx.rollback(); |
| 503 | } |
| 504 | })).collect(Collectors.toList()); |
| 505 | } |
| 506 | |
| 507 | @TestFactory |
| 508 | Collection<DynamicTest> testRegisterSeed() { |
| 509 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("RegisterSeed test #" + i, () -> { |
| 510 | EntityTransaction tx = em.getTransaction(); |
| 511 | tx.begin(); |
| 512 | try { |
| 513 | // 先插入User,保证seed.seeduserid外键依赖,且UserPT不为空 |
| 514 | User user = new User(); |
| 515 | String userId = testUserIds.get(i); |
| 516 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 517 | user.userid = userId; |
| 518 | user.email = testEmails.get(i); |
| 519 | user.username = "user" + i; |
| 520 | user.password = "pwd" + i; |
| 521 | user.sex = "m"; |
| 522 | user.school = "school" + i; |
| 523 | user.pictureurl = null; |
| 524 | user.profile = null; |
| 525 | user.accountstate = false; |
| 526 | user.invitetimes = 5; |
| 527 | // 关键:补充userPT字段,避免not-null property异常 |
| 528 | entity.UserPT userPT = new entity.UserPT(); |
| 529 | userPT.userid = userId; |
| 530 | userPT.magic = 0; |
| 531 | userPT.upload = 0L; |
| 532 | userPT.download = 0L; |
| 533 | userPT.share = 0.0; |
| 534 | userPT.farmurl = null; |
| 535 | userPT.viptime = 0; |
| 536 | userPT.user = user; |
| 537 | user.userPT = userPT; |
| 538 | em.persist(user); |
| 539 | em.persist(userPT); |
| 540 | // 再插入Seed |
| 541 | entity.Seed seed = new entity.Seed(); |
| 542 | String sid = ("test_seed_" + i + "_" + UUID.randomUUID()); |
| 543 | if (sid.length() > 64) sid = sid.substring(0, 64); |
| 544 | seed.seedid = sid; |
| 545 | seed.seeduserid = userId; |
| 546 | seed.faketime = i; |
| 547 | seed.lastfakecheck = new java.util.Date(); |
| 548 | seed.outurl = ("http://example.com/seed" + i); |
| 549 | if (seed.outurl != null && seed.outurl.length() > 255) seed.outurl = seed.outurl.substring(0, 255); |
| 550 | seed.title = ("title" + i); |
| 551 | if (seed.title.length() > 255) seed.title = seed.title.substring(0, 255); |
| 552 | seed.subtitle = ("subtitle" + i); |
| 553 | if (seed.subtitle != null && seed.subtitle.length() > 255) seed.subtitle = seed.subtitle.substring(0, 255); |
| 554 | seed.seedsize = "100MB"; |
| 555 | if (seed.seedsize.length() > 50) seed.seedsize = seed.seedsize.substring(0, 50); |
| 556 | seed.seedtag = ("tag" + i); |
| 557 | if (seed.seedtag != null && seed.seedtag.length() > 255) seed.seedtag = seed.seedtag.substring(0, 255); |
| 558 | seed.downloadtimes = 10 + i; |
| 559 | seed.url = ("http://download.com/seed" + i); |
| 560 | em.persist(seed); |
| 561 | em.flush(); |
| 562 | // 校验数据库中已存在 |
| 563 | entity.Seed found = em.find(entity.Seed.class, seed.seedid); |
| 564 | Assertions.assertNotNull(found, "数据库应存在该种子"); |
| 565 | Assertions.assertEquals(seed.seedid, found.seedid); |
| 566 | Assertions.assertEquals(seed.seeduserid, found.seeduserid); |
| 567 | Assertions.assertEquals(seed.faketime, found.faketime); |
| 568 | Assertions.assertEquals(seed.outurl, found.outurl); |
| 569 | Assertions.assertEquals(seed.title, found.title); |
| 570 | Assertions.assertEquals(seed.subtitle, found.subtitle); |
| 571 | Assertions.assertEquals(seed.seedsize, found.seedsize); |
| 572 | Assertions.assertEquals(seed.seedtag, found.seedtag); |
| 573 | Assertions.assertEquals(seed.downloadtimes, found.downloadtimes); |
| 574 | Assertions.assertEquals(seed.url, found.url); |
| 575 | // 再次注册同ID应返回1 |
| 576 | entity.Seed seed2 = new entity.Seed(); |
| 577 | seed2.seedid = sid; |
| 578 | seed2.seeduserid = userId; |
| 579 | seed2.faketime = 99; |
| 580 | seed2.lastfakecheck = new java.util.Date(); |
| 581 | seed2.outurl = ("http://example.com/seed_dup" + i); |
| 582 | if (seed2.outurl != null && seed2.outurl.length() > 255) seed2.outurl = seed2.outurl.substring(0, 255); |
| 583 | seed2.title = ("title_dup" + i); |
| 584 | if (seed2.title.length() > 255) seed2.title = seed2.title.substring(0, 255); |
| 585 | seed2.subtitle = ("subtitle_dup" + i); |
| 586 | if (seed2.subtitle != null && seed2.subtitle.length() > 255) seed2.subtitle = seed2.subtitle.substring(0, 255); |
| 587 | seed2.seedsize = "200MB"; |
| 588 | if (seed2.seedsize.length() > 50) seed2.seedsize = seed2.seedsize.substring(0, 50); |
| 589 | seed2.seedtag = ("tag_dup" + i); |
| 590 | if (seed2.seedtag != null && seed2.seedtag.length() > 255) seed2.seedtag = seed2.seedtag.substring(0, 255); |
| 591 | seed2.downloadtimes = 99; |
| 592 | seed2.url = ("http://download.com/seed_dup" + i); |
| 593 | int ret2 = db.RegisterSeed(seed2); |
| 594 | Assertions.assertEquals(1, ret2, "RegisterSeed should return 1 for duplicate id"); |
| 595 | } finally { |
| 596 | tx.rollback(); |
| 597 | } |
| 598 | })).collect(Collectors.toList()); |
| 599 | } |
| 600 | |
| 601 | @TestFactory |
| 602 | Collection<DynamicTest> testUpdateSeed() { |
| 603 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("UpdateSeed test #" + i, () -> { |
| 604 | EntityTransaction tx = em.getTransaction(); |
| 605 | tx.begin(); |
| 606 | try { |
| 607 | // 先插入User,保证seed.seeduserid外键依赖,且UserPT不为空 |
| 608 | User user = new User(); |
| 609 | String userId = testUserIds.get(i); |
| 610 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 611 | user.userid = userId; |
| 612 | user.email = testEmails.get(i); |
| 613 | user.username = "user" + i; |
| 614 | user.password = "pwd" + i; |
| 615 | user.sex = "m"; |
| 616 | user.school = "school" + i; |
| 617 | user.pictureurl = null; |
| 618 | user.profile = null; |
| 619 | user.accountstate = false; |
| 620 | user.invitetimes = 5; |
| 621 | // 关键:补充userPT字段,避免not-null property异常 |
| 622 | entity.UserPT userPT = new entity.UserPT(); |
| 623 | userPT.userid = userId; |
| 624 | userPT.magic = 0; |
| 625 | userPT.upload = 0L; |
| 626 | userPT.download = 0L; |
| 627 | userPT.share = 0.0; |
| 628 | userPT.farmurl = null; |
| 629 | userPT.viptime = 0; |
| 630 | userPT.user = user; |
| 631 | user.userPT = userPT; |
| 632 | em.persist(user); |
| 633 | em.persist(userPT); |
| 634 | // 再插入Seed |
| 635 | entity.Seed seed = new entity.Seed(); |
| 636 | String sid = ("test_seed_" + i + "_" + UUID.randomUUID()); |
| 637 | if (sid.length() > 64) sid = sid.substring(0, 64); |
| 638 | seed.seedid = sid; |
| 639 | seed.seeduserid = userId; |
| 640 | seed.faketime = i; |
| 641 | seed.lastfakecheck = new java.util.Date(); |
| 642 | seed.outurl = ("http://example.com/seed" + i); |
| 643 | if (seed.outurl != null && seed.outurl.length() > 255) seed.outurl = seed.outurl.substring(0, 255); |
| 644 | seed.title = ("title" + i); |
| 645 | if (seed.title.length() > 255) seed.title = seed.title.substring(0, 255); |
| 646 | seed.subtitle = ("subtitle" + i); |
| 647 | if (seed.subtitle != null && seed.subtitle.length() > 255) seed.subtitle = seed.subtitle.substring(0, 255); |
| 648 | seed.seedsize = "100MB"; |
| 649 | if (seed.seedsize.length() > 50) seed.seedsize = seed.seedsize.substring(0, 50); |
| 650 | seed.seedtag = ("tag" + i); |
| 651 | if (seed.seedtag != null && seed.seedtag.length() > 255) seed.seedtag = seed.seedtag.substring(0, 255); |
| 652 | seed.downloadtimes = 10 + i; |
| 653 | seed.url = ("http://download.com/seed" + i); |
| 654 | em.persist(seed); |
| 655 | em.flush(); |
| 656 | // 修改信息 |
| 657 | entity.Seed updated = new entity.Seed(); |
| 658 | updated.seedid = sid; |
| 659 | updated.seeduserid = userId + "_updated"; |
| 660 | if (updated.seeduserid.length() > 36) updated.seeduserid = updated.seeduserid.substring(0, 36); |
| 661 | updated.faketime = 99; |
| 662 | updated.lastfakecheck = new java.util.Date(); |
| 663 | updated.outurl = ("http://example.com/seed_updated" + i); |
| 664 | if (updated.outurl != null && updated.outurl.length() > 255) updated.outurl = updated.outurl.substring(0, 255); |
| 665 | updated.title = ("title_updated" + i); |
| 666 | if (updated.title.length() > 255) updated.title = updated.title.substring(0, 255); |
| 667 | updated.subtitle = ("subtitle_updated" + i); |
| 668 | if (updated.subtitle != null && updated.subtitle.length() > 255) updated.subtitle = updated.subtitle.substring(0, 255); |
| 669 | updated.seedsize = "200MB"; |
| 670 | if (updated.seedsize.length() > 50) updated.seedsize = updated.seedsize.substring(0, 50); |
| 671 | updated.seedtag = ("tag_updated" + i); |
| 672 | if (updated.seedtag != null && updated.seedtag.length() > 255) updated.seedtag = updated.seedtag.substring(0, 255); |
| 673 | updated.downloadtimes = 99; |
| 674 | updated.url = ("http://download.com/seed_updated" + i); |
| 675 | int ret = db.UpdateSeed(updated); |
| 676 | Assertions.assertEquals(0, ret, "UpdateSeed should return 0 for success"); |
| 677 | em.flush(); |
| 678 | em.clear(); |
| 679 | entity.Seed after = em.find(entity.Seed.class, sid); |
| 680 | Assertions.assertEquals(updated.seeduserid, after.seeduserid); |
| 681 | Assertions.assertEquals(99, after.faketime); |
| 682 | Assertions.assertEquals(updated.outurl, after.outurl); |
| 683 | Assertions.assertEquals(updated.title, after.title); |
| 684 | Assertions.assertEquals(updated.subtitle, after.subtitle); |
| 685 | Assertions.assertEquals(updated.seedsize, after.seedsize); |
| 686 | Assertions.assertEquals(updated.seedtag, after.seedtag); |
| 687 | Assertions.assertEquals(99, after.downloadtimes); |
| 688 | Assertions.assertEquals(updated.url, after.url); |
| 689 | // 测试不存在种子 |
| 690 | entity.Seed notExist = new entity.Seed(); |
| 691 | notExist.seedid = "not_exist_seed_" + i; |
| 692 | if (notExist.seedid.length() > 64) notExist.seedid = notExist.seedid.substring(0, 64); |
| 693 | notExist.seeduserid = "owner_x"; |
| 694 | if (notExist.seeduserid.length() > 36) notExist.seeduserid = notExist.seeduserid.substring(0, 36); |
| 695 | notExist.faketime = 0; |
| 696 | notExist.lastfakecheck = new java.util.Date(); |
| 697 | notExist.outurl = null; |
| 698 | notExist.title = "not_exist"; |
| 699 | if (notExist.title.length() > 255) notExist.title = notExist.title.substring(0, 255); |
| 700 | notExist.subtitle = null; |
| 701 | notExist.seedsize = "0MB"; |
| 702 | if (notExist.seedsize.length() > 50) notExist.seedsize = notExist.seedsize.substring(0, 50); |
| 703 | notExist.seedtag = null; |
| 704 | notExist.downloadtimes = 0; |
| 705 | notExist.url = null; |
| 706 | int ret2 = db.UpdateSeed(notExist); |
| 707 | Assertions.assertEquals(1, ret2, "UpdateSeed should return 1 for not found seed"); |
| 708 | } finally { |
| 709 | tx.rollback(); |
| 710 | } |
| 711 | })).collect(Collectors.toList()); |
| 712 | } |
| 713 | |
| 714 | @TestFactory |
| 715 | Collection<DynamicTest> testSearchSeed() { |
| 716 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("SearchSeed test #" + i, () -> { |
| 717 | EntityTransaction tx = em.getTransaction(); |
| 718 | tx.begin(); |
| 719 | try { |
| 720 | // 先插入User,保证seed.seeduserid外键依赖 |
| 721 | User user = new User(); |
| 722 | String userId = testUserIds.get(i); |
| 723 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 724 | user.userid = userId; |
| 725 | user.email = testEmails.get(i); |
| 726 | user.username = "user" + i; |
| 727 | user.password = "pwd" + i; |
| 728 | user.sex = "m"; |
| 729 | user.school = "school" + i; |
| 730 | user.pictureurl = null; |
| 731 | user.profile = null; |
| 732 | user.accountstate = false; |
| 733 | user.invitetimes = 5; |
| 734 | |
| 735 | // 插入UserPT,保证UserPT不为空 |
| 736 | entity.UserPT userPT = new entity.UserPT(); |
| 737 | userPT.userid = userId; |
| 738 | userPT.magic = 0; |
| 739 | userPT.upload = 0L; |
| 740 | userPT.download = 0L; |
| 741 | userPT.share = 0.0; |
| 742 | userPT.farmurl = null; |
| 743 | userPT.viptime = 0; |
| 744 | userPT.user = user; |
| 745 | user.userPT = userPT; |
| 746 | em.persist(user); |
| 747 | em.persist(userPT); |
| 748 | em.flush(); |
| 749 | // 插入多条Seed |
| 750 | List<entity.Seed> seeds = new ArrayList<>(); |
| 751 | for (int j = 0; j < 5; j++) { |
| 752 | entity.Seed seed = new entity.Seed(); |
| 753 | seed.seedid = "search_seed_" + i + "_" + j + "_" + UUID.randomUUID(); |
| 754 | seed.seeduserid = userId; |
| 755 | seed.faketime = j; |
| 756 | seed.lastfakecheck = new java.util.Date(); |
| 757 | seed.outurl = "http://example.com/seed" + j; |
| 758 | seed.title = "TestTitle" + i + (j % 2 == 0 ? "_Special" : "") + "_" + j; |
| 759 | seed.subtitle = "subtitle" + j; |
| 760 | seed.seedsize = "100MB"; |
| 761 | seed.seedtag = "tag" + j; |
| 762 | seed.downloadtimes = 10 + j; |
| 763 | seed.url = "http://download.com/seed" + j; |
| 764 | em.persist(seed); |
| 765 | seeds.add(seed); |
| 766 | } |
| 767 | em.flush(); |
| 768 | // 测试关键词能搜到相关种子 |
| 769 | String keyword = "Special"; |
| 770 | entity.Seed[] result = db.SearchSeed(keyword); |
| 771 | boolean found = false; |
| 772 | for (entity.Seed s : result) { |
| 773 | if (s.title.contains(keyword)) found = true; |
| 774 | } |
| 775 | Assertions.assertTrue(found, "SearchSeed should find seeds with keyword in title"); |
| 776 | // 测试空字符串返回所有种子 |
| 777 | entity.Seed[] all = db.SearchSeed(""); |
| 778 | Assertions.assertEquals(seeds.size() + 5, all.length, "SearchSeed with empty string should return all"); |
| 779 | // 测试无匹配关键词 |
| 780 | entity.Seed[] none = db.SearchSeed("NoSuchKeyword" + i); |
| 781 | Assertions.assertEquals(seeds.size() + 5, none.length, "SearchSeed with no match should return all, sorted"); |
| 782 | // 测试null关键词 |
| 783 | entity.Seed[] nullResult = db.SearchSeed(null); |
| 784 | Assertions.assertEquals(seeds.size() + 5, nullResult.length, "SearchSeed with null should return all"); |
| 785 | } finally { |
| 786 | tx.rollback(); |
| 787 | } |
| 788 | })).collect(Collectors.toList()); |
| 789 | } |
| 790 | |
| 791 | @TestFactory |
| 792 | Collection<DynamicTest> testAddNotice() { |
| 793 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("AddNotice test #" + i, () -> { |
| 794 | EntityTransaction tx = em.getTransaction(); |
| 795 | tx.begin(); |
| 796 | try { |
| 797 | // 构造 Notice |
| 798 | entity.Notice notice = new entity.Notice(); |
| 799 | notice.noticeid = "test_notice_" + i + "_" + UUID.randomUUID(); |
| 800 | notice.noticecontent = "内容" + i; |
| 801 | notice.state = (i % 2 == 0); |
| 802 | notice.posttag = "tag" + i; |
| 803 | // 正常插入 |
| 804 | int ret = db.AddNotice(notice); |
| 805 | Assertions.assertEquals(0, ret, "AddNotice 应返回0"); |
| 806 | // 再次插入同ID应返回1 |
| 807 | entity.Notice notice2 = new entity.Notice(); |
| 808 | notice2.noticeid = notice.noticeid; |
| 809 | notice2.noticecontent = "内容重复" + i; |
| 810 | notice2.state = false; |
| 811 | notice2.posttag = "tag_dup" + i; |
| 812 | int ret2 = db.AddNotice(notice2); |
| 813 | Assertions.assertEquals(1, ret2, "AddNotice 应返回1(重复ID)"); |
| 814 | // 校验数据库中已存在 |
| 815 | entity.Notice found = em.find(entity.Notice.class, notice.noticeid); |
| 816 | Assertions.assertNotNull(found, "数据库应存在该公告"); |
| 817 | Assertions.assertEquals(notice.noticecontent, found.noticecontent); |
| 818 | Assertions.assertEquals(notice.state, found.state); |
| 819 | Assertions.assertEquals(notice.posttag, found.posttag); |
| 820 | } finally { |
| 821 | tx.rollback(); |
| 822 | } |
| 823 | })).collect(Collectors.toList()); |
| 824 | } |
| 825 | |
| 826 | @TestFactory |
| 827 | Collection<DynamicTest> testUpdateNotice() { |
| 828 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("UpdateNotice test #" + i, () -> { |
| 829 | EntityTransaction tx = em.getTransaction(); |
| 830 | tx.begin(); |
| 831 | try { |
| 832 | // 先插入一个Notice |
| 833 | entity.Notice notice = new entity.Notice(); |
| 834 | notice.noticeid = "test_notice_update_" + i + "_" + UUID.randomUUID(); |
| 835 | notice.noticecontent = "原内容" + i; |
| 836 | notice.state = true; |
| 837 | notice.posttag = "tag" + i; |
| 838 | em.persist(notice); |
| 839 | em.flush(); |
| 840 | // 修改内容 |
| 841 | entity.Notice updated = new entity.Notice(); |
| 842 | updated.noticeid = notice.noticeid; |
| 843 | updated.noticecontent = "新内容" + i; |
| 844 | updated.state = false; |
| 845 | updated.posttag = "tag_updated" + i; |
| 846 | boolean ret = db.UpdateNotice(updated); |
| 847 | Assertions.assertTrue(ret, "UpdateNotice 应返回 true"); |
| 848 | em.flush(); |
| 849 | em.clear(); |
| 850 | entity.Notice after = em.find(entity.Notice.class, notice.noticeid); |
| 851 | Assertions.assertEquals("新内容" + i, after.noticecontent); |
| 852 | Assertions.assertFalse(after.state); |
| 853 | Assertions.assertEquals("tag_updated" + i, after.posttag); |
| 854 | // 测试不存在公告 |
| 855 | entity.Notice notExist = new entity.Notice(); |
| 856 | notExist.noticeid = "not_exist_notice_" + i; |
| 857 | notExist.noticecontent = "xxx"; |
| 858 | notExist.state = false; |
| 859 | notExist.posttag = "none"; |
| 860 | boolean ret2 = db.UpdateNotice(notExist); |
| 861 | Assertions.assertFalse(ret2, "UpdateNotice 应返回 false for not found"); |
| 862 | } finally { |
| 863 | tx.rollback(); |
| 864 | } |
| 865 | })).collect(Collectors.toList()); |
| 866 | } |
| 867 | |
| 868 | @TestFactory |
| 869 | Collection<DynamicTest> testDeleteNotice() { |
| 870 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("DeleteNotice test #" + i, () -> { |
| 871 | EntityTransaction tx = em.getTransaction(); |
| 872 | tx.begin(); |
| 873 | try { |
| 874 | // 先插入一个Notice |
| 875 | entity.Notice notice = new entity.Notice(); |
| 876 | notice.noticeid = "test_notice_delete_" + i + "_" + UUID.randomUUID(); |
| 877 | notice.noticecontent = "内容" + i; |
| 878 | notice.state = true; |
| 879 | notice.posttag = "tag" + i; |
| 880 | em.persist(notice); |
| 881 | em.flush(); |
| 882 | // 删除 |
| 883 | boolean ret = db.DeleteNotice(notice.noticeid); |
| 884 | Assertions.assertTrue(ret, "DeleteNotice 应返回 true"); |
| 885 | em.flush(); |
| 886 | em.clear(); |
| 887 | entity.Notice after = em.find(entity.Notice.class, notice.noticeid); |
| 888 | Assertions.assertNull(after, "DeleteNotice 后应查不到公告"); |
| 889 | // 删除不存在的公告 |
| 890 | boolean ret2 = db.DeleteNotice("not_exist_notice_" + i); |
| 891 | Assertions.assertFalse(ret2, "DeleteNotice 应返回 false for not found"); |
| 892 | } finally { |
| 893 | tx.rollback(); |
| 894 | } |
| 895 | })).collect(Collectors.toList()); |
| 896 | } |
| 897 | |
| 898 | @TestFactory |
| 899 | Collection<DynamicTest> testGetUserAvailableInviteTimes() { |
| 900 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("GetUserAvailableInviteTimes test #" + i, () -> { |
| 901 | EntityTransaction tx = em.getTransaction(); |
| 902 | tx.begin(); |
| 903 | try { |
| 904 | // 先插入一个User |
| 905 | entity.User user = new entity.User(); |
| 906 | String userId = testUserIds.get(i); |
| 907 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 908 | user.userid = userId; |
| 909 | user.email = "invite" + i + "@example.com"; |
| 910 | user.username = "user" + i; |
| 911 | user.password = "pwd" + i; |
| 912 | user.sex = "m"; |
| 913 | user.school = "school" + i; |
| 914 | user.pictureurl = "pic" + i + ".jpg"; |
| 915 | user.profile = "profile" + i; |
| 916 | user.accountstate = true; |
| 917 | user.invitetimes = 5 + i; |
| 918 | |
| 919 | // 插入UserPT,保证UserPT不为空 |
| 920 | entity.UserPT userPT = new entity.UserPT(); |
| 921 | userPT.userid = user.userid; |
| 922 | userPT.magic = 0; |
| 923 | userPT.upload = 0L; |
| 924 | userPT.download = 0L; |
| 925 | userPT.share = 0.0; |
| 926 | userPT.farmurl = null; |
| 927 | userPT.viptime = 0; |
| 928 | userPT.user = user; |
| 929 | user.userPT = userPT; |
| 930 | em.persist(user); |
| 931 | em.persist(userPT); |
| 932 | em.flush(); |
| 933 | // 查询 |
| 934 | int left = db.GetUserAvailableInviteTimes(user.userid); |
| 935 | Assertions.assertEquals(5 + i, left, "GetUserAvailableInviteTimes 应返回正确剩余次数"); |
| 936 | // 查询不存在用户 |
| 937 | int notExist = db.GetUserAvailableInviteTimes("not_exist_user_" + i); |
| 938 | Assertions.assertEquals(-1, notExist, "GetUserAvailableInviteTimes 不存在用户应返回-1"); |
| 939 | } finally { |
| 940 | tx.rollback(); |
| 941 | } |
| 942 | })).collect(Collectors.toList()); |
| 943 | } |
| 944 | |
| 945 | @TestFactory |
| 946 | Collection<DynamicTest> testInviteUser() { |
| 947 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("InviteUser test #" + i, () -> { |
| 948 | EntityTransaction tx = em.getTransaction(); |
| 949 | tx.begin(); |
| 950 | try { |
| 951 | // 先插入一个User |
| 952 | entity.User user = new entity.User(); |
| 953 | String userId = testUserIds.get(i); |
| 954 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 955 | user.userid = userId; |
| 956 | user.email = "invite" + i + "@example.com"; |
| 957 | user.username = "user" + i; |
| 958 | user.password = "pwd" + i; |
| 959 | user.sex = "m"; |
| 960 | user.school = "school" + i; |
| 961 | user.pictureurl = "pic" + i + ".jpg"; |
| 962 | user.profile = "profile" + i; |
| 963 | user.accountstate = true; |
| 964 | user.invitetimes = 3 + i; |
| 965 | |
| 966 | // 插入UserPT,保证UserPT不为空 |
| 967 | entity.UserPT userPT = new entity.UserPT(); |
| 968 | userPT.userid = user.userid; |
| 969 | userPT.magic = 0; |
| 970 | userPT.upload = 0L; |
| 971 | userPT.download = 0L; |
| 972 | userPT.share = 0.0; |
| 973 | userPT.farmurl = null; |
| 974 | userPT.viptime = 0; |
| 975 | userPT.user = user; |
| 976 | user.userPT = userPT; |
| 977 | em.persist(user); |
| 978 | em.persist(userPT); |
| 979 | em.flush(); |
| 980 | // 正常邀请 |
| 981 | int ret = db.InviteUser(user.userid, user.email); |
| 982 | Assertions.assertEquals(0, ret, "InviteUser 应返回0"); |
| 983 | em.flush(); |
| 984 | em.clear(); |
| 985 | entity.User after = em.find(entity.User.class, user.userid); |
| 986 | Assertions.assertEquals(2 + i, after.invitetimes, "邀请后次数应减少1"); |
| 987 | // 剩余次数不足 |
| 988 | after.invitetimes = 0; |
| 989 | em.merge(after); |
| 990 | em.flush(); |
| 991 | int ret2 = db.InviteUser(user.userid, user.email); |
| 992 | Assertions.assertEquals(1, ret2, "InviteUser 剩余次数不足应返回1"); |
| 993 | // 邮箱不匹配 |
| 994 | int ret3 = db.InviteUser(user.userid, "wrong" + i + "@example.com"); |
| 995 | Assertions.assertEquals(3, ret3, "InviteUser 邮箱不匹配应返回3"); |
| 996 | // 用户不存在 |
| 997 | int ret4 = db.InviteUser("not_exist_user_" + i, "invite" + i + "@example.com"); |
| 998 | Assertions.assertEquals(3, ret4, "InviteUser 用户不存在应返回3"); |
| 999 | } finally { |
| 1000 | tx.rollback(); |
| 1001 | } |
| 1002 | })).collect(Collectors.toList()); |
| 1003 | } |
| 1004 | |
| 1005 | @TestFactory |
| 1006 | Collection<DynamicTest> testAddCollect() { |
| 1007 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("AddCollect test #" + i, () -> { |
| 1008 | EntityTransaction tx = em.getTransaction(); |
| 1009 | tx.begin(); |
| 1010 | try { |
| 1011 | // 构造用户和种子 |
| 1012 | String userId = testUserIds.get(i); |
| 1013 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 1014 | |
| 1015 | entity.User user = new entity.User(); |
| 1016 | user.userid = userId; |
| 1017 | user.email = "invite" + i + "@example.com"; |
| 1018 | user.username = "user" + i; |
| 1019 | user.password = "pwd" + i; |
| 1020 | user.sex = "m"; |
| 1021 | user.school = "school" + i; |
| 1022 | user.pictureurl = "pic" + i + ".jpg"; |
| 1023 | user.profile = "profile" + i; |
| 1024 | user.accountstate = true; |
| 1025 | user.invitetimes = 5 + i; |
| 1026 | // 插入UserPT,保证UserPT不为空 |
| 1027 | entity.UserPT userPT = new entity.UserPT(); |
| 1028 | userPT.userid = user.userid; |
| 1029 | userPT.magic = 0; |
| 1030 | userPT.upload = 0L; |
| 1031 | userPT.download = 0L; |
| 1032 | userPT.share = 0.0; |
| 1033 | userPT.farmurl = null; |
| 1034 | userPT.viptime = 0; |
| 1035 | userPT.user = user; |
| 1036 | user.userPT = userPT; |
| 1037 | em.persist(user); |
| 1038 | em.persist(userPT); |
| 1039 | em.flush(); |
| 1040 | entity.Seed seed = new entity.Seed(); |
| 1041 | String seedid = "test_seed_collect_" + i + "_" + UUID.randomUUID(); |
| 1042 | seed.seedid = seedid; |
| 1043 | seed.seeduserid = userId; |
| 1044 | seed.faketime = i; |
| 1045 | seed.lastfakecheck = new java.util.Date(); |
| 1046 | seed.outurl = "http://example.com/seed" + i; |
| 1047 | seed.title = "title" + i; |
| 1048 | seed.subtitle = "subtitle" + i; |
| 1049 | seed.seedsize = "100MB"; |
| 1050 | seed.seedtag = "tag" + i; |
| 1051 | seed.downloadtimes = 10 + i; |
| 1052 | seed.url = "http://download.com/seed" + i; |
| 1053 | em.persist(seed); |
| 1054 | em.flush(); |
| 1055 | // 正常收藏 |
| 1056 | boolean ret = db.AddCollect(userId, seedid); |
| 1057 | Assertions.assertTrue(ret, "AddCollect 应返回 true"); |
| 1058 | em.flush(); |
| 1059 | em.clear(); |
| 1060 | entity.UserStar found = em.find(entity.UserStar.class, new entity.UserStarId(userId, seedid)); |
| 1061 | Assertions.assertNotNull(found, "收藏应已存在"); |
| 1062 | // 重复收藏 |
| 1063 | boolean ret2 = db.AddCollect(userId, seedid); |
| 1064 | Assertions.assertFalse(ret2, "AddCollect 重复应返回 false"); |
| 1065 | // 用户不存在 |
| 1066 | boolean ret3 = db.AddCollect("not_exist_user_" + i, seedid); |
| 1067 | Assertions.assertFalse(ret3, "AddCollect 用户不存在应返回 false"); |
| 1068 | // 帖子不存在 |
| 1069 | boolean ret4 = db.AddCollect(userId, "not_exist_post_" + i); |
| 1070 | Assertions.assertFalse(ret4, "AddCollect 帖子不存在应返回 false"); |
| 1071 | } finally { |
| 1072 | tx.rollback(); |
| 1073 | } |
| 1074 | })).collect(Collectors.toList()); |
| 1075 | } |
| 1076 | |
| 1077 | @TestFactory |
| 1078 | Collection<DynamicTest> testDeleteCollect() { |
| 1079 | return IntStream.range(0, 10).mapToObj(i -> DynamicTest.dynamicTest("DeleteCollect test #" + i, () -> { |
| 1080 | EntityTransaction tx = em.getTransaction(); |
| 1081 | tx.begin(); |
| 1082 | try { |
| 1083 | // 构造用户和种子 |
| 1084 | String userId = testUserIds.get(i); |
| 1085 | if (userId.length() > 36) userId = userId.substring(0, 36); |
| 1086 | |
| 1087 | entity.User user = new entity.User(); |
| 1088 | user.userid = userId; |
| 1089 | user.email = "invite" + i + "@example.com"; |
| 1090 | user.username = "user" + i; |
| 1091 | user.password = "pwd" + i; |
| 1092 | user.sex = "m"; |
| 1093 | user.school = "school" + i; |
| 1094 | user.pictureurl = "pic" + i + ".jpg"; |
| 1095 | user.profile = "profile" + i; |
| 1096 | user.accountstate = true; |
| 1097 | user.invitetimes = 5 + i; |
| 1098 | // 插入UserPT,保证UserPT不为空 |
| 1099 | entity.UserPT userPT = new entity.UserPT(); |
| 1100 | userPT.userid = user.userid; |
| 1101 | userPT.magic = 0; |
| 1102 | userPT.upload = 0L; |
| 1103 | userPT.download = 0L; |
| 1104 | userPT.share = 0.0; |
| 1105 | userPT.farmurl = null; |
| 1106 | userPT.viptime = 0; |
| 1107 | userPT.user = user; |
| 1108 | user.userPT = userPT; |
| 1109 | em.persist(user); |
| 1110 | em.persist(userPT); |
| 1111 | em.flush(); |
| 1112 | entity.Seed seed = new entity.Seed(); |
| 1113 | String seedid = "test_seed_collect_" + i + "_" + UUID.randomUUID(); |
| 1114 | seed.seedid = seedid; |
| 1115 | seed.seeduserid = userId; |
| 1116 | seed.faketime = i; |
| 1117 | seed.lastfakecheck = new java.util.Date(); |
| 1118 | seed.outurl = "http://example.com/seed" + i; |
| 1119 | seed.title = "title" + i; |
| 1120 | seed.subtitle = "subtitle" + i; |
| 1121 | seed.seedsize = "100MB"; |
| 1122 | seed.seedtag = "tag" + i; |
| 1123 | seed.downloadtimes = 10 + i; |
| 1124 | seed.url = "http://download.com/seed" + i; |
| 1125 | em.persist(seed); |
| 1126 | em.flush(); |
| 1127 | // 先收藏 |
| 1128 | entity.UserStar star = new entity.UserStar(); |
| 1129 | star.userid = userId; |
| 1130 | star.seedid = seedid; |
| 1131 | em.persist(star); |
| 1132 | em.flush(); |
| 1133 | // 正常删除 |
| 1134 | boolean ret = db.DeleteCollect(userId, seedid); |
| 1135 | Assertions.assertTrue(ret, "DeleteCollect 应返回 true"); |
| 1136 | em.flush(); |
| 1137 | em.clear(); |
| 1138 | entity.UserStar found = em.find(entity.UserStar.class, new entity.UserStarId(userId, seedid)); |
| 1139 | Assertions.assertNull(found, "删除后收藏应不存在"); |
| 1140 | // 删除不存在的收藏 |
| 1141 | boolean ret2 = db.DeleteCollect(userId, seedid); |
| 1142 | Assertions.assertTrue(ret2, "再次删除应返回 true(JPA remove null容忍)"); |
| 1143 | // 用户不存在 |
| 1144 | boolean ret3 = db.DeleteCollect("not_exist_user_" + i, seedid); |
| 1145 | Assertions.assertTrue(ret3, "用户不存在时应返回 true(JPA remove null容忍)"); |
| 1146 | // 帖子不存在 |
| 1147 | boolean ret4 = db.DeleteCollect(userId, "not_exist_seed_" + i); |
| 1148 | Assertions.assertTrue(ret4, "帖子不存在时应返回 true(JPA remove null容忍)"); |
| 1149 | } finally { |
| 1150 | tx.rollback(); |
| 1151 | } |
| 1152 | })).collect(Collectors.toList()); |
| 1153 | } |
| 1154 | } |