blob: bbcf7ffe8f0f41ab7fea9fdda30046c3cd6cb99f [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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 Jampanif2f086c2016-01-13 16:15:39 -080016package org.onlab.util;
Madan Jampani80984052015-07-23 13:11:36 -070017
18import static junit.framework.TestCase.assertEquals;
19import static junit.framework.TestCase.assertFalse;
20import static junit.framework.TestCase.assertTrue;
21
22import org.junit.Test;
23
24import com.google.common.base.Objects;
25
26/**
27 * Unit tests for Match.
28 */
29public 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 Feng6f9c6472017-06-22 11:39:42 -070046
47 Match<byte[]> m4 = Match.ifValue(new byte[8]);
48 assertTrue(m4.matches(new byte[8]));
49 assertFalse(m4.matches(new byte[7]));
Madan Jampani80984052015-07-23 13:11:36 -070050 }
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 Feng6f9c6472017-06-22 11:39:42 -070061 Object o = new Object();
62 assertFalse(Objects.equal(m1, o));
Madan Jampani80984052015-07-23 13:11:36 -070063 }
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 Feng6f9c6472017-06-22 11:39:42 -070073
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 Jampani80984052015-07-23 13:11:36 -0700118}