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;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.junit.jupiter.api.*;
22  
23  class BeanPropertyValue_Test extends TestBase {
24  
25  	public static class TestBean {
26  		public String name;
27  		public int age;
28  		public boolean active;
29  	}
30  
31  	BeanContext bc = BeanContext.DEFAULT;
32  
33  	//====================================================================================================
34  	// compareTo() - Line 51 coverage
35  	//====================================================================================================
36  
37  	@Test void a01_compareTo_lessThan() {
38  		var bm = bc.getBeanMeta(TestBean.class);
39  		var pMeta1 = bm.getPropertyMeta("active");
40  		var pMeta2 = bm.getPropertyMeta("age");
41  
42  		var pv1 = new BeanPropertyValue(pMeta1, "active", true, null);
43  		var pv2 = new BeanPropertyValue(pMeta2, "age", 25, null);
44  
45  		assertTrue(pv1.compareTo(pv2) < 0);
46  	}
47  
48  	@Test void a02_compareTo_greaterThan() {
49  		var bm = bc.getBeanMeta(TestBean.class);
50  		var pMeta1 = bm.getPropertyMeta("name");
51  		var pMeta2 = bm.getPropertyMeta("age");
52  
53  		var pv1 = new BeanPropertyValue(pMeta1, "name", "John", null);
54  		var pv2 = new BeanPropertyValue(pMeta2, "age", 25, null);
55  
56  		assertTrue(pv1.compareTo(pv2) > 0);
57  	}
58  
59  	@Test void a03_compareTo_equal() {
60  		var bm = bc.getBeanMeta(TestBean.class);
61  		var pMeta = bm.getPropertyMeta("name");
62  
63  		var pv1 = new BeanPropertyValue(pMeta, "name", "John", null);
64  		var pv2 = new BeanPropertyValue(pMeta, "name", "Jane", null);
65  
66  		assertEquals(0, pv1.compareTo(pv2));
67  	}
68  
69  	//====================================================================================================
70  	// properties() and toString() - Lines 89-100 coverage
71  	//====================================================================================================
72  
73  	@Test void b01_properties() {
74  		var bm = bc.getBeanMeta(TestBean.class);
75  		var pMeta = bm.getPropertyMeta("name");
76  		var pv = new BeanPropertyValue(pMeta, "name", "John", null);
77  
78  		var props = pv.properties();
79  		assertEquals("name", props.get("name"));
80  		assertEquals("John", props.get("value"));
81  		assertNotNull(props.get("type"));
82  	}
83  
84  	@Test void b02_toString() {
85  		var bm = bc.getBeanMeta(TestBean.class);
86  		var pMeta = bm.getPropertyMeta("name");
87  		var pv = new BeanPropertyValue(pMeta, "name", "John", null);
88  
89  		var str = pv.toString();
90  		assertNotNull(str);
91  		assertTrue(str.contains("name"));
92  		assertTrue(str.contains("John"));
93  	}
94  
95  	@Test void b03_toString_withNullValue() {
96  		var bm = bc.getBeanMeta(TestBean.class);
97  		var pMeta = bm.getPropertyMeta("name");
98  		var pv = new BeanPropertyValue(pMeta, "name", null, null);
99  
100 		var str = pv.toString();
101 		assertNotNull(str);
102 		assertTrue(str.contains("name"));
103 	}
104 }
105