|
| 1 | +package net.spy.memcached.collection.transcoder; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.concurrent.ExecutionException; |
| 5 | + |
| 6 | +import net.spy.memcached.collection.BaseIntegrationTest; |
| 7 | +import net.spy.memcached.collection.CollectionAttributes; |
| 8 | +import net.spy.memcached.transcoders.CollectionTranscoder; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +class CollectionTranscoderTest extends BaseIntegrationTest { |
| 18 | + |
| 19 | + private static final String KEY = "CollectionTranscoderTest"; |
| 20 | + |
| 21 | + private final CollectionTranscoder transcoder = new CollectionTranscoder.Builder() |
| 22 | + .disableOptimization() |
| 23 | + .build(); |
| 24 | + |
| 25 | + @AfterEach |
| 26 | + @Override |
| 27 | + protected void tearDown() throws Exception { |
| 28 | + mc.delete(KEY); |
| 29 | + super.tearDown(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void decodeIntegerAsString() throws ExecutionException, InterruptedException { |
| 34 | + // given |
| 35 | + Boolean b1 = mc.asyncMopInsert(KEY, "mkey1", "value", new CollectionAttributes()).get(); |
| 36 | + Boolean b2 = mc.asyncMopInsert(KEY, "mkey2", 35, new CollectionAttributes()).get(); |
| 37 | + assertTrue(b1); |
| 38 | + assertTrue(b2); |
| 39 | + |
| 40 | + // when |
| 41 | + Map<String, Object> map1 = mc.asyncMopGet(KEY, "mkey1", false, false).get(); |
| 42 | + Map<String, Object> map2 = mc.asyncMopGet(KEY, "mkey2", false, false).get(); |
| 43 | + |
| 44 | + // then |
| 45 | + assertEquals("value", map1.get("mkey1")); |
| 46 | + assertEquals("#", map2.get("mkey2")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void failToDecodeString() throws ExecutionException, InterruptedException { |
| 51 | + // given |
| 52 | + Boolean b2 = mc.asyncMopInsert(KEY, "mkey3", 35, new CollectionAttributes()) |
| 53 | + .get(); |
| 54 | + Boolean b1 = mc.asyncMopInsert(KEY, "mkey4", "value", new CollectionAttributes()) |
| 55 | + .get(); |
| 56 | + assertTrue(b1); |
| 57 | + assertTrue(b2); |
| 58 | + |
| 59 | + // when & then |
| 60 | + Map<String, Object> map1 = mc.asyncMopGet(KEY, "mkey3", false, false).get(); |
| 61 | + assertEquals(35, map1.get("mkey3")); |
| 62 | + assertThrows(AssertionError.class, () -> mc.asyncMopGet(KEY, "mkey4", false, false).get()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void decodeStringAndInteger() throws ExecutionException, InterruptedException { |
| 67 | + // given |
| 68 | + Boolean b1 = mc.asyncMopInsert(KEY, "mkey1", "value", new CollectionAttributes(), transcoder) |
| 69 | + .get(); |
| 70 | + Boolean b2 = mc.asyncMopInsert(KEY, "mkey2", 35, new CollectionAttributes(), transcoder) |
| 71 | + .get(); |
| 72 | + assertTrue(b1); |
| 73 | + assertTrue(b2); |
| 74 | + |
| 75 | + // when |
| 76 | + Map<String, Object> map1 = mc.asyncMopGet(KEY, "mkey1", false, false, transcoder).get(); |
| 77 | + Map<String, Object> map2 = mc.asyncMopGet(KEY, "mkey2", false, false, transcoder).get(); |
| 78 | + |
| 79 | + // then |
| 80 | + assertEquals("value", map1.get("mkey1")); |
| 81 | + assertEquals(35, map2.get("mkey2")); |
| 82 | + } |
| 83 | +} |
0 commit comments