Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | |
| 7 | import java.util.Arrays; |
| 8 | import java.util.HashSet; |
| 9 | import java.util.Set; |
| 10 | |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | /** |
| 14 | * Base facilities to test various intent tests. |
| 15 | */ |
| 16 | public abstract class IntentTest { |
| 17 | /** |
| 18 | * Produces a set of items from the supplied items. |
| 19 | * |
| 20 | * @param items items to be placed in set |
| 21 | * @param <T> item type |
| 22 | * @return set of items |
| 23 | */ |
| 24 | protected static <T> Set<T> itemSet(T[] items) { |
| 25 | return new HashSet<>(Arrays.asList(items)); |
| 26 | } |
| 27 | |
| 28 | @Test |
| 29 | public void equalsAndHashCode() { |
| 30 | Intent one = createOne(); |
| 31 | Intent like = createOne(); |
| 32 | Intent another = createAnother(); |
| 33 | |
| 34 | assertTrue("should be equal", one.equals(like)); |
| 35 | assertEquals("incorrect hashCode", one.hashCode(), like.hashCode()); |
| 36 | |
| 37 | assertFalse("should not be equal", one.equals(another)); |
| 38 | |
| 39 | assertFalse("should not be equal", one.equals(null)); |
| 40 | assertFalse("should not be equal", one.equals("foo")); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void testToString() { |
| 45 | Intent one = createOne(); |
| 46 | Intent like = createOne(); |
| 47 | assertEquals("incorrect toString", one.toString(), like.toString()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new intent, but always a like intent, i.e. all instances will |
| 52 | * be equal, but should not be the same. |
| 53 | * |
| 54 | * @return intent |
| 55 | */ |
| 56 | protected abstract Intent createOne(); |
| 57 | |
| 58 | /** |
| 59 | * Creates another intent, not equals to the one created by |
| 60 | * {@link #createOne()} and with a different hash code. |
| 61 | * |
| 62 | * @return another intent |
| 63 | */ |
| 64 | protected abstract Intent createAnother(); |
| 65 | } |