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.spec;
021    
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    import org.dom4j.Document;
027    import org.dom4j.Element;
028    import org.mactor.framework.MactorException;
029    import org.mactor.framework.ParseUtil;
030    
031    public class TestSpec extends ContainerSpec {
032            @Override
033            public String getType() {
034                    return "test";
035            }
036            private int delayBeforeStartSeconds;
037            private Map<String, SpecNode> nodeMap = new HashMap<String, SpecNode>();
038            private Document doc;
039            public String asXML() {
040                    return XmlUtil.getPrettyXML(doc.getRootElement());
041            }
042            private void buildIndex() throws MactorException {
043                    addContainerSpecToMap(this);
044            }
045            private void addContainerSpecToMap(ContainerSpec node) throws MactorException {
046                    addToNodeMap(node);
047                    if (node instanceof MessageReceiveSpec && !nodeMap.containsKey(((MessageReceiveSpec) node).getMessageSubscribeNodeName())) {
048                            throw new MactorException("The MessageReceiveSpec node '" + node.getName() + "' refers to an unknown MessageSubscribeSpec node '"
049                                            + ((MessageReceiveSpec) node).getMessageSubscribeNodeName() + "'");
050                    }
051                    Iterator it = node.getSpecNodes().iterator();
052                    while (it.hasNext()) {
053                            SpecNode n = (SpecNode) it.next();
054                            if (n instanceof ContainerSpec) {
055                                    addContainerSpecToMap((ContainerSpec) n);
056                            } else {
057                                    addToNodeMap(n);
058                            }
059                    }
060            }
061            private void addToNodeMap(SpecNode node) throws MactorException {
062                    if (nodeMap.containsKey(node.getName())) {
063                            throw new MactorException("Nameable nodes in the test-spec must have unique names. The name '" + node.getName() + "' has been used on multiple nodes");
064                    }
065                    nodeMap.put(node.getName(), node);
066            }
067            public int getDelayBeforeStartSeconds() {
068                    return delayBeforeStartSeconds;
069            }
070            public SpecNode findSpecNode(String nodeName) {
071                    return nodeMap.get(nodeName);
072            }
073            public static TestSpec loadFromFile(String name) throws MactorException {
074                    return TestSpec.loadFromDocument(ProjectContext.getGlobalInstance().readFromFile(name, false), name);
075            }
076            public static TestSpec loadFromDocument(Document doc, String name) throws MactorException {
077                    if (doc == null)
078                            throw new MactorException("Invalid document: null");
079                    Element element = doc.getRootElement();
080                    if (element == null)
081                            throw new MactorException("Invalid Test: null");
082                    if (!"test".equals(element.getName()))
083                            throw new MactorException("Invalid Test spec. Root node: '" + element.getName() + "'. Expected: 'test'");
084                    TestSpec s = new TestSpec();
085                    s.name = name;
086                    s.doc = element.getDocument();
087                    s.delayBeforeStartSeconds = ParseUtil.tryParseIntVal(element.attributeValue("delay_before_start_seconds"));
088                    s.loadContainedNodes(element);
089                    s.buildIndex();
090                    return s;
091            }
092            public Element addToElement(Element e) {
093                    throw new RuntimeException("Not implemented");
094            }
095            public String getShortDescription() {
096                    return "Test node - '" + name + "'";
097            }
098            public String getDescription() {
099                    return "Delay defore start (seconds):" + delayBeforeStartSeconds;
100            }
101    }