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.serializer;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.function.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.json.*;
25  import org.apache.juneau.msgpack.*;
26  import org.apache.juneau.reflect.*;
27  import org.apache.juneau.serializer.annotation.*;
28  import org.apache.juneau.svl.*;
29  import org.junit.jupiter.api.*;
30  
31  /**
32   * Tests the @SerializerConfig annotation.
33   */
34  class SerializerConfigAnnotationTest extends TestBase {
35  
36  	private static void check(String expected, Object o) {
37  		assertEquals(expected, TO_STRING.apply(o));
38  	}
39  
40  	private static final Function<Object,String> TO_STRING = t -> {
41  		if (t == null)
42  			return null;
43  		if (t instanceof AA)
44  			return "AA";
45  		return t.toString();
46  	};
47  
48  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
49  
50  	//-----------------------------------------------------------------------------------------------------------------
51  	// Basic tests
52  	//-----------------------------------------------------------------------------------------------------------------
53  
54  	public static class AA extends SerializerListener {}
55  
56  	@SerializerConfig(
57  		addBeanTypes="$X{true}",
58  		addRootType="$X{true}",
59  		binaryFormat="$X{HEX}",
60  		detectRecursions="$X{true}",
61  		ignoreRecursions="$X{true}",
62  		initialDepth="$X{1}",
63  		listener=AA.class,
64  		maxDepth="$X{1}",
65  		maxIndent="$X{1}",
66  		quoteChar="$X{'}",
67  		sortCollections="$X{true}",
68  		sortMaps="$X{true}",
69  		trimEmptyCollections="$X{true}",
70  		trimEmptyMaps="$X{true}",
71  		keepNullProperties="$X{false}",
72  		trimStrings="$X{true}",
73  		uriContext="{}",
74  		uriRelativity="$X{RESOURCE}",
75  		uriResolution="$X{ABSOLUTE}",
76  		useWhitespace="$X{true}"
77  	)
78  	static class A {}
79  	static ClassInfo a = ClassInfo.of(A.class);
80  
81  	@Test void basicWriterSerializer() {
82  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
83  		var x = JsonSerializer.create().apply(al).build().getSession();
84  		check("true", ((SerializerSession)x).isAddBeanTypes());
85  		check("true", x.isAddRootType());
86  		check("true", x.isDetectRecursions());
87  		check("true", x.isIgnoreRecursions());
88  		check("1", x.getInitialDepth());
89  		check("AA", x.getListener());
90  		check("1", x.getMaxDepth());
91  		check("1", x.getMaxIndent());
92  		check("'", x.getQuoteChar());
93  		check("true", x.isSortCollections());
94  		check("true", x.isSortMaps());
95  		check("true", x.isTrimEmptyCollections());
96  		check("true", x.isTrimEmptyMaps());
97  		check("false", x.isKeepNullProperties());
98  		check("true", x.isTrimStrings());
99  		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
100 		check("RESOURCE", x.getUriRelativity());
101 		check("ABSOLUTE", x.getUriResolution());
102 		check("true", x.isUseWhitespace());
103 	}
104 
105 	@Test void basicOutputStreamSerializer() {
106 		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
107 		var x = MsgPackSerializer.create().apply(al).build().getSession();
108 		check("true", ((SerializerSession)x).isAddBeanTypes());
109 		check("true", x.isAddRootType());
110 		check("HEX", x.getBinaryFormat());
111 		check("true", x.isDetectRecursions());
112 		check("true", x.isIgnoreRecursions());
113 		check("1", x.getInitialDepth());
114 		check("AA", x.getListener());
115 		check("1", x.getMaxDepth());
116 		check("true", x.isSortCollections());
117 		check("true", x.isSortMaps());
118 		check("true", x.isTrimEmptyCollections());
119 		check("true", x.isTrimEmptyMaps());
120 		check("false", x.isKeepNullProperties());
121 		check("true", x.isTrimStrings());
122 		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
123 		check("RESOURCE", x.getUriRelativity());
124 		check("ABSOLUTE", x.getUriResolution());
125 	}
126 
127 	//-----------------------------------------------------------------------------------------------------------------
128 	// Annotation with no values.
129 	//-----------------------------------------------------------------------------------------------------------------
130 
131 	@SerializerConfig()
132 	static class B {}
133 	static ClassInfo b = ClassInfo.of(B.class);
134 
135 	@Test void noValuesWriterSerializer() {
136 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
137 		var x = JsonSerializer.create().apply(al).build().getSession();
138 		check("false", ((SerializerSession)x).isAddBeanTypes());
139 		check("false", x.isAddRootType());
140 		check(null, x.getListener());
141 		check("100", x.getMaxIndent());
142 		check("\"", x.getQuoteChar());
143 		check("false", x.isSortCollections());
144 		check("false", x.isSortMaps());
145 		check("false", x.isTrimEmptyCollections());
146 		check("false", x.isTrimEmptyMaps());
147 		check("false", x.isKeepNullProperties());
148 		check("false", x.isTrimStrings());
149 		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
150 		check("RESOURCE", x.getUriRelativity());
151 		check("NONE", x.getUriResolution());
152 		check("false", x.isUseWhitespace());
153 	}
154 
155 	@Test void noValuesOutputStreamSerializer() {
156 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
157 		var x = MsgPackSerializer.create().apply(al).build().getSession();
158 		check("false", ((SerializerSession)x).isAddBeanTypes());
159 		check("false", x.isAddRootType());
160 		check("HEX", x.getBinaryFormat());
161 		check(null, x.getListener());
162 		check("false", x.isSortCollections());
163 		check("false", x.isSortMaps());
164 		check("false", x.isTrimEmptyCollections());
165 		check("false", x.isTrimEmptyMaps());
166 		check("false", x.isKeepNullProperties());
167 		check("false", x.isTrimStrings());
168 		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
169 		check("RESOURCE", x.getUriRelativity());
170 		check("NONE", x.getUriResolution());
171 	}
172 
173 	//-----------------------------------------------------------------------------------------------------------------
174 	// No annotation.
175 	//-----------------------------------------------------------------------------------------------------------------
176 
177 	static class C {}
178 	static ClassInfo c = ClassInfo.of(C.class);
179 
180 	@Test void noAnnotationWriterSerializer() {
181 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
182 		var x = JsonSerializer.create().apply(al).build().getSession();
183 		check("false", ((SerializerSession)x).isAddBeanTypes());
184 		check("false", x.isAddRootType());
185 		check(null, x.getListener());
186 		check("100", x.getMaxIndent());
187 		check("\"", x.getQuoteChar());
188 		check("false", x.isSortCollections());
189 		check("false", x.isSortMaps());
190 		check("false", x.isTrimEmptyCollections());
191 		check("false", x.isTrimEmptyMaps());
192 		check("false", x.isKeepNullProperties());
193 		check("false", x.isTrimStrings());
194 		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
195 		check("RESOURCE", x.getUriRelativity());
196 		check("NONE", x.getUriResolution());
197 		check("false", x.isUseWhitespace());
198 	}
199 
200 	@Test void noAnnotationOutputStreamSerializer() {
201 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
202 		var x = MsgPackSerializer.create().apply(al).build().getSession();
203 		check("false", ((SerializerSession)x).isAddBeanTypes());
204 		check("false", x.isAddRootType());
205 		check("HEX", x.getBinaryFormat());
206 		check(null, x.getListener());
207 		check("false", x.isSortCollections());
208 		check("false", x.isSortMaps());
209 		check("false", x.isTrimEmptyCollections());
210 		check("false", x.isTrimEmptyMaps());
211 		check("false", x.isKeepNullProperties());
212 		check("false", x.isTrimStrings());
213 		check("{absoluteAuthority:'/',absoluteContextRoot:'/',absolutePathInfo:'/',absolutePathInfoParent:'/',absoluteServletPath:'/',absoluteServletPathParent:'/',rootRelativeContextRoot:'/',rootRelativePathInfo:'/',rootRelativePathInfoParent:'/',rootRelativeServletPath:'/',rootRelativeServletPathParent:'/'}", x.getUriContext());
214 		check("RESOURCE", x.getUriRelativity());
215 		check("NONE", x.getUriResolution());
216 	}
217 }