Permalink
Browse files

Added dummy transport regular expressions

You can use the 'dummy' transport to test scripts, and configure regular expressions to match the messages your script will send.
  • Loading branch information...
ianso committed Nov 28, 2011
1 parent 94766ce commit 1d94d2580a187869a280a05a47c80674f86711e8
View
@@ -30,7 +30,7 @@ A Scriptus server can be stopped at any time. Programs will continue where they
(Scriptus uses Rhino and serialisable continuations to make long-term persistence of program state possible, and can uses either an in-memory, filesystem or AWS-backed datastore.)
-[The user guide documents how to run Scriptus](https://github.com/ianso/scriptus/blob/master/docs/userguide.md), and [the API documentation shows how to use it](https://github.com/ianso/scriptus/blob/master/docs/api.md). More is available via [the documentation index](https://github.com/ianso/scriptus/blob/master/docs/index.md).
+[The Scriptus minisite](http://ianso.github.com/scriptus/) includes a blog + RSS feed for notable updates. [The user guide documents how to run Scriptus](https://github.com/ianso/scriptus/blob/master/docs/userguide.md), and [the API documentation shows how to use it](https://github.com/ianso/scriptus/blob/master/docs/api.md). More is available at [the documentation index](https://github.com/ianso/scriptus/blob/master/docs/index.md).
You need JDK 6 or more recent, and [Maven 2.2+](http://maven.apache.org/) to build Scriptus. Once you've checked out the project, you need one command:
View
@@ -55,11 +55,11 @@ Using AWS also means that programs cannot be run offline, for obvious reasons.
##Choosing a transport
-The transport is the means via which Scriptus programs interact with their users. There are three mediums and they all have their uses.
+The transport is the means via which Scriptus programs interact with their users. There are three transports and they all have their uses.
+
+ * **Dummy response** just responds with a predetermined response to every `ask()` and not at all to a `listen()`.
+It can be useful for testing simple scripts. To help with this goal, it checks the messages against a series of regular expressions and can respond with a replacement expression if one matches, otherwise a default response is returned. The regular expressions are in the file `src/main/resources/spring/scriptus.xml`.
- * **Dummy response** just responds with "response" to every `ask()` and not at all to a `listen()`.
-It can be useful for testing simple scripts. In future a more flexible dummy will be put in place to allow for better automated testing of scripts.
-
* **Command prompt** sends all `ask()`s and `listen()`s to the command line, where the local user can respond.
To *not* respond simply hit enter when you're prompted for your response.
View
@@ -1,10 +1,14 @@
mvnDebug jetty:run -Dscriptus.config=test-scriptus.properties
mvnDebug jetty:run -Dscriptus.config=command-line-scriptus.properties
- regexp based responses in dummy
+!! WAR deployable for v0.1.0 & executable JAR?
+
+test regexp based responses in dummy
+
+
- test twitter integration
- - listen
+ twitter integration
+test - listen
- line diet
- simple calculus
@@ -2,7 +2,12 @@
package net.ex337.scriptus.transport.impl;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import net.ex337.scriptus.model.api.Message;
import net.ex337.scriptus.transport.Transport;
@@ -12,9 +17,9 @@
/**
* Dummy transport. Never responds to listen()s, and
- * always responds to ask()s with the supplied response.
+ * always responds to ask()s with the supplied defaultResponse.
*
- * The one response configured in the Spring configuration file.
+ * The one defaultResponse configured in the Spring configuration file.
* TODO A regexp-based matching might be a good idea to make testing script branches easier.
*/
public class DummyTransport implements Transport {
@@ -23,7 +28,21 @@
private MessageReceiver receiver;
- public String response;
+ private Map<String,String> regexpResponseMatchers = new HashMap<String, String>();
+ private Map<Pattern,String> cachedPatterns = new HashMap<Pattern, String>();
+
+ public String defaultResponse;
+
+ public void init() {
+ cachedPatterns = new HashMap<Pattern, String>();
+
+ for(Map.Entry<String, String> e : regexpResponseMatchers.entrySet()) {
+ Pattern p = Pattern.compile(e.getKey());
+ cachedPatterns.put(p, e.getValue());
+
+ }
+
+ }
//@Override
private void send(final UUID pid, final String to, final String msg) {
@@ -34,12 +53,35 @@ private void send(final UUID pid, final String to, final String msg) {
return;
}
- receiver.handleIncomings(new ArrayList<Message>() {{
- add(new Message(pid, to, response));
- }});
+ List<Message> responseList = new ArrayList<Message>();
+ responseList.add(new Message(pid, to, getResponse(msg)));
+
+ receiver.handleIncomings(responseList);
}
+ /**
+ * public for testing
+ * @param msg
+ * @return
+ */
+ public String getResponse(final String msg) {
+
+ String result = null;
+
+ for(Map.Entry<Pattern,String> e : cachedPatterns.entrySet()) {
+ Matcher m;
+ if((m = e.getKey().matcher(msg)).matches()) {
+ result = m.replaceAll(e.getValue());
+ break;
+ }
+
+ }
+
+ if(result == null) result = defaultResponse;
+ return result;
+ }
+
@Override
public void registerReceiver(MessageReceiver londonCalling) {
@@ -63,9 +105,13 @@ public void listen(UUID pid, String to) {
//do nothing
}
- public void setResponse(String response) {
- this.response = response;
+ public void setDefaultResponse(String response) {
+ this.defaultResponse = response;
}
+ public void setRegexpResponseMatchers(Map<String, String> regexpResponseMatchers) {
+ this.regexpResponseMatchers = regexpResponseMatchers;
+ }
+
}
@@ -20,7 +20,16 @@
<bean id="config" class="net.ex337.scriptus.config.ScriptusConfig" />
<bean id="dummyTransport" class="net.ex337.scriptus.transport.impl.DummyTransport"
- p:response="response"/>
+ p:defaultResponse="response">
+ <property name="regexpResponseMatchers">
+ <map>
+ <entry key="SYN" value="ACK"/>
+ <entry key="D[A-Z]*G" value="CAT"/>
+ <entry key="I have ([0-9]*) cats" value="I have $1 dogs"/>
+ </map>
+ </property>
+ </bean>
+
<bean id="cmdLineTransport" class="net.ex337.scriptus.transport.impl.CommandLineTransport" />
<bean id="twitterTransport" class="net.ex337.scriptus.transport.twitter.TwitterTransportImpl" />
@@ -0,0 +1,28 @@
+package net.ex337.scriptus.tests;
+
+import java.io.IOException;
+
+import net.ex337.scriptus.transport.impl.DummyTransport;
+
+public class Testcase_DummyTransport extends BaseTestCase {
+
+ @Override
+ protected void setUp() throws Exception {
+ System.setProperty("scriptus.config", "test-scriptus.properties");
+
+ super.setUp();
+ }
+
+
+ public void test_dummyTransport() throws IOException {
+
+ DummyTransport dummy = (DummyTransport) appContext.getBean("dummyTransport");
+
+ assertEquals("straight text replacement", "ACK", dummy.getResponse("SYN"));
+ assertEquals("regexp replacement", "CAT", dummy.getResponse("DIG"));
+ assertEquals("variable replacement", "I have 1024 dogs", dummy.getResponse("I have 1024 cats"));
+
+ }
+
+
+}

0 comments on commit 1d94d25

Please sign in to comment.