Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 16 | package org.onlab.util; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 17 | |
| 18 | import static junit.framework.TestCase.assertEquals; |
| 19 | import static junit.framework.TestCase.assertFalse; |
| 20 | import static junit.framework.TestCase.assertTrue; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import com.google.common.base.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for Match. |
| 28 | */ |
| 29 | public class MatchTest { |
| 30 | |
| 31 | @Test |
| 32 | public void testMatches() { |
| 33 | Match<String> m1 = Match.any(); |
| 34 | assertTrue(m1.matches(null)); |
| 35 | assertTrue(m1.matches("foo")); |
| 36 | assertTrue(m1.matches("bar")); |
| 37 | |
| 38 | Match<String> m2 = Match.ifNull(); |
| 39 | assertTrue(m2.matches(null)); |
| 40 | assertFalse(m2.matches("foo")); |
| 41 | |
| 42 | Match<String> m3 = Match.ifValue("foo"); |
| 43 | assertFalse(m3.matches(null)); |
| 44 | assertFalse(m3.matches("bar")); |
| 45 | assertTrue(m3.matches("foo")); |
Sarah Feng | 6f9c647 | 2017-06-22 11:39:42 -0700 | [diff] [blame] | 46 | |
| 47 | Match<byte[]> m4 = Match.ifValue(new byte[8]); |
| 48 | assertTrue(m4.matches(new byte[8])); |
| 49 | assertFalse(m4.matches(new byte[7])); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testEquals() { |
| 54 | Match<String> m1 = Match.any(); |
| 55 | Match<String> m2 = Match.any(); |
| 56 | Match<String> m3 = Match.ifNull(); |
| 57 | Match<String> m4 = Match.ifValue("bar"); |
| 58 | assertEquals(m1, m2); |
| 59 | assertFalse(Objects.equal(m1, m3)); |
| 60 | assertFalse(Objects.equal(m3, m4)); |
Sarah Feng | 6f9c647 | 2017-06-22 11:39:42 -0700 | [diff] [blame] | 61 | Object o = new Object(); |
| 62 | assertFalse(Objects.equal(m1, o)); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testMap() { |
| 67 | Match<String> m1 = Match.ifNull(); |
| 68 | assertEquals(m1.map(s -> "bar"), Match.ifNull()); |
| 69 | Match<String> m2 = Match.ifValue("foo"); |
| 70 | Match<String> m3 = m2.map(s -> "bar"); |
| 71 | assertTrue(m3.matches("bar")); |
| 72 | } |
Sarah Feng | 6f9c647 | 2017-06-22 11:39:42 -0700 | [diff] [blame] | 73 | |
| 74 | @Test |
| 75 | public void testIfNotNull() { |
| 76 | Match<String> m = Match.ifNotNull(); |
| 77 | assertFalse(m.matches(null)); |
| 78 | assertTrue(m.matches("foo")); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testIfNotValue() { |
| 83 | Match<String> m1 = Match.ifNotValue(null); |
| 84 | Match<String> m2 = Match.ifNotValue("foo"); |
| 85 | assertFalse(m1.matches(null)); |
| 86 | assertFalse(m2.matches("foo")); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testToString() { |
| 91 | Match<String> m1 = Match.any(); |
| 92 | Match<String> m2 = Match.any(); |
| 93 | Match<String> m3 = Match.ifValue("foo"); |
| 94 | Match<String> m4 = Match.ifValue("foo"); |
| 95 | Match<String> m5 = Match.ifNotValue("foo"); |
| 96 | |
| 97 | String note = "Results of toString() should be consistent -- "; |
| 98 | |
| 99 | assertTrue(note, m1.toString().equals(m2.toString())); |
| 100 | assertTrue(note, m3.toString().equals(m4.toString())); |
| 101 | assertFalse(note, m4.toString().equals(m5.toString())); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testHashCode() { |
| 106 | Match<String> m1 = Match.ifValue("foo"); |
| 107 | Match<String> m2 = Match.ifNotValue("foo"); |
| 108 | Match<String> m3 = Match.ifValue("foo"); |
| 109 | Match<String> m4 = Match.ifNotNull(); |
| 110 | Match<String> m5 = Match.ifNull(); |
| 111 | |
| 112 | assertTrue(m1.hashCode() == m3.hashCode()); |
| 113 | assertFalse(m2.hashCode() == m1.hashCode()); |
| 114 | assertFalse(m4.hashCode() == m5.hashCode()); |
| 115 | |
| 116 | } |
| 117 | |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 118 | } |