|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2020, Optimizely and contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.optimizely.ab.config.audience.match; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +public class ExactMatchTest { |
| 29 | + |
| 30 | + private ExactMatch match; |
| 31 | + private static final List<Object> INVALIDS = Collections.unmodifiableList(Arrays.asList(new byte[0], new Object(), null)); |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + match = new ExactMatch(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testInvalidConditionValues() { |
| 40 | + for (Object invalid : INVALIDS) { |
| 41 | + try { |
| 42 | + match.eval(invalid, "valid"); |
| 43 | + fail("should have raised exception"); |
| 44 | + } catch (UnexpectedValueTypeException e) { |
| 45 | + //pass |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testMismatchClasses() throws Exception { |
| 52 | + assertNull(match.eval(false, "false")); |
| 53 | + assertNull(match.eval("false", null)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testStringMatch() throws Exception { |
| 58 | + assertEquals(Boolean.TRUE, match.eval("", "")); |
| 59 | + assertEquals(Boolean.TRUE, match.eval("true", "true")); |
| 60 | + assertEquals(Boolean.FALSE, match.eval("true", "false")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testBooleanMatch() throws Exception { |
| 65 | + assertEquals(Boolean.TRUE, match.eval(true, true)); |
| 66 | + assertEquals(Boolean.TRUE, match.eval(false, false)); |
| 67 | + assertEquals(Boolean.FALSE, match.eval(true, false)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testNumberMatch() throws UnexpectedValueTypeException { |
| 72 | + assertEquals(Boolean.TRUE, match.eval(1, 1)); |
| 73 | + assertEquals(Boolean.TRUE, match.eval(1L, 1L)); |
| 74 | + assertEquals(Boolean.TRUE, match.eval(1.0, 1.0)); |
| 75 | + assertEquals(Boolean.TRUE, match.eval(1, 1.0)); |
| 76 | + assertEquals(Boolean.TRUE, match.eval(1L, 1.0)); |
| 77 | + |
| 78 | + assertEquals(Boolean.FALSE, match.eval(1, 2)); |
| 79 | + assertEquals(Boolean.FALSE, match.eval(1L, 2L)); |
| 80 | + assertEquals(Boolean.FALSE, match.eval(1.0, 2.0)); |
| 81 | + assertEquals(Boolean.FALSE, match.eval(1, 1.1)); |
| 82 | + assertEquals(Boolean.FALSE, match.eval(1L, 1.1)); |
| 83 | + } |
| 84 | +} |
0 commit comments