Imported and applied formatter

This commit is contained in:
Guillermo Casanova 2016-07-10 11:03:46 +02:00
parent f876a94fc5
commit 40cb44a47a

View File

@ -25,201 +25,201 @@ import redis.embedded.RedisServer;
*/ */
public class JedisTest { public class JedisTest {
private Jedis jedis; private Jedis jedis;
private static RedisServer redisServer; private static RedisServer redisServer;
public JedisTest() { public JedisTest() {
jedis = new Jedis(); jedis = new Jedis();
} }
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
redisServer = new RedisServer(6379); redisServer = new RedisServer(6379);
redisServer.start(); redisServer.start();
} }
@AfterClass @AfterClass
public static void destroy() { public static void destroy() {
redisServer.stop(); redisServer.stop();
} }
@After @After
public void flush() { public void flush() {
jedis.flushAll(); jedis.flushAll();
} }
@Test @Test
public void givenAStringThenSaveItAsRedisStrings() { public void givenAStringThenSaveItAsRedisStrings() {
String key = "key"; String key = "key";
String value = "value"; String value = "value";
jedis.set(key, value); jedis.set(key, value);
String value2 = jedis.get(key); String value2 = jedis.get(key);
Assert.assertEquals(value, value2); Assert.assertEquals(value, value2);
} }
@Test @Test
public void givenListElementsThenSaveThemInRedisList() { public void givenListElementsThenSaveThemInRedisList() {
String queue = "queue#tasks"; String queue = "queue#tasks";
String taskOne = "firstTask"; String taskOne = "firstTask";
String taskTwo = "secondTask"; String taskTwo = "secondTask";
String taskThree = "thirdTask"; String taskThree = "thirdTask";
jedis.lpush(queue, taskOne, taskTwo); jedis.lpush(queue, taskOne, taskTwo);
String taskReturnedOne = jedis.rpop(queue); String taskReturnedOne = jedis.rpop(queue);
jedis.lpush(queue, taskThree); jedis.lpush(queue, taskThree);
Assert.assertEquals(taskOne, taskReturnedOne); Assert.assertEquals(taskOne, taskReturnedOne);
String taskReturnedTwo = jedis.rpop(queue); String taskReturnedTwo = jedis.rpop(queue);
String taskReturnedThree = jedis.rpop(queue); String taskReturnedThree = jedis.rpop(queue);
Assert.assertEquals(taskTwo, taskReturnedTwo); Assert.assertEquals(taskTwo, taskReturnedTwo);
Assert.assertEquals(taskThree, taskReturnedThree); Assert.assertEquals(taskThree, taskReturnedThree);
String taskReturnedFour = jedis.rpop(queue); String taskReturnedFour = jedis.rpop(queue);
Assert.assertNull(taskReturnedFour); Assert.assertNull(taskReturnedFour);
} }
@Test @Test
public void givenSetElementsThenSaveThemInRedisSet() { public void givenSetElementsThenSaveThemInRedisSet() {
String countries = "countries"; String countries = "countries";
String countryOne = "Spain"; String countryOne = "Spain";
String countryTwo = "Ireland"; String countryTwo = "Ireland";
String countryThree = "Ireland"; String countryThree = "Ireland";
jedis.sadd(countries, countryOne); jedis.sadd(countries, countryOne);
Set<String> countriesSet = jedis.smembers(countries); Set<String> countriesSet = jedis.smembers(countries);
Assert.assertEquals(1, countriesSet.size()); Assert.assertEquals(1, countriesSet.size());
jedis.sadd(countries, countryTwo); jedis.sadd(countries, countryTwo);
countriesSet = jedis.smembers(countries); countriesSet = jedis.smembers(countries);
Assert.assertEquals(2, countriesSet.size()); Assert.assertEquals(2, countriesSet.size());
jedis.sadd(countries, countryThree); jedis.sadd(countries, countryThree);
countriesSet = jedis.smembers(countries); countriesSet = jedis.smembers(countries);
Assert.assertEquals(2, countriesSet.size()); Assert.assertEquals(2, countriesSet.size());
boolean exists = jedis.sismember(countries, countryThree); boolean exists = jedis.sismember(countries, countryThree);
Assert.assertTrue(exists); Assert.assertTrue(exists);
} }
@Test @Test
public void givenObjectFieldsThenSaveThemInRedisHash() { public void givenObjectFieldsThenSaveThemInRedisHash() {
String key = "user#1"; String key = "user#1";
String field = "name"; String field = "name";
String value = "William"; String value = "William";
String field2 = "job"; String field2 = "job";
String value2 = "politician"; String value2 = "politician";
jedis.hset(key, field, value); jedis.hset(key, field, value);
jedis.hset(key, field2, value2); jedis.hset(key, field2, value2);
String value3 = jedis.hget(key, field); String value3 = jedis.hget(key, field);
Assert.assertEquals(value, value3); Assert.assertEquals(value, value3);
Map<String, String> fields = jedis.hgetAll(key); Map<String, String> fields = jedis.hgetAll(key);
String value4 = fields.get(field2); String value4 = fields.get(field2);
Assert.assertEquals(value2, value4); Assert.assertEquals(value2, value4);
} }
@Test @Test
public void givenARankingThenSaveItInRedisSortedSet() { public void givenARankingThenSaveItInRedisSortedSet() {
String key = "ranking"; String key = "ranking";
Map<String, Double> scores = new HashMap<>(); Map<String, Double> scores = new HashMap<>();
scores.put("PlayerOne", 3000.0); scores.put("PlayerOne", 3000.0);
scores.put("PlayerTwo", 1500.0); scores.put("PlayerTwo", 1500.0);
scores.put("PlayerThree", 8200.0); scores.put("PlayerThree", 8200.0);
for (String player : scores.keySet()) { for (String player : scores.keySet()) {
jedis.zadd(key, scores.get(player), player); jedis.zadd(key, scores.get(player), player);
} }
Set<String> players = jedis.zrevrange(key, 0, 1); Set<String> players = jedis.zrevrange(key, 0, 1);
Assert.assertEquals("PlayerThree", players.iterator().next()); Assert.assertEquals("PlayerThree", players.iterator().next());
long rank = jedis.zrevrank(key, "PlayerOne"); long rank = jedis.zrevrank(key, "PlayerOne");
Assert.assertEquals(1, rank); Assert.assertEquals(1, rank);
} }
@Test @Test
public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() { public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() {
String friendsPrefix = "friends#"; String friendsPrefix = "friends#";
String userOneId = "4352523"; String userOneId = "4352523";
String userTwoId = "5552321"; String userTwoId = "5552321";
Transaction t = jedis.multi(); Transaction t = jedis.multi();
t.sadd(friendsPrefix + userOneId, userTwoId); t.sadd(friendsPrefix + userOneId, userTwoId);
t.sadd(friendsPrefix + userTwoId, userOneId); t.sadd(friendsPrefix + userTwoId, userOneId);
t.exec(); t.exec();
boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId); boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId);
Assert.assertTrue(exists); Assert.assertTrue(exists);
exists = jedis.sismember(friendsPrefix + userTwoId, userOneId); exists = jedis.sismember(friendsPrefix + userTwoId, userOneId);
Assert.assertTrue(exists); Assert.assertTrue(exists);
} }
@Test @Test
public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() { public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() {
String userOneId = "4352523"; String userOneId = "4352523";
String userTwoId = "4849888"; String userTwoId = "4849888";
Pipeline p = jedis.pipelined(); Pipeline p = jedis.pipelined();
p.sadd("searched#" + userOneId, "paris"); p.sadd("searched#" + userOneId, "paris");
p.zadd("ranking", 126, userOneId); p.zadd("ranking", 126, userOneId);
p.zadd("ranking", 325, userTwoId); p.zadd("ranking", 325, userTwoId);
Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris"); Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris");
Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1); Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1);
p.sync(); p.sync();
Assert.assertTrue(pipeExists.get()); Assert.assertTrue(pipeExists.get());
Assert.assertEquals(2, pipeRanking.get().size()); Assert.assertEquals(2, pipeRanking.get().size());
} }
@Test @Test
public void givenAPoolConfigurationThenCreateAJedisPool() { public void givenAPoolConfigurationThenCreateAJedisPool() {
final JedisPoolConfig poolConfig = buildPoolConfig(); final JedisPoolConfig poolConfig = buildPoolConfig();
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) { try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) {
// do simple operation to verify that the Jedis resource is working // do simple operation to verify that the Jedis resource is working
// properly // properly
String key = "key"; String key = "key";
String value = "value"; String value = "value";
jedis.set(key, value); jedis.set(key, value);
String value2 = jedis.get(key); String value2 = jedis.get(key);
Assert.assertEquals(value, value2); Assert.assertEquals(value, value2);
// flush Redis // flush Redis
jedis.flushAll(); jedis.flushAll();
} }
} }
private JedisPoolConfig buildPoolConfig() { private JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig(); final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128); poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128); poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16); poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(true); poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true); poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true); poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis()); poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis()); poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3); poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true); poolConfig.setBlockWhenExhausted(true);
return poolConfig; return poolConfig;
} }
} }