2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.concepts.Identifier;
22 public abstract class AbstractIdentifierTest<T extends Identifier> {
25 abstract T differentObject();
27 abstract T equalObject();
30 public final void testEquals() {
31 assertTrue(object().equals(object()));
32 assertTrue(object().equals(equalObject()));
33 assertFalse(object().equals(null));
34 assertFalse(object().equals("dummy"));
35 assertFalse(object().equals(differentObject()));
39 public final void testHashCode() {
40 assertEquals(object().hashCode(), equalObject().hashCode());
43 @SuppressWarnings("unchecked")
44 private static <T> T copy(T obj) throws IOException, ClassNotFoundException {
45 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
46 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
50 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
51 return (T) ois.readObject();
56 public final void testSerialization() throws Exception {
57 assertTrue(object().equals(copy(object())));
58 assertTrue(object().equals(copy(equalObject())));
59 assertFalse(differentObject().equals(copy(object())));