1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
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 }