001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.framework;
021    
022    import java.util.Iterator;
023    import java.util.LinkedList;
024    
025    public class EventReporter implements Runnable, TestFeedbackListener {
026            LinkedList<EI> eventQueue = new LinkedList<EI>();
027            private Object lock = new Object();
028            private TestFeedbackListener listener;
029            private boolean stopped = false;
030            private String testRunInstanceId;
031            public EventReporter(String testRunInstanceId, TestFeedbackListener listener) {
032                    super();
033                    this.listener = listener;
034                    this.testRunInstanceId = testRunInstanceId;
035            }
036            public void onNodeEvent(TestEvent event, TestContext context) {
037                    EI e = new EI();
038                    e.te = event;
039                    e.context = context;
040                    synchronized (lock) {
041                            eventQueue.add(e);
042                            lock.notifyAll();
043                    }
044            }
045            public void onTestRunCompleted(String testRunInstanceId, int succededCount, int failedCount) {
046            }
047            int succededCount;
048            int failedCount;
049            public void run() {
050                    while (!stopped) {
051                            LinkedList<EI> current = null;
052                            synchronized (lock) {
053                                    if (eventQueue.size() == 0) {
054                                            try {
055                                                    lock.wait();
056                                            } catch (InterruptedException ie) {
057                                            }
058                                    }
059                                    if (eventQueue.size() > 0) {
060                                            current = eventQueue;
061                                            eventQueue = new LinkedList<EI>();
062                                    }
063                            }
064                            if (current != null) {
065                                    Iterator<EI> it = current.iterator();
066                                    while (it.hasNext()) {
067                                            EI e = (EI) it.next();
068                                            if (e.te.isTestCompleteEvent()) {
069                                                    if (e.te.isSuccessful())
070                                                            succededCount++;
071                                                    else
072                                                            failedCount++;
073                                            }
074                                            listener.onNodeEvent(e.te, e.context);
075                                    }
076                            }
077                    }
078                    listener.onTestRunCompleted(testRunInstanceId, succededCount, failedCount);
079            }
080            public void start() {
081                    stopped = false;
082                    new Thread(this).start();
083            }
084            public void stop() {
085                    stopped = true;
086                    synchronized (lock) {
087                            lock.notifyAll();
088                    }
089            }
090            private static class EI {
091                    TestEvent te;
092                    TestContext context;
093            }
094    }