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.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 }