001/*
002 * #%L
003 * Netarchivesuite - common - test
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.testutils;
025
026import java.util.ArrayList;
027import java.util.Iterator;
028import java.util.List;
029
030import javax.jms.Message;
031import javax.jms.MessageListener;
032
033import dk.netarkivet.common.distribute.JMSConnection;
034import dk.netarkivet.common.distribute.NetarkivetMessage;
035
036/**
037 * A simple message listener that collects the messages given to it and lets you query them
038 */
039
040public class TestMessageListener implements MessageListener {
041    protected List<NetarkivetMessage> received = new ArrayList<NetarkivetMessage>();
042
043    public TestMessageListener() {
044    }
045
046    public void onMessage(Message msg) {
047        synchronized (this) {
048            NetarkivetMessage content = JMSConnection.unpack(msg);
049            received.add(content);
050        }
051    }
052
053    /**
054     * Get the last message received.
055     *
056     * @return The last message received by the listener.
057     * @throws IndexOutOfBoundsException if no messages have been received.
058     */
059    public NetarkivetMessage getReceived() {
060        return received.get(received.size() - 1);
061    }
062
063    /**
064     * Return the number of messages received so far.
065     *
066     * @return the number of messages received so far
067     */
068    public int getNumReceived() {
069        return received.size();
070    }
071
072    /**
073     * get a list of all messages received.
074     *
075     * @return a list of all messages received
076     */
077    public List<NetarkivetMessage> getAllReceived() {
078        return received;
079    }
080
081    /**
082     * Reset the list of messages returned.
083     */
084    public void reset() {
085        received.clear();
086    }
087
088    /**
089     * Returns the number of received messages that were ok.
090     *
091     * @return the number of received messages that were ok
092     */
093    public int getNumOk() {
094        int count = 0;
095        for (Iterator<NetarkivetMessage> i = received.iterator(); i.hasNext();) {
096            NetarkivetMessage msg = i.next();
097            if (msg.isOk()) {
098                count++;
099            }
100        }
101        return count;
102    }
103
104    /**
105     * Returns the number of received messages that were not ok.
106     *
107     * @return the number of received messages that were not ok
108     */
109    public int getNumNotOk() {
110        int count = 0;
111        for (Iterator<NetarkivetMessage> i = received.iterator(); i.hasNext();) {
112            NetarkivetMessage msg = i.next();
113            if (!msg.isOk()) {
114                count++;
115            }
116        }
117        return count;
118    }
119
120    /**
121     * Looks up the messages that are instances of the given class.
122     *
123     * @param msgClass A subclass of NetarkivetMessage, e.g. BatchMessage.class;
124     * @return The instance that was most recently received, or null if no messages of the specified type has been
125     * received.
126     */
127    @SuppressWarnings("rawtypes")
128    public NetarkivetMessage getLastInstance(Class msgClass) {
129        NetarkivetMessage result = null;
130        for (NetarkivetMessage msg : received) {
131            if (msgClass.isInstance(msg)) {
132                result = msg;
133            }
134        }
135        return result;
136    }
137}