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.json;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.function.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.commons.reflect.*;
26  import org.apache.juneau.json.annotation.*;
27  import org.apache.juneau.svl.*;
28  import org.junit.jupiter.api.*;
29  
30  /**
31   * Tests the @JsonConfig annotation.
32   */
33  class JsonConfigAnnotationTest extends TestBase {
34  
35  	private static void check(String expected, Object o) {
36  		assertEquals(expected, TO_STRING.apply(o));
37  	}
38  
39  	private static final Function<Object,String> TO_STRING = Object::toString;
40  
41  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
42  
43  	//-----------------------------------------------------------------------------------------------------------------
44  	// Basic tests
45  	//-----------------------------------------------------------------------------------------------------------------
46  
47  	@JsonConfig(
48  		addBeanTypes="$X{true}",
49  		escapeSolidus="$X{true}",
50  		simpleAttrs="$X{true}",
51  		validateEnd="$X{true}"
52  	)
53  	static class A {}
54  	static ClassInfo a = ClassInfo.of(A.class);
55  
56  	@Test void basicSerializer() {
57  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
58  		var x = JsonSerializer.create().apply(al).build().getSession();
59  		check("true", x.isAddBeanTypes());
60  		check("true", x.isEscapeSolidus());
61  		check("true", x.isSimpleAttrs());
62  	}
63  
64  	@Test void basicParser() {
65  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
66  		var x = JsonParser.create().apply(al).build().getSession();
67  		check("true", x.isValidateEnd());
68  	}
69  
70  	//-----------------------------------------------------------------------------------------------------------------
71  	// Annotation with no values.
72  	//-----------------------------------------------------------------------------------------------------------------
73  
74  	@JsonConfig()
75  	static class B {}
76  	static ClassInfo b = ClassInfo.of(B.class);
77  
78  	@Test void noValuesSerializer() {
79  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
80  		var x = JsonSerializer.create().apply(al).build().getSession();
81  		check("false", x.isAddBeanTypes());
82  		check("false", x.isEscapeSolidus());
83  		check("false", x.isSimpleAttrs());
84  	}
85  
86  	@Test void noValuesParser() {
87  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
88  		var x = JsonParser.create().apply(al).build().getSession();
89  		check("false", x.isValidateEnd());
90  	}
91  
92  	//-----------------------------------------------------------------------------------------------------------------
93  	// No annotation.
94  	//-----------------------------------------------------------------------------------------------------------------
95  
96  	static class C {}
97  	static ClassInfo c = ClassInfo.of(C.class);
98  
99  	@Test void noAnnotationSerializer() {
100 		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
101 		var x = JsonSerializer.create().apply(al).build().getSession();
102 		check("false", x.isAddBeanTypes());
103 		check("false", x.isEscapeSolidus());
104 		check("false", x.isSimpleAttrs());
105 	}
106 
107 	@Test void noAnnotationParser() {
108 		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
109 		var x = JsonParser.create().apply(al).build().getSession();
110 		check("false", x.isValidateEnd());
111 	}
112 }