001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. 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: ******************************************************************************/package org.mactor.framework;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026: import org.mactor.brokers.Message;
027: import org.mactor.framework.spec.GlobalConfig;
028: import org.mactor.framework.spec.SpecNode;
029: import org.mactor.framework.spec.TestSpec;
030: import org.mactor.framework.spec.GlobalConfig.Group;
031:
032: /**
033: * Maintains context information about the current executing test
034: *
035: * @author Lars Ivar Almli
036: */
037: public class TestContextImpl implements TestContext {
038: private static final String START_TAG = "##{";
039: private static final String END_TAG = "}";
040: private Map<String, String> valueDic = new HashMap<String, String>();
041: private MessageHistoryStore sentMessageHistory = new MessageHistoryStore();
042: private MessageHistoryStore receivedMessageHistory = new MessageHistoryStore();
043: private GlobalConfig globalConfig;
044: private TestSpec testSpec;
045:
046: public TestContextImpl(GlobalConfig globalConfig, TestSpec testSpec) {
047: this .globalConfig = globalConfig;
048: this .testSpec = testSpec;
049: }
050:
051: public void setValues(Map<String, String> nameValueMap) {
052: valueDic.putAll(nameValueMap);
053: }
054:
055: public void setValue(String name, String value) {
056: valueDic.put(name, value);
057: }
058:
059: public Object getValue(String name) {
060: return valueDic.get(name);
061: }
062:
063: public Map<String, String> getValues() {
064: return valueDic; // Collections.unmodifiableMap(
065: }
066:
067: public void addReceivedMessage(String nodeName, Message message) {
068: receivedMessageHistory.addMessage(nodeName, message);
069: }
070:
071: public void addOutgoingMessage(String nodeName, Message message) {
072: sentMessageHistory.addMessage(nodeName, message);
073: }
074:
075: public Message getLastIncomingMessage() {
076: return receivedMessageHistory.getLastMessage();
077: }
078:
079: public Message getLastOutgoingMessage() {
080: return sentMessageHistory.getLastMessage();
081: }
082:
083: public List<Message> getIncomingMessageHistory(String nodeName) {
084: return receivedMessageHistory.getMessageHistory(nodeName);
085: }
086:
087: public List<Message> getOutgoingMessageHistory(String nodeName) {
088: return sentMessageHistory.getMessageHistory(nodeName);
089: }
090:
091: public List<Message> getIncomingMessageHistoryForCurrentNode() {
092: return receivedMessageHistory.getLastModifiedMessageHistory();
093: }
094:
095: public List<Message> getOutgoingMessageHistoryForCurrentNode() {
096: return sentMessageHistory.getLastModifiedMessageHistory();
097: }
098:
099: public List<String> substitute(List<String> candidates) {
100: List<String> results = new LinkedList<String>();
101: for (Iterator iter = candidates.iterator(); iter.hasNext();) {
102: results.add(substitute((String) iter.next()));
103: }
104: return results;
105: }
106:
107: public SpecNode getSpecNode(String nodeName) {
108: return testSpec.findSpecNode(nodeName);
109: }
110:
111: public String substitute(String candidate) {
112: if (candidate == null)
113: return null;
114: StringBuffer sb = new StringBuffer();
115: sub(sb, candidate);
116: String v = sb.toString().trim();
117: if (v.length() == 0)
118: return null;
119: return v;
120: }
121:
122: private void sub(StringBuffer sb, String candidate) {
123: if (candidate == null)
124: return;
125: int start = candidate.indexOf(START_TAG);
126: if (start >= 0) {
127: int end = candidate.indexOf(END_TAG, start + 3);
128: if (end > 0) {
129: String tag = candidate.substring(start + 3, end);
130: if (valueDic.containsKey(tag)) {
131: sb.append(candidate.substring(0, start)).append(
132: valueDic.get(tag));
133: if (end + 1 >= candidate.length())
134: return;
135: else
136: sub(sb, candidate.substring(end + 1, candidate
137: .length()));
138: } else {
139: sb.append(candidate.substring(0, end + 1));
140: if (end + 1 >= candidate.length())
141: return;
142: else
143: sub(sb, candidate.substring(end + 1, candidate
144: .length()));
145: }
146: return;
147: }
148: }
149: sb.append(candidate);
150: }
151:
152: // Config related methods:
153: public GlobalConfig getGlobalConfig() {
154: return globalConfig;
155: }
156:
157: public String getGlobalConfigValue(String valueName) {
158: return globalConfig.getValue(valueName);
159: }
160:
161: public Group getGlobalConfigGroup(String groupName) {
162: return globalConfig.getGroup(groupName);
163: }
164:
165: public Group getRequieredGlobalConfigGroup(String groupName)
166: throws ConfigException {
167: return globalConfig.getRequieredGroup(groupName);
168: }
169:
170: private static class MessageHistoryStore {
171: private MessageHistory lastModifedMessageHistory;
172: private Map<String, MessageHistory> messageHistoryMap = new HashMap<String, MessageHistory>();
173:
174: public synchronized void addMessage(String node, Message message) {
175: MessageHistory mh = messageHistoryMap.get(node);
176: if (mh == null) {
177: mh = new MessageHistory();
178: messageHistoryMap.put(node, mh);
179: }
180: mh.addMessage(message);
181: lastModifedMessageHistory = mh;
182: }
183:
184: public synchronized List<Message> getLastModifiedMessageHistory() {
185: if (lastModifedMessageHistory == null)
186: return null;
187: return lastModifedMessageHistory.getMessages();
188: }
189:
190: public synchronized Message getLastMessage() {
191: if (lastModifedMessageHistory == null)
192: return null;
193: return lastModifedMessageHistory.getLastMessage();
194: }
195:
196: public synchronized List<Message> getMessageHistory(
197: String nodeName) {
198: MessageHistory mh = messageHistoryMap.get(nodeName);
199: if (mh == null)
200: return null;
201: return mh.getMessages();
202: }
203: }
204:
205: public LinkedList<Message> getAllMessages() {
206: LinkedList<Message> all = new LinkedList<Message>();
207: for (MessageHistory mh : sentMessageHistory.messageHistoryMap
208: .values()) {
209: all.addAll(mh.getMessages());
210: }
211: for (MessageHistory mh : receivedMessageHistory.messageHistoryMap
212: .values()) {
213: all.addAll(mh.getMessages());
214: }
215: return all;
216: }
217:
218: private static class MessageHistory {
219: private LinkedList<Message> history = new LinkedList<Message>();
220:
221: public void addMessage(Message message) {
222: history.add(message);
223: }
224:
225: public LinkedList<Message> getMessages() {
226: return history;
227: }
228:
229: public Message getLastMessage() {
230: if (history.size() == 0)
231: return null;
232: return history.getLast();
233: }
234: }
235:
236: public TestSpec getTestSpec() {
237: return testSpec;
238: }
239: }
|