/*
* Created by Andrey Cherkashin (acherkashin)
* http://acherkashin.me
*
* License
* Copyright (c) 2015 Andrey Cherkashin
* The project released under the MIT license: http://opensource.org/licenses/MIT
*/
package ragefist.core;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author acherkashin
*/
public class ProcessorTest
{
private Processor _processor;
private List<Integer> _testList;
@Before
public void setUp() {
_processor = new Processor();
_testList = new ArrayList<>();
}
private abstract class TestProcessorTask extends ProcessorTask {
private final List<Integer> _testListRef;
private TestProcessorTask(List<Integer> testList) {
_testListRef = testList;
}
}
@Test
public void testTaskProcessing() {
TestProcessorTask task = new TestProcessorTask(_testList)
{
@Override
protected boolean _executeImpl(Processor processor) {
_testList.add(1);
return true;
}
};
// adding a task
_processor.addTask(task);
Assert.assertEquals(0, _testList.size());
// processing tasks
_processor.processTasks();
Assert.assertEquals(1, _testList.size());
}
@Test
public void testTasksChain() {
TestProcessorTask task1 = new TestProcessorTask(_testList)
{
@Override
protected boolean _executeImpl(Processor processor) {
_testList.add(1);
return true;
}
};
TestProcessorTask task2 = new TestProcessorTask(_testList)
{
@Override
protected boolean _executeImpl(Processor processor) {
_testList.add(2);
return false; // returning FALSE
}
};
TestProcessorTask task3 = new TestProcessorTask(_testList)
{
@Override
protected boolean _executeImpl(Processor processor) {
_testList.add(3);
return true;
}
};
task2.setNextTask(task3);
task1.setNextTask(task2);
_processor.addTask(task1);
// check we have only one task in a queue
Assert.assertEquals(1, _processor.getTasksCount());
Assert.assertEquals(0, _testList.size());
// processing
_processor.processTasks();
// check our chain is processed even though task #2 failed
Assert.assertEquals(3, _testList.size());
Assert.assertTrue(1 == _testList.get(0));
Assert.assertTrue(2 == _testList.get(1));
Assert.assertTrue(3 == _testList.get(2));
}
}