1 package com.fasterxml.jackson.databind.introspect;
2 
3 import java.beans.Transient;
4 
5 import com.fasterxml.jackson.annotation.JsonProperty;
6 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7 import com.fasterxml.jackson.databind.*;
8 
9 /**
10  * Tests for both `transient` keyword and JDK 7
11  * {@link java.beans.Transient} annotation.
12  */
13 public class TransientTest extends BaseMapTest
14 {
15     // for [databind#296]
16     @JsonPropertyOrder({ "x" })
17     static class ClassyTransient
18     {
19         public transient int value = 3;
20 
getValue()21         public int getValue() { return value; }
22 
getX()23         public int getX() { return 42; }
24     }
25 
26     static class SimplePrunableTransient {
27         public int a = 1;
28         public transient int b = 2;
29     }
30 
31     // for [databind#857]
32     static class BeanTransient {
33         @Transient
getX()34         public int getX() { return 3; }
35 
getY()36         public int getY() { return 4; }
37     }
38 
39     // for [databind#1184]
40     static class OverridableTransient {
41         @JsonProperty
42 //        @JsonProperty("value") // should override transient here, to force inclusion
43         public transient int tValue;
44 
OverridableTransient(int v)45         public OverridableTransient(int v) { tValue = v; }
46     }
47 
48     /*
49     /**********************************************************
50     /* Unit tests
51     /**********************************************************
52      */
53 
54     private final ObjectMapper MAPPER = objectMapper();
55 
56     // for [databind#296]
testTransientFieldHandling()57     public void testTransientFieldHandling() throws Exception
58     {
59         // default handling: remove transient field but do not propagate
60         assertEquals(aposToQuotes("{'x':42,'value':3}"),
61                 MAPPER.writeValueAsString(new ClassyTransient()));
62         assertEquals(aposToQuotes("{'a':1}"),
63                 MAPPER.writeValueAsString(new SimplePrunableTransient()));
64 
65         // but may change that
66         ObjectMapper m = jsonMapperBuilder()
67             .enable(MapperFeature.PROPAGATE_TRANSIENT_MARKER)
68             .build();
69         assertEquals(aposToQuotes("{'x':42}"),
70                 m.writeValueAsString(new ClassyTransient()));
71     }
72 
73     // for [databind#857]
testBeanTransient()74     public void testBeanTransient() throws Exception
75     {
76         assertEquals(aposToQuotes("{'y':4}"),
77                 MAPPER.writeValueAsString(new BeanTransient()));
78     }
79 
80     // for [databind#1184]
testOverridingTransient()81     public void testOverridingTransient() throws Exception
82     {
83         assertEquals(aposToQuotes("{'tValue':38}"),
84                 MAPPER.writeValueAsString(new OverridableTransient(38)));
85     }
86 }
87