1 package com.fasterxml.jackson.databind.ser; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.annotation.*; 6 7 import com.fasterxml.jackson.databind.*; 8 9 /** 10 * This unit test suite tests use of Annotations for 11 * bean serialization. 12 */ 13 public class TestAnnotationInheritance 14 extends BaseMapTest 15 { 16 /* 17 /********************************************************** 18 /* Annotated helper classes 19 /********************************************************** 20 */ 21 22 /// Base class for testing {@link JsonProperty} annotations 23 static class BasePojo 24 { width()25 @JsonProperty public int width() { return 3; } length()26 @JsonProperty public int length() { return 7; } 27 } 28 29 /** 30 * It should also be possible to specify annotations on interfaces, 31 * to be implemented by classes. This should not only work when interface 32 * is used (which may be the case for de-serialization) but also 33 * when implementing class is used and overrides methods. In latter 34 * case overriding methods should still "inherit" annotations -- this 35 * is not something JVM runtime provides, but Jackson class 36 * instrospector does. 37 */ 38 interface PojoInterface 39 { width()40 @JsonProperty int width(); length()41 @JsonProperty int length(); 42 } 43 44 /** 45 * Sub-class for testing that inheritance is handled properly 46 * wrt annotations. 47 */ 48 static class PojoSubclass extends BasePojo 49 { 50 /** 51 * Should still be recognized as a Getter here. 52 */ 53 @Override width()54 public int width() { return 9; } 55 } 56 57 static class PojoImpl implements PojoInterface 58 { 59 // Both should be recognized as getters here 60 61 @Override width()62 public int width() { return 1; } 63 @Override length()64 public int length() { return 2; } 65 getFoobar()66 public int getFoobar() { return 5; } 67 } 68 69 /* 70 /********************************************************** 71 /* Main tests 72 /********************************************************** 73 */ 74 testSimpleGetterInheritance()75 public void testSimpleGetterInheritance() throws Exception 76 { 77 ObjectMapper m = new ObjectMapper(); 78 Map<String,Object> result = writeAndMap(m, new PojoSubclass()); 79 assertEquals(2, result.size()); 80 assertEquals(Integer.valueOf(7), result.get("length")); 81 assertEquals(Integer.valueOf(9), result.get("width")); 82 } 83 testSimpleGetterInterfaceImpl()84 public void testSimpleGetterInterfaceImpl() throws Exception 85 { 86 ObjectMapper m = new ObjectMapper(); 87 Map<String,Object> result = writeAndMap(m, new PojoImpl()); 88 // should get 2 from interface, and one more from impl itself 89 assertEquals(3, result.size()); 90 assertEquals(Integer.valueOf(5), result.get("foobar")); 91 assertEquals(Integer.valueOf(1), result.get("width")); 92 assertEquals(Integer.valueOf(2), result.get("length")); 93 } 94 } 95