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.HashSet;
25  import java.util.Set;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  /**
31   * Test the ThreeDHashMap class.
32   * 
33   * @author James Bassett
34   */
35  public class ThreeDHashMapTest {
36  	
37  	// 3D map of people's ages by country, city and name
38  	private ThreeDHashMap<String, String, String, Integer> personMap;
39  	
40  	/**
41  	 * Sets up the map for the test.
42  	 */
43  	@Before
44  	public void setUp() {
45  		personMap = new ThreeDHashMap<String, String, String, Integer>();
46  		personMap.set("Australia", "Brisbane", "Jim", 26);
47  		personMap.set("Australia", "Brisbane", "Jane", 57);
48  		personMap.set("Australia", "Sydney", "Harry", 19);
49  		personMap.set("New Zealand", "Auckland", "Sally", 34);
50  		personMap.set("New Zealand", "Wellington", "John", 88);
51  		personMap.set("United Kingdom", "London", "Elizabeth", 34);
52  		personMap.set("India", "Mumbai", "Vernon", 27);
53  	}
54  	
55  	/**
56  	 * Tests the containsKey() method (with 2 parameters).
57  	 */
58  	@Test
59  	public void testContainsKeyWithTwoParams() {
60  		
61  		final String firstKey = "Australia";
62  		final String secondKey = "Sydney";
63  		
64  		// both keys exist
65  		assertTrue(personMap.containsKey(firstKey, secondKey));
66  		
67  		// first key doesn't exist
68  		assertFalse(personMap.containsKey("invalid", secondKey));
69  		
70  		// second key doesn't exist
71  		assertFalse(personMap.containsKey(firstKey, "invalid"));
72  		
73  	}
74  	
75  	/**
76  	 * Tests the containsKey() method (with 3 parameters).
77  	 */
78  	@Test
79  	public void testContainsKeyWithThreeParams() {
80  		
81  		final String firstKey = "United Kingdom";
82  		final String secondKey = "London";
83  		final String thirdKey = "Elizabeth";
84  		
85  		// both keys exist
86  		assertTrue(personMap.containsKey(firstKey, secondKey, thirdKey));
87  		
88  		// first key doesn't exist
89  		assertFalse(personMap.containsKey("invalid", secondKey, thirdKey));
90  		
91  		// second key doesn't exist
92  		assertFalse(personMap.containsKey(firstKey, "invalid", thirdKey));
93  		
94  		// third key doesn't exist
95  		assertFalse(personMap.containsKey(firstKey, secondKey, "invalid"));
96  		
97  	}
98  	
99  	/**
100 	 * Tests the get() method (with 1 parameter).
101 	 */
102 	@Test
103 	public void testGet() {
104 		
105 		// invalid key
106 		assertNull(personMap.get("invalid"));
107 		
108 		assertEquals(2, personMap.get("Australia").size());
109 		assertEquals(2, personMap.get("New Zealand").size());
110 		assertEquals(1, personMap.get("United Kingdom").size());
111 		assertEquals(1, personMap.get("India").size());
112 	}
113 	
114 	/**
115 	 * Tests the getAs2d() method.
116 	 */
117 	@Test
118 	public void testGetAs2d() {
119 		
120 		// invalid key (should be empty)
121 		assertEquals(0, personMap.getAs2d("invalid").size());
122 		
123 		assertEquals(2, personMap.getAs2d("Australia").size());
124 		assertEquals(2, personMap.getAs2d("New Zealand").size());
125 		assertEquals(1, personMap.getAs2d("United Kingdom").size());
126 		assertEquals(1, personMap.getAs2d("India").size());
127 	}
128 	
129 	/**
130 	 * Tests the get() method (with 2 parameters).
131 	 */
132 	@Test
133 	public void testGetWithTwoParams() {
134 		
135 		// invalid first key
136 		assertNull(personMap.get("United States", "New York"));
137 		
138 		// invalid second key
139 		assertNull(personMap.get("India", "Chennai"));
140 		
141 		assertEquals(2, personMap.get("Australia", "Brisbane").size());
142 		assertEquals(1, personMap.get("Australia", "Sydney").size());
143 		assertEquals(1, personMap.get("New Zealand", "Auckland").size());
144 		assertEquals(1, personMap.get("New Zealand", "Wellington").size());
145 		assertEquals(1, personMap.get("United Kingdom", "London").size());
146 		assertEquals(1, personMap.get("India", "Mumbai").size());
147 	}
148 	
149 	/**
150 	 * Tests the get() method (with 3 parameters).
151 	 */
152 	@Test
153 	public void testGetWithThreeParams() {
154 		personMap.get(null, null, null);
155 		
156 		// invalid first key
157 		assertNull(personMap.get("United States", "New York", "George"));
158 		
159 		// invalid second key
160 		assertNull(personMap.get("India", "Chennai", "Raj"));
161 		
162 		// invalid third key
163 		assertNull(personMap.get("India", "Mumbai", "Raj"));
164 		
165 		assertEquals(26, personMap.get("Australia", "Brisbane", "Jim").intValue());
166 		assertEquals(57, personMap.get("Australia", "Brisbane", "Jane").intValue());
167 		assertEquals(19, personMap.get("Australia", "Sydney", "Harry").intValue());
168 		assertEquals(34, personMap.get("New Zealand", "Auckland", "Sally").intValue());
169 		assertEquals(88, personMap.get("New Zealand", "Wellington", "John").intValue());
170 		assertEquals(34, personMap.get("United Kingdom", "London", "Elizabeth").intValue());
171 		assertEquals(27, personMap.get("India", "Mumbai", "Vernon").intValue());
172 	}
173 	
174 	/**
175 	 * Tests the size() method (with no parameters).
176 	 */
177 	@Test
178 	public void testSize() {
179 		assertEquals(4, personMap.size());
180 	}
181 	
182 	/**
183 	 * Tests the size() method (with 1 parameter).
184 	 */
185 	@Test
186 	public void testSizeWithOneParam() {
187 		
188 		// invalid key
189 		assertEquals(0, personMap.size("United States"));
190 		
191 		assertEquals(2, personMap.size("Australia"));
192 		assertEquals(2, personMap.size("New Zealand"));
193 		assertEquals(1, personMap.size("United Kingdom"));
194 		assertEquals(1, personMap.size("India"));
195 	}
196 	
197 	/**
198 	 * Tests the size() method (with 2 parameters).
199 	 */
200 	@Test
201 	public void testSizeWithTwoParams() {
202 		
203 		// invalid first key
204 		assertEquals(0, personMap.size("United States", "New York"));
205 		
206 		// invalid second key
207 		assertEquals(0, personMap.size("India", "Chennai"));
208 		
209 		assertEquals(2, personMap.size("Australia", "Brisbane"));
210 		assertEquals(1, personMap.size("Australia", "Sydney"));
211 		assertEquals(1, personMap.size("New Zealand", "Auckland"));
212 		assertEquals(1, personMap.size("New Zealand", "Wellington"));
213 		assertEquals(1, personMap.size("United Kingdom", "London"));
214 		assertEquals(1, personMap.size("India", "Mumbai"));
215 	}
216 	
217 	/**
218 	 * Tests the keySet() method.
219 	 */
220 	@Test
221 	public void keySet() {
222 		final Set<String> expectedKeys = new HashSet<String>(Arrays.asList("Australia", "New Zealand",
223 			"United Kingdom", "India"));
224 		assertEquals(expectedKeys.size(), personMap.keySet().size());
225 		assertTrue(personMap.keySet().containsAll(expectedKeys));
226 	}
227 	
228 }