/** 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);
 }
 /** 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));
 }