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