1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
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 }