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