Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 18 | import org.junit.Test; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Arrays; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Set; |
| 23 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.*; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Base facilities to test various intent tests. |
| 28 | */ |
| 29 | public abstract class IntentTest { |
| 30 | /** |
| 31 | * Produces a set of items from the supplied items. |
| 32 | * |
| 33 | * @param items items to be placed in set |
| 34 | * @param <T> item type |
| 35 | * @return set of items |
| 36 | */ |
| 37 | protected static <T> Set<T> itemSet(T[] items) { |
| 38 | return new HashSet<>(Arrays.asList(items)); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void equalsAndHashCode() { |
| 43 | Intent one = createOne(); |
| 44 | Intent like = createOne(); |
| 45 | Intent another = createAnother(); |
| 46 | |
| 47 | assertTrue("should be equal", one.equals(like)); |
| 48 | assertEquals("incorrect hashCode", one.hashCode(), like.hashCode()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 49 | assertFalse("should not be equal", one.equals(another)); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testToString() { |
| 54 | Intent one = createOne(); |
| 55 | Intent like = createOne(); |
| 56 | assertEquals("incorrect toString", one.toString(), like.toString()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a new intent, but always a like intent, i.e. all instances will |
| 61 | * be equal, but should not be the same. |
| 62 | * |
| 63 | * @return intent |
| 64 | */ |
| 65 | protected abstract Intent createOne(); |
| 66 | |
| 67 | /** |
| 68 | * Creates another intent, not equals to the one created by |
| 69 | * {@link #createOne()} and with a different hash code. |
| 70 | * |
| 71 | * @return another intent |
| 72 | */ |
| 73 | protected abstract Intent createAnother(); |
| 74 | } |