示例#1
0
  @Test
  public void EntrySet() throws Exception {
    final Map<String, String> base = new LinkedHashMap<String, String>();
    final ObservableMap<String, String> observable = new ObservableMap<String, String>(base);

    base.put("a", "1");
    base.put("b", "2");
    base.put("c", "3");

    final EventLog log = new EventLog();
    observable.attach(log);

    final Iterator<Map.Entry<String, String>> itr = observable.entrySet().iterator();

    assertTrue(itr.hasNext());

    final Map.Entry<String, String> a = itr.next();

    assertEquals("a", a.getKey());
    assertEquals("1", a.getValue());

    assertTrue(itr.hasNext());

    final Map.Entry<String, String> b = itr.next();

    assertEquals("b", b.getKey());
    assertEquals("2", b.getValue());

    itr.remove();

    assertEquals(MapFactory.start().add("a", "1").add("c", "3").result(), base);
    assertEquals(1, log.size());

    assertTrue(itr.hasNext());

    final Map.Entry<String, String> c = itr.next();

    assertEquals("c", c.getKey());
    assertEquals("3", c.getValue());

    assertFalse(itr.hasNext());
  }
示例#2
0
  @Test
  public void PutAll() throws Exception {
    final Map<String, String> base = new HashMap<String, String>();
    final ObservableMap<String, String> observable = new ObservableMap<String, String>(base);

    base.put("a", "1");
    base.put("b", "2");

    final EventLog log = new EventLog();
    observable.attach(log);

    observable.putAll(MapFactory.<String, String>start().add("c", "3").add("d", "4").result());

    assertEquals(
        MapFactory.start().add("a", "1").add("b", "2").add("c", "3").add("d", "4").result(), base);
    assertEquals(1, log.size());

    log.clear();
    observable.putAll(MapFactory.<String, String>start().add("a", "11").add("b", "22").result());

    assertEquals(
        MapFactory.start().add("a", "11").add("b", "22").add("c", "3").add("d", "4").result(),
        base);
    assertEquals(1, log.size());

    log.clear();
    observable.putAll(MapFactory.<String, String>start().add("c", "3").add("d", "4").result());

    assertEquals(
        MapFactory.start().add("a", "11").add("b", "22").add("c", "3").add("d", "4").result(),
        base);
    assertEquals(0, log.size());
  }
示例#3
0
  @Test
  public void Clear() throws Exception {
    final Map<String, String> base = new HashMap<String, String>();
    final ObservableMap<String, String> observable = new ObservableMap<String, String>(base);

    base.put("a", "1");
    base.put("b", "2");
    base.put("c", "3");

    final EventLog log = new EventLog();
    observable.attach(log);

    observable.clear();

    assertTrue(base.isEmpty());
    assertEquals(1, log.size());

    log.clear();
    observable.clear();

    assertTrue(base.isEmpty());
    assertEquals(0, log.size());
  }