1 /* 2 * Copyright (C) 2007 The Guava Authors 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 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.collect.Multiset.Entry; 21 import java.util.Collections; 22 import junit.framework.TestCase; 23 import org.checkerframework.checker.nullness.qual.Nullable; 24 25 /** 26 * Tests for {@link Multisets#immutableEntry}. 27 * 28 * @author Mike Bostock 29 */ 30 @GwtCompatible 31 public class MultisetsImmutableEntryTest extends TestCase { 32 private static final @Nullable String NE = null; 33 entry(final E element, final int count)34 private static <E> Entry<E> entry(final E element, final int count) { 35 return Multisets.immutableEntry(element, count); 36 } 37 control(E element, int count)38 private static <E> Entry<E> control(E element, int count) { 39 return HashMultiset.create(Collections.nCopies(count, element)).entrySet().iterator().next(); 40 } 41 testToString()42 public void testToString() { 43 assertEquals("foo", entry("foo", 1).toString()); 44 assertEquals("bar x 2", entry("bar", 2).toString()); 45 } 46 testToStringNull()47 public void testToStringNull() { 48 assertEquals("null", entry(NE, 1).toString()); 49 assertEquals("null x 2", entry(NE, 2).toString()); 50 } 51 testEquals()52 public void testEquals() { 53 assertEquals(control("foo", 1), entry("foo", 1)); 54 assertEquals(control("bar", 2), entry("bar", 2)); 55 assertFalse(control("foo", 1).equals(entry("foo", 2))); 56 assertFalse(entry("foo", 1).equals(control("bar", 1))); 57 assertFalse(entry("foo", 1).equals(new Object())); 58 assertFalse(entry("foo", 1).equals(null)); 59 } 60 testEqualsNull()61 public void testEqualsNull() { 62 assertEquals(control(NE, 1), entry(NE, 1)); 63 assertFalse(control(NE, 1).equals(entry(NE, 2))); 64 assertFalse(entry(NE, 1).equals(control("bar", 1))); 65 assertFalse(entry(NE, 1).equals(new Object())); 66 assertFalse(entry(NE, 1).equals(null)); 67 } 68 testHashCode()69 public void testHashCode() { 70 assertEquals(control("foo", 1).hashCode(), entry("foo", 1).hashCode()); 71 assertEquals(control("bar", 2).hashCode(), entry("bar", 2).hashCode()); 72 } 73 testHashCodeNull()74 public void testHashCodeNull() { 75 assertEquals(control(NE, 1).hashCode(), entry(NE, 1).hashCode()); 76 } 77 testNegativeCount()78 public void testNegativeCount() { 79 try { 80 entry("foo", -1); 81 fail(); 82 } catch (IllegalArgumentException expected) { 83 } 84 } 85 } 86