/*
 * Copyright (C) 2013 Sebastian Kaspari
 *
 * 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.androidzeitgeist.ani.discovery;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.junit.Test;

import com.androidzeitgeist.ani.internal.AndroidNetworkIntents;

/**
 * Unit tests for the {@link DiscoveryThread} class.
 */
public class DiscoveryThreadTest {
    /**
     * Running the {@link DiscoveryThread} will call {@link DiscoveryListener#onDiscoveryStarted()}
     * and {@link DiscoveryListener#onDiscoveryStopped()}.
     */
    @Test
    public void testListenerWillBeNotifiedAboutStartAndEnd() throws Exception{
        DiscoveryListener listener = mock(DiscoveryListener.class);

        DiscoveryThread thread = spy(new DiscoveryThread(
            AndroidNetworkIntents.DEFAULT_MULTICAST_ADDRESS,
            AndroidNetworkIntents.DEFAULT_PORT,
            listener
        ));

        doNothing().when(thread).receiveIntents();

        thread.run();

        verify(listener).onDiscoveryStarted();
        verify(listener).onDiscoveryStopped();
    }
}

@@ -690,16 +690,60 @@
 othing;%0A
+import static org.mockito.Mockito.doReturn;%0A
 import s
@@ -852,24 +852,58 @@
 to.verify;%0A%0A
+import java.net.MulticastSocket;%0A%0A
 import org.j
@@ -1783,11 +1783,663 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Verify that close() will be called on the created socket.%0A     */%0A    @Test%0A    public void testSocketWillBeClosed() throws Exception %7B%0A        DiscoveryListener listener = mock(DiscoveryListener.class);%0A%0A        DiscoveryThread thread = spy(new DiscoveryThread(%0A            AndroidNetworkIntents.DEFAULT_MULTICAST_ADDRESS,%0A            AndroidNetworkIntents.DEFAULT_PORT,%0A            listener%0A        ));%0A%0A        MulticastSocket socket = mock(MulticastSocket.class);%0A        doReturn(socket).when(thread).createSocket();%0A%0A        doNothing().when(thread).receiveIntents();%0A%0A        thread.run();%0A%0A        verify(socket).close();%0A    %7D
 %0A%7D%0A

Add another test case.