• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.views;
2 
3 import java.util.*;
4 
5 import com.fasterxml.jackson.annotation.*;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
8 
9 public class ViewsWithSchemaTest extends BaseMapTest
10 {
11     interface ViewBC { }
12     interface ViewAB { }
13 
14     @JsonPropertyOrder({ "a", "b", "c" })
15     static class POJO {
16         @JsonView({ ViewAB.class })
17         public int a;
18 
19         @JsonView({ ViewAB.class, ViewBC.class })
20         public int b;
21 
22         @JsonView({ ViewBC.class })
23         public int c;
24     }
25 
26     static class ListingVisitor extends JsonFormatVisitorWrapper.Base
27     {
28         public final List<String> names = new ArrayList<String>();
29 
30         @Override
expectObjectFormat(JavaType type)31         public JsonObjectFormatVisitor expectObjectFormat(JavaType type) {
32             return new JsonObjectFormatVisitor.Base() {
33                 @Override
34                 public void optionalProperty(BeanProperty writer) {
35                     names.add(writer.getName());
36                 }
37 
38                 @Override
39                 public void optionalProperty(String name,
40                         JsonFormatVisitable handler, JavaType propertyTypeHint) {
41                     names.add(name);
42                 }
43             };
44         }
45     }
46 
47     /*
48     /**********************************************************
49     /* Test methods
50     /**********************************************************
51      */
52 
53     private final ObjectMapper MAPPER = new ObjectMapper();
54 
55     public void testSchemaWithViews() throws Exception
56     {
57         ListingVisitor v = new ListingVisitor();
58         MAPPER.writerWithView(ViewBC.class)
59             .acceptJsonFormatVisitor(POJO.class, v);
60         assertEquals(Arrays.asList("b", "c"), v.names);
61 
62         v = new ListingVisitor();
63         MAPPER.writerWithView(ViewAB.class)
64             .acceptJsonFormatVisitor(POJO.class, v);
65         assertEquals(Arrays.asList("a", "b"), v.names);
66     }
67 
68     public void testSchemaWithoutViews() throws Exception
69     {
70         ListingVisitor v = new ListingVisitor();
71         MAPPER.acceptJsonFormatVisitor(POJO.class, v);
72         assertEquals(Arrays.asList("a", "b", "c"), v.names);
73     }
74 }
75