View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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  package org.supercsv.util;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * Test the TwoDHashMap class.
33   * 
34   * @author James Bassett
35   */
36  public class TwoDHashMapTest {
37  	
38  	// 2D maps of typical orchestral instrument numbers by section and instrument name
39  	private TwoDHashMap<String, String, Integer> orchestraMap;
40  	private TwoDHashMap<String, String, Integer> orchestraMap2;
41  	
42  	/**
43  	 * Sets up the maps TwoDHashMaps for the test, using both constructors.
44  	 * 
45  	 * @throws Exception
46  	 */
47  	@Before
48  	public void setUp() {
49  		orchestraMap = new TwoDHashMap<String, String, Integer>();
50  		populateTwoDHashMap(orchestraMap);
51  		
52  		orchestraMap2 = new TwoDHashMap<String, String, Integer>(new HashMap<String, HashMap<String, Integer>>());
53  		populateTwoDHashMap(orchestraMap2);
54  	}
55  	
56  	/**
57  	 * Populates a TwoDHashMap with test data.
58  	 * 
59  	 * @param map
60  	 *            the map to populate
61  	 */
62  	private static void populateTwoDHashMap(TwoDHashMap<String, String, Integer> map) {
63  		map.set("Woodwind", "Piccolo", 1);
64  		map.set("Woodwind", "Flute", 3);
65  		map.set("Woodwind", "Oboe", 2);
66  		map.set("Woodwind", "Cor Anglais", 1);
67  		map.set("Woodwind", "Clarinet", 3);
68  		map.set("Woodwind", "Bass Clarinet", 1);
69  		map.set("Woodwind", "Soprano Saxophone", 1);
70  		map.set("Woodwind", "Alto Saxophone", 1);
71  		map.set("Woodwind", "Tenor Saxophone", 1);
72  		map.set("Woodwind", "Baritone Saxophone", 1);
73  		map.set("Woodwind", "Bassoon", 2);
74  		map.set("Woodwind", "Contrabassoon", 1);
75  		map.set("Brass", "Horn", 4);
76  		map.set("Brass", "Trumpet", 3);
77  		map.set("Brass", "Trombone", 4);
78  		map.set("Brass", "Tuba", 1);
79  		map.set("Percussion", "Timpani", 2);
80  		map.set("Percussion", "Snare Drum", 1);
81  		map.set("Percussion", "Tenor Drum", 1);
82  		map.set("Percussion", "Bass Drum", 1);
83  		map.set("Percussion", "Cymbals", 1);
84  		map.set("Strings", "Harp", 1);
85  		map.set("Strings", "Violin (I)", 16);
86  		map.set("Strings", "Violin (II)", 16);
87  		map.set("Strings", "Viola", 12);
88  		map.set("Strings", "Cello", 10);
89  		map.set("Strings", "Double Bass", 8);
90  	}
91  	
92  	/**
93  	 * Tests the containsKey() method on both maps.
94  	 */
95  	@Test
96  	public void testContainsKey() {
97  		
98  		final String key1 = "Woodwind";
99  		final String key2 = "Bassoon";
100 		
101 		// both keys exist
102 		assertTrue(orchestraMap.containsKey(key1, key2));
103 		assertTrue(orchestraMap2.containsKey(key1, key2));
104 		
105 		// first key doesn't exist
106 		assertFalse(orchestraMap.containsKey("invalid", key2));
107 		assertFalse(orchestraMap2.containsKey("invalid", key2));
108 		
109 		// second key doesn't exist
110 		assertFalse(orchestraMap.containsKey(key1, "invalid"));
111 		assertFalse(orchestraMap2.containsKey(key1, "invalid"));
112 		
113 	}
114 	
115 	/**
116 	 * Tests the get() method on both maps.
117 	 */
118 	@Test
119 	public void testGet() {
120 		
121 		final String key1 = "Percussion";
122 		final String key2 = "Timpani";
123 		final Integer expectedNo = 2;
124 		
125 		// both keys exist
126 		assertEquals(expectedNo, orchestraMap.get(key1, key2));
127 		assertEquals(expectedNo, orchestraMap2.get(key1, key2));
128 		
129 		// first key doesn't exist
130 		assertNull(orchestraMap.get("invalid", key2));
131 		assertNull(orchestraMap2.get("invalid", key2));
132 		
133 		// second key doesn't exist
134 		assertNull(orchestraMap.get(key1, "invalid"));
135 		assertNull(orchestraMap2.get(key1, "invalid"));
136 		
137 	}
138 	
139 	/**
140 	 * Tests the size() method on both maps.
141 	 */
142 	@Test
143 	public void testSize() {
144 		final int expectedSize = 4;
145 		assertEquals(expectedSize, orchestraMap.size());
146 		assertEquals(expectedSize, orchestraMap2.size());
147 	}
148 	
149 	/**
150 	 * Tests the size() method (for the inner map) on both maps.
151 	 */
152 	@Test
153 	public void testSizeWithParam() {
154 		final int expectedWoodwinds = 12;
155 		final int expectedBrass = 4;
156 		final int expectedPercussion = 5;
157 		final int expectedStrings = 6;
158 		
159 		// first key doesn't exist
160 		assertEquals(0, orchestraMap.size("invalid"));
161 		assertEquals(0, orchestraMap2.size("invalid"));
162 		
163 		assertEquals(expectedWoodwinds, orchestraMap.size("Woodwind"));
164 		assertEquals(expectedWoodwinds, orchestraMap2.size("Woodwind"));
165 		
166 		assertEquals(expectedBrass, orchestraMap.size("Brass"));
167 		assertEquals(expectedBrass, orchestraMap2.size("Brass"));
168 		
169 		assertEquals(expectedPercussion, orchestraMap.size("Percussion"));
170 		assertEquals(expectedPercussion, orchestraMap2.size("Percussion"));
171 		
172 		assertEquals(expectedStrings, orchestraMap.size("Strings"));
173 		assertEquals(expectedStrings, orchestraMap2.size("Strings"));
174 	}
175 	
176 	/**
177 	 * Tests the keySet() method on both maps.
178 	 */
179 	@Test
180 	public void keySet() {
181 		final Set<String> expectedKeys = new HashSet<String>(
182 			Arrays.asList("Woodwind", "Brass", "Percussion", "Strings"));
183 		assertEquals(expectedKeys.size(), orchestraMap.keySet().size());
184 		assertEquals(expectedKeys.size(), orchestraMap2.keySet().size());
185 		assertTrue(orchestraMap.keySet().containsAll(expectedKeys));
186 		assertTrue(orchestraMap2.keySet().containsAll(expectedKeys));
187 	}
188 	
189 	/**
190 	 * Tests the constructor with a null argument (should throw an Exception).
191 	 */
192 	@Test(expected = NullPointerException.class)
193 	public void testConstructorWithNull() {
194 		new TwoDHashMap<String, String, Integer>(null);
195 		
196 	}
197 }