package iterator;
import java.util.Iterator;
import java.util.Random;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class IteratorTest {
@Test(dataProvider = "generate-accounts-lazy")
public void testAccount(Account a) {
System.out.println("Testing account: " + a);
}
@DataProvider(name = "generate-accounts-lazy")
public Iterator genetateAccountsLazy() {
return new AccountIterator();
}
@DataProvider(name = "generate-accounts")
public Object[][] generateAccounts() {
return new Object[][] {
new Object[] { new Account() },
new Object[] { new Account() },
new Object[] { new Account() },
new Object[] { new Account() }
};
}
}
class AccountIterator implements Iterator {
private static final int MAX = 4;
private static int index = 0;
@Override
public boolean hasNext() {
return index < MAX;
}
@Override
public Object next() {
index++;
return new Object[] { new Account() };
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* 帐号
*/
class Account {
private String number;
public Account() {
System.out.println("Creating account");
this.number = "" + new Random().nextInt();
}
public String toString() {
return "account -> " + number;
}
}