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.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.svl.*;
27  import org.apache.juneau.uon.annotation.*;
28  import org.junit.jupiter.api.*;
29  
30  /**
31   * Tests the @UonConfig annotation.
32   */
33  class UonConfigAnnotationTest 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  	@UonConfig(
48  		addBeanTypes="$X{true}",
49  		decoding="$X{true}",
50  		encoding="$X{true}",
51  		paramFormat="$X{UON}",
52  		validateEnd="$X{true}"
53  	)
54  	static class A {}
55  	static ClassInfo a = ClassInfo.of(A.class);
56  
57  	@Test void basicSerializer() {
58  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
59  		var x = UonSerializer.create().apply(al).build().getSession();
60  		check("true", x.isAddBeanTypes());
61  		check("true", x.isEncoding());
62  		check("UON", x.getParamFormat());
63  	}
64  
65  	@Test void basicParser() {
66  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
67  		var x = UonParser.create().apply(al).build().getSession();
68  		check("true", x.isDecoding());
69  		check("true", x.isValidateEnd());
70  	}
71  
72  	//-----------------------------------------------------------------------------------------------------------------
73  	// Annotation with no values.
74  	//-----------------------------------------------------------------------------------------------------------------
75  
76  	@UonConfig()
77  	static class B {}
78  	static ClassInfo b = ClassInfo.of(B.class);
79  
80  	@Test void noValuesSerializer() {
81  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
82  		var x = UonSerializer.create().apply(al).build().getSession();
83  		check("false", x.isAddBeanTypes());
84  		check("false", x.isEncoding());
85  		check("UON", x.getParamFormat());
86  	}
87  
88  	@Test void noValuesParser() {
89  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
90  		var x = UonParser.create().apply(al).build().getSession();
91  		check("false", x.isDecoding());
92  		check("false", x.isValidateEnd());
93  	}
94  
95  	//-----------------------------------------------------------------------------------------------------------------
96  	// No annotation.
97  	//-----------------------------------------------------------------------------------------------------------------
98  
99  	static class C {}
100 	static ClassInfo c = ClassInfo.of(C.class);
101 
102 	@Test void noAnnotationSerializer() {
103 		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
104 		var x = UonSerializer.create().apply(al).build().getSession();
105 		check("false", x.isAddBeanTypes());
106 		check("false", x.isEncoding());
107 		check("UON", x.getParamFormat());
108 	}
109 
110 	@Test void noAnnotationParser() {
111 		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
112 		var x = UonParser.create().apply(al).build().getSession();
113 		check("false", x.isDecoding());
114 		check("false", x.isValidateEnd());
115 	}
116 }