|
| 1 | +package com.google.gson.internal; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import java.lang.reflect.Type; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.google.gson.InstanceCreator; |
| 13 | +import com.google.gson.reflect.TypeToken; |
| 14 | + |
| 15 | +public class ConstructorConstructorTest { |
| 16 | + private static final Map<Type, InstanceCreator<?>> NO_INSTANCE_CREATORS = Collections.emptyMap(); |
| 17 | + |
| 18 | + private abstract static class AbstractClass { |
| 19 | + @SuppressWarnings("unused") |
| 20 | + public AbstractClass() { } |
| 21 | + } |
| 22 | + private interface Interface { } |
| 23 | + |
| 24 | + /** |
| 25 | + * Verify that ConstructorConstructor does not try to invoke no-arg constructor |
| 26 | + * of abstract class. |
| 27 | + */ |
| 28 | + @Test |
| 29 | + public void testGet_AbstractClassNoArgConstructor() { |
| 30 | + ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS, true); |
| 31 | + ObjectConstructor<AbstractClass> constructor = constructorFactory.get(TypeToken.get(AbstractClass.class)); |
| 32 | + try { |
| 33 | + constructor.construct(); |
| 34 | + fail("Expected exception"); |
| 35 | + } catch (RuntimeException exception) { |
| 36 | + assertEquals( |
| 37 | + "Unable to create instance of class com.google.gson.internal.ConstructorConstructorTest$AbstractClass. " |
| 38 | + + "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.", |
| 39 | + exception.getMessage() |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testGet_Interface() { |
| 46 | + ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS, true); |
| 47 | + ObjectConstructor<Interface> constructor = constructorFactory.get(TypeToken.get(Interface.class)); |
| 48 | + try { |
| 49 | + constructor.construct(); |
| 50 | + fail("Expected exception"); |
| 51 | + } catch (RuntimeException exception) { |
| 52 | + assertEquals( |
| 53 | + "Unable to create instance of interface com.google.gson.internal.ConstructorConstructorTest$Interface. " |
| 54 | + + "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.", |
| 55 | + exception.getMessage() |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments