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.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   * Tests: {@link PartBeanMeta}
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 }