You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2008/11/27 21:12:12 UTC
svn commit: r721262 - in /james/protocol-tester/trunk/main/src:
main/java/org/apache/james/mpt/ test/java/org/apache/james/mpt/
Author: rdonkin
Date: Thu Nov 27 12:12:12 2008
New Revision: 721262
URL: http://svn.apache.org/viewvc?rev=721262&view=rev
Log:
Add support for variable substitution.
Added:
james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java (with props)
james/protocol-tester/trunk/main/src/test/java/org/apache/james/mpt/TestFileProtocolSessionBuilder.java
Modified:
james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/AbstractSimpleScriptedTestProtocol.java
james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/FileProtocolSessionBuilder.java
james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolSession.java
james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/Runner.java
Modified: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/AbstractSimpleScriptedTestProtocol.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/AbstractSimpleScriptedTestProtocol.java?rev=721262&r1=721261&r2=721262&view=diff
==============================================================================
--- james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/AbstractSimpleScriptedTestProtocol.java (original)
+++ james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/AbstractSimpleScriptedTestProtocol.java Thu Nov 27 12:12:12 2008
@@ -75,7 +75,7 @@
* @param session
* The ProtocolSession to add elements to.
*/
- protected void addTestFile(String fileName, ProtocolSession session)
+ protected void addTestFile(String fileName, ProtocolInteractor session)
throws Exception {
fileName = "/org/apache/james/test/functional/imap/scripts/" + fileName;
// Need to find local resource.
@@ -84,6 +84,6 @@
throw new Exception("Test Resource '" + fileName + "' not found.");
}
- builder.addProtocolLinesFromStream(is, session, fileName);
+ builder.addProtocolLines(fileName, is, session);
}
}
Modified: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/FileProtocolSessionBuilder.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/FileProtocolSessionBuilder.java?rev=721262&r1=721261&r2=721262&view=diff
==============================================================================
--- james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/FileProtocolSessionBuilder.java (original)
+++ james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/FileProtocolSessionBuilder.java Thu Nov 27 12:12:12 2008
@@ -22,8 +22,12 @@
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.Reader;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Properties;
/**
@@ -34,20 +38,39 @@
* @version $Revision$
*/
public class FileProtocolSessionBuilder {
- private static final String SERVER_CONTINUATION_TAG = "S: \\+";
+
+ public static final String SERVER_CONTINUATION_TAG = "S: \\+";
- private static final String CLIENT_TAG = "C:";
+ public static final String CLIENT_TAG = "C:";
- private static final String SERVER_TAG = "S:";
+ public static final String SERVER_TAG = "S:";
- private static final String OPEN_UNORDERED_BLOCK_TAG = "SUB {";
+ public static final String OPEN_UNORDERED_BLOCK_TAG = "SUB {";
- private static final String CLOSE_UNORDERED_BLOCK_TAG = "}";
+ public static final String CLOSE_UNORDERED_BLOCK_TAG = "}";
- private static final String COMMENT_TAG = "#";
+ public static final String COMMENT_TAG = "#";
- private static final String SESSION_TAG = "SESSION:";
+ public static final String SESSION_TAG = "SESSION:";
+ private final Properties variables;
+
+ public FileProtocolSessionBuilder() {
+ variables = new Properties();
+ }
+
+ /**
+ * Sets a substitution varaible.
+ * The value of a variable will be substituted whereever
+ * ${<code>NAME</code>} is found in the input
+ * where <code>NAME</code> is the name of the variable.
+ * @param name not null
+ * @param value not null
+ */
+ public void setVariable(final String name, final String value) {
+ variables.put(name, value);
+ }
+
/**
* Builds a ProtocolSession by reading lines from the test file with the
* supplied name.
@@ -56,9 +79,9 @@
* The name of the protocol session file.
* @return The ProtocolSession
*/
- public ProtocolSession buildProtocolSession(String fileName)
+ public ProtocolInteractor buildProtocolSession(String fileName)
throws Exception {
- ProtocolSession session = new ProtocolSession();
+ ProtocolInteractor session = new ProtocolSession();
addTestFile(fileName, session);
return session;
}
@@ -72,7 +95,7 @@
* @param session
* The ProtocolSession to add the elements to.
*/
- public void addTestFile(String fileName, ProtocolSession session)
+ public void addTestFile(String fileName, ProtocolInteractor session)
throws Exception {
// Need to find local resource.
InputStream is = this.getClass().getResourceAsStream(fileName);
@@ -80,74 +103,109 @@
throw new Exception("Test Resource '" + fileName + "' not found.");
}
- addProtocolLinesFromStream(is, session, fileName);
+ addProtocolLines(fileName, is, session);
}
/**
* Reads ProtocolElements from the supplied InputStream and adds them to the
* ProtocolSession.
- *
+ * @param scriptName
+ * The name of the source file, for error messages.
* @param is
* The input stream containing the protocol definition.
* @param session
* The ProtocolSession to add elements to.
- * @param fileName
+ */
+ public void addProtocolLines(String scriptName, InputStream is, ProtocolInteractor session) throws Exception {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+
+ doAddProtocolLines(session, scriptName, reader);
+ }
+
+ /**
+ * Reads ProtocolElements from the supplied Reader and adds them to the
+ * ProtocolSession.
+ * @param scriptName
+ * The name of the source file, for error messages.
+ * @param reader
+ * the reader containing the protocol definition.
+ * @param session
+ * The ProtocolSession to add elements to.
+ */
+ public void addProtocolLines(String scriptName, Reader reader, ProtocolInteractor session) throws Exception {
+ final BufferedReader bufferedReader;
+ if (reader instanceof BufferedReader) {
+ bufferedReader = (BufferedReader) reader;
+ } else {
+ bufferedReader = new BufferedReader(reader);
+ }
+ doAddProtocolLines(session, scriptName, bufferedReader);
+ }
+
+ /**
+ * Reads ProtocolElements from the supplied Reader and adds them to the
+ * ProtocolSession.
+ *
+ * @param reader
+ * the reader containing the protocol definition.
+ * @param session
+ * The ProtocolSession to add elements to.
+ * @param scriptName
* The name of the source file, for error messages.
*/
- public void addProtocolLinesFromStream(InputStream is,
- ProtocolSession session, String fileName) throws Exception {
+ private void doAddProtocolLines(ProtocolInteractor session, String scriptName, BufferedReader reader) throws Exception {
+ String line;
int sessionNumber = -1;
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- String next;
int lineNumber = -1;
String lastClientMsg = "";
- while ((next = reader.readLine()) != null) {
- String location = fileName + ":" + lineNumber;
- if (SERVER_CONTINUATION_TAG.equals(next)) {
+ while ((line = reader.readLine()) != null) {
+ line = substituteVariables(line);
+ String location = scriptName + ":" + lineNumber;
+ if (SERVER_CONTINUATION_TAG.equals(line)) {
session.CONT(sessionNumber);
- } else if (next.startsWith(CLIENT_TAG)) {
+ } else if (line.startsWith(CLIENT_TAG)) {
String clientMsg = "";
- if (next.length() > 3) {
- clientMsg = next.substring(3);
+ if (line.length() > 3) {
+ clientMsg = line.substring(3);
}
session.CL(sessionNumber, clientMsg);
lastClientMsg = clientMsg;
- } else if (next.startsWith(SERVER_TAG)) {
+ } else if (line.startsWith(SERVER_TAG)) {
String serverMsg = "";
- if (next.length() > 3) {
- serverMsg = next.substring(3);
+ if (line.length() > 3) {
+ serverMsg = line.substring(3);
}
session.SL(sessionNumber, serverMsg, location, lastClientMsg);
- } else if (next.startsWith(OPEN_UNORDERED_BLOCK_TAG)) {
+ } else if (line.startsWith(OPEN_UNORDERED_BLOCK_TAG)) {
List unorderedLines = new ArrayList(5);
- next = reader.readLine();
+ line = reader.readLine();
- while (!next.startsWith(CLOSE_UNORDERED_BLOCK_TAG)) {
- if (!next.startsWith(SERVER_TAG)) {
+ while (!line.startsWith(CLOSE_UNORDERED_BLOCK_TAG)) {
+ if (!line.startsWith(SERVER_TAG)) {
throw new Exception(
"Only 'S: ' lines are permitted inside a 'SUB {' block.");
}
- String serverMsg = next.substring(3);
+ String serverMsg = line.substring(3);
unorderedLines.add(serverMsg);
- next = reader.readLine();
+ line = reader.readLine();
lineNumber++;
}
session.SUB(sessionNumber, unorderedLines, location,
lastClientMsg);
- } else if (next.startsWith(COMMENT_TAG)
- || next.trim().length() == 0) {
+ } else if (line.startsWith(COMMENT_TAG)
+ || line.trim().length() == 0) {
// ignore these lines.
- } else if (next.startsWith(SESSION_TAG)) {
- String number = next.substring(SESSION_TAG.length()).trim();
+ } else if (line.startsWith(SESSION_TAG)) {
+ String number = line.substring(SESSION_TAG.length()).trim();
if (number.length() == 0) {
throw new Exception("No session number specified");
}
sessionNumber = Integer.parseInt(number);
} else {
- String prefix = next;
- if (next.length() > 3) {
- prefix = next.substring(0, 3);
+ String prefix = line;
+ if (line.length() > 3) {
+ prefix = line.substring(0, 3);
}
throw new Exception("Invalid line prefix: " + prefix);
}
@@ -155,4 +213,36 @@
}
}
+ /**
+ * Replaces ${<code>NAME</code>} with variable value.
+ * @param line not null
+ * @return not null
+ */
+ private String substituteVariables(String line) {
+ if (variables.size() > 0) {
+ final StringBuffer buffer = new StringBuffer(line);
+ int start = 0;
+ int end = 0;
+ while (start >= 0 && end >= 0) {
+ start = buffer.indexOf("${", end);
+ if (start < 0) {
+ break;
+ }
+ end = buffer.indexOf("}", start);
+ if (end < 0) {
+ break;
+ }
+ final String name = buffer.substring(start+2, end);
+ final String value = variables.getProperty(name);
+ if (value != null) {
+ buffer.replace(start, end + 1, value);
+ final int variableLength = (end - start + 2);
+ end = end + (value.length() - variableLength);
+ }
+ }
+ line = buffer.toString();
+ }
+ return line;
+ }
+
}
Added: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java?rev=721262&view=auto
==============================================================================
--- james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java (added)
+++ james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java Thu Nov 27 12:12:12 2008
@@ -0,0 +1,68 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mpt;
+
+import java.util.List;
+
+/**
+ * Scripts a protocol interaction.
+ */
+public interface ProtocolInteractor {
+
+ /**
+ * adds a new Client request line to the test elements
+ */
+ public abstract void CL(String clientLine);
+
+ /**
+ * adds a new Server Response line to the test elements, with the specified
+ * location.
+ */
+ public abstract void SL(String serverLine, String location);
+
+ /**
+ * adds a new Server Unordered Block to the test elements.
+ */
+ public abstract void SUB(List serverLines, String location);
+
+ /**
+ * adds a new Client request line to the test elements
+ */
+ public abstract void CL(int sessionNumber, String clientLine);
+
+ /**
+ * Adds a continuation. To allow one thread to be used for testing.
+ */
+ public abstract void CONT(int sessionNumber) throws Exception;
+
+ /**
+ * adds a new Server Response line to the test elements, with the specified
+ * location.
+ */
+ public abstract void SL(int sessionNumber, String serverLine,
+ String location, String lastClientMessage);
+
+ /**
+ * adds a new Server Unordered Block to the test elements.
+ */
+ public abstract void SUB(int sessionNumber, List serverLines,
+ String location, String lastClientMessage);
+
+}
\ No newline at end of file
Propchange: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolInteractor.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolSession.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolSession.java?rev=721262&r1=721261&r2=721262&view=diff
==============================================================================
--- james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolSession.java (original)
+++ james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/ProtocolSession.java Thu Nov 27 12:12:12 2008
@@ -36,7 +36,7 @@
*
* @version $Revision$
*/
-public class ProtocolSession {
+public class ProtocolSession implements ProtocolInteractor {
private boolean continued = false;
private boolean continuationExpected = false;
@@ -120,23 +120,22 @@
}
}
- /**
- * adds a new Client request line to the test elements
+ /* (non-Javadoc)
+ * @see org.apache.james.mpt.ProtocolScript#CL(java.lang.String)
*/
public void CL(String clientLine) {
testElements.add(new ClientRequest(clientLine));
}
/**
- * adds a new Server Response line to the test elements, with the specified
- * location.
+ * @see org.apache.james.mpt.ProtocolScript#SL(java.lang.String, java.lang.String)
*/
public void SL(String serverLine, String location) {
testElements.add(new ServerResponse(serverLine, location));
}
/**
- * adds a new Server Unordered Block to the test elements.
+ * @see org.apache.james.mpt.ProtocolScript#SUB(java.util.List, java.lang.String)
*/
public void SUB(List serverLines, String location) {
testElements
@@ -144,7 +143,7 @@
}
/**
- * adds a new Client request line to the test elements
+ * @see org.apache.james.mpt.ProtocolScript#CL(int, java.lang.String)
*/
public void CL(int sessionNumber, String clientLine) {
this.maxSessionNumber = Math.max(this.maxSessionNumber, sessionNumber);
@@ -152,7 +151,7 @@
}
/**
- * Adds a continuation. To allow one thread to be used for testing.
+ * @see org.apache.james.mpt.ProtocolScript#CONT(int)
*/
public void CONT(int sessionNumber) throws Exception {
this.maxSessionNumber = Math.max(this.maxSessionNumber, sessionNumber);
@@ -160,8 +159,7 @@
}
/**
- * adds a new Server Response line to the test elements, with the specified
- * location.
+ * @see org.apache.james.mpt.ProtocolScript#SL(int, java.lang.String, java.lang.String, java.lang.String)
*/
public void SL(int sessionNumber, String serverLine, String location,
String lastClientMessage) {
@@ -171,7 +169,7 @@
}
/**
- * adds a new Server Unordered Block to the test elements.
+ * @see org.apache.james.mpt.ProtocolScript#SUB(int, java.util.List, java.lang.String, java.lang.String)
*/
public void SUB(int sessionNumber, List serverLines, String location,
String lastClientMessage) {
Modified: james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/Runner.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/Runner.java?rev=721262&r1=721261&r2=721262&view=diff
==============================================================================
--- james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/Runner.java (original)
+++ james/protocol-tester/trunk/main/src/main/java/org/apache/james/mpt/Runner.java Thu Nov 27 12:12:12 2008
@@ -43,7 +43,7 @@
* Gets protocol session run after test.
* @return not null
*/
- public ProtocolSession getPostElements() {
+ public ProtocolInteractor getPostElements() {
return postElements;
}
@@ -51,14 +51,14 @@
* Gets protocol session run before test.
* @return not null
*/
- public ProtocolSession getPreElements() {
+ public ProtocolInteractor getPreElements() {
return preElements;
}
/**
* Gets protocol session run on test.
* @return not null
*/
- public ProtocolSession getTestElements() {
+ public ProtocolInteractor getTestElements() {
return testElements;
}
Added: james/protocol-tester/trunk/main/src/test/java/org/apache/james/mpt/TestFileProtocolSessionBuilder.java
URL: http://svn.apache.org/viewvc/james/protocol-tester/trunk/main/src/test/java/org/apache/james/mpt/TestFileProtocolSessionBuilder.java?rev=721262&view=auto
==============================================================================
--- james/protocol-tester/trunk/main/src/test/java/org/apache/james/mpt/TestFileProtocolSessionBuilder.java (added)
+++ james/protocol-tester/trunk/main/src/test/java/org/apache/james/mpt/TestFileProtocolSessionBuilder.java Thu Nov 27 12:12:12 2008
@@ -0,0 +1,86 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mpt;
+
+import java.io.StringReader;
+
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+public class TestFileProtocolSessionBuilder extends MockObjectTestCase {
+
+ private static final String SCRIPT_WITH_VARIABLES = "HELLO ${not} ${foo} WORLD ${bar}";
+ private static final String SCRIPT_WITH_FOO_REPLACED_BY_WHATEVER = "HELLO ${not} whatever WORLD ${bar}";
+ private static final String SCRIPT_WITH_VARIABLES_INLINED = "HELLO not foo WORLD bar";
+
+ FileProtocolSessionBuilder builder;
+ ProtocolInteractor session;
+
+ private Mock mockSession;
+
+ //@Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ builder = new FileProtocolSessionBuilder();
+ mockSession = mock(ProtocolInteractor.class);
+ session = (ProtocolInteractor) mockSession.proxy();
+ }
+
+ //@Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ private void addLines() throws Exception {
+ builder.addProtocolLines("A Script", new StringReader(FileProtocolSessionBuilder.CLIENT_TAG + " " + SCRIPT_WITH_VARIABLES), session);
+ }
+
+ public void testShouldPreserveContentsWhenNoVariablesSet() throws Exception {
+ mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_VARIABLES));
+ addLines();
+ }
+
+ public void testShouldReplaceVariableWhenSet() throws Exception {
+ mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_FOO_REPLACED_BY_WHATEVER));
+ builder.setVariable("foo", "whatever");
+ addLines();
+ }
+
+ public void testShouldReplaceAllVariablesWhenSet() throws Exception {
+ mockSession.expects(once()).method("CL").with(eq(-1), eq(SCRIPT_WITH_VARIABLES_INLINED));
+ builder.setVariable("bar", "bar");
+ builder.setVariable("foo", "foo");
+ builder.setVariable("not", "not");
+ addLines();
+ }
+
+ public void testShouldReplaceVariableAtBeginningAndEnd() throws Exception {
+ mockSession.expects(once()).method("CL").with(eq(-1), eq("whatever Some Other Scriptwhateverwhatever"));
+ builder.setVariable("foo", "whatever");
+ builder.addProtocolLines("A Script", new StringReader(FileProtocolSessionBuilder.CLIENT_TAG + " " + "${foo} Some Other Script${foo}${foo}"), session);
+ }
+
+ public void testShouldIgnoreNotQuiteVariables() throws Exception {
+ final String NEARLY = "{foo}${}${foo Some Other Script${foo}";
+ mockSession.expects(once()).method("CL").with(eq(-1), eq(NEARLY));
+ builder.setVariable("foo", "whatever");
+ builder.addProtocolLines("A Script", new StringReader(FileProtocolSessionBuilder.CLIENT_TAG + " " + NEARLY), session);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org