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.objecttools;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.junit.jupiter.api.*;
23  
24  /**
25   * Test the PojoMerge class.
26   */
27  class ObjectMerger_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// Basic tests
31  	//====================================================================================================
32  	@Test void a01_basic() {
33  		IA a1, a2, am;
34  
35  		a1 = new A("1"); a2 = new A("2");
36  		am = ObjectMerger.merge(IA.class, a1, a2);
37  		assertEquals("1", am.getA());
38  		am.setA("x");
39  		assertEquals("x", am.getA());
40  		assertEquals("x", a1.getA());
41  		assertEquals("2", a2.getA());
42  
43  		a1 = new A("1"); a2 = new A("2");
44  		am = ObjectMerger.merge(IA.class, true, a1, a2);
45  		assertEquals("1", am.getA());
46  		am.setA("x");
47  		assertEquals("x", am.getA());
48  		assertEquals("x", a1.getA());
49  		assertEquals("x", a2.getA());
50  
51  		a1 = new A(null); a2 = new A("2");
52  		am = ObjectMerger.merge(IA.class, a1, a2);
53  		assertEquals("2", am.getA());
54  		am.setA("x");
55  		assertEquals("x", am.getA());
56  		assertEquals("x", a1.getA());
57  		assertEquals("2", a2.getA());
58  
59  		a1 = new A(null); a2 = new A(null);
60  		am = ObjectMerger.merge(IA.class, a1, a2);
61  		assertEquals(null, am.getA());
62  
63  		a1 = new A(null); a2 = new A("2");
64  		am = ObjectMerger.merge(IA.class, null, a1, null, null, a2, null);
65  		assertEquals("2", am.getA());
66  	}
67  
68  	public interface IA {
69  		String getA();
70  		void setA(String a);
71  	}
72  
73  	public static class A implements IA {
74  		public A(String a) {
75  			this.a = a;
76  		}
77  
78  		private String a;
79  		@Override public String getA() { return a; }
80  		@Override public void setA(String v) { a = v; }
81  	}
82  }