1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau;
18
19 import static org.apache.juneau.TestUtils.*;
20
21 import org.apache.juneau.annotation.*;
22 import org.junit.jupiter.api.*;
23
24 class Annotations_Test extends TestBase {
25
26
27
28
29 @Test void a01_beanWithExplicitProperties() throws Exception {
30 var bc = BeanContext.DEFAULT;
31
32
33 var bm = bc.newBeanMap(Person1.class).load("{age:21,name:'foobar'}");
34 assertBean(bm.getBean(), "name,age", "foobar,21");
35
36 bm.put("age", 65);
37 bm.put("name", "futbol");
38 assertMap(bm, "name,age", "futbol,65");
39 assertBean(bm.getBean(), "name,age", "futbol,65");
40 }
41
42
43 @Bean(properties="age,name")
44 public static class Person1 {
45 public int age;
46
47 private String name;
48 public String getName() { return name; }
49 public void setName(String v) { name = v; }
50 }
51
52 @Test void a02_beanWithExplicitProperties2() throws Exception {
53 var bc = BeanContext.DEFAULT;
54
55
56 var bm = bc.newBeanMap(Person2.class).load("{age:21,name:'foobar'}");
57 assertBean(bm.getBean(), "name,age", "foobar,21");
58
59 bm.put("age", 65);
60 bm.put("name", "futbol");
61 assertMap(bm, "name,age", "futbol,65");
62 assertBean(bm.getBean(), "name,age", "futbol,65");
63 }
64
65
66 @Bean(p="age,name")
67 public static class Person2 {
68 public int age;
69
70 private String name;
71 public String getName() { return name; }
72 public void setName(String v) { name = v; }
73 }
74
75 @Test void a03_beanWithExplicitProperties3() throws Exception {
76 var bc = BeanContext.DEFAULT;
77
78
79 var bm = bc.newBeanMap(Person3.class).load("{age:21,name:'foobar'}");
80 assertBean(bm.getBean(), "name,age", "foobar,21");
81
82 bm.put("age", 65);
83 bm.put("name", "futbol");
84 assertMap(bm, "name,age", "futbol,65");
85 assertBean(bm.getBean(), "name,age", "futbol,65");
86 }
87
88
89 @Bean(properties="age",p="name")
90 public static class Person3 {
91 public int age;
92
93 private String name;
94 public String getName() { return name; }
95 public void setName(String v) { name = v; }
96 }
97
98 @Test void a04_beanWithExplicitProperties_usingConfig() throws Exception {
99 var bc = BeanContext.DEFAULT.copy().applyAnnotations(PersonConfig.class).build();
100
101
102 var bm = bc.newBeanMap(Person4.class).load("{age:21,name:'foobar'}");
103 assertBean(bm.getBean(), "name,age", "foobar,21");
104
105 bm.put("age", 65);
106 bm.put("name", "futbol");
107 assertMap(bm, "name,age", "futbol,65");
108 assertBean(bm.getBean(), "name,age", "futbol,65");
109 }
110
111
112 public static class Person4 {
113 public int age;
114
115 private String name;
116 public String getName() { return name; }
117 public void setName(String v) { name = v; }
118 }
119
120 @Bean(on="Person4",properties="age,name")
121 public static class PersonConfig {}
122
123
124
125
126 @Test void a05_forOnlyPublicFields() throws Exception {
127 var bc = BeanContext.DEFAULT;
128
129
130 var bm = bc.newBeanMap(A.class).load("{publicField:123}");
131 assertMap(bm, "publicField", "123");
132 }
133
134 public static class A {
135 public int publicField;
136 protected int protectedField;
137 @SuppressWarnings("unused")
138 private int privateField;
139 int defaultField;
140 }
141 }