2 * Copyright 2016-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils;
18 import com.google.common.base.Objects;
19 import io.atomix.utils.misc.Match;
20 import org.junit.Test;
22 import static junit.framework.TestCase.assertEquals;
23 import static junit.framework.TestCase.assertFalse;
24 import static junit.framework.TestCase.assertTrue;
27 * Unit tests for Match.
29 public class MatchTest {
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"));
38 Match<String> m2 = Match.ifNull();
39 assertTrue(m2.matches(null));
40 assertFalse(m2.matches("foo"));
42 Match<String> m3 = Match.ifValue("foo");
43 assertFalse(m3.matches(null));
44 assertFalse(m3.matches("bar"));
45 assertTrue(m3.matches("foo"));
47 Match<byte[]> m4 = Match.ifValue(new byte[8]);
48 assertTrue(m4.matches(new byte[8]));
49 assertFalse(m4.matches(new byte[7]));
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");
59 assertFalse(Objects.equal(m1, m3));
60 assertFalse(Objects.equal(m3, m4));
61 Object o = new Object();
62 assertFalse(Objects.equal(m1, o));
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"));
75 public void testIfNotNull() {
76 Match<String> m = Match.ifNotNull();
77 assertFalse(m.matches(null));
78 assertTrue(m.matches("foo"));
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"));
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");
97 String note = "Results of toString() should be consistent -- ";
99 assertTrue(note, m1.toString().equals(m2.toString()));
100 assertTrue(note, m3.toString().equals(m4.toString()));
101 assertFalse(note, m4.toString().equals(m5.toString()));
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();
112 assertTrue(m1.hashCode() == m3.hashCode());
113 assertFalse(m2.hashCode() == m1.hashCode());
114 assertFalse(m4.hashCode() == m5.hashCode());