ProxyContext extends AbstractContext { @Override public <T> void addContextListener(Class<T> type, ContextListener<T> listener) { super.addContextListener(type, listener); contexts.forEach(context -> context.addContextListener(type, listener)); } ProxyContext(); @Override T find(Class<T> type); @Override Collection<T> findAll(Class<T> type); @Override void addContextListener(Class<T> type, ContextListener<T> listener); @Override void removeContextListener(Class<T> type, ContextListener<T> listener); void addContext(Context context); void addContexts(Collection<Context> contexts); void removeContext(Context context); void setContexts(Context... contexts); void setContexts(List<? extends Context> contexts); boolean isEmpty();  }
@Test public void testAddContextListener() { System.out.println("addContextListener"); TestContextListener<Foo> fooContextListener = new TestContextListener<>(); context.addContextListener(Foo.class, fooContextListener); TestContextListener<MyCustomFoo> myCustomFooContextListener = new TestContextListener<>(); TestContextListener<AbstractFoo> abstractFooContextListener = new TestContextListener<>(); TestContextListener<Object> objectContextListener = new TestContextListener<>(); TestContextListener<Date> dateContextListener = new TestContextListener<>(); SimpleContextContent content1 = new SimpleContextContent(); SimpleContext context1 = new SimpleContext(content1); context.addContext(context1); context.addContextListener(MyCustomFoo.class, myCustomFooContextListener); context.addContextListener(AbstractFoo.class, abstractFooContextListener); context.addContextListener(Object.class, objectContextListener); context.addContextListener(Date.class, dateContextListener); assertFalse(fooContextListener.isContextChanged()); assertFalse(myCustomFooContextListener.isContextChanged()); assertFalse(abstractFooContextListener.isContextChanged()); assertFalse(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); Foo foo1 = new MyCustomFoo(); content1.add(foo1); assertTrue(fooContextListener.isContextChanged()); assertTrue(myCustomFooContextListener.isContextChanged()); assertTrue(abstractFooContextListener.isContextChanged()); assertTrue(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); fooContextListener.reset(); myCustomFooContextListener.reset(); abstractFooContextListener.reset(); objectContextListener.reset(); assertFalse(fooContextListener.isContextChanged()); assertFalse(myCustomFooContextListener.isContextChanged()); assertFalse(abstractFooContextListener.isContextChanged()); assertFalse(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); context.removeContext(context1); assertTrue(fooContextListener.isContextChanged()); assertTrue(myCustomFooContextListener.isContextChanged()); assertTrue(abstractFooContextListener.isContextChanged()); assertTrue(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); fooContextListener.reset(); myCustomFooContextListener.reset(); abstractFooContextListener.reset(); objectContextListener.reset(); assertFalse(fooContextListener.isContextChanged()); assertFalse(myCustomFooContextListener.isContextChanged()); assertFalse(abstractFooContextListener.isContextChanged()); assertFalse(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); context.addContext(context1); assertTrue(fooContextListener.isContextChanged()); assertTrue(myCustomFooContextListener.isContextChanged()); assertTrue(abstractFooContextListener.isContextChanged()); assertTrue(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); fooContextListener.reset(); myCustomFooContextListener.reset(); abstractFooContextListener.reset(); objectContextListener.reset(); assertFalse(fooContextListener.isContextChanged()); assertFalse(myCustomFooContextListener.isContextChanged()); assertFalse(abstractFooContextListener.isContextChanged()); assertFalse(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); content1.remove(foo1); assertTrue(fooContextListener.isContextChanged()); assertTrue(myCustomFooContextListener.isContextChanged()); assertTrue(abstractFooContextListener.isContextChanged()); assertTrue(objectContextListener.isContextChanged()); assertFalse(dateContextListener.isContextChanged()); }