package com.fasterxml.jackson.databind.module;

import java.util.*;


import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class TestAbstractTypes extends BaseMapTest
{
    /*
    /**********************************************************
    /* Helper classes; simple beans and their handlers
    /**********************************************************
     */

    static class MyString implements CharSequence
    {
        protected String value;
        
        public MyString(String s) { value = s; }

        @Override
        public char charAt(int index) {
            return value.charAt(index);
        }

        @Override
        public int length() {
            return value.length();
        }

        @Override
        public CharSequence subSequence(int arg0, int arg1) { return this; }
    }
    
    /*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */

    public void testCollectionDefaulting() throws Exception
    {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
        // let's ensure we get hierarchic mapping
        mod.addAbstractTypeMapping(Collection.class, List.class);
        mod.addAbstractTypeMapping(List.class, LinkedList.class);
        mapper.registerModule(mod);
        Collection<?> result = mapper.readValue("[]", Collection.class);
        assertEquals(LinkedList.class, result.getClass());
    }

    public void testMapDefaulting() throws Exception
    {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
        // default is HashMap, so:
        mod.addAbstractTypeMapping(Map.class, TreeMap.class);
        mapper.registerModule(mod);
        Map<?,?> result = mapper.readValue("{}", Map.class);
        assertEquals(TreeMap.class, result.getClass());
    }
    
    public void testInterfaceDefaulting() throws Exception
    {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
        // let's ensure we get hierarchic mapping
        mod.addAbstractTypeMapping(CharSequence.class, MyString.class);
        mapper.registerModule(mod);
        Object result = mapper.readValue(quote("abc"), CharSequence.class);
        assertEquals(MyString.class, result.getClass());
        assertEquals("abc", ((MyString) result).value);
    }
}

@@ -1716,32 +1716,37 @@
 estMapDefaulting
+Basic
 () throws Except
@@ -1741,32 +1741,32 @@
 hrows Exception%0A
-
     %7B%0A        Ob
@@ -2136,28 +2136,703 @@
 s());%0A    %7D%0A
+%0A
     
+/* 11-Feb-2015, tatu: too tricky to fix in 2.5.x; move to 2.6%0A    // %5Bdatabind#700%5D%0A    public void testMapDefaultingRecursive() throws Exception%0A    %7B%0A        ObjectMapper mapper = new ObjectMapper();%0A        SimpleModule mod = new SimpleModule(%22test%22, Version.unknownVersion());%0A        // default is HashMap, so:%0A        mod.addAbstractTypeMapping(Map.class, TreeMap.class);%0A        mapper.registerModule(mod);%0A        Object result = mapper.readValue(%22%5B %7B%7D %5D%22, Object.class);%0A        assertEquals(ArrayList.class, result.getClass());%0A        Object v = ((List%3C?%3E) result).get(0);%0A        assertNotNull(v);%0A        assertEquals(TreeMap.class, v.getClass());%0A    %7D%0A    */%0A
 %0A    public 

Add a (failing) test for #700: most likely can only fix in 2.6