1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.a.rttests;
18
19 import static org.apache.juneau.junit.bct.BctAssertions.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.util.*;
23
24 import org.apache.juneau.annotation.*;
25 import org.junit.jupiter.params.*;
26 import org.junit.jupiter.params.provider.*;
27
28
29
30
31
32 class NameProperty_RoundTripTest extends RoundTripTest_Base {
33
34
35
36
37
38 @ParameterizedTest
39 @MethodSource("testers")
40 void a01_nameProperty(RoundTrip_Tester t) throws Exception {
41 var x = new NamePropertyMethodContainer().init();
42 x = t.roundTrip(x);
43 assertBean(x, "bean{f2},m{k1{f2}}", "{2},{{2}}");
44 if (t.isValidationOnly())
45 return;
46 assertBean(x, "bean{name}", "{bean}");
47 assertBean(x, "m{k1{name}}", "{{k1}}");
48 }
49
50 public static class NamePropertyMethodContainer {
51 public NamePropertyMethodBean bean;
52 public Map<String,NamePropertyMethodBean> m;
53
54 NamePropertyMethodContainer init() {
55 bean = new NamePropertyMethodBean().init();
56 m = new LinkedHashMap<>();
57 m.put("k1", new NamePropertyMethodBean().init());
58 return this;
59 }
60
61 }
62 public static class NamePropertyMethodBean {
63 String name;
64 public int f2;
65
66 @NameProperty
67 protected void setName(String name) {
68 this.name = name;
69 }
70
71 NamePropertyMethodBean init() {
72 f2 = 2;
73 return this;
74 }
75 }
76
77
78
79
80
81 @ParameterizedTest
82 @MethodSource("testers")
83 void a02_namePropertyField(RoundTrip_Tester t) throws Exception {
84 var x = new NamePropertyFieldContainer().init();
85 x = t.roundTrip(x);
86 assertBean(x, "bean{f2},m{k1{f2}}", "{2},{{2}}");
87 if (t.isValidationOnly())
88 return;
89 assertBean(x, "bean{name}", "{bean}");
90 assertBean(x, "m{k1{name}}", "{{k1}}");
91 }
92
93 public static class NamePropertyFieldContainer {
94 public NamePropertyFieldBean bean;
95 public Map<String,NamePropertyFieldBean> m;
96
97 NamePropertyFieldContainer init() {
98 bean = new NamePropertyFieldBean().init();
99 m = new LinkedHashMap<>();
100 m.put("k1", new NamePropertyFieldBean().init());
101 return this;
102 }
103 }
104 public static class NamePropertyFieldBean {
105 @NameProperty
106 public String name;
107 public int f2;
108
109 NamePropertyFieldBean init() {
110 f2 = 2;
111 return this;
112 }
113 }
114
115
116
117
118
119 @ParameterizedTest
120 @MethodSource("testers")
121 void a03_readOnlyNameProperty(RoundTrip_Tester t) throws Exception {
122 var x = new ReadOnlyNamePropertyContainer().init();
123 var originalName = x.bean.getName();
124 x = t.roundTrip(x);
125 assertBean(x, "bean{f2},m{k1{f2}}", "{2},{{2}}");
126 if (t.isValidationOnly())
127 return;
128
129 assertEquals(originalName, x.bean.getName(), "Read-only name property should not be set by parser");
130 assertBean(x, "bean{name}", "{initialName}");
131 assertBean(x, "m{k1{name}}", "{{initialName}}");
132 }
133
134 public static class ReadOnlyNamePropertyContainer {
135 public ReadOnlyNamePropertyBean bean;
136 public Map<String,ReadOnlyNamePropertyBean> m;
137
138 ReadOnlyNamePropertyContainer init() {
139 bean = new ReadOnlyNamePropertyBean().init();
140 m = new LinkedHashMap<>();
141 m.put("k1", new ReadOnlyNamePropertyBean().init());
142 return this;
143 }
144 }
145 public static class ReadOnlyNamePropertyBean {
146 private String name = "initialName";
147 public int f2;
148
149 @NameProperty
150 public String getName() {
151 return name;
152 }
153
154 ReadOnlyNamePropertyBean init() {
155 f2 = 2;
156 return this;
157 }
158 }
159 }
160