Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0da03e

Browse files
committed
feat: add junit test for java code
1 parent 2e2c789 commit b0da03e

13 files changed

+179
-34
lines changed

‎java/src/main/java/AutoComplete.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import redis.clients.jedis.Jedis;
2+
import utils.JedisUtils;
23

34
import java.util.Set;
45

56
public class AutoComplete {
6-
private Jedis client = newJedis();
7+
private Jedis client = JedisUtils.getClient();
78

89
public AutoComplete() {
9-
client.flushAll();
10+
1011
}
1112

1213
/**

‎java/src/main/java/DistributedLock.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import redis.clients.jedis.Jedis;
22
import redis.clients.jedis.params.SetParams;
3+
import utils.JedisUtils;
34

45
import java.util.UUID;
56

67

78
public class DistributedLock {
89

9-
private Jedis client;
10+
private Jedis client = JedisUtils.getClient();
1011
private String key;
1112

12-
public DistributedLock(Jedis client, String key) {
13-
this.client = client;
13+
public DistributedLock(String key) {
1414
this.key = key;
1515
}
1616

‎java/src/main/java/LoginSession.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import org.apache.commons.codec.binary.Hex;
22
import redis.clients.jedis.Jedis;
3+
import utils.JedisUtils;
34

45
import java.security.MessageDigest;
56
import java.security.NoSuchAlgorithmException;
@@ -17,11 +18,10 @@ public class LoginSession {
1718
private final String SESSION_TOKEN_CORRECT = "SESSION_TOKEN_CORRECT";
1819
private final String SESSION_TOKEN_INCORRECT = "SESSION_TOKEN_INCORRECT";
1920

20-
private Jedis client;
21+
private Jedis client = JedisUtils.getClient();
2122
private String userId;
2223

23-
public LoginSession(Jedis client, String userId) {
24-
this.client = client;
24+
public LoginSession(String userId) {
2525
this.userId = userId;
2626
}
2727

‎java/src/main/java/Paginate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import redis.clients.jedis.Jedis;
2+
import utils.JedisUtils;
23

34
import java.util.List;
45

56
public class Paginate {
67

7-
private Jedis client;
8+
private Jedis client = JedisUtils.getClient();
89
private String key;
910

10-
public Paginate(Jedis client, String key) {
11-
this.client = client;
11+
public Paginate(String key) {
1212
this.key = key;
1313
}
1414

‎java/src/main/java/Ranking.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import redis.clients.jedis.Jedis;
22
import redis.clients.jedis.Tuple;
3+
import utils.JedisUtils;
34

45
import java.util.Calendar;
56
import java.util.Set;
@@ -9,11 +10,10 @@
910
* @date 2019年9月7日
1011
*/
1112
public class Ranking {
12-
private Jedis client = newJedis();
13+
private Jedis client = JedisUtils.getClient();
1314
private Calendar calendar = Calendar.getInstance();
1415

1516
public Ranking() {
16-
client.flushAll();
1717
calendar.setFirstDayOfWeek(Calendar.MONDAY);
1818
}
1919

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import redis.clients.jedis.Jedis;
2+
import utils.JedisUtils;
3+
4+
import java.util.Set;
5+
6+
public class SocialRelationship {
7+
private Jedis client = JedisUtils.getClient();
8+
private String user;
9+
10+
public SocialRelationship(String user) {
11+
this.user = user;
12+
}
13+
14+
/**
15+
* 关注目标用户
16+
*
17+
* @param target 用户
18+
*/
19+
public void follow(String target) {
20+
String followingKey = JedisUtils.getFollowingKey(user);
21+
String followersKey = JedisUtils.getFollowersKey(target);
22+
long now = System.currentTimeMillis();
23+
client.zadd(followingKey, now, target);
24+
client.zadd(followersKey, now, user);
25+
}
26+
27+
/**
28+
* 取关目标用户
29+
*
30+
* @param target 用户
31+
*/
32+
public void unfollow(String target) {
33+
String followingKey = JedisUtils.getFollowingKey(user);
34+
String followersKey = JedisUtils.getFollowersKey(target);
35+
client.zrem(followingKey, target);
36+
client.zrem(followersKey, user);
37+
}
38+
39+
/**
40+
* 判断是否关注着目标用户
41+
*
42+
* @param target 用户
43+
* @return 是否关注
44+
*/
45+
public boolean isFollowing(String target) {
46+
String followingKey = JedisUtils.getFollowingKey(user);
47+
return client.zrank(followingKey, target) != null;
48+
49+
}
50+
51+
/**
52+
* 获取当前用户关注的所有人,并按最近关注时间倒序排列
53+
*
54+
* @return 关注人集合
55+
*/
56+
public Set<String> getAllFollowing() {
57+
String followingKey = JedisUtils.getFollowingKey(user);
58+
return client.zrevrange(followingKey, 0, -1);
59+
}
60+
61+
/**
62+
* 获取当前用户的所有粉丝,并按最近关注时间倒序排列
63+
*
64+
* @return 粉丝集合
65+
*/
66+
public Set<String> getAllFollowers() {
67+
String followersKey = JedisUtils.getFollowersKey(user);
68+
return client.zrevrange(followersKey, 0, -1);
69+
}
70+
71+
/**
72+
* 获取当前用户关注的人数
73+
*
74+
* @return 人数
75+
*/
76+
public Long countFollowing() {
77+
String followingKey = JedisUtils.getFollowingKey(user);
78+
return client.zcard(followingKey);
79+
}
80+
81+
/**
82+
* 获取当前用户的粉丝数
83+
*
84+
* @return 人数
85+
*/
86+
public Long countFollowers() {
87+
String followersKey = JedisUtils.getFollowersKey(user);
88+
return client.zcard(followersKey);
89+
}
90+
91+
/**
92+
* 获取与某用户的共同关注
93+
*
94+
* @param one 用户
95+
* @return 共同关注的用户集合
96+
*/
97+
public Set<String> getCommonFollowing(String one) {
98+
String commonKey = JedisUtils.getCommonFollowingKey(user, one);
99+
client.zinterstore(commonKey, JedisUtils.getFollowingKey(user), JedisUtils.getFollowingKey(one));
100+
return client.zrevrange(commonKey, 0, -1);
101+
}
102+
}

‎java/src/main/java/URLShorten.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import redis.clients.jedis.Jedis;
2+
import utils.JedisUtils;
23

34
public class URLShorten {
4-
private Jedis client;
5+
private Jedis client = JedisUtils.getClient();
56

67
private final String URL_HASH_SHORT_SOURCE_KEY = "url_hash:short_source";
78
private final String ID_COUNTER = "short_url:id_counter";
@@ -24,8 +25,7 @@ private String base10ToBase36(long number) {
2425
return result.toString();
2526
}
2627

27-
public URLShorten(Jedis client) {
28-
this.client = client;
28+
public URLShorten() {
2929
// 设置初始ID值,保留1-5位的短码,从6位的短码开始生成
3030
this.client.set(ID_COUNTER, String.valueOf((long) Math.pow(36, 5) - 1));
3131
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package utils;
2+
3+
import redis.clients.jedis.Jedis;
4+
5+
public class JedisUtils {
6+
7+
public static Jedis getClient() {
8+
Jedis client = new Jedis();
9+
client.flushAll();
10+
return client;
11+
}
12+
13+
public static String getFollowingKey(String user) {
14+
return "following:" + user;
15+
}
16+
17+
public static String getFollowersKey(String user) {
18+
return "followers:" + user;
19+
}
20+
21+
public static String getCommonFollowingKey(String user1, String user2) {
22+
return String.format("common:following:%s:%s", user1, user2);
23+
}
24+
}

‎java/src/test/java/DistributedLockTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import org.junit.jupiter.api.Test;
2-
import redis.clients.jedis.Jedis;
32

43
import java.util.concurrent.TimeUnit;
54

@@ -10,10 +9,7 @@ public class DistributedLockTest {
109

1110
@Test
1211
public void testDistributedLock() throws InterruptedException {
13-
Jedis jedis = new Jedis();
14-
jedis.flushAll();
15-
16-
DistributedLock lock = new DistributedLock(jedis, "bingo_distributed_lock");
12+
DistributedLock lock = new DistributedLock("bingo_distributed_lock");
1713
assertTrue(lock.acquire(10));
1814
assertFalse(lock.acquire(10));
1915

‎java/src/test/java/LoginSessionTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import org.junit.jupiter.api.Test;
2-
import redis.clients.jedis.Jedis;
32

43
import static org.junit.jupiter.api.Assertions.assertEquals;
54

65
public class LoginSessionTest {
76

87
@Test
98
public void testLoginSession() {
10-
Jedis client = new Jedis();
11-
client.flushAll();
12-
LoginSession session = new LoginSession(client, "bingo");
9+
LoginSession session = new LoginSession("bingo");
1310

1411
String token = session.create();
1512

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /