package com.fasterxml.jackson.databind.jsontype.vld;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Base2534;
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Good2534;
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.ObjectWrapper;

public class CustomPTVMatchersTest extends BaseMapTest
{
    static abstract class CustomBase {
        public int x = 3;
    }

    static class CustomGood extends CustomBase {
        protected CustomGood() { }
        public CustomGood(int x) {
            super();
            this.x = x;
        }
    }

    static class CustomBad extends CustomBase {
        protected CustomBad() { }
        public CustomBad(int x) {
            super();
            this.x = x;
        }
    }

    static final class ObjectWrapper {
        public Object value;

        protected ObjectWrapper() { }
        public ObjectWrapper(Object v) { value = v; }
    }

    /*
    /**********************************************************************
    /* Test methods
    /**********************************************************************
     */

    public void testCustomBaseMatchers() throws Exception
    {
        // TO BE COMPLETED
    }
}

@@ -47,16 +47,38 @@
 e.vld;%0A%0A
+import java.net.URL;%0A%0A
 import c
@@ -120,16 +120,16 @@
 apTest;%0A
-
 import c
@@ -458,265 +458,8 @@
 tor;
-%0Aimport com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Base2534;%0Aimport com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Good2534;%0Aimport com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.ObjectWrapper;
 %0A%0Apu
@@ -1376,26 +1376,2828 @@
     
-// TO BE COMPLETED
+PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()%0A                .allowIfBaseType(new BasicPolymorphicTypeValidator.TypeMatcher() %7B%0A                    @Override%0A                    public boolean match(Class%3C?%3E base) %7B%0A                        // Allow types within our packages%0A                        return base.getName().startsWith(%22com.fasterxml.%22);%0A                    %7D%0A                %7D)%0A                .build();%0A        ObjectMapper mapper = jsonMapperBuilder()%0A                .activateDefaultTyping(ptv, DefaultTyping.EVERYTHING)%0A                .build();%0A        // First: in this case, allow %22Bad%22 one too (note: default typing based on%0A        // runtime type here)%0A        final String goodJson = mapper.writeValueAsString(new CustomBad(42) );%0A        CustomBase result = mapper.readValue(goodJson, CustomBase.class);%0A        assertEquals(CustomBad.class, result.getClass());%0A%0A        // but other types not so good%0A        final String badJson = mapper.writeValueAsString(new URL(%22http://localhost%22) );%0A        try %7B%0A            mapper.readValue(badJson, URL.class);%0A            fail(%22Should not pass%22);%0A        %7D catch (InvalidTypeIdException e) %7B%0A            verifyException(e, %22Could not resolve type id 'java.net.URL'%22);%0A            verifyException(e, %22as a subtype of%22);%0A        %7D%0A        assertEquals(CustomBad.class, result.getClass());%0A    %7D%0A%0A    public void testCustomSubtypeMatchers() throws Exception%0A    %7B%0A        PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()%0A                .allowIfSubType(new BasicPolymorphicTypeValidator.TypeMatcher() %7B%0A                    @Override%0A                    public boolean match(Class%3C?%3E clazz) %7B%0A                        // Allow anything that looks %22Good%22 :)%0A                        return clazz.getSimpleName().endsWith(%22Good%22);%0A                    %7D%0A                %7D)%0A                .build();%0A        ObjectMapper mapper = jsonMapperBuilder()%0A                .activateDefaultTyping(ptv, DefaultTyping.NON_FINAL)%0A                .build();        %0A%0A        // First: allow %22Good%22 one:%0A        final String goodJson = mapper.writeValueAsString(new ObjectWrapper(new CustomGood(42) ));%0A        ObjectWrapper w = mapper.readValue(goodJson, ObjectWrapper.class);%0A        assertNotNull(w);%0A        assertNotNull(w.value);%0A        assertEquals(CustomGood.class, w.value.getClass());%0A%0A        // Then something else%0A        final String badJson = mapper.writeValueAsString(new ObjectWrapper(new CustomBad(42) ));%0A        try %7B%0A            mapper.readValue(badJson, ObjectWrapper.class);%0A            fail(%22Should not pass%22);%0A        %7D catch (InvalidTypeIdException e) %7B%0A            verifyException(e, %22Could not resolve type id 'com.fasterxml.jackson.%22);%0A            verifyException(e, %22as a subtype of%22);%0A        %7D
 %0A   

Add tests for custom filters for PTV