View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests designed to serialize and parse objects to make sure we end up
30   * with the same objects for all serializers and parsers.
31   */
32  class NameProperty_RoundTripTest extends RoundTripTest_Base {
33  
34  	//====================================================================================================
35  	// @NameProperty method.
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  	// @NameProperty field.
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 	// @NameProperty read-only (getter only, no setter).
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(); // Should be "initialName"
124 		x = t.roundTrip(x);
125 		assertBean(x, "bean{f2},m{k1{f2}}", "{2},{{2}}");
126 		if (t.isValidationOnly())
127 			return;
128 		// The name should NOT have changed because the property is read-only
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