01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.extensions;
20:
21: import java.util.List;
22: import org.mactor.brokers.Message;
23: import org.mactor.framework.ConfigException;
24: import org.mactor.framework.MactorException;
25: import org.mactor.framework.ParseUtil;
26: import org.mactor.framework.TestContext;
27: import org.mactor.framework.extensioninterface.ActionCommand;
28: import org.mactor.framework.spec.MessagePublishSpec;
29: import org.mactor.framework.spec.MessageReceiveSpec;
30: import org.mactor.framework.spec.SpecNode;
31:
32: /**
33: * Asserts that the number of messages recived by the node specified by the first parameter (by name)
34: * equals the number specifed in the second parameter
35: *
36: * @author Lars Ivar Almli
37: */
38:
39: public class NodeMessageCountValidator implements ActionCommand {
40: public void perform(TestContext context, List<String> params)
41: throws MactorException {
42: if (params.size() != 2)
43: throw new ConfigException(
44: "Invalid testspec. Two parameters requiered: [<node name>, <message count>]>");
45: String nodeName = params.get(0);
46: Integer expectedCount = ParseUtil.tryParseInt(params.get(1));
47: if (expectedCount == null)
48: throw new ConfigException(
49: "The second parameter must be a numeric value. Was: "
50: + params.get(1));
51: SpecNode node = context.getSpecNode(nodeName);
52: if (node == null)
53: throw new ConfigException("Unknown node:" + nodeName);
54: int count = 0;
55: if (node instanceof MessageReceiveSpec) {
56: List<Message> history = context
57: .getIncomingMessageHistory(nodeName);
58: if (history != null)
59: count = history.size();
60: } else if (node instanceof MessagePublishSpec) {
61: List<Message> history = context
62: .getOutgoingMessageHistory(nodeName);
63: if (history != null)
64: count = history.size();
65: } else {
66: throw new ConfigException(
67: "The specified node is not a message node");
68: }
69: if (expectedCount.intValue() != count)
70: throw new MactorException("Expected '" + expectedCount
71: + "' got '" + count + "'");
72: }
73: }
|