package com.marcospassos.phpserializer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;

/**
 * @author Marcos Passos
 * @since 1.0
 */
public class AdapterRegistryTest
{
    @Test
    public void getAdapterReturnsMostSpecializedAdapter() throws Exception
    {
        TypeAdapter numberAdapter = mock(TypeAdapter.class);
        TypeAdapter integerAdapter = mock(TypeAdapter.class);
        TypeAdapter doubleAdapter = mock(TypeAdapter.class);

        Map<Class, TypeAdapter> adapters = new LinkedHashMap<>();
        adapters.put(Double.class, doubleAdapter);
        adapters.put(Number.class, numberAdapter);
        adapters.put(Integer.class, integerAdapter);

        AdapterRegistry registry = new AdapterRegistry(adapters);

        assertSame(doubleAdapter, registry.getAdapter(Double.class));
        assertSame(integerAdapter, registry.getAdapter(Integer.class));
        assertSame(numberAdapter, registry.getAdapter(Float.class));
    }

    @Test(expected = IllegalArgumentException.class)
    public void getAdapterFailsIfNoAdapterIsFound() throws Exception
    {
        AdapterRegistry registry = new AdapterRegistry();
        registry.getAdapter(Integer.class);
    }

    @Test
    public void getAdaptersReturnsMapOfAdapters() throws Exception
    {
        TypeAdapter adapterInteger = mock(TypeAdapter.class);
        TypeAdapter adapterFloat = mock(TypeAdapter.class);

        AdapterRegistry registry = new AdapterRegistry();
        registry.registerAdapter(Integer.class, adapterInteger);
        registry.registerAdapter(Float.class, adapterFloat);

        Map<Class, TypeAdapter> adapters = registry.getAdapters();

        assertEquals(2, adapters.size());
        assertTrue(adapters.containsKey(Integer.class));
        assertTrue(adapters.containsValue(adapterInteger));
        assertTrue(adapters.containsKey(Float.class));
        assertTrue(adapters.containsValue(adapterFloat));
    }
}
@@ -34,16 +34,220 @@
 lizer;%0A%0A
+import java.util.Collection;%0Aimport java.util.HashSet;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.TreeSet;%0Aimport org.junit.Test;%0A%0A
 import s
@@ -414,86 +414,8 @@
 k;%0A%0A
-import java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport org.junit.Test;%0A%0A
 /**%0A
@@ -771,167 +771,509 @@
 s);%0A
-%0A        Map%3CClass, TypeAdapter%3E adapters = new LinkedHashMap%3C%3E();%0A        adapters.put(Double.class, doubleAdapter);%0A        adapters.put(Number.class, number
+        TypeAdapter collectionAdapter = mock(TypeAdapter.class);%0A        TypeAdapter setAdapter = mock(TypeAdapter.class);%0A        TypeAdapter hashSetAdapter = mock(TypeAdapter.class);%0A%0A        Map%3CClass, TypeAdapter%3E adapters = new LinkedHashMap%3C%3E();%0A        adapters.put(Double.class, doubleAdapter);%0A        adapters.put(Set.class, setAdapter);%0A        adapters.put(Collection.class, collectionAdapter);%0A        adapters.put(Number.class, numberAdapter);%0A        adapters.put(HashSet.class, hashSet
 Adap
@@ -1606,24 +1606,236 @@
 at.class));%0A
+        assertSame(hashSetAdapter, registry.getAdapter(HashSet.class));%0A        assertSame(setAdapter, registry.getAdapter(TreeSet.class));%0A        assertSame(collectionAdapter, registry.getAdapter(List.class));%0A
     %7D%0A%0A    @

Add tests to cover interface registering