1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.http.header;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import org.apache.juneau.*;
23 import org.apache.juneau.common.utils.*;
24 import org.apache.juneau.http.annotation.*;
25 import org.junit.jupiter.api.*;
26
27 class HeaderBeanMeta_Test extends TestBase {
28
29 private static final String TNAME = HeaderBeanMeta_Test.class.getName();
30
31 public static class A1 {
32 public String name, value;
33
34 public A1(String name, String value) {
35 this.name = name;
36 this.value = value;
37 }
38 }
39
40 public static class A2 {
41 public String name, value;
42
43 public A2(String name, Object value) {
44 this.name = name;
45 this.value = Utils.s(value);
46 }
47 }
48
49 @Header(name="A3")
50 public static class A3 {
51 public String value;
52
53 public A3(String value) {
54 this.value = value;
55 }
56 }
57
58 @Header("A4")
59 public static class A4 {
60 public String value;
61
62 public A4(Object value) {
63 this.value = Utils.s(value);
64 }
65 }
66
67 @Header(name="A5")
68 public static class A5 {}
69
70 public static class A6 {
71 public A6(String name, Object value) {
72 throw new RuntimeException("oops");
73 }
74 }
75
76 @Test void a01_basic() {
77 var a1 = HeaderBeanMeta.of(A1.class);
78 assertSame(a1, HeaderBeanMeta.of(A1.class));
79 assertBean(a1.construct("X", "foo"), "name,value", "X,foo");
80 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A1 requires a name as the first argument.", ()->a1.construct("foo"));
81 assertNull(a1.getSchema().getName());
82
83 var a2 = HeaderBeanMeta.of(A2.class);
84 assertBean(a2.construct("X", "foo"), "name,value", "X,foo");
85 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A2 requires a name as the first argument.", ()->a2.construct("foo"));
86 assertNull(a2.getSchema().getName());
87
88 var a3 = HeaderBeanMeta.of(A3.class);
89 assertBean(a3.construct("X", "foo"), "value", "foo");
90 assertBean(a3.construct("foo"), "value", "foo");
91 assertEquals("A3", a3.getSchema().getName());
92
93 var a4 = HeaderBeanMeta.of(A4.class);
94 assertBean(a4.construct("X", "foo"), "value", "foo");
95 assertBean(a4.construct("foo"), "value", "foo");
96 assertEquals("A4", a4.getSchema().getName());
97
98 var a5 = HeaderBeanMeta.of(A5.class);
99 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A5 could not be found.", ()->a5.construct("foo"));
100 assertEquals("A5", a5.getSchema().getName());
101
102 var a6 = HeaderBeanMeta.of(A6.class);
103 assertThrowsWithMessage(Exception.class, "oops", ()->a6.construct("X", "foo"));
104 }
105 }