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