1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.http.part;
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
28
29
30 class PartBeanMeta_Test extends TestBase {
31
32 private static final String TNAME = PartBeanMeta_Test.class.getName();
33
34 public static class A1 {
35 public String name, value;
36
37 public A1(String name, String value) {
38 this.name = name;
39 this.value = value;
40 }
41 }
42
43 public static class A2 {
44 public String name, value;
45
46 public A2(String name, Object value) {
47 this.name = name;
48 this.value = Utils.s(value);
49 }
50 }
51
52 @Query(name="A3")
53 public static class A3 {
54 public String value;
55
56 public A3(String value) {
57 this.value = value;
58 }
59 }
60
61 @FormData("A4")
62 public static class A4 {
63 public String value;
64
65 public A4(Object value) {
66 this.value = Utils.s(value);
67 }
68 }
69
70 @Path(name="A5")
71 public static class A5 {}
72
73 public static class A6 {
74 public A6(String name, Object value) {
75 throw new RuntimeException("oops");
76 }
77 }
78
79 @Header(name="A7")
80 public static class A7 {}
81
82 @Test void a01_basic() {
83 var a1 = PartBeanMeta.of(A1.class);
84 assertSame(a1, PartBeanMeta.of(A1.class));
85 assertBean(a1.construct("X", "foo"), "name,value", "X,foo");
86 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A1 requires a name as the first argument.", ()->a1.construct("foo"));
87 assertNull(a1.getSchema().getName());
88
89 var a2 = PartBeanMeta.of(A2.class);
90 assertBean(a2.construct("X", "foo"), "name,value", "X,foo");
91 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A2 requires a name as the first argument.", ()->a2.construct("foo"));
92 assertNull(a2.getSchema().getName());
93
94 var a3 = PartBeanMeta.of(A3.class);
95 assertBean(a3.construct("X", "foo"), "value", "foo");
96 assertBean(a3.construct("foo"), "value", "foo");
97 assertEquals("A3", a3.getSchema().getName());
98
99 var a4 = PartBeanMeta.of(A4.class);
100 assertBean(a4.construct("X", "foo"), "value", "foo");
101 assertBean(a4.construct("foo"), "value", "foo");
102 assertEquals("A4", a4.getSchema().getName());
103
104 var a5 = PartBeanMeta.of(A5.class);
105 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A5 could not be found.", ()->a5.construct("foo"));
106 assertEquals("A5", a5.getSchema().getName());
107
108 var a6 = PartBeanMeta.of(A6.class);
109 assertThrowsWithMessage(Exception.class, "oops", ()->a6.construct("X", "foo"));
110
111 var a7 = PartBeanMeta.of(A7.class);
112 assertThrowsWithMessage(Exception.class, "Constructor for type "+TNAME+"$A7 could not be found.", ()->a7.construct("foo"));
113 assertEquals("A7", a7.getSchema().getName());
114 }
115 }