|
| 1 | +package com.couchbase.lite; |
| 2 | + |
| 3 | +import com.couchbase.litecore.LiteCoreException; |
| 4 | +import com.couchbase.litecore.fleece.AllocSlice; |
| 5 | +import com.couchbase.litecore.fleece.Encoder; |
| 6 | +import com.couchbase.litecore.fleece.FLValue; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +public class JavaTest extends BaseTest { |
| 19 | + |
| 20 | + // https://github.com/couchbase/couchbase-lite-android/issues/1453 |
| 21 | + @Test |
| 22 | + public void testFLEncode() throws LiteCoreException { |
| 23 | + testRoundTrip(42L); |
| 24 | + testRoundTrip(Long.MIN_VALUE); |
| 25 | + testRoundTrip("Fleece"); |
| 26 | + Map<String, Object> map = new HashMap<>(); |
| 27 | + map.put("foo", "bar"); |
| 28 | + testRoundTrip(map); |
| 29 | + testRoundTrip(Arrays.asList((Object) "foo", "bar")); |
| 30 | + testRoundTrip(true); |
| 31 | + testRoundTrip(3.14F); |
| 32 | + testRoundTrip(Math.PI); |
| 33 | + } |
| 34 | + |
| 35 | + private void testRoundTrip(Object item) throws LiteCoreException { |
| 36 | + Encoder encoder = new Encoder(); |
| 37 | + assertNotNull(encoder); |
| 38 | + try { |
| 39 | + assertTrue(encoder.writeObject(item)); |
| 40 | + AllocSlice slice = encoder.finish(); |
| 41 | + assertNotNull(slice); |
| 42 | + FLValue flValue = FLValue.fromData(slice); |
| 43 | + assertNotNull(flValue); |
| 44 | + Object obj = FLValue.toObject(flValue); |
| 45 | + assertEquals(item, obj); |
| 46 | + } finally { |
| 47 | + encoder.release(); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments