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