1 package edu.umd.cs.findbugs; 2 3 import java.util.Iterator ; 4 import java.util.NoSuchElementException ; 5 6 import junit.framework.Assert; 7 import junit.framework.TestCase; 8 9 public class BugInstanceTest extends TestCase { 10 static { 11 DetectorFactoryCollection.instance(); 13 } 14 15 BugInstance b; 16 17 @Override 19 protected void setUp() throws Exception { 20 b = new BugInstance("NP_NULL_DEREF", Detector.NORMAL_PRIORITY); 21 b.setProperty("A", "a"); 22 b.setProperty("B", "b"); 23 b.setProperty("C", "c"); 24 } 25 26 public void testPropertyIterator() { 27 checkPropertyIterator(b.propertyIterator(), 28 new String []{"A","B","C"}, new String []{"a","b","c"}); 29 } 30 31 public void testRemoveThroughIterator1() { 32 removeThroughIterator(b.propertyIterator(), "A"); 33 checkPropertyIterator(b.propertyIterator(), new String []{"B","C"}, new String []{"b","c"}); 34 } 35 36 public void testRemoveThroughIterator2() { 37 removeThroughIterator(b.propertyIterator(), "B"); 38 checkPropertyIterator(b.propertyIterator(), new String []{"A","C"}, new String []{"a","c"}); 39 } 40 41 public void testRemoveThroughIterator3() { 42 removeThroughIterator(b.propertyIterator(), "C"); 43 checkPropertyIterator(b.propertyIterator(), new String []{"A","B"}, new String []{"a","b"}); 44 } 45 46 public void testIterateTooFar() { 47 Iterator <BugProperty> iter = b.propertyIterator(); 48 get(iter); 49 get(iter); 50 get(iter); 51 noMore(iter); 52 } 53 54 public void testMultipleRemove() { 55 Iterator <BugProperty> iter = b.propertyIterator(); 56 iter.next(); 57 iter.remove(); 58 try { 59 iter.remove(); 60 Assert.assertTrue(false); 61 } catch (IllegalStateException e) { 62 } 64 } 65 66 public void testRemoveBeforeNext() { 67 Iterator <BugProperty> iter = b.propertyIterator(); 68 try { 69 iter.remove(); 70 Assert.assertTrue(false); 71 } catch (IllegalStateException e) { 72 } 74 } 75 76 public void testRemoveAndAdd() { 77 removeThroughIterator(b.propertyIterator(), "C"); 78 b.setProperty("D", "d"); 79 checkPropertyIterator(b.propertyIterator(), 80 new String []{"A","B","D"}, new String []{"a","b","d"}); 81 b.setProperty("E", "e"); 82 checkPropertyIterator(b.propertyIterator(), 83 new String []{"A","B","D","E"}, new String []{"a","b","d","e"}); 84 } 85 86 public void testRemoveAll1() { 87 removeThroughIterator(b.propertyIterator(), "A"); 88 checkPropertyIterator(b.propertyIterator(), new String []{"B","C"}, new String []{"b","c"}); 89 removeThroughIterator(b.propertyIterator(), "B"); 90 checkPropertyIterator(b.propertyIterator(), new String []{"C"}, new String []{"c"}); 91 removeThroughIterator(b.propertyIterator(), "C"); 92 checkPropertyIterator(b.propertyIterator(), new String [0], new String [0]); 93 } 94 95 private void get(Iterator <BugProperty> iter) { 96 try { 97 iter.next(); 98 } catch (NoSuchElementException e) { 100 Assert.assertTrue(false); 101 } 102 } 103 104 private void noMore(Iterator <BugProperty> iter) { 105 try { 106 iter.next(); 107 Assert.assertTrue(false); 108 } catch (NoSuchElementException e) { 109 } 111 } 112 113 private void checkPropertyIterator(Iterator <BugProperty> iter, String [] names, String [] values) { 114 if (names.length != values.length) 115 throw new IllegalArgumentException (); 116 for (int i = 0; i < names.length; ++i) { 117 Assert.assertTrue(iter.hasNext()); 118 String name = names[i]; 119 String value = values[i]; 120 checkProperty(iter.next(), name, value); 121 } 122 Assert.assertFalse(iter.hasNext()); 123 } 124 125 private void checkProperty(BugProperty property, String name, String value) { 126 Assert.assertEquals(property.getName(), name); 127 Assert.assertEquals(property.getValue(), value); 128 } 129 130 private void removeThroughIterator(Iterator <BugProperty> iter, String name) { 131 while (iter.hasNext()) { 132 BugProperty prop = iter.next(); 133 if (prop.getName().equals(name)) 134 iter.remove(); 135 } 136 } 137 } 138 | Popular Tags |