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.parser;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.nio.charset.*;
22  import java.util.function.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.json.*;
26  import org.apache.juneau.msgpack.*;
27  import org.apache.juneau.parser.annotation.*;
28  import org.apache.juneau.reflect.*;
29  import org.apache.juneau.svl.*;
30  import org.junit.jupiter.api.*;
31  
32  /**
33   * Tests the @ParserConfig annotation.
34   */
35  class ParserConfigAnnotationTest extends TestBase {
36  
37  	private static void check(String expected, Object o) {
38  		assertEquals(expected, TO_STRING.apply(o));
39  	}
40  
41  	private static final Function<Object,String> TO_STRING = t -> {
42  		if (t == null)
43  			return null;
44  		if (t instanceof AA)
45  			return "AA";
46  		return t.toString();
47  	};
48  
49  	static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
50  
51  	//-----------------------------------------------------------------------------------------------------------------
52  	// Basic tests
53  	//-----------------------------------------------------------------------------------------------------------------
54  
55  	public static class AA extends ParserListener {}
56  
57  	@ParserConfig(
58  		autoCloseStreams="$X{true}",
59  		binaryFormat="$X{HEX}",
60  		debugOutputLines="$X{1}",
61  		fileCharset="$X{US-ASCII}",
62  		streamCharset="$X{US-ASCII}",
63  		listener=AA.class,
64  		strict="$X{true}",
65  		trimStrings="$X{true}",
66  		unbuffered="$X{true}"
67  	)
68  	static class A {}
69  	static ClassInfo a = ClassInfo.of(A.class);
70  
71  	@Test void basicReaderParser() {
72  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
73  		var x = JsonParser.create().apply(al).build().getSession();
74  		check("true", x.isAutoCloseStreams());
75  		check("1", x.getDebugOutputLines());
76  		check("US-ASCII", x.getFileCharset());
77  		check("US-ASCII", x.getStreamCharset());
78  		check("AA", x.getListener());
79  		check("true", x.isStrict());
80  		check("true", x.isTrimStrings());
81  		check("true", x.isUnbuffered());
82  	}
83  
84  	@Test void basicInputStreamParser() {
85  		var al = AnnotationWorkList.of(sr, a.getAnnotationList());
86  		var x = MsgPackParser.create().apply(al).build().getSession();
87  		check("true", x.isAutoCloseStreams());
88  		check("HEX", x.getBinaryFormat());
89  		check("1", x.getDebugOutputLines());
90  		check("AA", x.getListener());
91  		check("true", x.isStrict());
92  		check("true", x.isTrimStrings());
93  		check("true", x.isUnbuffered());
94  	}
95  
96  	//-----------------------------------------------------------------------------------------------------------------
97  	// Annotation with no values.
98  	//-----------------------------------------------------------------------------------------------------------------
99  
100 	@ParserConfig()
101 	static class B {}
102 	static ClassInfo b = ClassInfo.of(B.class);
103 
104 	@Test void noValuesReaderParser() {
105 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
106 		var x = JsonParser.create().apply(al).build().getSession();
107 		check("false", x.isAutoCloseStreams());
108 		check("5", x.getDebugOutputLines());
109 		check(Charset.defaultCharset().toString(), x.getFileCharset());
110 		check("UTF-8", x.getStreamCharset());
111 		check(null, x.getListener());
112 		check("false", x.isStrict());
113 		check("false", x.isTrimStrings());
114 		check("false", x.isUnbuffered());
115 	}
116 
117 	@Test void noValuesInputStreamParser() {
118 		var al = AnnotationWorkList.of(sr, b.getAnnotationList());
119 		var x = MsgPackParser.create().apply(al).build().getSession();
120 		check("false", x.isAutoCloseStreams());
121 		check("HEX", x.getBinaryFormat());
122 		check("5", x.getDebugOutputLines());
123 		check(null, x.getListener());
124 		check("false", x.isStrict());
125 		check("false", x.isTrimStrings());
126 		check("false", x.isUnbuffered());
127 	}
128 
129 	//-----------------------------------------------------------------------------------------------------------------
130 	// No annotation.
131 	//-----------------------------------------------------------------------------------------------------------------
132 
133 	static class C {}
134 	static ClassInfo c = ClassInfo.of(C.class);
135 
136 	@Test void noAnnotationReaderParser() {
137 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
138 		var x = JsonParser.create().apply(al).build().getSession();
139 		check("false", x.isAutoCloseStreams());
140 		check("5", x.getDebugOutputLines());
141 		check(Charset.defaultCharset().toString(), x.getFileCharset());
142 		check("UTF-8", x.getStreamCharset());
143 		check(null, x.getListener());
144 		check("false", x.isStrict());
145 		check("false", x.isTrimStrings());
146 		check("false", x.isUnbuffered());
147 	}
148 
149 	@Test void noAnnotationInputStreamParser() {
150 		var al = AnnotationWorkList.of(sr, c.getAnnotationList());
151 		var x = MsgPackParser.create().apply(al).build().getSession();
152 		check("false", x.isAutoCloseStreams());
153 		check("HEX", x.getBinaryFormat());
154 		check("5", x.getDebugOutputLines());
155 		check(null, x.getListener());
156 		check("false", x.isStrict());
157 		check("false", x.isTrimStrings());
158 		check("false", x.isUnbuffered());
159 	}
160 }