@Test public void recalculate() throws EditStateException, IllegalEditException { // union beed has no source $unionBeed.recalculate(); assertTrue($unionBeed.get().isEmpty()); // create source EditableSetBeed<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>> source = new EditableSetBeed<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($owner); // add source to mean beed $unionBeed.setSource(source); // {} // recalculate (source contains no elements) $unionBeed.recalculate(); assertTrue($unionBeed.get().isEmpty()); SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>> sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>(source); sourceEdit.addElementToAdd($setBeed1); sourceEdit.perform(); // recalculate (source contains {1,2}) $unionBeed.recalculate(); assertEquals($unionBeed.get().size(), 2); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); // recalculate (setBeed contains beed 1) $unionBeed.recalculate(); assertEquals($unionBeed.get().size(), 2); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); // add beed sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>(source); sourceEdit.addElementToAdd($setBeed2); sourceEdit.perform(); // recalculate (setBeed contains {1,2} and {3}) $unionBeed.recalculate(); assertEquals($unionBeed.get().size(), 3); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); // recalculate (setBeed contains {1,2} and {3}) assertEquals($unionBeed.get().size(), 3); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); // add beed sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>(source); sourceEdit.addElementToAdd($setBeed3); sourceEdit.perform(); // recalculate (setBeed contains {1,2} and {3} and {0,1,3}) $unionBeed.recalculate(); assertEquals($unionBeed.get().size(), 4); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); }
/** Source is null. */ @Test public void setSource1() throws EditStateException, IllegalEditException { // register listeners to the UnionBeed $unionBeed.addListener($listener3); assertNull($listener3.$event); // check setSource SetBeed<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>, ?> source = null; $unionBeed.setSource(source); assertEquals($unionBeed.getSource(), source); assertTrue($unionBeed.get().isEmpty()); // value has not changed, so the listeners are not notified assertNull($listener3.$event); }
@Test public void getSizeAndCardinality() throws EditStateException, IllegalEditException { // add a listener to the size beed IntegerBeed<ActualLongEvent> sizeBeed = $unionBeed.getSize(); sizeBeed.addListener($listener5); assertNull($listener5.$event); // check the size assertEquals($unionBeed.getSize().getLong(), 0L); assertEquals($unionBeed.getCardinality().getLong(), 0L); // add source $unionBeed.setSource($source); // {1,2} union {3} union {0,1,3} // check the size assertEquals($unionBeed.getSize().getLong(), 4L); assertEquals($unionBeed.getCardinality().getLong(), 4L); // check the listener assertNotNull($listener5.$event); assertEquals($listener5.$event.getSource(), sizeBeed); assertEquals($listener5.$event.getOldLong(), 0L); assertEquals($listener5.$event.getNewLong(), 4L); assertEquals($listener5.$event.getEdit(), null); // reset $listener5.reset(); assertNull($listener5.$event); // remove elements SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>> sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($source); sourceEdit.addElementToRemove($setBeed1); sourceEdit.perform(); // {3} union {0,1,3} // check the size assertEquals($unionBeed.getSize().getLong(), 3L); assertEquals($unionBeed.getCardinality().getLong(), 3L); // check the listener assertNotNull($listener5.$event); assertEquals($listener5.$event.getSource(), sizeBeed); assertEquals($listener5.$event.getOldLong(), 4L); assertEquals($listener5.$event.getNewLong(), 3L); assertEquals($listener5.$event.getEdit(), sourceEdit); // reset $listener5.reset(); assertNull($listener5.$event); // add elements EditableSetBeed<WellBeanBeed> setBeed = new EditableSetBeed<WellBeanBeed>($owner); addElement(setBeed, $well1); sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($source); sourceEdit.addElementToAdd(setBeed); sourceEdit.perform(); // {3} union {0,1,3} union {1} // check the size assertEquals($unionBeed.getSize().getLong(), 3L); assertEquals($unionBeed.getCardinality().getLong(), 3L); // check the listener (size has not changed, so no events should be sent) assertNull($listener5.$event); // reset $listener5.reset(); assertNull($listener5.$event); // add elements sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($source); sourceEdit.addElementToAdd($setBeed1); sourceEdit.perform(); // {3} union {0,1,3} union {1} union {1,2} // check the size assertEquals($unionBeed.getSize().getLong(), 4L); assertEquals($unionBeed.getCardinality().getLong(), 4L); // check the listener assertNotNull($listener5.$event); assertEquals($listener5.$event.getSource(), sizeBeed); assertEquals($listener5.$event.getOldLong(), 3L); assertEquals($listener5.$event.getNewLong(), 4L); assertEquals($listener5.$event.getEdit(), sourceEdit); }
@Test public void get() throws EditStateException, IllegalEditException { // add a source $unionBeed.setSource($source); // {1,2} union {3} union {0,1,3} Set<WellBeanBeed> result = $unionBeed.get(); assertEquals(result.size(), 4); assertTrue(result.contains($well0)); assertTrue(result.contains($well1)); assertTrue(result.contains($well2)); assertTrue(result.contains($well3)); Iterator<WellBeanBeed> iterator = result.iterator(); // we do not know the order assertTrue(iterator.hasNext()); WellBeanBeed next = iterator.next(); assertTrue(next == $well0 || next == $well1 || next == $well2 || next == $well3); assertTrue(iterator.hasNext()); next = iterator.next(); assertTrue(next == $well0 || next == $well1 || next == $well2 || next == $well3); assertTrue(iterator.hasNext()); next = iterator.next(); assertTrue(next == $well0 || next == $well1 || next == $well2 || next == $well3); assertTrue(iterator.hasNext()); next = iterator.next(); assertTrue(next == $well0 || next == $well1 || next == $well2 || next == $well3); assertFalse(iterator.hasNext()); try { iterator.next(); // should not be reached assertTrue(false); } catch (NoSuchElementException exc) { assertTrue(true); } // add another source $source = new EditableSetBeed<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($owner); addElement($source, $setBeed1); addElement($source, $setBeed2); $unionBeed.setSource($source); result = $unionBeed.get(); // {1,2} union {3} assertEquals(result.size(), 3); assertTrue(result.contains($well1)); assertTrue(result.contains($well2)); assertTrue(result.contains($well3)); iterator = result.iterator(); assertTrue(iterator.hasNext()); // we do not know the order next = iterator.next(); assertTrue(next == $well1 || next == $well2 || next == $well3); assertTrue(iterator.hasNext()); next = iterator.next(); assertTrue(next == $well1 || next == $well2 || next == $well3); assertTrue(iterator.hasNext()); next = iterator.next(); assertTrue(next == $well1 || next == $well2 || next == $well3); assertFalse(iterator.hasNext()); try { iterator.next(); // should not be reached assertTrue(false); } catch (NoSuchElementException exc) { assertTrue(true); } // source = null $source = null; $unionBeed.setSource($source); result = $unionBeed.get(); assertEquals(result.size(), 0); iterator = result.iterator(); assertFalse(iterator.hasNext()); try { iterator.next(); // should not be reached assertTrue(false); } catch (NoSuchElementException exc) { assertTrue(true); } }
/** Source is effective. */ @Test public void setSource2() throws EditStateException, IllegalEditException { // register listeners to the UnionBeed $unionBeed.addListener($listener3); assertNull($listener3.$event); // check setSource $unionBeed.setSource($source); // {1,2} union {3} union {0,1,3} assertEquals($unionBeed.getSource(), $source); assertEquals($unionBeed.get().size(), 4); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); // value has changed, so the listeners of the union beed are notified Set<WellBeanBeed> added = new HashSet<WellBeanBeed>(); added.add($well0); added.add($well1); added.add($well2); added.add($well3); Set<WellBeanBeed> removed = new HashSet<WellBeanBeed>(); assertNotNull($listener3.$event); assertEquals($listener3.$event.getSource(), $unionBeed); assertEquals($listener3.$event.getAddedElements(), added); assertEquals($listener3.$event.getRemovedElements(), removed); assertEquals($listener3.$event.getEdit(), null); // The UnionBeed is registered as listener of the source, so when // the source changes, the beed should be notified $listener3.reset(); assertNull($listener3.$event); WellBeanBeed well4 = createWellBeanBeed(4L); EditableSetBeed<WellBeanBeed> setBeed = createSetBeed(well4); SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>> sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($source); sourceEdit.addElementToAdd(setBeed); sourceEdit.perform(); // {1,2} union {3} union {0,1,3} union {4} removed = new HashSet<WellBeanBeed>(); added = new HashSet<WellBeanBeed>(); added.add(well4); assertNotNull($listener3.$event); assertEquals($listener3.$event.getSource(), $unionBeed); assertEquals($listener3.$event.getAddedElements(), added); assertEquals($listener3.$event.getRemovedElements(), removed); assertEquals($listener3.$event.getEdit(), sourceEdit); assertEquals($unionBeed.get().size(), 5); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); assertTrue($unionBeed.get().contains(well4)); // The UnionBeed is registered as listener of all beeds in the source, // so when one of them changes, the beed should be notified WellBeanBeed well5 = new WellBeanBeed(); SetEdit<WellBeanBeed> setEdit = new SetEdit<WellBeanBeed>(setBeed); setEdit.addElementToAdd(well5); setEdit.perform(); // {1,2} union {3} union {0,1,3} union {4,5} removed = new HashSet<WellBeanBeed>(); added = new HashSet<WellBeanBeed>(); added.add(well5); assertNotNull($listener3.$event); assertEquals($listener3.$event.getSource(), $unionBeed); assertEquals($listener3.$event.getAddedElements(), added); assertEquals($listener3.$event.getRemovedElements(), removed); assertEquals($listener3.$event.getEdit(), setEdit); assertEquals($unionBeed.get().size(), 6); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); assertTrue($unionBeed.get().contains(well4)); assertTrue($unionBeed.get().contains(well5)); // change the beed again $listener3.reset(); assertNull($listener3.$event); setEdit = new SetEdit<WellBeanBeed>(setBeed); setEdit.addElementToRemove(well4); setEdit.perform(); // {1,2} union {3} union {0,1,3} union {5} removed = new HashSet<WellBeanBeed>(); removed.add(well4); added = new HashSet<WellBeanBeed>(); assertNotNull($listener3.$event); assertEquals($listener3.$event.getSource(), $unionBeed); assertEquals($listener3.$event.getAddedElements(), added); assertEquals($listener3.$event.getRemovedElements(), removed); assertEquals($listener3.$event.getEdit(), setEdit); assertEquals($unionBeed.get().size(), 5); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well2)); assertTrue($unionBeed.get().contains($well3)); assertTrue($unionBeed.get().contains(well5)); // When a beed is removed from the source, the UnionBeed is removed as listener // of that beed. $listener3.reset(); assertNull($listener3.$event); sourceEdit = new SetEdit<SetBeed<WellBeanBeed, SetEvent<WellBeanBeed>>>($source); sourceEdit.addElementToRemove($setBeed1); sourceEdit.perform(); // {3} union {0,1,3} union {5} removed = new HashSet<WellBeanBeed>(); removed.add($well2); added = new HashSet<WellBeanBeed>(); assertNotNull($listener3.$event); assertEquals($listener3.$event.getSource(), $unionBeed); assertEquals($listener3.$event.getAddedElements(), added); assertEquals($listener3.$event.getRemovedElements(), removed); assertEquals($listener3.$event.getEdit(), sourceEdit); assertEquals($unionBeed.get().size(), 4); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well3)); assertTrue($unionBeed.get().contains(well5)); $listener3.reset(); assertNull($listener3.$event); // so, when the removed beed is edited, the union beed is NOT notified setEdit = new SetEdit<WellBeanBeed>($setBeed1); setEdit.addElementToAdd(well4); setEdit.perform(); // {3} union {0,1,3} union {5} assertNull($listener3.$event); // the UnionBeed is NOT notified // and the value of the union beed is correct assertEquals($unionBeed.get().size(), 4); assertTrue($unionBeed.get().contains($well0)); assertTrue($unionBeed.get().contains($well1)); assertTrue($unionBeed.get().contains($well3)); assertTrue($unionBeed.get().contains(well5)); }