package org.wrml.util;

import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Map;

import static org.mockito.Mockito.*;

public class DelegatingObservableMapTest {

    @Test
    public void shouldFireClearedEvent() {
        ObservableMap<String, Integer> observableMap = new DelegatingObservableMap<String, Integer>(mock(Map.class));
        MapEventListener<String, Integer> listener = mock(MapEventListener.class);

        observableMap.addEventListener(listener);

        observableMap.clear();

        verify(listener).clearing(Matchers.<MapEvent<String, Integer>>any());
        verify(listener).cleared(Matchers.<MapEvent<String, Integer>>any());
        verifyNoMoreInteractions(listener);
    }

    public void shouldCancelPut() {
        final Map mockMap = mock(Map.class);
        ObservableMap<String, Integer> observableMap = new DelegatingObservableMap<String, Integer>(mockMap);
        MapEventListener<String, Integer> listener = mock(MapEventListener.class);

        // Cancel the event during a call to listener.updatingEntry(...)
        doAnswer(new Answer() {
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                MapEvent<String, Integer> event = (MapEvent<String, Integer>) invocationOnMock.getArguments()[0];
                event.setCancelled(true);
                return null;
            }
        }).when(listener).updatingEntry(Matchers.<MapEvent<String, Integer>>any());

        observableMap.addEventListener(listener);

        observableMap.put("test", 123);

        verify(listener).updatingEntry(Matchers.<MapEvent<String, Integer>>any());
        verifyNoMoreInteractions(listener);

        verifyZeroInteractions(mockMap);


    }
}

@@ -803,16 +803,26 @@
 %0A    %7D%0A%0A
+    @Test%0A
     publ
@@ -867,20 +867,23 @@
 nal Map 
-mock
+backing
 Map = mo
@@ -997,20 +997,23 @@
 nteger%3E(
-mock
+backing
 Map);%0A  
@@ -1808,41 +1808,55 @@
 rify
-ZeroInteractions(mockMap);%0A%0A
+(backingMap, times(0)).put(any(), any());
 %0A    %7D%0A
+%0A
 %7D%0A

Fix unit test to actually run.