|
21 | 21 |
|
22 | 22 | import static io.lettuce.TestTags.INTEGRATION_TEST; |
23 | 23 | import static io.lettuce.core.SetArgs.Builder.*; |
24 | | -import static io.lettuce.core.StringMatchResult.*; |
25 | | -import static org.assertj.core.api.Assertions.*; |
| 24 | +import static io.lettuce.core.StringMatchResult.Position; |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
26 | 27 |
|
| 28 | +import java.lang.reflect.Proxy; |
27 | 29 | import java.time.Duration; |
28 | 30 | import java.time.Instant; |
29 | 31 | import java.util.LinkedHashMap; |
|
32 | 34 |
|
33 | 35 | import javax.inject.Inject; |
34 | 36 |
|
| 37 | +import org.junit.jupiter.api.Assumptions; |
35 | 38 | import org.junit.jupiter.api.BeforeEach; |
36 | 39 | import org.junit.jupiter.api.Tag; |
37 | 40 | import org.junit.jupiter.api.Test; |
38 | 41 | import org.junit.jupiter.api.TestInstance; |
39 | 42 | import org.junit.jupiter.api.extension.ExtendWith; |
40 | 43 |
|
41 | | -import io.lettuce.core.GetExArgs; |
42 | | -import io.lettuce.core.KeyValue; |
43 | | -import io.lettuce.core.RedisException; |
44 | | -import io.lettuce.core.SetArgs; |
45 | | -import io.lettuce.core.StrAlgoArgs; |
46 | | -import io.lettuce.core.StringMatchResult; |
47 | | -import io.lettuce.core.TestSupport; |
| 44 | +import io.lettuce.core.*; |
| 45 | +import io.lettuce.core.api.StatefulConnection; |
| 46 | +import io.lettuce.core.api.StatefulRedisConnection; |
48 | 47 | import io.lettuce.core.api.sync.RedisCommands; |
| 48 | +import io.lettuce.core.dynamic.Commands; |
| 49 | +import io.lettuce.core.dynamic.RedisCommandFactory; |
| 50 | +import io.lettuce.core.dynamic.annotation.Command; |
| 51 | +import io.lettuce.core.dynamic.annotation.Param; |
49 | 52 | import io.lettuce.test.KeyValueStreamingAdapter; |
50 | 53 | import io.lettuce.test.LettuceExtension; |
51 | 54 | import io.lettuce.test.condition.EnabledOnCommand; |
@@ -376,4 +379,117 @@ void strAlgoWithIdx() { |
376 | 379 | assertThat(matchResult.getLen()).isEqualTo(6); |
377 | 380 | } |
378 | 381 |
|
| 382 | + @Test |
| 383 | + @EnabledOnCommand("LCS") |
| 384 | + void lcs() { |
| 385 | + redis.set("key1", "ohmytext"); |
| 386 | + redis.set("key2", "mynewtext"); |
| 387 | + |
| 388 | + // LCS key1 key2 |
| 389 | + CustomStringCommands commands = CustomStringCommands.instance(getConnection()); |
| 390 | + StringMatchResult matchResult = commands.lcs("key1", "key2"); |
| 391 | + assertThat(matchResult.getMatchString()).isEqualTo("mytext"); |
| 392 | + |
| 393 | + // LCS a b IDX MINMATCHLEN 4 WITHMATCHLEN |
| 394 | + // Keys don't exist. |
| 395 | + matchResult = commands.lcsMinMatchLenWithMatchLen("a", "b", 4); |
| 396 | + assertThat(matchResult.getMatchString()).isNullOrEmpty(); |
| 397 | + assertThat(matchResult.getLen()).isEqualTo(0); |
| 398 | + } |
| 399 | + |
| 400 | + @Test |
| 401 | + @EnabledOnCommand("LCS") |
| 402 | + void lcsUsingKeys() { |
| 403 | + |
| 404 | + redis.set("key1{k}", "ohmytext"); |
| 405 | + redis.set("key2{k}", "mynewtext"); |
| 406 | + |
| 407 | + CustomStringCommands commands = CustomStringCommands.instance(getConnection()); |
| 408 | + |
| 409 | + StringMatchResult matchResult = commands.lcs("key1{k}", "key2{k}"); |
| 410 | + assertThat(matchResult.getMatchString()).isEqualTo("mytext"); |
| 411 | + |
| 412 | + // STRALGO LCS STRINGS a b |
| 413 | + matchResult = commands.lcsMinMatchLenWithMatchLen("a", "b", 4); |
| 414 | + assertThat(matchResult.getMatchString()).isNullOrEmpty(); |
| 415 | + assertThat(matchResult.getLen()).isEqualTo(0); |
| 416 | + } |
| 417 | + |
| 418 | + @Test |
| 419 | + @EnabledOnCommand("LCS") |
| 420 | + void lcsJustLen() { |
| 421 | + redis.set("one", "ohmytext"); |
| 422 | + redis.set("two", "mynewtext"); |
| 423 | + |
| 424 | + CustomStringCommands commands = CustomStringCommands.instance(getConnection()); |
| 425 | + |
| 426 | + StringMatchResult matchResult = commands.lcsLen("one", "two"); |
| 427 | + |
| 428 | + assertThat(matchResult.getLen()).isEqualTo(6); |
| 429 | + } |
| 430 | + |
| 431 | + @Test |
| 432 | + @EnabledOnCommand("LCS") |
| 433 | + void lcsWithMinMatchLen() { |
| 434 | + redis.set("key1", "ohmytext"); |
| 435 | + redis.set("key2", "mynewtext"); |
| 436 | + |
| 437 | + CustomStringCommands commands = CustomStringCommands.instance(getConnection()); |
| 438 | + |
| 439 | + StringMatchResult matchResult = commands.lcsMinMatchLen("key1", "key2", 4); |
| 440 | + |
| 441 | + assertThat(matchResult.getMatchString()).isEqualTo("mytext"); |
| 442 | + } |
| 443 | + |
| 444 | + @Test |
| 445 | + @EnabledOnCommand("LCS") |
| 446 | + void lcsMinMatchLenIdxMatchLen() { |
| 447 | + redis.set("key1", "ohmytext"); |
| 448 | + redis.set("key2", "mynewtext"); |
| 449 | + |
| 450 | + CustomStringCommands commands = CustomStringCommands.instance(getConnection()); |
| 451 | + |
| 452 | + // LCS key1 key2 IDX MINMATCHLEN 4 WITHMATCHLEN |
| 453 | + StringMatchResult matchResult = commands.lcsMinMatchLenWithMatchLen("key1", "key2", 4); |
| 454 | + |
| 455 | + assertThat(matchResult.getMatches()).hasSize(1); |
| 456 | + assertThat(matchResult.getMatches().get(0).getMatchLen()).isEqualTo(4); |
| 457 | + |
| 458 | + Position a = matchResult.getMatches().get(0).getA(); |
| 459 | + Position b = matchResult.getMatches().get(0).getB(); |
| 460 | + |
| 461 | + assertThat(a.getStart()).isEqualTo(4); |
| 462 | + assertThat(a.getEnd()).isEqualTo(7); |
| 463 | + assertThat(b.getStart()).isEqualTo(5); |
| 464 | + assertThat(b.getEnd()).isEqualTo(8); |
| 465 | + assertThat(matchResult.getLen()).isEqualTo(6); |
| 466 | + } |
| 467 | + |
| 468 | + protected StatefulConnection<String, String> getConnection() { |
| 469 | + StatefulRedisConnection<String, String> src = redis.getStatefulConnection(); |
| 470 | + Assumptions.assumeFalse(Proxy.isProxyClass(src.getClass()), "Redis connection is proxy, skipping."); |
| 471 | + return src; |
| 472 | + } |
| 473 | + |
| 474 | + private interface CustomStringCommands extends Commands { |
| 475 | + |
| 476 | + @Command("LCS :k1 :k2") |
| 477 | + StringMatchResult lcs(@Param("k1") String k1, @Param("k2") String k2); |
| 478 | + |
| 479 | + @Command("LCS :k1 :k2 LEN") |
| 480 | + StringMatchResult lcsLen(@Param("k1") String k1, @Param("k2") String k2); |
| 481 | + |
| 482 | + @Command("LCS :k1 :k2 MINMATCHLEN :mml") |
| 483 | + StringMatchResult lcsMinMatchLen(@Param("k1") String k1, @Param("k2") String k2, @Param("mml") int mml); |
| 484 | + |
| 485 | + @Command("LCS :k1 :k2 IDX MINMATCHLEN :mml WITHMATCHLEN") |
| 486 | + StringMatchResult lcsMinMatchLenWithMatchLen(@Param("k1") String k1, @Param("k2") String k2, @Param("mml") int mml); |
| 487 | + |
| 488 | + static CustomStringCommands instance(StatefulConnection<String, String> conn) { |
| 489 | + RedisCommandFactory factory = new RedisCommandFactory(conn); |
| 490 | + return factory.getCommands(CustomStringCommands.class); |
| 491 | + } |
| 492 | + |
| 493 | + } |
| 494 | + |
379 | 495 | } |
0 commit comments