1 package org.apidesign.reentrant;
3 import java.util.ArrayList;
4 import java.util.Iterator;
7 import static org.junit.Assert.*;
9 public abstract class CriticalSectionBase {
10 protected abstract CriticalSection<Integer> create();
13 public void testCriticalSectionWith15() {
14 final CriticalSection<Integer> cs = create();
18 final void testFor15(CriticalSection<Integer> cs) {
21 List<Integer> ints = new ArrayList<Integer>();
31 int cnt = cs.sumBigger(ints);
33 assertEquals("18 is bigger than 15", 1, cnt);
38 public void teaseCriticalSectionWithReentrantCalls() {
39 final CriticalSection<Integer> cs = create();
43 class ChangePilotTo15 implements Runnable {
49 List<Integer> ints = new MyCollection(new ChangePilotTo15(), 3);
59 int cnt = cs.sumBigger(ints);
61 assertEquals("11, 12, 18, 13 are bigger than 10", 4, cnt);
63 assertEquals("Global count is sum of ints(e.g. 4) plus result of testFor15(e.g. 1)", 5, cs.getCount());
66 class MyCollection extends ArrayList<Integer> {
67 private final Runnable callback;
68 private final int callbackBeforeIndex;
70 public MyCollection(Runnable callback, int callbackAtIndex) {
71 this.callback = callback;
72 this.callbackBeforeIndex = callbackAtIndex;
76 public Iterator<Integer> iterator() {
77 final Iterator<Integer> delegate = super.iterator();
78 class It implements Iterator<Integer> {
81 public boolean hasNext() {
82 return delegate.hasNext();
85 public Integer next() {
86 if (index++ == callbackBeforeIndex) {
90 return delegate.next();
93 public void remove() {