@SuppressWarnings("unchecked")
 @Before
 public void beforeTest() {
   this.serializer1 = RecordSerializer.get();
   this.serializer2 = RecordSerializer.get();
   this.comparator1 = new RecordComparator(new int[] {0}, new Class[] {TestData.Key.class});
   this.comparator2 = new RecordComparator(new int[] {0}, new Class[] {TestData.Key.class});
   this.pairComparator =
       new RecordPairComparator(new int[] {0}, new int[] {0}, new Class[] {TestData.Key.class});
 }
public class SpillingResettableMutableObjectIteratorTest {

  private static final int NUM_TESTRECORDS = 50000;

  private static final int MEMORY_CAPACITY = 10 * 1024 * 1024;

  private IOManager ioman;

  private MemoryManager memman;

  private MutableObjectIterator<Record> reader;

  private final TypeSerializer<Record> serializer = RecordSerializer.get();

  @Before
  public void startup() {
    // set up IO and memory manager
    this.memman = new DefaultMemoryManager(MEMORY_CAPACITY, 32 * 1024);
    this.ioman = new IOManager();

    // create test objects
    final ArrayList<Record> objects = new ArrayList<Record>(NUM_TESTRECORDS);

    for (int i = 0; i < NUM_TESTRECORDS; ++i) {
      Record tmp = new Record(new IntValue(i));
      objects.add(tmp);
    }
    this.reader = new MutableObjectIteratorWrapper(objects.iterator());
  }

  @After
  public void shutdown() {
    this.ioman.shutdown();
    if (!this.ioman.isProperlyShutDown()) {
      Assert.fail("I/O Manager Shutdown was not completed properly.");
    }
    this.ioman = null;

    if (!this.memman.verifyEmpty()) {
      Assert.fail(
          "A memory leak has occurred: Not all memory was properly returned to the memory manager.");
    }
    this.memman.shutdown();
    this.memman = null;
  }

  /**
   * Tests the resettable iterator with too little memory, so that the data has to be written to
   * disk.
   */
  @Test
  public void testResettableIterator() {
    try {
      final AbstractInvokable memOwner = new DummyInvokable();

      // create the resettable Iterator
      SpillingResettableMutableObjectIterator<Record> iterator =
          new SpillingResettableMutableObjectIterator<Record>(
              this.reader, this.serializer, this.memman, this.ioman, 2, memOwner);

      // open the iterator
      iterator.open();

      // now test walking through the iterator
      int count = 0;
      Record target = new Record();
      while ((target = iterator.next(target)) != null) {
        Assert.assertEquals(
            "In initial run, element " + count + " does not match expected value!",
            count++,
            target.getField(0, IntValue.class).getValue());
      }
      Assert.assertEquals(
          "Too few elements were deserialzied in initial run!", NUM_TESTRECORDS, count);
      // test resetting the iterator a few times
      for (int j = 0; j < 10; ++j) {
        count = 0;
        iterator.reset();
        target = new Record();
        // now we should get the same results
        while ((target = iterator.next(target)) != null) {
          Assert.assertEquals(
              "After reset nr. " + j + 1 + " element " + count + " does not match expected value!",
              count++,
              target.getField(0, IntValue.class).getValue());
        }
        Assert.assertEquals(
            "Too few elements were deserialzied after reset nr. " + j + 1 + "!",
            NUM_TESTRECORDS,
            count);
      }
      // close the iterator
      iterator.close();
    } catch (Exception ex) {
      ex.printStackTrace();
      Assert.fail("Test encountered an exception.");
    }
  }

  /**
   * Tests the resettable iterator with enough memory so that all data is kept locally in memory.
   */
  @Test
  public void testResettableIteratorInMemory() {
    try {
      final AbstractInvokable memOwner = new DummyInvokable();

      // create the resettable Iterator
      SpillingResettableMutableObjectIterator<Record> iterator =
          new SpillingResettableMutableObjectIterator<Record>(
              this.reader, this.serializer, this.memman, this.ioman, 20, memOwner);

      // open the iterator
      iterator.open();

      // now test walking through the iterator
      int count = 0;
      Record target = new Record();
      while ((target = iterator.next(target)) != null) {
        Assert.assertEquals(
            "In initial run, element " + count + " does not match expected value!",
            count++,
            target.getField(0, IntValue.class).getValue());
      }
      Assert.assertEquals(
          "Too few elements were deserialzied in initial run!", NUM_TESTRECORDS, count);
      // test resetting the iterator a few times
      for (int j = 0; j < 10; ++j) {
        count = 0;
        iterator.reset();
        target = new Record();
        // now we should get the same results
        while ((target = iterator.next(target)) != null) {
          Assert.assertEquals(
              "After reset nr. " + j + 1 + " element " + count + " does not match expected value!",
              count++,
              target.getField(0, IntValue.class).getValue());
        }
        Assert.assertEquals(
            "Too few elements were deserialzied after reset nr. " + j + 1 + "!",
            NUM_TESTRECORDS,
            count);
      }
      // close the iterator
      iterator.close();
    } catch (Exception ex) {
      ex.printStackTrace();
      Assert.fail("Test encountered an exception.");
    }
  }
}