Skip to content

Commit 40cb44a

Browse files
committed
Imported and applied formatter
1 parent f876a94 commit 40cb44a

1 file changed

Lines changed: 157 additions & 157 deletions

File tree

‎redis/src/test/java/com/baeldung/JedisTest.java‎

Lines changed: 157 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -25,201 +25,201 @@
2525
*/
2626
public class JedisTest {
2727

28-
private Jedis jedis;
29-
private static RedisServer redisServer;
28+
private Jedis jedis;
29+
private static RedisServer redisServer;
3030

31-
public JedisTest() {
32-
jedis = new Jedis();
33-
}
31+
public JedisTest() {
32+
jedis = new Jedis();
33+
}
3434

35-
@BeforeClass
36-
public static void setUp() throws IOException {
37-
redisServer = new RedisServer(6379);
38-
redisServer.start();
39-
}
35+
@BeforeClass
36+
public static void setUp() throws IOException {
37+
redisServer = new RedisServer(6379);
38+
redisServer.start();
39+
}
4040

41-
@AfterClass
42-
public static void destroy() {
43-
redisServer.stop();
44-
}
41+
@AfterClass
42+
public static void destroy() {
43+
redisServer.stop();
44+
}
4545

46-
@After
47-
public void flush() {
48-
jedis.flushAll();
49-
}
46+
@After
47+
public void flush() {
48+
jedis.flushAll();
49+
}
5050

51-
@Test
52-
public void givenAStringThenSaveItAsRedisStrings() {
53-
String key = "key";
54-
String value = "value";
51+
@Test
52+
public void givenAStringThenSaveItAsRedisStrings() {
53+
String key = "key";
54+
String value = "value";
5555

56-
jedis.set(key, value);
57-
String value2 = jedis.get(key);
56+
jedis.set(key, value);
57+
String value2 = jedis.get(key);
5858

59-
Assert.assertEquals(value, value2);
60-
}
59+
Assert.assertEquals(value, value2);
60+
}
6161

62-
@Test
63-
public void givenListElementsThenSaveThemInRedisList() {
64-
String queue = "queue#tasks";
62+
@Test
63+
public void givenListElementsThenSaveThemInRedisList() {
64+
String queue = "queue#tasks";
6565

66-
String taskOne = "firstTask";
67-
String taskTwo = "secondTask";
68-
String taskThree = "thirdTask";
66+
String taskOne = "firstTask";
67+
String taskTwo = "secondTask";
68+
String taskThree = "thirdTask";
6969

70-
jedis.lpush(queue, taskOne, taskTwo);
70+
jedis.lpush(queue, taskOne, taskTwo);
7171

72-
String taskReturnedOne = jedis.rpop(queue);
72+
String taskReturnedOne = jedis.rpop(queue);
7373

74-
jedis.lpush(queue, taskThree);
75-
Assert.assertEquals(taskOne, taskReturnedOne);
74+
jedis.lpush(queue, taskThree);
75+
Assert.assertEquals(taskOne, taskReturnedOne);
7676

77-
String taskReturnedTwo = jedis.rpop(queue);
78-
String taskReturnedThree = jedis.rpop(queue);
77+
String taskReturnedTwo = jedis.rpop(queue);
78+
String taskReturnedThree = jedis.rpop(queue);
7979

80-
Assert.assertEquals(taskTwo, taskReturnedTwo);
81-
Assert.assertEquals(taskThree, taskReturnedThree);
80+
Assert.assertEquals(taskTwo, taskReturnedTwo);
81+
Assert.assertEquals(taskThree, taskReturnedThree);
8282

83-
String taskReturnedFour = jedis.rpop(queue);
84-
Assert.assertNull(taskReturnedFour);
85-
}
83+
String taskReturnedFour = jedis.rpop(queue);
84+
Assert.assertNull(taskReturnedFour);
85+
}
8686

87-
@Test
88-
public void givenSetElementsThenSaveThemInRedisSet() {
89-
String countries = "countries";
87+
@Test
88+
public void givenSetElementsThenSaveThemInRedisSet() {
89+
String countries = "countries";
9090

91-
String countryOne = "Spain";
92-
String countryTwo = "Ireland";
93-
String countryThree = "Ireland";
91+
String countryOne = "Spain";
92+
String countryTwo = "Ireland";
93+
String countryThree = "Ireland";
9494

95-
jedis.sadd(countries, countryOne);
95+
jedis.sadd(countries, countryOne);
9696

97-
Set<String> countriesSet = jedis.smembers(countries);
98-
Assert.assertEquals(1, countriesSet.size());
97+
Set<String> countriesSet = jedis.smembers(countries);
98+
Assert.assertEquals(1, countriesSet.size());
9999

100-
jedis.sadd(countries, countryTwo);
101-
countriesSet = jedis.smembers(countries);
102-
Assert.assertEquals(2, countriesSet.size());
100+
jedis.sadd(countries, countryTwo);
101+
countriesSet = jedis.smembers(countries);
102+
Assert.assertEquals(2, countriesSet.size());
103103

104-
jedis.sadd(countries, countryThree);
105-
countriesSet = jedis.smembers(countries);
106-
Assert.assertEquals(2, countriesSet.size());
104+
jedis.sadd(countries, countryThree);
105+
countriesSet = jedis.smembers(countries);
106+
Assert.assertEquals(2, countriesSet.size());
107107

108-
boolean exists = jedis.sismember(countries, countryThree);
109-
Assert.assertTrue(exists);
110-
}
108+
boolean exists = jedis.sismember(countries, countryThree);
109+
Assert.assertTrue(exists);
110+
}
111111

112-
@Test
113-
public void givenObjectFieldsThenSaveThemInRedisHash() {
114-
String key = "user#1";
112+
@Test
113+
public void givenObjectFieldsThenSaveThemInRedisHash() {
114+
String key = "user#1";
115115

116-
String field = "name";
117-
String value = "William";
116+
String field = "name";
117+
String value = "William";
118118

119-
String field2 = "job";
120-
String value2 = "politician";
119+
String field2 = "job";
120+
String value2 = "politician";
121121

122-
jedis.hset(key, field, value);
123-
jedis.hset(key, field2, value2);
122+
jedis.hset(key, field, value);
123+
jedis.hset(key, field2, value2);
124124

125-
String value3 = jedis.hget(key, field);
126-
Assert.assertEquals(value, value3);
125+
String value3 = jedis.hget(key, field);
126+
Assert.assertEquals(value, value3);
127127

128-
Map<String, String> fields = jedis.hgetAll(key);
129-
String value4 = fields.get(field2);
130-
Assert.assertEquals(value2, value4);
131-
}
128+
Map<String, String> fields = jedis.hgetAll(key);
129+
String value4 = fields.get(field2);
130+
Assert.assertEquals(value2, value4);
131+
}
132132

133-
@Test
134-
public void givenARankingThenSaveItInRedisSortedSet() {
135-
String key = "ranking";
133+
@Test
134+
public void givenARankingThenSaveItInRedisSortedSet() {
135+
String key = "ranking";
136136

137-
Map<String, Double> scores = new HashMap<>();
137+
Map<String, Double> scores = new HashMap<>();
138138

139-
scores.put("PlayerOne", 3000.0);
140-
scores.put("PlayerTwo", 1500.0);
141-
scores.put("PlayerThree", 8200.0);
139+
scores.put("PlayerOne", 3000.0);
140+
scores.put("PlayerTwo", 1500.0);
141+
scores.put("PlayerThree", 8200.0);
142142

143-
for (String player : scores.keySet()) {
144-
jedis.zadd(key, scores.get(player), player);
145-
}
143+
for (String player : scores.keySet()) {
144+
jedis.zadd(key, scores.get(player), player);
145+
}
146146

147-
Set<String> players = jedis.zrevrange(key, 0, 1);
148-
Assert.assertEquals("PlayerThree", players.iterator().next());
147+
Set<String> players = jedis.zrevrange(key, 0, 1);
148+
Assert.assertEquals("PlayerThree", players.iterator().next());
149149

150-
long rank = jedis.zrevrank(key, "PlayerOne");
151-
Assert.assertEquals(1, rank);
152-
}
150+
long rank = jedis.zrevrank(key, "PlayerOne");
151+
Assert.assertEquals(1, rank);
152+
}
153153

154-
@Test
155-
public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() {
156-
String friendsPrefix = "friends#";
154+
@Test
155+
public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() {
156+
String friendsPrefix = "friends#";
157157

158-
String userOneId = "4352523";
159-
String userTwoId = "5552321";
158+
String userOneId = "4352523";
159+
String userTwoId = "5552321";
160160

161-
Transaction t = jedis.multi();
162-
t.sadd(friendsPrefix + userOneId, userTwoId);
163-
t.sadd(friendsPrefix + userTwoId, userOneId);
164-
t.exec();
161+
Transaction t = jedis.multi();
162+
t.sadd(friendsPrefix + userOneId, userTwoId);
163+
t.sadd(friendsPrefix + userTwoId, userOneId);
164+
t.exec();
165165

166-
boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId);
167-
Assert.assertTrue(exists);
166+
boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId);
167+
Assert.assertTrue(exists);
168168

169-
exists = jedis.sismember(friendsPrefix + userTwoId, userOneId);
170-
Assert.assertTrue(exists);
171-
}
169+
exists = jedis.sismember(friendsPrefix + userTwoId, userOneId);
170+
Assert.assertTrue(exists);
171+
}
172172

