1 package com.mockobjects; 2 3 import java.util.Collection ; 4 import java.util.Enumeration ; 5 import java.util.Iterator ; 6 import java.util.HashSet ; 7 8 abstract public class AbstractExpectationCollection extends AbstractExpectation implements ExpectationCollection { 9 10 public AbstractExpectationCollection(String name) { 11 super(name); 12 } 13 14 public void addActual(Object actualItem) { 15 getActualCollection().add(actualItem); 16 if (shouldCheckImmediately()) { 17 checkImmediateValues(actualItem); 18 } 19 } 20 21 public void addActual(int actualItem) { 22 addActual(new Integer (actualItem)); 23 } 24 25 public void addActualMany(Object [] items) { 26 if(items == null) return; 27 28 for (int i = 0; i < items.length; ++i) { 29 addActual(items[i]); 30 } 31 } 32 33 public void addActualMany(Enumeration items) { 34 while (items.hasMoreElements()) { 35 addActual(items.nextElement()); 36 } 37 } 38 39 public void addActualMany(Iterator items) { 40 while (items.hasNext()) { 41 addActual(items.next()); 42 } 43 } 44 45 public void addExpected(int expectedItem) { 46 addExpected(new Integer (expectedItem)); 47 } 48 49 public void addExpected(Object expectedItem) { 50 getExpectedCollection().add(expectedItem); 51 setHasExpectations(); 52 } 53 54 public void addExpectedMany(Object [] expectedItems) { 55 for (int i = 0; i < expectedItems.length; ++i) { 56 addExpected(expectedItems[i]); 57 } 58 setHasExpectations(); 59 } 60 61 public void addExpectedMany(Enumeration expectedItems) { 62 while (expectedItems.hasMoreElements()) { 63 addExpected(expectedItems.nextElement()); 64 } 65 setHasExpectations(); 66 } 67 68 public void addExpectedMany(Iterator expectedItems) { 69 while (expectedItems.hasNext()) { 70 addExpected(expectedItems.next()); 71 } 72 setHasExpectations(); 73 } 74 75 abstract protected void checkImmediateValues(Object actualItem); 76 77 public void clearActual() { 78 getActualCollection().clear(); 79 } 80 81 protected void clearExpectation() { 82 getExpectedCollection().clear(); 83 } 84 85 abstract protected Collection getActualCollection(); 86 87 abstract protected Collection getExpectedCollection(); 88 89 public void setExpectNothing() { 90 clearExpectation(); 91 setHasExpectations(); 92 } 93 94 public void verify() { 95 assertEquals( 96 "did not receive the expected collection items.", 97 new HashSet(getExpectedCollection()), 98 new HashSet(getActualCollection())); 99 } 100 } 101 | Popular Tags |