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.uon;
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.reflect.*;
25  import org.apache.juneau.svl.*;
26  import org.apache.juneau.uon.annotation.*;
27  import org.junit.jupiter.api.*;
28  
29  /**
30   * Tests the @UonConfig annotation.
31   */
32  class UonConfigAnnotationTest extends TestBase {
33  
34  	private static void check(String expected, Object o) {
35  		assertEquals(expected, TO_STRING.apply(o));
36  	}
37  
38  	private static final Function<Object,String> TO_STRING = Object::toString;
39  
40  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
41  
42  	//-----------------------------------------------------------------------------------------------------------------
43  	// Basic tests
44  	//-----------------------------------------------------------------------------------------------------------------
45  
46  	@UonConfig(
47  		addBeanTypes="$X{true}",
48  		decoding="$X{true}",
49  		encoding="$X{true}",
50  		paramFormat="$X{UON}",
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, a.getAnnotationList());
58  		var x = UonSerializer.create().apply(al).build().getSession();
59  		check("true", x.isAddBeanTypes());
60  		check("true", x.isEncoding());
61  		check("UON", x.getParamFormat());
62  	}
63  
64  	@Test void basicParser() {
65  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
66  		var x = UonParser.create().apply(al).build().getSession();
67  		check("true", x.isDecoding());
68  		check("true", x.isValidateEnd());
69  	}
70  
71  	//-----------------------------------------------------------------------------------------------------------------
72  	// Annotation with no values.
73  	//-----------------------------------------------------------------------------------------------------------------
74  
75  	@UonConfig()
76  	static class B {}
77  	static ClassInfo b = ClassInfo.of(B.class);
78  
79  	@Test void noValuesSerializer() {
80  		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
81  		var x = UonSerializer.create().apply(al).build().getSession();
82  		check("false", x.isAddBeanTypes());
83  		check("false", x.isEncoding());
84  		check("UON", x.getParamFormat());
85  	}
86  
87  	@Test void noValuesParser() {
88  		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
89  		var x = UonParser.create().apply(al).build().getSession();
90  		check("false", x.isDecoding());
91  		check("false", x.isValidateEnd());
92  	}
93  
94  	//-----------------------------------------------------------------------------------------------------------------
95  	// No annotation.
96  	//-----------------------------------------------------------------------------------------------------------------
97  
98  	static class C {}
99  	static ClassInfo c = ClassInfo.of(C.class);
100 
101 	@Test void noAnnotationSerializer() {
102 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
103 		var x = UonSerializer.create().apply(al).build().getSession();
104 		check("false", x.isAddBeanTypes());
105 		check("false", x.isEncoding());
106 		check("UON", x.getParamFormat());
107 	}
108 
109 	@Test void noAnnotationParser() {
110 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
111 		var x = UonParser.create().apply(al).build().getSession();
112 		check("false", x.isDecoding());
113 		check("false", x.isValidateEnd());
114 	}
115 }