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.urlencoding;
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.urlencoding.annotation.*;
28  import org.junit.jupiter.api.*;
29  
30  /**
31   * Tests the @UrlEncodingConfig annotation.
32   */
33  class UrlEncodingConfigAnnotationTest 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  	@UrlEncodingConfig(
48  		expandedParams="$X{true}"
49  	)
50  	static class A {}
51  	static ClassInfo a = ClassInfo.of(A.class);
52  
53  	@Test void basicSerializer() {
54  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
55  		var x = UrlEncodingSerializer.create().apply(al).build().getSession();
56  		check("true", x.isExpandedParams());
57  	}
58  
59  	@Test void basicParser() {
60  		var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
61  		var x = UrlEncodingParser.create().apply(al).build().getSession();
62  		check("true", x.isExpandedParams());
63  	}
64  
65  	//-----------------------------------------------------------------------------------------------------------------
66  	// Annotation with no values.
67  	//-----------------------------------------------------------------------------------------------------------------
68  
69  	@UrlEncodingConfig()
70  	static class B {}
71  	static ClassInfo b = ClassInfo.of(B.class);
72  
73  	@Test void noValuesSerializer() {
74  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
75  		var x = UrlEncodingSerializer.create().apply(al).build().getSession();
76  		check("false", x.isExpandedParams());
77  	}
78  
79  	@Test void noValuesParser() {
80  		var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
81  		var x = UrlEncodingParser.create().apply(al).build().getSession();
82  		check("false", x.isExpandedParams());
83  	}
84  
85  	//-----------------------------------------------------------------------------------------------------------------
86  	// No annotation.
87  	//-----------------------------------------------------------------------------------------------------------------
88  
89  	static class C {}
90  	static ClassInfo c = ClassInfo.of(C.class);
91  
92  	@Test void noAnnotationSerializer() {
93  		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
94  		var x = UrlEncodingSerializer.create().apply(al).build().getSession();
95  		check("false", x.isExpandedParams());
96  	}
97  
98  	@Test void noAnnotationParser() {
99  		var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
100 		var x = UrlEncodingParser.create().apply(al).build().getSession();
101 		check("false", x.isExpandedParams());
102 	}
103 }