/*
 * Copyright(C)  2017 大前良介(OHMAE Ryosuke)
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/MIT
 */

package net.mm2d.upnp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(JUnit4.class)
public class DeviceHolderTest {
    private static final String UDN = "uuid:01234567-89ab-cdef-0123-456789abcdef";

    @Test(timeout = 1000L)
    public void start_shutdown_デッドロックしない() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        holder.start();
        Thread.sleep(1);
        holder.shutdownRequest();
    }

    @Test
    public void add() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        holder.add(device);

        assertThat(holder.get(UDN), sameInstance(device));
    }

    @Test
    public void remove() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        holder.add(device);

        assertThat(holder.get(UDN), sameInstance(device));

        holder.remove(device);
        assertThat(holder.get(UDN), is(nullValue()));
    }

    @Test
    public void clear() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        holder.add(device);

        assertThat(holder.get(UDN), sameInstance(device));

        holder.clear();
        assertThat(holder.getDeviceList(), hasSize(0));
    }

    @Test
    public void getDeviceList() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        holder.add(device);

        assertThat(holder.getDeviceList(), hasSize(1));
        assertThat(holder.getDeviceList(), hasItem(device));
    }

    @Test
    public void size() throws Exception {
        final DeviceHolder holder = new DeviceHolder(mock(ControlPoint.class));
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        holder.add(device);

        assertThat(holder.size(), is(1));
    }

    @Test(timeout = 20000L)
    public void expireDevice_時間経過後に削除される() throws Exception {
        final ControlPoint cp = mock(ControlPoint.class);
        final DeviceHolder holder = new DeviceHolder(cp);
        final Device device = mock(Device.class);
        doReturn(UDN).when(device).getUdn();
        doReturn(System.currentTimeMillis() + 100L).when(device).getExpireTime();

        holder.start();
        Thread.sleep(1);
        holder.add(device);

        assertThat(holder.size(), is(1));

        Thread.sleep(11000L); // 内部で10秒のマージンを持っているため十分な時間を開ける

        assertThat(holder.size(), is(0));
        verify(cp).lostDevice(device);
        holder.shutdownRequest();
    }
}

@@ -748,24 +748,58 @@
 nRequest();%0A
+        holder.shutdownRequest();%0A
     %7D%0A%0A    @
@@ -2847,32 +2847,33 @@
 al Device device
+1
  = mock(Device.c
@@ -2904,32 +2904,33 @@
 UDN).when(device
+1
 ).getUdn();%0A    
@@ -2937,63 +2937,251 @@
     
-doReturn(System.currentTimeMillis() + 100L).when(device
+final Device device2 = mock(Device.class);%0A        doReturn(UDN + %222%22).when(device2).getUdn();%0A%0A        doReturn(System.currentTimeMillis() + 100L).when(device1).getExpireTime();%0A        doReturn(System.currentTimeMillis() + 200L).when(device2
 ).ge
@@ -3262,32 +3262,62 @@
 older.add(device
+1);%0A        holder.add(device2
 );%0A%0A        asse
@@ -3337,25 +3337,25 @@
 .size(), is(
-1
+2
 ));%0A%0A       
@@ -3480,32 +3480,33 @@
 ostDevice(device
+1
 );%0A        holde

add DeviceHolderTest