001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.web.framework;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.apache.log4j.Logger;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034:
035: import edu.iu.uis.eden.test.web.framework.actions.AssertAction;
036: import edu.iu.uis.eden.test.web.framework.actions.EchoAction;
037: import edu.iu.uis.eden.test.web.framework.actions.ParametersAction;
038: import edu.iu.uis.eden.test.web.framework.actions.SleepAction;
039: import edu.iu.uis.eden.test.web.framework.actions.SubmitAction;
040: import edu.iu.uis.eden.test.web.framework.actions.UserAction;
041: import edu.iu.uis.eden.test.web.framework.actions.VariableAction;
042: import edu.iu.uis.eden.util.XmlHelper;
043:
044: /**
045: * A web site interaction script.
046: * In general, where it makes sense, action attributes are inspected according to the
047: * algorithm defined in {@link edu.iu.uis.eden.test.web.framework.Util#getResolvableAttribute(Node, String, PropertyScheme)}
048: * <pre>
049: * <script>
050: * ... script actions ...
051: * </script>
052: * </pre>
053: * @author Aaron Hamid (arh14 at cornell dot edu)
054: */
055: public class Script {
056: private static final Logger LOG = Logger.getLogger(Script.class);
057:
058: private static final Class[] SCRIPT_ACTIONS = { AssertAction.class,
059: EchoAction.class, ParametersAction.class,
060: SleepAction.class, SubmitAction.class, UserAction.class,
061: VariableAction.class };
062:
063: private static final Map ACTION_MAP = new HashMap();
064: static {
065: for (int i = 0; i < SCRIPT_ACTIONS.length; i++) {
066: Class c = SCRIPT_ACTIONS[i];
067: ScriptAction action;
068: try {
069: action = (ScriptAction) c.newInstance();
070: } catch (IllegalAccessException iae) {
071: throw new RuntimeException(
072: "Error loading ScriptAction: " + c, iae);
073: } catch (InstantiationException ie) {
074: throw new RuntimeException(
075: "Error loading ScriptAction: " + c, ie);
076: }
077: String[] names = action.getNames();
078: for (int j = 0; j < names.length; j++) {
079: ACTION_MAP.put(names[j], action);
080: }
081: }
082: }
083:
084: private static final InteractionController MOCK_INTERACTION_CONTROLLER = new InteractionController() {
085: public String submit(String method, String uri, Script script)
086: throws Exception {
087: return null;
088: }
089: };
090:
091: private Document script;
092: private InteractionController controller;
093: private ScriptState state = new ScriptState();
094:
095: public Script(String script) throws IOException,
096: ParserConfigurationException, SAXException {
097: this (script, MOCK_INTERACTION_CONTROLLER);
098: }
099:
100: public Script(String script, InteractionController controller)
101: throws IOException, ParserConfigurationException,
102: SAXException {
103: this (new ByteArrayInputStream(script.getBytes()), controller);
104: }
105:
106: public Script(InputStream script) throws IOException,
107: ParserConfigurationException, SAXException {
108: this (script, MOCK_INTERACTION_CONTROLLER);
109: }
110:
111: public Script(InputStream script, InteractionController controller)
112: throws IOException, ParserConfigurationException,
113: SAXException {
114: this (new InputSource(script), controller);
115: }
116:
117: public Script(InputSource script) throws IOException,
118: ParserConfigurationException, SAXException {
119: this (script, MOCK_INTERACTION_CONTROLLER);
120: }
121:
122: public Script(Document doc) {
123: this (doc, MOCK_INTERACTION_CONTROLLER);
124: }
125:
126: public Script(InputSource script, InteractionController controller)
127: throws IOException, ParserConfigurationException,
128: SAXException {
129: this (DocumentBuilderFactory.newInstance().newDocumentBuilder()
130: .parse(script), controller);
131: }
132:
133: public Script(Document script, InteractionController controller) {
134: this .script = script;
135: this .controller = controller;
136: }
137:
138: public InteractionController getController() {
139: return controller;
140: }
141:
142: public ScriptState getState() {
143: return state;
144: }
145:
146: public void run(Map context) {
147: // init standard script variables
148: state.reset();
149: state.setContext(context);
150:
151: NodeList list = script.getDocumentElement().getChildNodes();
152:
153: Node node = null;
154: for (int i = 0; i < list.getLength(); i++) {
155: node = list.item(i);
156: if (node.getNodeType() != Node.ELEMENT_NODE) {
157: continue;
158: }
159:
160: String name = node.getNodeName();
161: LOG.debug("Processing: " + name);
162:
163: ScriptAction action = (ScriptAction) ACTION_MAP.get(name);
164: if (action == null) {
165: LOG.error("Invalid action: '" + name + "'");
166: return;
167: }
168: try {
169: action.process(this , node);
170: } catch (RuntimeException e) {
171: LOG.error("Exception occurred at node: "
172: + XmlHelper.jotNode(node));
173: throw e;
174: } catch (Error e) {
175: LOG.error("Error occurred at node: "
176: + XmlHelper.jotNode(node));
177: throw e;
178: }
179: }
180: }
181: }
|