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