Search in sources :

Example 1 with LocalMap

use of io.vertx.core.shareddata.LocalMap in project vert.x by eclipse.

the class LocalSharedDataTest method testMapTypes.

@Test
public void testMapTypes() throws Exception {
    LocalMap map = sharedData.getLocalMap("foo");
    String key = "key";
    double d = new Random().nextDouble();
    map.put(key, d);
    assertEquals(d, map.get(key));
    float f = new Random().nextFloat();
    map.put(key, f);
    assertEquals(f, map.get(key));
    byte b = (byte) new Random().nextInt();
    map.put(key, b);
    assertEquals(b, map.get(key));
    short s = (short) new Random().nextInt();
    map.put(key, s);
    assertEquals(s, map.get(key));
    int i = new Random().nextInt();
    map.put(key, i);
    assertEquals(i, map.get(key));
    long l = new Random().nextLong();
    map.put(key, l);
    assertEquals(l, map.get(key));
    map.put(key, true);
    assertTrue((Boolean) map.get(key));
    map.put(key, false);
    assertFalse((Boolean) map.get(key));
    char c = (char) new Random().nextLong();
    map.put(key, c);
    assertEquals(c, map.get(key));
    Buffer buff = TestUtils.randomBuffer(100);
    map.put(key, buff);
    Buffer got1 = (Buffer) map.get(key);
    // Make sure it's copied
    assertTrue(got1 != buff);
    assertEquals(buff, map.get(key));
    Buffer got2 = (Buffer) map.get(key);
    // Should be copied each time
    assertTrue(got1 != got2);
    assertTrue(got2 != buff);
    assertEquals(buff, map.get(key));
    byte[] bytes = TestUtils.randomByteArray(100);
    map.put(key, bytes);
    byte[] bgot1 = (byte[]) map.get(key);
    assertTrue(bgot1 != bytes);
    assertTrue(TestUtils.byteArraysEqual(bytes, bgot1));
    byte[] bgot2 = (byte[]) map.get(key);
    assertTrue(bgot2 != bytes);
    assertTrue(bgot1 != bgot2);
    assertTrue(TestUtils.byteArraysEqual(bytes, bgot2));
    assertIllegalArgumentException(() -> map.put(key, new SomeOtherClass()));
    JsonObject obj = new JsonObject().put("foo", "bar");
    map.put("obj", obj);
    JsonObject other = (JsonObject) map.get("obj");
    assertEquals(obj, other);
    // Should be copied
    assertNotSame(obj, other);
    JsonArray arr = new JsonArray().add("foo");
    map.put("arr", arr);
    JsonArray otherArr = (JsonArray) map.get("arr");
    assertEquals(arr, otherArr);
    // Should be copied
    assertNotSame(arr, otherArr);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonObject(io.vertx.core.json.JsonObject) JsonArray(io.vertx.core.json.JsonArray) LocalMap(io.vertx.core.shareddata.LocalMap) Random(java.util.Random) Test(org.junit.Test)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 LocalMap (io.vertx.core.shareddata.LocalMap)1 Random (java.util.Random)1 Test (org.junit.Test)1