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