001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email ian.dickinson@hp.com
006: * Package @package@
007: * Web site http://jena.sourceforge.net/
008: * Created 21-Jan-2005
009: * Filename $RCSfile: TestOneToManyMap.java,v $
010: * Revision $Revision: 1.5 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:08:35 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * [See end of file]
018: *****************************************************************************/package com.hp.hpl.jena.util.test;
019:
020: // Imports
021: ///////////////
022: import java.util.*;
023: import java.util.HashSet;
024: import java.util.Map;
025:
026: import com.hp.hpl.jena.util.OneToManyMap;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * <p>
032: * Unit tests for one-to-many map
033: * </p>
034: *
035: * @author Ian Dickinson, HP Labs (<a href="mailto:Ian.Dickinson@hp.com">email</a>)
036: * @version Release @release@ ($Id: TestOneToManyMap.java,v 1.5 2008/01/02 12:08:35 andy_seaborne Exp $)
037: */
038: public class TestOneToManyMap extends TestCase {
039: // Constants
040: //////////////////////////////////
041:
042: // Static variables
043: //////////////////////////////////
044:
045: // Instance variables
046: //////////////////////////////////
047:
048: private String s0 = "s0";
049: private String s1 = "s1";
050: private String s2 = "s2";
051: private String s3 = "s3";
052: private String s4 = "s4";
053:
054: // Constructors
055: //////////////////////////////////
056:
057: // External signature methods
058: //////////////////////////////////
059:
060: public void setUp() {
061: }
062:
063: public void tearDown() {
064: }
065:
066: public void testConstruct0() {
067: OneToManyMap map0 = new OneToManyMap();
068: assertNotNull(map0);
069:
070: assertTrue(map0.isEmpty());
071:
072: OneToManyMap map1 = new OneToManyMap(map0);
073: assertNotNull(map1);
074: assertTrue(map1.isEmpty());
075: }
076:
077: public void testConstruct1() {
078: OneToManyMap map0 = new OneToManyMap();
079:
080: map0.put(s0, s1);
081: assertTrue(map0.contains(s0, s1));
082:
083: OneToManyMap map1 = new OneToManyMap(map0);
084: assertTrue(map0.contains(s0, s1));
085:
086: map0.put(s0, s2);
087: assertTrue(map0.contains(s0, s2));
088: assertFalse(map0.contains(s1, s2));
089:
090: }
091:
092: public void testClear() {
093: OneToManyMap map0 = new OneToManyMap();
094:
095: map0.put(s0, s1);
096: assertTrue(map0.contains(s0, s1));
097: assertFalse(map0.isEmpty());
098:
099: map0.clear();
100: assertFalse(map0.contains(s0, s1));
101: assertTrue(map0.isEmpty());
102: }
103:
104: public void testContainsKey() {
105: OneToManyMap map0 = new OneToManyMap();
106: assertFalse(map0.containsKey(s0));
107: assertFalse(map0.containsKey(s1));
108: map0.put(s0, s1);
109: assertTrue(map0.containsKey(s0));
110: assertFalse(map0.containsKey(s1));
111: }
112:
113: public void testContainsValue() {
114: OneToManyMap map0 = new OneToManyMap();
115: assertFalse(map0.containsValue(s0));
116: assertFalse(map0.containsValue(s1));
117: assertFalse(map0.containsValue(s2));
118: map0.put(s0, s1);
119: assertFalse(map0.containsValue(s0));
120: assertTrue(map0.containsValue(s1));
121: assertFalse(map0.containsValue(s2));
122: map0.put(s0, s2);
123: assertFalse(map0.containsValue(s0));
124: assertTrue(map0.containsValue(s1));
125: assertTrue(map0.containsValue(s2));
126: }
127:
128: public void testContains() {
129: OneToManyMap map0 = new OneToManyMap();
130: assertFalse(map0.contains(s0, s1));
131: assertFalse(map0.contains(s0, s2));
132: assertFalse(map0.contains(s1, s2));
133: map0.put(s0, s1);
134: assertTrue(map0.contains(s0, s1));
135: assertFalse(map0.contains(s0, s2));
136: assertFalse(map0.contains(s1, s2));
137: map0.put(s0, s2);
138: assertTrue(map0.contains(s0, s1));
139: assertTrue(map0.contains(s0, s2));
140: assertFalse(map0.contains(s1, s2));
141: }
142:
143: public void testEntrySet() {
144: OneToManyMap map0 = new OneToManyMap();
145: map0.put(s0, s1);
146: map0.put(s0, s2);
147: map0.put(s3, s4);
148:
149: boolean s0s1 = false;
150: boolean s0s2 = false;
151: boolean s3s4 = false;
152:
153: for (Iterator i = map0.entrySet().iterator(); i.hasNext();) {
154: Map.Entry e = (Map.Entry) i.next();
155: if (e.getKey().equals(s0) && e.getValue().equals(s1)) {
156: s0s1 = true;
157: } else if (e.getKey().equals(s0) && e.getValue().equals(s2)) {
158: s0s2 = true;
159: } else if (e.getKey().equals(s3) && e.getValue().equals(s4)) {
160: s3s4 = true;
161: } else {
162: throw new IllegalArgumentException("unexpected: " + e);
163: }
164: }
165:
166: assertTrue(s0s1);
167: assertTrue(s0s2);
168: assertTrue(s3s4);
169: }
170:
171: public void testEquals() {
172: OneToManyMap map0 = new OneToManyMap();
173: map0.put(s0, s1);
174: map0.put(s0, s2);
175: map0.put(s3, s4);
176: OneToManyMap map1 = new OneToManyMap();
177: map1.put(s3, s4);
178: map1.put(s0, s1);
179: map1.put(s0, s2);
180: OneToManyMap map2 = new OneToManyMap();
181: map2.put(s0, s2);
182: map2.put(s3, s4);
183:
184: assertTrue(map0.equals(map1));
185: assertTrue(map1.equals(map0));
186: assertTrue(map0.hashCode() == map1.hashCode());
187:
188: assertFalse(map0.equals(map2));
189: assertFalse(map2.equals(map0));
190: }
191:
192: public void testGet() {
193: OneToManyMap map0 = new OneToManyMap();
194: assertNull(map0.get(s0));
195: map0.put(s0, s1);
196: assertEquals(s1, map0.get(s0));
197: map0.put(s0, s2);
198: assertTrue(map0.get(s0).equals(s1) || map0.get(s0).equals(s2));
199: }
200:
201: public void testGetAll() {
202: OneToManyMap map0 = new OneToManyMap();
203: Iterator i = map0.getAll(s0);
204: assertNotNull(i);
205: assertFalse(i.hasNext());
206:
207: map0.put(s0, s1);
208: i = map0.getAll(s0);
209: assertNotNull(i);
210: assertTrue(i.hasNext());
211: assertEquals(s1, i.next());
212: assertFalse(i.hasNext());
213:
214: map0.put(s0, s2);
215: i = map0.getAll(s0);
216: assertNotNull(i);
217: boolean founds1 = false, founds2 = false;
218: while (i.hasNext()) {
219: Object x = i.next();
220: if (x.equals(s1)) {
221: founds1 = true;
222: } else if (x.equals(s2)) {
223: founds2 = true;
224: } else {
225: throw new IllegalArgumentException(x.toString());
226: }
227: }
228: assertTrue(founds1);
229: assertTrue(founds2);
230: }
231:
232: public void testKeySet() {
233: OneToManyMap map0 = new OneToManyMap();
234: Set keys = new HashSet();
235: assertEquals(keys, map0.keySet());
236:
237: map0.put(s0, s1);
238: keys.add(s0);
239: assertEquals(keys, map0.keySet());
240:
241: map0.put(s2, s1);
242: keys.add(s2);
243: assertEquals(keys, map0.keySet());
244: }
245:
246: public void testPutAll0() {
247: OneToManyMap map0 = new OneToManyMap();
248: map0.put(s0, s1);
249: map0.put(s0, s2);
250: map0.put(s3, s4);
251:
252: OneToManyMap map1 = new OneToManyMap();
253: map1.put(s0, s2);
254: map1.put(s3, s4);
255: map1.put(s0, s1);
256:
257: OneToManyMap map2 = new OneToManyMap();
258: map2.putAll(map1);
259: assertEquals(map0, map2);
260: }
261:
262: public void testPutAll1() {
263: OneToManyMap map0 = new OneToManyMap();
264: map0.put(s0, s1);
265: map0.put(s3, s4);
266:
267: Map map1 = new HashMap();
268: map1.put(s3, s4);
269: map1.put(s0, s1);
270:
271: OneToManyMap map2 = new OneToManyMap();
272: map2.putAll(map1);
273: assertEquals(map0, map2);
274: }
275:
276: public void testRemove0() {
277: OneToManyMap map0 = new OneToManyMap();
278: map0.put(s0, s1);
279: map0.put(s3, s4);
280:
281: map0.remove(s0);
282: map0.remove(s3);
283:
284: assertTrue(map0.isEmpty());
285: }
286:
287: public void testRemove1() {
288: OneToManyMap map0 = new OneToManyMap();
289: map0.put(s0, s1);
290: map0.put(s0, s2);
291: map0.put(s3, s4);
292:
293: map0.remove(s0, s2);
294: map0.remove(s3, s4);
295:
296: assertFalse(map0.isEmpty());
297:
298: map0.remove(s0, s1);
299: assertTrue(map0.isEmpty());
300: }
301:
302: public void testSize() {
303: OneToManyMap map0 = new OneToManyMap();
304: assertEquals(0, map0.size());
305: map0.put(s0, s1);
306: assertEquals(1, map0.size());
307: map0.put(s0, s2);
308: assertEquals(2, map0.size());
309: map0.put(s3, s4);
310: assertEquals(3, map0.size());
311: map0.remove(s0, s2);
312: assertEquals(2, map0.size());
313: map0.remove(s3, s4);
314: assertEquals(1, map0.size());
315: map0.remove(s0, s1);
316: assertEquals(0, map0.size());
317: }
318:
319: public void testValues() {
320: OneToManyMap map0 = new OneToManyMap();
321: Set vals = new HashSet();
322: assertEquals(vals, map0.values());
323:
324: map0.put(s0, s1);
325: vals.add(s1);
326: assertEquals(vals, map0.values());
327:
328: map0.put(s2, s1);
329: assertEquals(vals, map0.values());
330:
331: map0.put(s2, s3);
332: vals.add(s3);
333: assertEquals(vals, map0.values());
334: }
335:
336: // Internal implementation methods
337: //////////////////////////////////
338:
339: //==============================================================================
340: // Inner class definitions
341: //==============================================================================
342:
343: }
344:
345: /*
346: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
347: * All rights reserved.
348: *
349: * Redistribution and use in source and binary forms, with or without
350: * modification, are permitted provided that the following conditions
351: * are met:
352: * 1. Redistributions of source code must retain the above copyright
353: * notice, this list of conditions and the following disclaimer.
354: * 2. Redistributions in binary form must reproduce the above copyright
355: * notice, this list of conditions and the following disclaimer in the
356: * documentation and/or other materials provided with the distribution.
357: * 3. The name of the author may not be used to endorse or promote products
358: * derived from this software without specific prior written permission.
359: *
360: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
361: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
362: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
363: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
364: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
365: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
366: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
368: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
369: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
370: */
|