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.extensions;
021    
022    import java.util.List;
023    
024    import org.mactor.brokers.Message;
025    import org.mactor.framework.ConfigException;
026    import org.mactor.framework.MactorException;
027    import org.mactor.framework.ParseUtil;
028    import org.mactor.framework.TestContext;
029    import org.mactor.framework.extensioninterface.ActionCommand;
030    import org.mactor.framework.spec.MessagePublishSpec;
031    import org.mactor.framework.spec.MessageReceiveSpec;
032    import org.mactor.framework.spec.SpecNode;
033    
034    /**
035     * Asserts that the number of messages recived by the node specified by the
036     * first parameter (by name) equals the number specifed in the second parameter
037     * 
038     * @author Lars Ivar Almli
039     */
040    public class NodeMessageCountValidator implements ActionCommand {
041            public void perform(TestContext context, List<String> params) throws MactorException {
042                    if (params.size() != 2)
043                            throw new ConfigException("Invalid testspec. Two parameters expected: [<node name>, <message count>]>");
044                    String nodeName = params.get(0);
045                    Integer expectedCount = ParseUtil.tryParseInt(params.get(1));
046                    if (expectedCount == null)
047                            throw new ConfigException("The second parameter must be a numeric value. Was: " + params.get(1));
048                    SpecNode node = context.getSpecNode(nodeName);
049                    if (node == null)
050                            throw new ConfigException("Unknown node:" + nodeName);
051                    int count = 0;
052                    if (node instanceof MessageReceiveSpec) {
053                            List<Message> history = context.getIncomingMessageHistory(nodeName);
054                            if (history != null)
055                                    count = history.size();
056                    } else if (node instanceof MessagePublishSpec) {
057                            List<Message> history = context.getOutgoingMessageHistory(nodeName);
058                            if (history != null)
059                                    count = history.size();
060                    } else {
061                            throw new ConfigException("The specified node is not a message node");
062                    }
063                    if (expectedCount.intValue() != count)
064                            throw new MactorException("Expected '" + expectedCount + "' got '" + count + "'");
065            }
066    }