package com.xebia.fitnesse; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Stack; import fit.Fixture; import fit.Parse; import fitlibrary.DoFixture; import fitlibrary.ListFixture; public class StackingDoFixture extends DoFixture { private Stack suts = new Stack(); public StackingDoFixture() { } public StackingDoFixture(Object sut) { super(sut); } @Override protected Object interpretCells(Parse cells, Fixture namedFixture) { Object superResult = super.interpretCells(cells, namedFixture); if (superResult != null && superResult.getClass() == DoFixture.class) { // Push the current on the stack for later retrieval. suts.push(getSystemUnderTest()); // Grab the wrapped system under test. setSystemUnderTest(((DoFixture)superResult).getSystemUnderTest()); return null; } else if (superResult instanceof ListFixture) { // Push the current on the stack for later retrieval. suts.push(getSystemUnderTest()); // Grab the wrapped actuals. setSystemUnderTest(getActuals((ListFixture) superResult)); return null; } else { return superResult; } } private Object getActuals(ListFixture superResult) { try { Field field = ListFixture.class.getDeclaredField("actuals"); field.setAccessible(true); return new ArrayList((Collection)field.get(superResult)); } catch (Exception e) { throw new RuntimeException(e); } } public void pop() { setSystemUnderTest(suts.pop()); } }