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.html;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.*;
22  import java.util.function.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.html.annotation.*;
26  import org.apache.juneau.reflect.*;
27  import org.apache.juneau.svl.*;
28  import org.junit.jupiter.api.*;
29  
30  /**
31   * Tests the @HtmlConfig annotation.
32   */
33  class HtmlConfigAnnotation_Test 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 = t -> Objects.toString(t, null);
40  
41  
42  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
43  
44  	//-----------------------------------------------------------------------------------------------------------------
45  	// Basic tests
46  	//-----------------------------------------------------------------------------------------------------------------
47  
48  	@HtmlConfig(
49  		addBeanTypes="$X{true}",
50  		addKeyValueTableHeaders="$X{true}",
51  		disableDetectLabelParameters="$X{true}",
52  		disableDetectLinksInStrings="$X{true}",
53  		labelParameter="$X{foo}",
54  		uriAnchorText="$X{TO_STRING}"
55  	)
56  	static class A {}
57  	static ClassInfo a = ClassInfo.of(A.class);
58  
59  	@Test void basicSerializer() {
60  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
61  		var x = HtmlSerializer.create().apply(al).build().getSession();
62  		check("true", x.isAddBeanTypes());
63  		check("true", x.isAddKeyValueTableHeaders());
64  		check("false", x.isDetectLabelParameters());
65  		check("false", x.isDetectLinksInStrings());
66  		check("foo", x.getLabelParameter());
67  		check("TO_STRING", x.getUriAnchorText());
68  	}
69  
70  	@Test void basicParser() {
71  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
72  		assertDoesNotThrow(()->HtmlParser.create().apply(al).build().createSession());
73  	}
74  
75  	//-----------------------------------------------------------------------------------------------------------------
76  	// Annotation with no values.
77  	//-----------------------------------------------------------------------------------------------------------------
78  
79  	@HtmlConfig()
80  	static class B {}
81  	static ClassInfo b = ClassInfo.of(B.class);
82  
83  	@Test void defaultsSerializer() {
84  		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
85  		var x = HtmlSerializer.create().apply(al).build().getSession();
86  		check("false", x.isAddBeanTypes());
87  		check("false", x.isAddKeyValueTableHeaders());
88  		check("true", x.isDetectLabelParameters());
89  		check("true", x.isDetectLinksInStrings());
90  		check("label", x.getLabelParameter());
91  		check("TO_STRING", x.getUriAnchorText());
92  	}
93  
94  	@Test void defaultsParser() {
95  		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
96  		assertDoesNotThrow(()->HtmlParser.create().apply(al).build().createSession());
97  	}
98  
99  	//-----------------------------------------------------------------------------------------------------------------
100 	// No annotation.
101 	//-----------------------------------------------------------------------------------------------------------------
102 
103 	static class C {}
104 	static ClassInfo c = ClassInfo.of(C.class);
105 
106 	@Test void noAnnotationSerializer() {
107 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
108 		var x = HtmlSerializer.create().apply(al).build().getSession();
109 		check("false", x.isAddBeanTypes());
110 		check("false", x.isAddKeyValueTableHeaders());
111 		check("true", x.isDetectLabelParameters());
112 		check("true", x.isDetectLinksInStrings());
113 		check("label", x.getLabelParameter());
114 		check("TO_STRING", x.getUriAnchorText());
115 	}
116 
117 	@Test void noAnnotationParser() {
118 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
119 		assertDoesNotThrow(()->HtmlParser.create().apply(al).build().createSession());
120 	}
121 }