equalsHelper (MoreObjects2)
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / EvenMoreObjectsTest.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.testing.EqualsTester;
12 import java.util.Objects;
13 import org.junit.Test;
14
15 public class EvenMoreObjectsTest {
16
17     @Test
18     public void test() {
19         new EqualsTester()
20                 .addEqualityGroup(new Thing("hello", 123), new Thing("hello", 123))
21                 .addEqualityGroup(new Thing("hoi", 123), new Thing("hoi", 123))
22                 .addEqualityGroup(new Thing("hoi", null))
23                 .addEqualityGroup(new Thing(null, null))
24                 .testEquals();
25     }
26
27     static class Thing {
28
29         String name;
30         Integer age;
31
32         @Override
33         public boolean equals(Object obj) {
34             return EvenMoreObjects.equalsHelper(this, obj,
35                 (one, another) -> Objects.equals(one.name, another.name) && Objects.equals(one.age, another.age));
36         }
37
38         @Override
39         public int hashCode() {
40             return Objects.hash(name, age);
41         }
42
43         @Override
44         public String toString() {
45             return MoreObjects.toStringHelper(this).add("name", name).add("age", age).toString();
46         }
47
48         Thing(String name, Integer age) {
49             super();
50             this.name = name;
51             this.age = age;
52         }
53
54     }
55
56 }