• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.mixins;
2 
3 import java.io.IOException;
4 import java.util.Map;
5 
6 import com.fasterxml.jackson.annotation.JsonProperty;
7 import com.fasterxml.jackson.databind.BaseMapTest;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9 
10 public class TestMixinInheritance
11     extends BaseMapTest
12 {
13     static class Beano {
14         public int ido = 42;
15         public String nameo = "Bob";
16     }
17 
18     static class BeanoMixinSuper {
19         @JsonProperty("name")
20         public String nameo;
21     }
22 
23     static class BeanoMixinSub extends BeanoMixinSuper {
24         @JsonProperty("id")
25         public int ido;
26     }
27 
28     static class Beano2 {
getIdo()29         public int getIdo() { return 13; }
getNameo()30         public String getNameo() { return "Bill"; }
31     }
32 
33     static abstract class BeanoMixinSuper2 extends Beano2 {
34         @Override
35         @JsonProperty("name")
getNameo()36         public abstract String getNameo();
37     }
38 
39     static abstract class BeanoMixinSub2 extends BeanoMixinSuper2 {
40         @Override
41         @JsonProperty("id")
getIdo()42         public abstract int getIdo();
43     }
44 
45     /*
46     /**********************************************************
47     /* Unit tests
48     /**********************************************************
49      */
50 
testMixinFieldInheritance()51     public void testMixinFieldInheritance() throws IOException
52     {
53         ObjectMapper mapper = new ObjectMapper();
54         mapper.addMixIn(Beano.class, BeanoMixinSub.class);
55         Map<String,Object> result;
56         result = writeAndMap(mapper, new Beano());
57         assertEquals(2, result.size());
58         if (!result.containsKey("id")
59                 || !result.containsKey("name")) {
60             fail("Should have both 'id' and 'name', but content = "+result);
61         }
62     }
63 
testMixinMethodInheritance()64     public void testMixinMethodInheritance() throws IOException
65     {
66         ObjectMapper mapper = new ObjectMapper();
67         mapper.addMixIn(Beano2.class, BeanoMixinSub2.class);
68         Map<String,Object> result;
69         result = writeAndMap(mapper, new Beano2());
70         assertEquals(2, result.size());
71         assertTrue(result.containsKey("id"));
72         assertTrue(result.containsKey("name"));
73     }
74 }
75