59bcd8521763bbe9e8e53e20d42ec8707461debf
[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 static org.junit.Assert.assertTrue;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.testing.EqualsTester;
14 import java.util.Objects;
15 import org.junit.Test;
16
17 public class EvenMoreObjectsTest {
18
19     @Test
20     public void thingPassesEqualsTester() {
21         new EqualsTester()
22                 .addEqualityGroup(new Thing("hello", 123), new Thing("hello", 123))
23                 .addEqualityGroup(new Thing("hoi", 123), new Thing("hoi", 123))
24                 .addEqualityGroup(new Thing("hoi", null))
25                 .addEqualityGroup(new Thing(null, null))
26                 .testEquals();
27     }
28
29     @Test
30     public void nullEqualsNull() {
31         assertTrue(EvenMoreObjects.equalsHelper(null, null, (one, another) -> true));
32     }
33
34     private static class Thing {
35
36         String name;
37         Integer age;
38
39         @Override
40         public boolean equals(Object obj) {
41             return EvenMoreObjects.equalsHelper(this, obj,
42                 (one, another) -> Objects.equals(one.name, another.name) && Objects.equals(one.age, another.age));
43         }
44
45         @Override
46         public int hashCode() {
47             return Objects.hash(name, age);
48         }
49
50         @Override
51         public String toString() {
52             return MoreObjects.toStringHelper(this).add("name", name).add("age", age).toString();
53         }
54
55         Thing(String name, Integer age) {
56             super();
57             this.name = name;
58             this.age = age;
59         }
60
61     }
62
63 }