BUG-5280: add cds-access-api identifiers
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / AbstractIdentifierTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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.controller.cluster.access.concepts;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.concepts.Identifier;
20
21 public abstract class AbstractIdentifierTest<T extends Identifier> {
22     abstract T object();
23     abstract T differentObject();
24     abstract T equalObject();
25
26     @Test
27     public final void testEquals() {
28         assertTrue(object().equals(object()));
29         assertTrue(object().equals(equalObject()));
30         assertFalse(object().equals(null));
31         assertFalse(object().equals("dummy"));
32         assertFalse(object().equals(differentObject()));
33     }
34
35     @Test
36     public final void testHashCode() {
37         assertEquals(object().hashCode(), equalObject().hashCode());
38     }
39
40     @SuppressWarnings("unchecked")
41     private static <T> T copy(T o) throws IOException, ClassNotFoundException {
42         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
43         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
44             oos.writeObject(o);
45         }
46
47         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
48             return (T) ois.readObject();
49         }
50     }
51
52     @Test
53     public final void testSerialization() throws Exception {
54         assertTrue(object().equals(copy(object())));
55         assertTrue(object().equals(copy(equalObject())));
56         assertFalse(differentObject().equals(copy(object())));
57     }
58 }