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.xml;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.collections.*;
23  import org.apache.juneau.serializer.*;
24  import org.apache.juneau.xml.annotation.*;
25  import org.junit.jupiter.params.*;
26  import org.junit.jupiter.params.provider.*;
27  
28  /**
29   * Verifies that the correct error messages are displayed when you do something wrong with the @Xml annotation.
30   */
31  class InvalidXmlBeans_Test extends TestBase {
32  
33  	private static final XmlSerializer
34  		s1 = XmlSerializer.DEFAULT_SQ;
35  
36  	private static final Input[] INPUT = {
37  
38  		input(
39  			"BeanWithAttrFormat",
40  			new BeanWithAttrFormat(),
41  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithAttrFormat: Invalid format specified in @Xml annotation on bean: ATTR.  Must be one of the following: DEFAULT,ATTRS,ELEMENTS,VOID"
42  		),
43  		input(
44  			"BeanWithElementFormat",
45  			new BeanWithElementFormat(),
46  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithElementFormat: Invalid format specified in @Xml annotation on bean: ELEMENT.  Must be one of the following: DEFAULT,ATTRS,ELEMENTS,VOID"
47  		),
48  		input(
49  			"BeanWithCollapsedFormat",
50  			new BeanWithCollapsedFormat(),
51  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithCollapsedFormat: Invalid format specified in @Xml annotation on bean: COLLAPSED.  Must be one of the following: DEFAULT,ATTRS,ELEMENTS,VOID"
52  		),
53  		input(
54  			"BeanWithMixedFormat",
55  			new BeanWithMixedFormat(),
56  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithMixedFormat: Invalid format specified in @Xml annotation on bean: MIXED.  Must be one of the following: DEFAULT,ATTRS,ELEMENTS,VOID"
57  		),
58  		input(
59  			"BeanWithMultipleAttrs",
60  			new BeanWithMultipleAttrs(),
61  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithMultipleAttrs: Multiple instances of ATTRS properties defined on class.  Only one property can be designated as such."
62  		),
63  		input(
64  			"BeanWithWrongAttrsType",
65  			new BeanWithWrongAttrsType(),
66  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithWrongAttrsType: Invalid type for ATTRS property.  Only properties of type Map and bean can be used."
67  		),
68  		input(
69  			"BeanWithMulipleElements",
70  			new BeanWithMulipleElements(),
71  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithMulipleElements: Multiple instances of ELEMENTS properties defined on class.  Only one property can be designated as such."
72  		),
73  		input(
74  			"BeanWithWrongElementsType",
75  			new BeanWithWrongElementsType(),
76  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithWrongElementsType: Invalid type for ELEMENTS property.  Only properties of type Collection and array can be used."
77  		),
78  		input(
79  			"BeanWithMulipleMixed",
80  			new BeanWithMulipleMixed(),
81  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithMulipleMixed: Multiple instances of MIXED properties defined on class.  Only one property can be designated as such."
82  		),
83  		input(
84  			"BeanWithConflictingChildNames",
85  			new BeanWithConflictingChildNames(),
86  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithConflictingChildNames: Multiple properties found with the child name 'X'."
87  		),
88  		input(
89  			"BeanWithElementsAndMixed",
90  			new BeanWithElementsAndMixed(),
91  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithElementsAndMixed: ELEMENTS and MIXED properties found on the same bean.  Only one property can be designated as such."
92  		),
93  		input(
94  			"BeanWithElementsAndElement",
95  			new BeanWithElementsAndElement(),
96  			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithElementsAndElement: ELEMENTS and ELEMENT properties found on the same bean.  These cannot be mixed."
97  		),
98  		input(
99  			"BeanWithElementsAndDefault",
100 			new BeanWithElementsAndDefault(),
101 			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithElementsAndDefault: ELEMENTS and ELEMENT properties found on the same bean.  These cannot be mixed."
102 		),
103 		input(
104 			"BeanWithElementsAndCollapsed",
105 			new BeanWithElementsAndCollapsed(),
106 			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithElementsAndCollapsed: ELEMENTS and COLLAPSED properties found on the same bean.  These cannot be mixed."
107 		),
108 		input(
109 			"BeanWithChildAndPropNameConflict",
110 			new BeanWithChildAndPropNameConflict(),
111 			"org.apache.juneau.xml.InvalidXmlBeansTest$BeanWithChildAndPropNameConflict: Child element name conflicts found with another property."
112 		)
113 	};
114 
115 	private static class Input {
116 		final String expected;
117 		final Object in;
118 
119 		public Input(String label, Object in, String expected) {
120 			this.in = in;
121 			this.expected = expected;
122 		}
123 	}
124 
125 	private static Input input(String label, Object in, String expected) {
126 		return new Input(label, in, expected);
127 	}
128 
129 	static Input[] input() {
130 		return INPUT;
131 	}
132 
133 	@ParameterizedTest
134 	@MethodSource("input")
135 	void a01_basic(Input input) {
136 		assertThrows(SerializeException.class, ()->s1.serialize(input.in), input.expected);
137 	}
138 
139 	//-----------------------------------------------------------------------------------------------------------------
140 	// Test beans
141 	//-----------------------------------------------------------------------------------------------------------------
142 
143 	@Xml(format=XmlFormat.ATTR)
144 	public static class BeanWithAttrFormat {
145 		public int f1;
146 	}
147 
148 	@Xml(format=XmlFormat.ELEMENT)
149 	public static class BeanWithElementFormat {
150 		public int f1;
151 	}
152 
153 	@Xml(format=XmlFormat.COLLAPSED)
154 	public static class BeanWithCollapsedFormat {
155 		public int f1;
156 	}
157 
158 	@Xml(format=XmlFormat.MIXED)
159 	public static class BeanWithMixedFormat {
160 		public int f1;
161 	}
162 
163 	public static class BeanWithMultipleAttrs {
164 		@Xml(format=XmlFormat.ATTRS)
165 		public JsonMap f1;
166 		@Xml(format=XmlFormat.ATTRS)
167 		public JsonMap f2;
168 	}
169 
170 	public static class BeanWithWrongAttrsType {
171 		@Xml(format=XmlFormat.ATTRS)
172 		public JsonList f1;
173 	}
174 
175 	public static class BeanWithMulipleElements {
176 		@Xml(format=XmlFormat.ELEMENTS)
177 		public JsonList f1;
178 		@Xml(format=XmlFormat.ELEMENTS)
179 		public JsonList f2;
180 	}
181 
182 	public static class BeanWithWrongElementsType {
183 		@Xml(format=XmlFormat.ELEMENTS)
184 		public JsonMap f1;
185 	}
186 
187 	public static class BeanWithMulipleMixed {
188 		@Xml(format=XmlFormat.MIXED)
189 		public JsonList f1;
190 		@Xml(format=XmlFormat.MIXED)
191 		public JsonList f2;
192 	}
193 
194 	public static class BeanWithConflictingChildNames {
195 		@Xml(format=XmlFormat.COLLAPSED, childName="X")
196 		public JsonList f1;
197 		@Xml(format=XmlFormat.COLLAPSED, childName="X")
198 		public JsonList f2;
199 	}
200 
201 	public static class BeanWithElementsAndMixed {
202 		@Xml(format=XmlFormat.ELEMENTS)
203 		public JsonList f1;
204 		@Xml(format=XmlFormat.MIXED)
205 		public JsonList f2;
206 	}
207 
208 	public static class BeanWithElementsAndElement {
209 		@Xml(format=XmlFormat.ELEMENTS)
210 		public JsonList f1;
211 		@Xml(format=XmlFormat.ELEMENT)
212 		public JsonList f2;
213 	}
214 
215 	public static class BeanWithElementsAndDefault {
216 		@Xml(format=XmlFormat.ELEMENTS)
217 		public JsonList f1;
218 		public JsonList f2;
219 	}
220 
221 	public static class BeanWithElementsAndCollapsed {
222 		@Xml(format=XmlFormat.ELEMENTS)
223 		public JsonList f1;
224 		@Xml(format=XmlFormat.COLLAPSED)
225 		public JsonList f2;
226 	}
227 
228 	public static class BeanWithChildAndPropNameConflict {
229 		@Xml(format=XmlFormat.COLLAPSED, childName="f2")
230 		public JsonList f1;
231 		public JsonList f2;
232 	}
233 }