001: package org.depunit;
002:
003: import java.util.*;
004:
005: public class ProcessQueue {
006: private class ProcessLoop {
007: private DepLink m_start;
008: private DepLink m_end;
009: private ProcessLoop m_innerLoop;
010: private DepLink m_loopPos;
011: private DataDriver m_dataDriver;
012:
013: public ProcessLoop(DepLink start, DataDriver dd) {
014: //System.out.println("Innerloop");
015: start.setLoopStart();
016: m_dataDriver = dd;
017: m_innerLoop = null;
018: m_start = start;
019: m_loopPos = start;
020: // TODO: find end
021: m_end = getLastLink(m_start);
022: }
023:
024: private DepLink getLastLink(DepLink start) {
025: DepLink last = start;
026: Set<DepLink> set = start.getHardObservers();
027:
028: Iterator<DepLink> it = set.iterator();
029: while (it.hasNext()) {
030: //We recursively look for the very last dependency link
031: DepLink dl = getLastLink(it.next());
032: if (dl.getPosition() > last.getPosition())
033: last = dl;
034: }
035:
036: return (last);
037: }
038:
039: public DepLink getLoopEnd() {
040: return (m_end);
041: }
042:
043: public DepLink getNextMethod() {
044: DepLink ret = null;
045: if (m_innerLoop != null) {
046: ret = m_innerLoop.getNextMethod();
047: if (ret == null) //Inner loop is done
048: {
049: //System.out.println("Inner loop done");
050: m_loopPos = m_innerLoop.getLoopEnd().getNext();
051: m_innerLoop = null;
052: }
053: }
054:
055: if ((ret == null) && (m_loopPos != m_end.getNext())) {
056: ret = m_loopPos;
057: m_loopPos = m_loopPos.getNext();
058: //Check for Data driver
059: DataDriver dd = ((TestMethod) ret).getTestClass()
060: .getDataDriver();
061: if ((dd != null) && (dd != m_dataDriver)
062: && dd.hasNextDataSet()) {
063: //System.out.println("NEW PROCESS LOOP");
064:
065: m_innerLoop = new ProcessLoop(ret, dd);
066: ret = m_innerLoop.getNextMethod();
067: }
068: }
069:
070: //If still null check to see if we need to loop again
071: if (ret == null) {
072: if (m_dataDriver.hasNextDataSet()) {
073: m_loopPos = m_start.getNext();
074: ret = m_start;
075: m_dataDriver.lockReset();
076: ((TestMethod) ret).resetStatus();
077: m_dataDriver.unlockReset();
078: } else
079: m_dataDriver.reset();
080: }
081:
082: return (ret);
083: }
084: }
085:
086: private HashSet<TestMethod> m_queueLookup;
087: //private List<TestMethod> m_processList;
088: //private int m_runPos;
089: private ProcessLoop m_innerLoop;
090:
091: private DepLink m_head;
092: private DepLink m_tail;
093: private DepLink m_runPos;
094:
095: public ProcessQueue() {
096: m_head = new DepLink() //Create a dead head
097: {
098: public void update(Observable o, Object arg) {
099: }
100: };
101: m_tail = m_head;
102: m_runPos = m_head;
103: m_queueLookup = new HashSet<TestMethod>();
104: //m_processList = new ArrayList<TestMethod>();
105: }
106:
107: public boolean contains(TestMethod tm) {
108: return (m_queueLookup.contains(tm));
109: }
110:
111: public void add(TestMethod tm) {
112: //System.out.println("Adding "+tm.getFullName());
113: m_queueLookup.add(tm);
114: //System.out.println("Position "+m_queueLookup.size());
115: tm.setPosition(m_queueLookup.size());
116: //m_processList.add(tm);
117: m_tail.setNext(tm);
118: m_tail = tm;
119: }
120:
121: public synchronized TestMethod getNextMethod() {
122: //System.out.println("ProcessQueue.getNextMethod");
123: TestMethod ret = null;
124: if (m_innerLoop != null) {
125: ret = (TestMethod) m_innerLoop.getNextMethod();
126: if (ret == null) //Inner loop is done
127: {
128: //System.out.println("Loop done");
129: m_runPos = m_innerLoop.getLoopEnd();
130: m_innerLoop = null;
131: }
132: }
133:
134: if ((ret == null)
135: && ((ret = (TestMethod) m_runPos.getNext()) != null)) {
136: //Check ret for data driver
137: DataDriver dd = ret.getTestClass().getDataDriver();
138: if (dd != null) {
139: //System.out.println("NEW PROCES LOOP");
140:
141: m_innerLoop = new ProcessLoop(ret, dd);
142: ret = (TestMethod) m_innerLoop.getNextMethod();
143: }
144:
145: m_runPos = m_runPos.getNext();
146: }
147:
148: /* if (ret != null)
149: System.out.println("Returning "+ret.getFullName());
150: else
151: System.out.println("Returning null"); */
152:
153: return (ret);
154: }
155: }
|