KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > test > TestOneToManyMap


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email ian.dickinson@hp.com
6  * Package @package@
7  * Web site http://jena.sourceforge.net/
8  * Created 21-Jan-2005
9  * Filename $RCSfile: TestOneToManyMap.java,v $
10  * Revision $Revision: 1.2 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:19:22 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
17  * [See end of file]
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.util.test;
23
24
25 // Imports
26
///////////////
27
import java.util.*;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import com.hp.hpl.jena.util.OneToManyMap;
32
33 import junit.framework.TestCase;
34
35
36
37 /**
38  * <p>
39  * Unit tests for one-to-many map
40  * </p>
41  *
42  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
43  * @version Release @release@ ($Id: TestOneToManyMap.java,v 1.2 2005/02/21 12:19:22 andy_seaborne Exp $)
44  */

45 public class TestOneToManyMap
46     extends TestCase
47 {
48     // Constants
49
//////////////////////////////////
50

51     // Static variables
52
//////////////////////////////////
53

54     // Instance variables
55
//////////////////////////////////
56

57     private String JavaDoc s0 = "s0";
58     private String JavaDoc s1 = "s1";
59     private String JavaDoc s2 = "s2";
60     private String JavaDoc s3 = "s3";
61     private String JavaDoc s4 = "s4";
62
63     
64     // Constructors
65
//////////////////////////////////
66

67     // External signature methods
68
//////////////////////////////////
69

70     public void setUp() {
71     }
72     
73     public void tearDown() {
74     }
75     
76     public void testConstruct0() {
77         OneToManyMap map0 = new OneToManyMap();
78         assertNotNull( map0 );
79         
80         assertTrue( map0.isEmpty() );
81         
82         OneToManyMap map1 = new OneToManyMap( map0 );
83         assertNotNull( map1 );
84         assertTrue( map1.isEmpty() );
85     }
86     
87     public void testConstruct1() {
88         OneToManyMap map0 = new OneToManyMap();
89         
90         map0.put( s0, s1 );
91         assertTrue( map0.contains( s0, s1 ) );
92         
93         OneToManyMap map1 = new OneToManyMap( map0 );
94         assertTrue( map0.contains( s0, s1 ) );
95         
96         map0.put( s0, s2 );
97         assertTrue( map0.contains( s0, s2 ) );
98         assertFalse( map0.contains( s1, s2 ) );
99         
100     }
101     
102     public void testClear() {
103         OneToManyMap map0 = new OneToManyMap();
104         
105         map0.put( s0, s1 );
106         assertTrue( map0.contains( s0, s1 ) );
107         assertFalse( map0.isEmpty() );
108         
109         map0.clear();
110         assertFalse( map0.contains( s0, s1 ) );
111         assertTrue( map0.isEmpty() );
112     }
113
114     public void testContainsKey() {
115         OneToManyMap map0 = new OneToManyMap();
116         assertFalse( map0.containsKey( s0 ) );
117         assertFalse( map0.containsKey( s1 ) );
118         map0.put( s0, s1 );
119         assertTrue( map0.containsKey( s0 ) );
120         assertFalse( map0.containsKey( s1 ) );
121     }
122     
123     public void testContainsValue() {
124         OneToManyMap map0 = new OneToManyMap();
125         assertFalse( map0.containsValue( s0 ) );
126         assertFalse( map0.containsValue( s1 ) );
127         assertFalse( map0.containsValue( s2 ) );
128         map0.put( s0, s1 );
129         assertFalse( map0.containsValue( s0 ) );
130         assertTrue( map0.containsValue( s1 ) );
131         assertFalse( map0.containsValue( s2 ) );
132         map0.put( s0, s2 );
133         assertFalse( map0.containsValue( s0 ) );
134         assertTrue( map0.containsValue( s1 ) );
135         assertTrue( map0.containsValue( s2 ) );
136     }
137     
138     public void testContains() {
139         OneToManyMap map0 = new OneToManyMap();
140         assertFalse( map0.contains( s0, s1 ) );
141         assertFalse( map0.contains( s0, s2 ) );
142         assertFalse( map0.contains( s1, s2 ) );
143         map0.put( s0, s1 );
144         assertTrue( map0.contains( s0, s1 ) );
145         assertFalse( map0.contains( s0, s2 ) );
146         assertFalse( map0.contains( s1, s2 ) );
147         map0.put( s0, s2 );
148         assertTrue( map0.contains( s0, s1 ) );
149         assertTrue( map0.contains( s0, s2 ) );
150         assertFalse( map0.contains( s1, s2 ) );
151     }
152     
153     public void testEntrySet() {
154         OneToManyMap map0 = new OneToManyMap();
155         map0.put( s0, s1 );
156         map0.put( s0, s2 );
157         map0.put( s3, s4 );
158         
159         boolean s0s1 = false;
160         boolean s0s2 = false;
161         boolean s3s4 = false;
162         
163         for (Iterator i = map0.entrySet().iterator(); i.hasNext(); ) {
164             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
165             if (e.getKey().equals( s0 ) && e.getValue().equals( s1 )) {
166                 s0s1 = true;
167             }
168             else if (e.getKey().equals( s0 ) && e.getValue().equals( s2 )) {
169                 s0s2 = true;
170             }
171             else if (e.getKey().equals( s3 ) && e.getValue().equals( s4 )) {
172                 s3s4 = true;
173             }
174             else {
175                 throw new IllegalArgumentException JavaDoc( "unexpected: " + e );
176             }
177         }
178         
179         assertTrue( s0s1 );
180         assertTrue( s0s2 );
181         assertTrue( s3s4 );
182     }
183     
184     public void testEquals() {
185         OneToManyMap map0 = new OneToManyMap();
186         map0.put( s0, s1 );
187         map0.put( s0, s2 );
188         map0.put( s3, s4 );
189         OneToManyMap map1 = new OneToManyMap();
190         map1.put( s3, s4 );
191         map1.put( s0, s1 );
192         map1.put( s0, s2 );
193         OneToManyMap map2 = new OneToManyMap();
194         map2.put( s0, s2 );
195         map2.put( s3, s4 );
196         
197         assertTrue( map0.equals( map1 ) );
198         assertTrue( map1.equals( map0 ) );
199         assertTrue( map0.hashCode() == map1.hashCode() );
200         
201         assertFalse( map0.equals( map2 ));
202         assertFalse( map2.equals( map0 ));
203     }
204     
205     public void testGet() {
206         OneToManyMap map0 = new OneToManyMap();
207         assertNull( map0.get( s0 ));
208         map0.put( s0, s1 );
209         assertEquals( s1, map0.get( s0 ));
210         map0.put( s0, s2 );
211         assertTrue( map0.get( s0 ).equals( s1 ) ||
212                     map0.get( s0 ).equals( s2 ));
213     }
214     
215     public void testGetAll() {
216         OneToManyMap map0 = new OneToManyMap();
217         Iterator i = map0.getAll(s0);
218         assertNotNull( i );
219         assertFalse( i.hasNext() );
220
221         map0.put( s0, s1 );
222         i = map0.getAll(s0);
223         assertNotNull( i );
224         assertTrue( i.hasNext() );
225         assertEquals( s1, i.next() );
226         assertFalse( i.hasNext() );
227         
228         map0.put( s0, s2 );
229         i = map0.getAll(s0);
230         assertNotNull( i );
231         boolean founds1 = false, founds2 = false;
232         while (i.hasNext()) {
233             Object JavaDoc x = i.next();
234             if (x.equals(s1)) {
235                 founds1 = true;
236             }
237             else if (x.equals(s2)) {
238                 founds2 = true;
239             }
240             else {
241                 throw new IllegalArgumentException JavaDoc( x.toString() );
242             }
243         }
244         assertTrue( founds1 );
245         assertTrue( founds2 );
246     }
247     
248     public void testKeySet() {
249         OneToManyMap map0 = new OneToManyMap();
250         Set JavaDoc keys = new HashSet JavaDoc();
251         assertEquals( keys, map0.keySet() );
252         
253         map0.put( s0, s1 );
254         keys.add( s0 );
255         assertEquals( keys, map0.keySet() );
256         
257         map0.put( s2, s1 );
258         keys.add( s2 );
259         assertEquals( keys, map0.keySet() );
260     }
261     
262     public void testPutAll0() {
263         OneToManyMap map0 = new OneToManyMap();
264         map0.put( s0, s1 );
265         map0.put( s0, s2 );
266         map0.put( s3, s4 );
267         
268         OneToManyMap map1 = new OneToManyMap();
269         map1.put( s0, s2 );
270         map1.put( s3, s4 );
271         map1.put( s0, s1 );
272
273         OneToManyMap map2 = new OneToManyMap();
274         map2.putAll( map1 );
275         assertEquals( map0, map2 );
276     }
277     
278     public void testPutAll1() {
279         OneToManyMap map0 = new OneToManyMap();
280         map0.put( s0, s1 );
281         map0.put( s3, s4 );
282         
283         Map JavaDoc map1 = new HashMap();
284         map1.put( s3, s4 );
285         map1.put( s0, s1 );
286
287         OneToManyMap map2 = new OneToManyMap();
288         map2.putAll( map1 );
289         assertEquals( map0, map2 );
290     }
291     
292     public void testRemove0() {
293         OneToManyMap map0 = new OneToManyMap();
294         map0.put( s0, s1 );
295         map0.put( s3, s4 );
296         
297         map0.remove( s0 );
298         map0.remove( s3 );
299         
300         assertTrue( map0.isEmpty() );
301     }
302     
303     public void testRemove1() {
304         OneToManyMap map0 = new OneToManyMap();
305         map0.put( s0, s1 );
306         map0.put( s0, s2 );
307         map0.put( s3, s4 );
308         
309         map0.remove( s0, s2 );
310         map0.remove( s3, s4 );
311         
312         assertFalse( map0.isEmpty() );
313         
314         map0.remove( s0, s1 );
315         assertTrue( map0.isEmpty() );
316     }
317     
318     public void testSize() {
319         OneToManyMap map0 = new OneToManyMap();
320         assertEquals( 0, map0.size() );
321         map0.put( s0, s1 );
322         assertEquals( 1, map0.size() );
323         map0.put( s0, s2 );
324         assertEquals( 2, map0.size() );
325         map0.put( s3, s4 );
326         assertEquals( 3, map0.size() );
327         map0.remove( s0, s2 );
328         assertEquals( 2, map0.size() );
329         map0.remove( s3, s4 );
330         assertEquals( 1, map0.size() );
331         map0.remove( s0, s1 );
332         assertEquals( 0, map0.size() );
333     }
334
335     public void testValues() {
336         OneToManyMap map0 = new OneToManyMap();
337         Set JavaDoc vals = new HashSet JavaDoc();
338         assertEquals( vals, map0.values() );
339         
340         map0.put( s0, s1 );
341         vals.add( s1 );
342         assertEquals( vals, map0.values() );
343         
344         map0.put( s2, s1 );
345         assertEquals( vals, map0.values() );
346         
347         map0.put( s2, s3 );
348         vals.add( s3 );
349         assertEquals( vals, map0.values() );
350     }
351     
352     // Internal implementation methods
353
//////////////////////////////////
354

355     //==============================================================================
356
// Inner class definitions
357
//==============================================================================
358

359 }
360
361
362 /*
363  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
364  * All rights reserved.
365  *
366  * Redistribution and use in source and binary forms, with or without
367  * modification, are permitted provided that the following conditions
368  * are met:
369  * 1. Redistributions of source code must retain the above copyright
370  * notice, this list of conditions and the following disclaimer.
371  * 2. Redistributions in binary form must reproduce the above copyright
372  * notice, this list of conditions and the following disclaimer in the
373  * documentation and/or other materials provided with the distribution.
374  * 3. The name of the author may not be used to endorse or promote products
375  * derived from this software without specific prior written permission.
376  *
377  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
378  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
379  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
380  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
381  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
382  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
383  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
384  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
385  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
386  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
387  */

388
Popular Tags