001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015 package lapisx.util;
016
017 import junit.framework.*;
018 import java.util.*;
019
020 /**
021 * A collection of JUnit testcases to test the SafeIteratorSet class.
022 */
023 public class SafeIteratorSetTest extends TestCase {
024
025 public SafeIteratorSetTest (String name) {
026 super(name);
027 }
028
029 protected void setUp () {
030 }
031
032 public static Test suite () {
033 return new TestSuite (SafeIteratorSetTest.class);
034 }
035
036 public void testSafeIteration () {
037 // fill a SafeIteratorSet from another set
038 Set s1 = new HashSet (Arrays.asList (new String[] {"a", "b", "c"}));
039 Set s2 = new SafeIteratorSet (s1);
040 assertEquals (s1, s2);
041
042 // test whether s2's iterator returns same values as s1
043 Iterator g1 = s2.iterator ();
044 assertEquals (s1, accumulate (g1));
045
046 // survives adds?
047 Iterator g2 = s2.iterator ();
048 s2.add ("d");
049 assertEquals (s1, accumulate (g2));
050 s1.add ("d");
051
052 // survives removes?
053 Iterator g3 = s2.iterator ();
054 s2.remove ("a");
055 assertEquals (s1, accumulate (g3));
056 s1.remove ("a");
057
058 // survives clear?
059 Iterator g4 = s2.iterator ();
060 s2.clear ();
061 assertEquals (s1, accumulate (g4));
062
063 // iterator itself refuses remove?
064 Iterator g5 = s2.iterator ();
065 try {
066 g5.remove ();
067 fail ();
068 } catch (UnsupportedOperationException e) {
069 }
070 }
071
072 private Set accumulate (Iterator g) {
073 Set s = new HashSet ();
074 while (g.hasNext ())
075 s.add (g.next ());
076 return s;
077 }
078 }
079