/* * Copyright (c) 2015 Spotify AB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.spotify.dns; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import java.util.Set; import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class AbstractChangeNotifierTest { @Mock public ChangeNotifier.Listener listener; @Mock public ChangeNotifier.ChangeNotification changeNotification; @Rule public ExpectedException thrown = ExpectedException.none(); AbstractChangeNotifier sut; @Before public void setUp() { MockitoAnnotations.initMocks(this); sut = new AbstractChangeNotifier() { @Override public Set current() { return Sets.newHashSet("foo", "bar"); } @Override public void closeImplementation() { } }; } @Test public void shouldRegisterListener() { sut.setListener(listener, false); sut.fireRecordsUpdated(changeNotification); verify(listener).onChange(changeNotification); } @Test @SuppressWarnings("unchecked") public void shouldNotFireImmediatelyIfFalse() { sut.setListener(listener, false); verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class)); } @Test @SuppressWarnings("unchecked") public void shouldFireImmediatelyIfTrue() { sut.setListener(listener, true); final ArgumentCaptor captor = ArgumentCaptor.forClass(ChangeNotifier.ChangeNotification.class); verify(listener).onChange(captor.capture()); final ChangeNotifier.ChangeNotification notification = captor.getValue(); assertThat(notification.previous().size(), is(0)); assertThat(notification.current().size(), is(2)); assertThat(notification.current(), containsInAnyOrder("foo", "bar")); } @Test @SuppressWarnings("unchecked") public void shouldNotFireAfterClose() { sut.setListener(listener, false); sut.close(); sut.fireRecordsUpdated(changeNotification); verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class)); } @Test @SuppressWarnings("unchecked") public void shouldNotAllowMultipleListeners() { sut.setListener(listener, false); thrown.expect(IllegalStateException.class); sut.setListener(mock(ChangeNotifier.Listener.class), false); } @Test @SuppressWarnings("unchecked") public void shouldIgnoreListenerExceptions() { doThrow(new RuntimeException("stupid listener")) .when(listener) .onChange(any(ChangeNotifier.ChangeNotification.class)); sut.setListener(listener, false); sut.fireRecordsUpdated(changeNotification); } }