173-
@Test
174-
public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() {
175-
String userOneId = "4352523";
176-
String userTwoId = "4849888";
177-
178-
Pipeline p = jedis.pipelined();
179-
p.sadd("searched#" + userOneId, "paris");
180-
p.zadd("ranking", 126, userOneId);
181-
p.zadd("ranking", 325, userTwoId);
182-
Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris");
183-
Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1);
184-
p.sync();
185-
186-
Assert.assertTrue(pipeExists.get());
187-
Assert.assertEquals(2, pipeRanking.get().size());
188-
}
189-
190-
@Test
191-
public void givenAPoolConfigurationThenCreateAJedisPool() {
192-
final JedisPoolConfig poolConfig = buildPoolConfig();
193-
194-
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) {
195-
196-
// do simple operation to verify that the Jedis resource is working
197-
// properly
198-
String key = "key";
199-
String value = "value";
200-
201-
jedis.set(key, value);
202-
String value2 = jedis.get(key);
203-
204-
Assert.assertEquals(value, value2);
205-
206-
// flush Redis
207-
jedis.flushAll();
208-
}
209-
}
210-
211-
private JedisPoolConfig buildPoolConfig() {
212-
final JedisPoolConfig poolConfig = new JedisPoolConfig();
213-
poolConfig.setMaxTotal(128);
214-
poolConfig.setMaxIdle(128);
215-
poolConfig.setMinIdle(16);
216-
poolConfig.setTestOnBorrow(true);
217-
poolConfig.setTestOnReturn(true);
218-
poolConfig.setTestWhileIdle(true);
219-
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
220-
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
221-
poolConfig.setNumTestsPerEvictionRun(3);
222-
poolConfig.setBlockWhenExhausted(true);
223-
return poolConfig;
224-
}
173+
@Test
174+
public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() {
175+
String userOneId = "4352523";
176+
String userTwoId = "4849888";
177+
178+
Pipeline p = jedis.pipelined();
179+
p.sadd("searched#" + userOneId, "paris");
180+
p.zadd("ranking", 126, userOneId);
181+
p.zadd("ranking", 325, userTwoId);
182+
Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris");
183+
Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1);
184+
p.sync();
185+
186+
Assert.assertTrue(pipeExists.get());
187+
Assert.assertEquals(2, pipeRanking.get().size());
188+
}
189+
190+
@Test
191+
public void givenAPoolConfigurationThenCreateAJedisPool() {
192+
final JedisPoolConfig poolConfig = buildPoolConfig();
193+
194+
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) {
195+
196+
// do simple operation to verify that the Jedis resource is working
197+
// properly
198+
String key = "key";
199+
String value = "value";
200+
201+
jedis.set(key, value);
202+
String value2 = jedis.get(key);
203+
204+
Assert.assertEquals(value, value2);
205+
206+
// flush Redis
207+
jedis.flushAll();
208+
}
209+
}
210+
211+
private JedisPoolConfig buildPoolConfig() {
212+
final JedisPoolConfig poolConfig = new JedisPoolConfig();
213+
poolConfig.setMaxTotal(128);
214+
poolConfig.setMaxIdle(128);
215+
poolConfig.setMinIdle(16);
216+
poolConfig.setTestOnBorrow(true);
217+
poolConfig.setTestOnReturn(true);
218+
poolConfig.setTestWhileIdle(true);
219+
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
220+
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
221+
poolConfig.setNumTestsPerEvictionRun(3);
222+
poolConfig.setBlockWhenExhausted(true);
223+
return poolConfig;
224+
}
225225
}

0 commit comments

Comments
 (0)