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.utils;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  class BeanDiffTest extends TestBase {
26  
27  	public static class A {
28  		public int f1;
29  		public String f2;
30  
31  		static A create(int f1, String f2) {
32  			var a = new A();
33  			a.f1 = f1;
34  			a.f2 = f2;
35  			return a;
36  		}
37  	}
38  
39  	@Test void a01_same() {
40  		var bd = BeanDiff.create(A.create(1, "a"), A.create(1, "a")).build();
41  		assertFalse(bd.hasDiffs());
42  		assertEquals("{v1:{},v2:{}}", bd.toString());
43  	}
44  
45  	@Test void a02_different() {
46  		var bd = BeanDiff.create(A.create(1, "a"), A.create(2, "b")).build();
47  		assertTrue(bd.hasDiffs());
48  		assertEquals("{v1:{f1:1,f2:'a'},v2:{f1:2,f2:'b'}}", bd.toString());
49  	}
50  
51  	@Test void a03_firstNull() {
52  		var bd = BeanDiff.create(null, A.create(2, "b")).build();
53  		assertTrue(bd.hasDiffs());
54  		assertEquals("{v1:{},v2:{f1:2,f2:'b'}}", bd.toString());
55  	}
56  
57  	@Test void a04_secondNull() {
58  		var bd = BeanDiff.create(A.create(1, "a"), null).build();
59  		assertTrue(bd.hasDiffs());
60  		assertEquals("{v1:{f1:1,f2:'a'},v2:{}}", bd.toString());
61  	}
62  
63  	@Test void a05_bothNull() {
64  		var bd = BeanDiff.create(null, null).build();
65  		assertFalse(bd.hasDiffs());
66  		assertEquals("{v1:{},v2:{}}", bd.toString());
67  	}
68  
69  	@Test void a06_nullFields() {
70  		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).build();
71  		assertTrue(bd.hasDiffs());
72  		assertEquals("{v1:{f1:1},v2:{f1:2,f2:'b'}}", bd.toString());
73  	}
74  
75  	@Test void a07_includes() {
76  		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).include("f1").build();
77  		assertTrue(bd.hasDiffs());
78  		assertEquals("{v1:{f1:1},v2:{f1:2}}", bd.toString());
79  	}
80  
81  	@Test void a08_includesSet() {
82  		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).include(set("f1")).build();
83  		assertTrue(bd.hasDiffs());
84  		assertEquals("{v1:{f1:1},v2:{f1:2}}", bd.toString());
85  	}
86  
87  	@Test void a09_excludes() {
88  		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).exclude("f2").build();
89  		assertTrue(bd.hasDiffs());
90  		assertEquals("{v1:{f1:1},v2:{f1:2}}", bd.toString());
91  	}
92  
93  	@Test void a10_excludesSet() {
94  		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).exclude(set("f2")).build();
95  		assertTrue(bd.hasDiffs());
96  		assertEquals("{v1:{f1:1},v2:{f1:2}}", bd.toString());
97  	}
98  
99  	@Test void a11_differentBeanContext() {
100 		var bd = BeanDiff.create(A.create(1, null), A.create(2, "b")).beanContext(BeanContext.DEFAULT_SORTED).build();
101 		assertTrue(bd.hasDiffs());
102 		assertEquals("{v1:{f1:1},v2:{f1:2,f2:'b'}}", bd.toString());
103 	}
104 }