From: <am...@us...> - 2009-03-05 22:07:40
|
Revision: 6070 http://jython.svn.sourceforge.net/jython/?rev=6070&view=rev Author: amak Date: 2009-03-05 22:07:35 +0000 (Thu, 05 Mar 2009) Log Message: ----------- Committing the unit tests. There is a readme.txt file in the directory which explains how to run the tests, which depend on mockrunner. Added Paths: ----------- branches/modjy/tests/modjy/ branches/modjy/tests/modjy/__init__.py branches/modjy/tests/modjy/build.xml branches/modjy/tests/modjy/empty.txt branches/modjy/tests/modjy/java/ branches/modjy/tests/modjy/java/com/ branches/modjy/tests/modjy/java/com/xhaus/ branches/modjy/tests/modjy/java/com/xhaus/modjy/ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestAppInvocation.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestContentHeaders.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestEnviron.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestHeaders.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestReturnIterable.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWSGIStreams.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWebInf.java branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWriteCallable.java branches/modjy/tests/modjy/lib_python_folder/ branches/modjy/tests/modjy/lib_python_folder/__init__.py branches/modjy/tests/modjy/lib_python_folder/add_zips.pth branches/modjy/tests/modjy/lib_python_folder/do_import.pth branches/modjy/tests/modjy/lib_python_folder/mock_framework/ branches/modjy/tests/modjy/lib_python_folder/mock_framework/__init__.py branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/ branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/__init__.py branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/ branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/__init__.py branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/wsgi_handlers.py branches/modjy/tests/modjy/lib_python_folder/script_name_path_info.py branches/modjy/tests/modjy/lib_python_folder/test_lib/ branches/modjy/tests/modjy/lib_python_folder/test_lib/__init__.py branches/modjy/tests/modjy/lib_python_folder/test_lib/some_libs.py branches/modjy/tests/modjy/lib_python_folder/test_modules.zip branches/modjy/tests/modjy/lines.txt branches/modjy/tests/modjy/readme.txt branches/modjy/tests/modjy/test_apps_dir/ branches/modjy/tests/modjy/test_apps_dir/__init__.py branches/modjy/tests/modjy/test_apps_dir/content_header_tests.py branches/modjy/tests/modjy/test_apps_dir/environ_tests.py branches/modjy/tests/modjy/test_apps_dir/header_tests.py branches/modjy/tests/modjy/test_apps_dir/return_tests.py branches/modjy/tests/modjy/test_apps_dir/simple_app.py branches/modjy/tests/modjy/test_apps_dir/stream_tests.py branches/modjy/tests/modjy/test_apps_dir/web_inf_tests.py Added: branches/modjy/tests/modjy/build.xml =================================================================== --- branches/modjy/tests/modjy/build.xml (rev 0) +++ branches/modjy/tests/modjy/build.xml 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,109 @@ +<project name="modjy" default="test"> + + <description>Modjy test build.xml</description> + + <!-- set global properties for this build --> + + <property name="test_src" location="java"/> + <property name="build" location="build"/> + + <!-- Import the environment --> + <property environment="env"/> + + <!-- Jython properties --> + + <property name="jython_home" location="${env.JYTHON_HOME}"/> + <property name="jython_jar" value="jython.jar"/> + <property name="jython_jar_path" location="${jython_home}/${jython_jar}"/> + <property name="jython_cachedir" location="${jython_home}/cachedir"/> + + <target name="init"> + <available property="jython.home.set" file="${env.JYTHON_HOME}" /> + <fail unless="jython.home.set" message="JYTHON_HOME not set" /> + <available property="mockrunner.home.set" file="${env.MOCKRUNNER_HOME}" /> + <fail unless="mockrunner.home.set" message="MOCKRUNNER_HOME not set" /> + <!-- Create the build directory structure used by compile --> + <mkdir dir="${build}"/> + </target> + + <target name="clean" description="clean up" > + <!-- Delete the ${build} and ${dist} directory trees --> + <delete dir="${build}"/> + </target> + + <!-- Now testing related targets --> + + <target name="do_test" depends="init" description="Run unit tests against a single jdk/servlet combo"> + <echo message="Running tests against JDK ${jdk_version}, Servlet ${servlet_version}"/> + <echo message="Clearing jython package cache located at ${jython_home}/cachedir"/> + + <delete quiet="true"> + <fileset dir="${jython_cachedir}"/> + </delete> + + <property name="mockrunner_home" value="${env.MOCKRUNNER_HOME}"/> + <property name="mockrunner_jar" location="${mockrunner_home}/jar"/> + <property name="mockrunner_lib" location="${mockrunner_home}/lib/jdk${jdk_version}/${servlet_version}"/> + + <path id="test.classpath"> + <pathelement path="${build}"/> + <pathelement path="${jython_jar_path}"/> + <fileset dir="${jython_home}/javalib" includes="**/*.jar"/> + <fileset dir="${mockrunner_jar}" includes="**/*.jar"/> + <fileset dir="${mockrunner_lib}" includes="**/*.jar"/> + </path> + + <javac + srcdir="${test_src}" + destdir="${build}" + classpathref="test.classpath" + debug="on" + /> + + <java + classname="com.xhaus.modjy.ModjyTestBase" + dir="." + fork="yes" + classpathref="test.classpath" + /> + </target> + + <target name="test"> + <antcall target="do_test"> + <param name="jdk_version" value="1.5"/> + <param name="servlet_version" value="j2ee1.3"/> + </antcall> + </target> + + <target name="test_java_15" depends="clean"> + <antcall target="do_test"> + <param name="jdk_version" value="1.5"/> + <param name="servlet_version" value="j2ee1.3"/> + </antcall> + <antcall target="do_test"> + <param name="jdk_version" value="1.5"/> + <param name="servlet_version" value="j2ee1.4"/> + </antcall> + <antcall target="do_test"> + <param name="jdk_version" value="1.5"/> + <param name="servlet_version" value="jee5"/> + </antcall> + </target> + + <target name="test_java_16" depends="clean"> + <antcall target="do_test"> + <param name="jdk_version" value="1.6"/> + <param name="servlet_version" value="j2ee1.3"/> + </antcall> + <antcall target="do_test"> + <param name="jdk_version" value="1.6"/> + <param name="servlet_version" value="j2ee1.4"/> + </antcall> + <antcall target="do_test"> + <param name="jdk_version" value="1.6"/> + <param name="servlet_version" value="jee5"/> + </antcall> + </target> + +</project> + Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestAppInvocation.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestAppInvocation.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestAppInvocation.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,224 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import java.io.File; + +import org.python.core.PyDictionary; +import org.python.core.PyInteger; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyTuple; + +public class ModjyTestAppInvocation extends ModjyTestBase +{ + + protected void appInvocationTestSetUp() + throws Exception + { + baseSetUp(); + setAppDir(""); + setAppFile(""); + setAppName(""); + setAppImportable(""); + } + + String[] importablepathElements = new String[] { + "mock_framework", + "web", + "handlers", + "wsgi_handlers", + }; + + public void testRelativeDirectory ( ) + throws Exception + { + baseSetUp(); + setRealPath("/test_apps_dir", "test_apps_dir"); + setAppDir("$/test_apps_dir"); + setAppFile("simple_app.py"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("Hello World!", result); + } + + public String setupAppImport ( String appCallableName ) + throws Exception + { + StringBuffer filePathBuffer = new StringBuffer(); + StringBuffer importPath = new StringBuffer(); + for (int ix = 0 ; ix < importablepathElements.length ; ix++) + { + String resourcePath; + String filePath; + filePathBuffer.append(importablepathElements[ix]); + resourcePath = "/WEB-INF/"+LIB_PYTHON_DIR+"/"+filePathBuffer.toString(); + filePath = LIB_PYTHON_TEST_PATH+"/"+filePathBuffer.toString(); + setRealPath(resourcePath, filePath); + filePathBuffer.append("/"); + resourcePath = "/WEB-INF/"+LIB_PYTHON_DIR+"/"+filePathBuffer.toString()+"__init__.py"; + filePath = LIB_PYTHON_TEST_PATH+"/"+filePathBuffer.toString()+"__init__.py"; + setRealPath(resourcePath, filePath); + importPath.append(importablepathElements[ix]); + importPath.append("."); + } + importPath.append(appCallableName); + return importPath.toString(); + } + + public void testAppImportCallable ( ) + throws Exception + { + appInvocationTestSetUp(); + String importableName = setupAppImport("WSGIHandlerFunction"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("WSGIHandlerFunction called.", result); + } + + public void testAppImportBadInstantiable ( ) + throws Exception + { + appInvocationTestSetUp(); + String importableName = setupAppImport("WSGIHandlerClass"); + setAppImportable(importableName); + createServlet(); + doGet(); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + } + + public void testAppImportInstantiable ( ) + throws Exception + { + appInvocationTestSetUp(); + String importableName = setupAppImport("WSGIHandlerClass()"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("__call__ counter = 0", result); + } + + public void testAppImportInstantiableCached ( ) + throws Exception + { + appInvocationTestSetUp(); + setInitParameter("cache_callables", "1"); + String importableName = setupAppImport("WSGIHandlerClass()"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("__call__ counter = 0", result); + clearOutput(); + doGet(); + result = getOutput(); + assertEquals("__call__ counter = 1", result); + } + + public void testAppImportInstantiableNotCached ( ) + throws Exception + { + appInvocationTestSetUp(); + setInitParameter("cache_callables", "0"); + String importableName = setupAppImport("WSGIHandlerClass()"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("__call__ counter = 0", result); + clearOutput(); + doGet(); + result = getOutput(); + assertEquals("__call__ counter = 0", result); + } + + public void testAppImportInstantiableMethod ( ) + throws Exception + { + appInvocationTestSetUp(); + String importableName = setupAppImport("WSGIHandlerClass().handler_fn"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("handler_fn counter = 0", result); + } + + public void testAppImportInstantiableBadMethod ( ) + throws Exception + { + appInvocationTestSetUp(); + String importableName = setupAppImport("WSGIHandlerClass().handler_fn.fn_attr"); + setAppImportable(importableName); + createServlet(); + doGet(); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + } + + public void testAppImportInstantiableMethodCached ( ) + throws Exception + { + appInvocationTestSetUp(); + setInitParameter("cache_callables", "1"); + String importableName = setupAppImport("WSGIHandlerClass().handler_fn"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("handler_fn counter = 0", result); + clearOutput(); + doGet(); + result = getOutput(); + assertEquals("handler_fn counter = 1", result); + } + + public void testAppImportInstantiableMethodNotCached ( ) + throws Exception + { + appInvocationTestSetUp(); + setInitParameter("cache_callables", "0"); + String importableName = setupAppImport("WSGIHandlerClass().handler_fn"); + setAppImportable(importableName); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("handler_fn counter = 0", result); + clearOutput(); + doGet(); + result = getOutput(); + assertEquals("handler_fn counter = 0", result); + } + + public void testBadAppImport() + throws Exception + { + appInvocationTestSetUp(); + setAppImportable("never.existed"); + createServlet(); + doGet(); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,287 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import java.io.File; + +import java.util.Map; +import java.util.Iterator; + +import junit.framework.*; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServlet; + +import com.mockrunner.servlet.BasicServletTestCaseAdapter; +import com.mockrunner.mock.web.WebMockObjectFactory; +import com.mockrunner.mock.web.MockServletConfig; +import com.mockrunner.mock.web.MockServletContext; +import com.mockrunner.mock.web.MockHttpServletRequest; +import com.mockrunner.mock.web.MockHttpServletResponse; + +import org.jdom.Element; +import org.jdom.output.XMLOutputter; + +import org.python.core.PyObject; +import org.python.util.PythonInterpreter; + +import com.xhaus.modjy.ModjyJServlet; + +/** + * + */ + +public class ModjyTestBase extends BasicServletTestCaseAdapter +{ + + final static String DEFAULT_APP_DIR = "test_apps_dir"; + final static String LIB_PYTHON_DIR = "lib-python"; + final static String LIB_PYTHON_TEST_PATH = "lib_python_folder"; + final static String DEFAULT_APP_FILE = "simple_app.py"; + final static String DEFAULT_APP_NAME = "simple_app"; + + public WebMockObjectFactory factory; + public MockServletConfig servletConfig; + public MockServletContext servletContext; + + public WebMockObjectFactory getFactory () + { + if (factory == null) + factory = getWebMockObjectFactory(); + return factory; + } + + public MockServletConfig getConfig () + { + if (servletConfig == null) + servletConfig = getFactory().getMockServletConfig(); + return servletConfig; + } + + public MockServletContext getContext () + { + if (servletContext == null) + servletContext = getFactory().getMockServletContext(); + return servletContext; + } + +// public void dumpContextRealPaths ( ) +// { +// Map pathMap = ((LoggingMockServletContext)getContext()).actualPaths; +// Iterator it = pathMap.keySet().iterator(); +// while (it.hasNext()) +// { +// String pathName = (String) it.next(); +// System.out.println("Path '"+pathName+"'-->'"+pathMap.get(pathName)+"'"); +// } +// } + + public void setInitParameter ( String name, String value ) + { + getConfig().setInitParameter(name, value); + } + + public void setRealPath ( String source, String target ) + { + getContext().setRealPath(source, target); + } + + public void addHeader(String headerName, String headerValue) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.addHeader(headerName, headerValue); + getFactory().addRequestWrapper(request); + } + + public void setBodyContent(String content) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setBodyContent(content); + getFactory().addRequestWrapper(request); + } + + public void setServletContextPath(String path) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setContextPath(path); + getFactory().addRequestWrapper(request); + } + + public void setServletPath(String path) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setServletPath(path); + getFactory().addRequestWrapper(request); + } + + public void setRequestURI(String uri) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setRequestURI(uri); + getFactory().addRequestWrapper(request); + } + + public void setScheme(String scheme) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setScheme(scheme); + getFactory().addRequestWrapper(request); + } + + public void setPathInfo(String pathInfo) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setPathInfo(pathInfo); + getFactory().addRequestWrapper(request); + } + + public void setQueryString(String qString) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setQueryString(qString); + getFactory().addRequestWrapper(request); + } + + public void setProtocol(String protocol) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setProtocol(protocol); + getFactory().addRequestWrapper(request); + } + + public void setServerName(String name) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + // Using setLocalName() here: See here for more: http://docs.sun.com/source/819-0077/J2EE.html + request.setLocalName(name); + getFactory().addRequestWrapper(request); + } + + public void setServerPort(int port) + { + MockHttpServletRequest request = (MockHttpServletRequest) getFactory().getWrappedRequest(); + request.setLocalPort(port); + getFactory().addRequestWrapper(request); + } + + public void setPythonHome(String app_dir) + { + setInitParameter("python.home", app_dir); + } + + public void setAppDir(String app_dir) + { + setInitParameter("app_directory", app_dir); + } + + public void setAppFile(String app_file) + { + setInitParameter("app_filename", app_file); + } + + public void setAppName(String app_name) + { + setInitParameter("app_callable_name", app_name); + } + + public void setAppImportable(String app_path) + { + setAppDir(""); + setAppFile(""); + setAppName(""); + setInitParameter("app_import_name", app_path); + } + + public MockHttpServletResponse getResponse() + { + MockHttpServletResponse response = (MockHttpServletResponse) getFactory().getWrappedResponse(); + return response; + } + + public int getStatus() + { + MockHttpServletResponse response = (MockHttpServletResponse) getFactory().getWrappedResponse(); + return response.getStatusCode(); + } + + protected void baseSetUp() + throws Exception + { + super.setUp(); + String jythonHome = System.getenv("JYTHON_HOME"); + setRealPath(jythonHome, jythonHome); + setRealPath("/WEB-INF/"+LIB_PYTHON_DIR, LIB_PYTHON_TEST_PATH); + setRealPath("/WEB-INF/lib/modjy.jar", "../modjy.jar"); + setPythonHome(jythonHome); + setAppDir(DEFAULT_APP_DIR); + setAppFile(DEFAULT_APP_FILE); + setAppName(DEFAULT_APP_NAME); + setInitParameter("exc_handler", "testing"); +// dumpContextRealPaths(); + } + + protected PyObject evalPythonString(String pyString) + { + // Efficiency be damned: it's a testing phase + PythonInterpreter interp = new PythonInterpreter(); + try + { + return interp.eval(pyString); + } + catch (Exception x) + { + System.err.println("Exception evaling '"+pyString+"': " + x); + return null; + } + } + + protected void createServlet() + { + createServlet(ModjyJServlet.class); + // Set zero content: this can be overridden later + setBodyContent(""); + clearOutput(); + } + + // Leave this here as a simple template for a test + public void testHelloWorld() throws Exception + { + baseSetUp(); + createServlet(); + doGet(); + String result = new XMLOutputter().outputString(getOutputAsJDOMDocument()); + } + + public static void main(String args[]) + { + TestSuite suite = new TestSuite(); + suite.addTestSuite(ModjyTestBase.class); + suite.addTestSuite(ModjyTestAppInvocation.class); + suite.addTestSuite(ModjyTestEnviron.class); + suite.addTestSuite(ModjyTestHeaders.class); + suite.addTestSuite(ModjyTestContentHeaders.class); + suite.addTestSuite(ModjyTestReturnIterable.class); + suite.addTestSuite(ModjyTestWebInf.class); + suite.addTestSuite(ModjyTestWSGIStreams.class); + junit.textui.TestRunner.run(suite); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestContentHeaders.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestContentHeaders.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestContentHeaders.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,81 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import com.mockrunner.base.NestedApplicationException; +import org.python.core.PyException; + +public class ModjyTestContentHeaders extends ModjyTestBase +{ + + // From: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 + + protected void contentHeaderTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("content_header_tests.py"); + } + + public void doHeaderTest(String appName, String queryString) + throws Exception + { + contentHeaderTestSetUp(); + setAppName(appName); + createServlet(); + if (queryString != null) + setQueryString(queryString); + doGet(); + } + + public void doHeaderTest(String appName) + throws Exception + { + doHeaderTest(appName, null); + } + + public void testSetContentLengthHeader ( ) + throws Exception + { + doHeaderTest("test_set_content_length"); + assertEquals("Status code != 200: ServerError, =='"+getStatus()+"'", 200, getStatus()); + } + + public void testSetBadContentLengthHeader ( ) + throws Exception + { + doHeaderTest("test_set_bad_content_length"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'BadArgument' in output", + getOutput().indexOf("BadArgument")!=-1); + } + + public void testInferredContentLength ( ) + throws Exception + { + String appReturn = "Hello World!"; + doHeaderTest("test_inferred_content_length", appReturn); + assertEquals("Status code != 200: ServerError, =='"+getStatus()+"'", 200, getStatus()); + assertEquals("Content-length != '"+appReturn.length()+"', == '"+getResponse().getHeader("content-length")+"' ", + Integer.toString(appReturn.length()), getResponse().getHeader("content-length")); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestEnviron.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestEnviron.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestEnviron.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,328 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import org.python.core.PyDictionary; +import org.python.core.PyInteger; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyTuple; + +public class ModjyTestEnviron extends ModjyTestBase +{ + + protected void environTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("environ_tests.py"); + } + + public void testEnvIsDict ( ) + throws Exception + { + // Spec says that the env MUST be a dict, not a subclass or emulation. + environTestSetUp(); + setAppName("test_env_is_dict"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals(result, "true"); + } + + public void testEnvIsMutable ( ) + throws Exception + { + // Spec says that the env must be mutable. + environTestSetUp(); + setAppName("test_env_is_mutable"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals(result, "true"); + } + + public void doRequestMethodTest ( String method ) + throws Exception + { + // Check that the environment contains the REQUEST_METHOD var + // REQUEST_METHOD + environTestSetUp(); + setAppName("test_env_contains_request_method"); + createServlet(); + method = method.toLowerCase(); + // Java needs first class functions + if (method == "delete") doDelete(); + else if (method == "get") doGet(); + else if (method == "head") doHead(); + else if (method == "options") doOptions(); + else if (method == "post") doPost(); + else if (method == "put") doPut(); + else if (method == "trace") doTrace(); + String result = getOutput(); + assertEquals(result, method.toUpperCase()); + } + + public void testRequestMethod() + throws Exception + { + doRequestMethodTest("delete"); + doRequestMethodTest("get"); + doRequestMethodTest("head"); + doRequestMethodTest("options"); + doRequestMethodTest("post"); + doRequestMethodTest("put"); + doRequestMethodTest("trace"); + } + + // This test doesn't really achieve much, because mockrunner doesn't actually do any computation + // on the incoming URI: you have to set the servletContextPath(SCRIPT_NAME) and PathInfo(PATH_INFO) + // yourself. + // Still, best to include the test. + // Maybe in the future we can implement a subclass of MockHttpServletRequest + // which actually does computation on the incoming URI to work out the pathInfo. + + public void doScriptNamePathInfoTest ( String contextPath, String servletPath, String URI, String pathInfo, + String expectedScriptName, String expectedPathInfo, boolean appImportable ) + throws Exception + { + environTestSetUp(); + if (appImportable) + setAppImportable("script_name_path_info.test_env_script_name_path_info"); + else + setAppName("test_env_script_name_path_info"); + createServlet(); + setServletContextPath(contextPath); + setServletPath(servletPath); + setRequestURI(URI); + setPathInfo(pathInfo); + doGet(); + String output = getOutput(); + String[] results = output.split(":::"); + assertEquals("ScriptName '"+results[0]+"' != '"+expectedScriptName+"'", expectedScriptName, results[0]); + String actualPathInfo = ""; + if (results.length > 1) + actualPathInfo = results[1]; + assertEquals("PathInfo '"+actualPathInfo+"' != '"+expectedPathInfo+"'", actualPathInfo, expectedPathInfo); + } + + public void testScriptNamePathInfo ( ) + throws Exception + { + // Check that the environment contains correct values for SCRIPT_NAME and PATH_INFO + String[][] testData = new String[][] { + {"", "/", "" }, + {"", "/", "info" }, + {"/ctx", "/srv", "" }, + {"/ctx", "/srv", "/info" }, + {"/ctx", "/srv/sub", "" }, + {"/ctx", "/srv/sub", "/info" }, + }; + for (int ix = 0 ; ix < testData.length ; ix++) + { + String contextPath = testData[ix][0]; + String servletPath = testData[ix][1]; + String pathInfo = testData[ix][2]; + String uri = contextPath+servletPath+pathInfo; + String expectedScriptName = contextPath+servletPath; + String expectedPathInfo = pathInfo; + doScriptNamePathInfoTest(contextPath, servletPath, uri, pathInfo, contextPath+servletPath, expectedPathInfo, false); + doScriptNamePathInfoTest(contextPath, servletPath, uri, pathInfo, contextPath+servletPath, expectedPathInfo, true); + } + } + + // Again, this test doesn't achieve a lot under mockrunner, because you have to + // set the query string yourself. + + public void doQueryStringTest ( String actualQString, String expectedQString) + throws Exception + { + environTestSetUp(); + setAppName("test_env_query_string"); + createServlet(); + setQueryString(actualQString); + doGet(); + String result = getOutput(); + assertEquals("QueryString '"+result+"' != '"+expectedQString+"'", result, expectedQString); + } + + public void testQueryString() + throws Exception + { + // Test no query string + doQueryStringTest(null, ""); + // Test empty query string + doQueryStringTest("", ""); + // Test variable no value + doQueryStringTest("name", "name"); + // Test variable and value + doQueryStringTest("name=value", "name=value"); + // Test two name/value pairs + doQueryStringTest("n1=v1&n2=v2", "n1=v1&n2=v2"); + // Test no unquoting done + doQueryStringTest("var=hello+world!", "var=hello+world!"); + // Any illegal values to test? + } + + public void testContentVars ( ) + { + // Test CONTENT_TYPE and CONTENT_LENGTH + // Perhaps these should be done with the WSGIInput tests? + } + + public PyObject doEnvVarTest(String name) + throws Exception + { + environTestSetUp(); + setAppName("test_echo_wsgi_env"); + createServlet(); + setQueryString(name); + doGet(); + String output = getOutput(); + PyDictionary result = (PyDictionary) evalPythonString(output); + return result.__finditem__(name); + } + + public void doEnvVarTestCheckReturn(String name, String value) + throws Exception + { + PyString envVar = (PyString) doEnvVarTest(name); + assertTrue("Env var '"+name+"' != '"+value+"', == '"+envVar.toString()+"'", + envVar.toString().compareTo(value)==0); + } + + public void testServerParams_ServerName() + throws Exception + { + String serverName = "ModjyServerTest"; + setServerName(serverName); + doEnvVarTestCheckReturn("SERVER_NAME", serverName); + } + + public void testServerParams_ServerPort() + throws Exception + { + int serverPort = 54321; + setServerPort(serverPort); + doEnvVarTestCheckReturn("SERVER_PORT", String.valueOf(serverPort)); + } + + public void testServerParams_ServerProtocol() + throws Exception + { + String serverProtocol = "HTTP/1.1"; + setProtocol(serverProtocol); + doEnvVarTestCheckReturn("SERVER_PROTOCOL", serverProtocol); + } + + public void testOtherHttpVars ( ) + { + // Test other vars that begin with HTTP + } + + public void testMandatoryWSGIVars_WSGIVersion ( ) + throws Exception + { + PyObject result = doEnvVarTest("wsgi.version"); + assertTrue("'wsgi.version' != PyTuple", result instanceof org.python.core.PyTuple); + assertEquals("'wsgi.version[0]' != '1'", ((PyInteger)result.__getitem__(0)).getValue(), 1); + assertEquals("'wsgi.version[1]' != '0'", ((PyInteger)result.__getitem__(1)).getValue(), 0); + } + + public void testMandatoryWSGIVars_URLScheme_HTTP ( ) + throws Exception + { + String scheme = "http"; + setScheme(scheme); + doEnvVarTestCheckReturn("wsgi.url_scheme", scheme); + } + + public void testMandatoryWSGIVars_URLScheme_HTTPS ( ) + throws Exception + { + String scheme = "https"; + setScheme(scheme); + doEnvVarTestCheckReturn("wsgi.url_scheme", scheme); + } + + public void testMandatoryWSGIVars_Other ( ) + throws Exception + { + // These should be checked under input and error handling respectively + //wsgi.input + //wsgi.errors + // These should be checked under app invocation + //wsgi.multithread + //wsgi.multiprocess + //wsgi.run_once + } + + public void testEnvContainsModjySpecificVars ( ) + { + // Test that the environment contains all of the modjy specific vars + } + + public void testUserSpecifiedEnv ( ) + throws Exception + { + // Set a user specified environment in the servlet config, then test that it is passed through. + String envName1 = "ENVVAR1"; + String envValue1 = "ENVVAL1"; + String envName2 = "ENVVAR2"; + String envValue2 = "ENVVAL2"; + String initialEnv = "\n"+envName1+"\t: "+envValue1+"\n"+envName2+" :\t"+envValue2+"\n"; + setInitParameter("initial_env", initialEnv); + doEnvVarTestCheckReturn(envName1, envValue1); + doEnvVarTestCheckReturn(envName2, envValue2); + } + + public void testUserConfigVars ( ) + { + // Set some environment vars in the servlet config, then test that they are passed through. + } + + public void testCgiVarsAreStr ( ) + throws Exception + { + // PEP-333 states that all CGI vars must be str, not unicode + // http://www.python.org/dev/peps/pep-0333/#unicode-issues + environTestSetUp(); + setAppName("test_cgi_vars_are_str"); + createServlet(); + doGet(); + String output = getOutput(); + assertEquals("pass", output); + } + + public void testMultipleHeaderValues ( ) + throws Exception + { + environTestSetUp(); + setAppName("test_multiple_header_values"); + createServlet(); + addHeader("MULTIPLE", "value 1"); + addHeader("MULTIPLE", "value 2"); + doGet(); + String output = getOutput(); + assertEquals("pass", output); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestHeaders.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestHeaders.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestHeaders.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,200 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import com.mockrunner.base.NestedApplicationException; +import org.python.core.PyException; + +public class ModjyTestHeaders extends ModjyTestBase +{ + + // From: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 + + final static String[] hop_by_hop_headers = new String[] { + "connection", + "keep-alive", + "proxy-authenticate", + "proxy-authorization", + "te", + "trailers", + "transfer-encoding", + "upgrade", + }; + + protected void headerTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("header_tests.py"); + } + + public void doHeaderTest(String appName, String queryString) + throws Exception + { + headerTestSetUp(); + setAppName(appName); + createServlet(); + if (queryString != null) + setQueryString(queryString); + doGet(); + } + + public void doHeaderTest(String appName) + throws Exception + { + doHeaderTest(appName, null); + } + + public void testInvalidStatusCode() + throws Exception + { + doHeaderTest("test_invalid_status_code"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'BadArgument' in output: " + getOutput(), + getOutput().indexOf("BadArgument")!=-1); + } + + public void testNonLatin1StatusString() + throws Exception + { + // We'll let this one pass: + // 1. The integer status code can't be anything but an ASCII-encoded integer + // 2. The reason phrase is discarded on J2EE anyway + // Modjy takes no action if a non latin-1 status string is passed + doHeaderTest("test_non_latin1_status_string"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'BadArgument' in output: " + getOutput(), + getOutput().indexOf("BadArgument")!=-1); + } + + public void testLatin1StatusStringWithControlChars() + throws Exception + { + // We'll let this one pass: + // 1. The integer status code can't be anything but an ASCII-encoded integer + // 2. The reason phrase is discarded on J2EE anyway + // Modjy takes no action if a status string with control chars is passed + doHeaderTest("test_control_chars_in_status_string"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'BadArgument' in output: " + getOutput(), + getOutput().indexOf("BadArgument")!=-1); + } + + public void doBadHeadersListTest(String appName) + throws Exception + { + for (int i = 1 ; i < 5 ; i++) + { + doHeaderTest(appName, String.valueOf(i)); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + String firstLine = getOutput().split("\n")[0]; + assertTrue("Could not find exception 'BadArgument' in output: " + firstLine, + firstLine.indexOf("BadArgument")!=-1); + } + } + + public void testHeadersNotList ( ) + throws Exception + { + // Ensure that setting headers to anything other than a list raises an error + doBadHeadersListTest("test_headers_not_list"); + } + + public void testHeadersListContainsNonTuples ( ) + throws Exception + { + // Ensure that setting headers to anything other than a list raises an error + doBadHeadersListTest("test_headers_list_non_tuples"); + } + + public void testHeadersListContainsWrongLengthTuples ( ) + throws Exception + { + // Ensure that setting headers to anything other than a list of 2-tuples raises an error + doBadHeadersListTest("test_headers_list_wrong_length_tuples"); + } + + public void testHeadersListContainsWrongTypeTuples ( ) + throws Exception + { + // Ensure that setting headers to anything other than a list of 2-tuples of strings raises an error + doBadHeadersListTest("test_headers_list_wrong_types_in_tuples"); + } + + public void testHeadersListContainsNonLatin1Values ( ) + throws Exception + { + // Ensure that setting header values to non-latin1 strings raises an error + doBadHeadersListTest("test_headers_list_contains_non_latin1_values"); + } + + public void testHeadersListContainsValuesWithControlChars ( ) + throws Exception + { + // Ensure that setting header values to strings raises an error + // Disable this test: Modjy doesn't test for control characters. + // doBadHeadersListTest("test_headers_list_contains_values_with_control_chars"); + } + + public void testHeadersListContainsAccentedLatin1Values ( ) + throws Exception + { + // Ensure that setting header values to 8-bit latin1 strings works properly + String headerName = "x-latin1-header"; + String headerValue = "\u00e1\u00e9\u00ed\u00f3\u00fa"; + String headerQString = headerName + "=" + headerValue; + doHeaderTest("test_headers_list_contains_accented_latin1_values", headerQString); + assertEquals("Status code != 200: ServerError, =='"+getStatus()+"'", 200, getStatus()); + assertTrue("Header '"+headerName+"' not returned: ", getResponse().containsHeader(headerName)); + assertEquals("Header '"+headerName+"' != '"+headerValue+"', == '"+getResponse().getHeader(headerName)+"' ", + headerValue, getResponse().getHeader(headerName)); + } + + public void testHopByHopHeaders ( ) + throws Exception + { + // Test that attempts to set hop-by-hop headers raise exception. + for (int i = 0 ; i < hop_by_hop_headers.length ; i++) + { + doHeaderTest("test_hop_by_hop", hop_by_hop_headers[i]); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'HopByHopHeaderSet' in output", + getOutput().indexOf("HopByHopHeaderSet")!=-1); + } + } + + public void testSetHeader ( ) + throws Exception + { + // test that we can set headers + } + + public void testMultilineHeaders () + { + // Need to do some research on this + } + + public void testRFC2047EncodedHeaders ( ) + { + // Need to do some research on this + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestReturnIterable.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestReturnIterable.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestReturnIterable.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,184 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +public class ModjyTestReturnIterable extends ModjyTestBase +{ + + protected void returnTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("return_tests.py"); + } + + public void doReturnTest(String appName, String queryString) + throws Exception + { + returnTestSetUp(); + setAppName(appName); + createServlet(); + if (queryString != null) + setQueryString(queryString); + doGet(); + } + + public void doReturnTest(String appName) + throws Exception + { + doReturnTest(appName, null); + } + + public void doWrongReturnTypeTest(String appName, String typeString) + throws Exception + { + // Try to return not-iterables + doReturnTest(appName, typeString); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + String firstLine = getOutput().split("\n")[0]; + assertTrue("Could not find exception 'ReturnNotIterable' in output: " + firstLine, + firstLine.indexOf("ReturnNotIterable")!=-1); + + } + + public void testReturnString ( ) + throws Exception + { + doWrongReturnTypeTest("test_non_iterable_return", "str"); + } + + public void testReturnUnicode ( ) + throws Exception + { + doWrongReturnTypeTest("test_non_iterable_return", "unicode"); + } + + public void testReturnInt ( ) + throws Exception + { + doWrongReturnTypeTest("test_non_iterable_return", "int"); + } + + public void testReturnFloat ( ) + throws Exception + { + doWrongReturnTypeTest("test_non_iterable_return", "float"); + } + + public void testReturnNone ( ) + throws Exception + { + doWrongReturnTypeTest("test_non_iterable_return", "none"); + } + + public void doIterableContainsWrongReturnTypeTest(String appName, String typeString) + throws Exception + { + // Try to return not-iterables + doReturnTest(appName, typeString); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + String firstLine = getOutput().split("\n")[0]; + assertTrue("Could not find exception 'NonStringOutput' in output: " + firstLine, + firstLine.indexOf("NonStringOutput")!=-1); + } + + public void testReturnIterableContainingInt ( ) + throws Exception + { + doIterableContainsWrongReturnTypeTest("test_iterable_containing_non_strings_return", "int"); + } + + public void testReturnIterableContainingFloat ( ) + throws Exception + { + doIterableContainsWrongReturnTypeTest("test_iterable_containing_non_strings_return", "float"); + } + + public void testReturnIterableContainingNone ( ) + throws Exception + { + doIterableContainsWrongReturnTypeTest("test_iterable_containing_non_strings_return", "none"); + } + + public void testStartResponseNotCalled ( ) + throws Exception + { + doReturnTest("test_start_response_not_called"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + String firstLine = getOutput().split("\n")[0]; + assertTrue("Could not find exception 'StartResponseNotCalled' in output: " + firstLine, + firstLine.indexOf("StartResponseNotCalled")!=-1); + } + + public void testBadLengthIterator ( ) + throws Exception + { + doReturnTest("test_bad_length_iterator"); + assertEquals("Status code != 500: ServerError, =='"+getStatus()+"'", 500, getStatus()); + assertTrue("Could not find exception 'WrongLength' in output", + getOutput().indexOf("WrongLength")!=-1); + } + + String testData = "Drifting breeze-blown clouds:\r\n"+ + "Shadows glide across the grass..\r\n"+ + "Apple blossom falls.\r\n"; + + public void doCorrectIterableTest(String appName) + throws Exception + { + // Try to return not-iterables + doReturnTest(appName, testData); + assertEquals("Status code != 200: ServerError, =='"+getStatus()+"'", 200, getStatus()); + String output = getOutput(); + assertEquals(output, testData); + } + + public void testReturnListOfString() + throws Exception + { + doCorrectIterableTest("test_return_list_strings"); + } + + public void testReturnGenerator() + throws Exception + { + doCorrectIterableTest("test_return_generator"); + } + + public void testReturnFileLike() + throws Exception + { + doCorrectIterableTest("test_return_file_like"); + } + + public void testLineEndsNotTranslated ( ) + throws Exception + { + doCorrectIterableTest("test_return_list_strings"); + } + + public void testIterableInstance ( ) + throws Exception + { + doCorrectIterableTest("test_iterable_instance"); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWSGIStreams.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWSGIStreams.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWSGIStreams.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,142 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import org.python.core.PyDictionary; +import org.python.core.PyString; + +public class ModjyTestWSGIStreams extends ModjyTestBase + { + + protected void streamTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("stream_tests.py"); + } + + // Tests in here are really container independent: no guarantee + // that mockrunner correctly simulates container behaviour + + public void doInputTest ( String appName, String bodyContent, String expectedContent, int expectedLength, int readSize ) + throws Exception + { + streamTestSetUp(); + setAppName(appName); + createServlet(); + // createServlet sets a default content string of "", so must override after, not before + setBodyContent(bodyContent); + // If there is a read length, set it in the query string + if (readSize != 0) + setQueryString("readsize="+String.valueOf(readSize)); + doGet(); + String app_output = getOutput(); + PyDictionary result = (PyDictionary) evalPythonString(app_output); + String instream_contents = ((PyString)result.__finditem__("data")).toString(); + assertEquals("Application output length != "+expectedLength+", =='"+instream_contents.length()+"'", + instream_contents.length(), expectedLength); + assertEquals("Application output != '"+expectedContent+"', =='"+instream_contents+"'", + instream_contents, expectedContent); + } + + public void doInputTest ( String appName, String bodyContent, String expectedContent, int expectedLength ) + throws Exception + { + doInputTest(appName, bodyContent, expectedContent, expectedLength, 0); + } + + public void testEmptyInput ( ) + throws Exception + { + doInputTest("test_read_input_stream", "", "", 0); + } + + public void testEmptyInputWithReadSize ( ) + throws Exception + { + doInputTest("test_read_input_stream", "", "", 0, 1024); + } + + public void testAsciiInput ( ) + throws Exception + { + doInputTest("test_read_input_stream", "Hello World!", "Hello World!", 12); + } + + public void testAsciiInputWithReadSize ( ) + throws Exception + { + for (int i = 0 ; i < 14 ; i++) + doInputTest("test_read_input_stream", "Hello World!", "Hello World!", 12, i); + } + + public void testAsciiInputReadline ( ) + throws Exception + { + doInputTest("test_readline_input_stream", "Hello\nWorld!\n", "Hello\n", 6); + doInputTest("test_readline_input_stream", "Hello", "Hello", 5); + } + + public void testAsciiInputReadlineWithSize ( ) + throws Exception + { + // Let's test this: although PEP-333 says it's not supported, modjy can do it + doInputTest("test_readline_input_stream", "Hello\nWorld!\n", "Hello", 5, 5); + } + + public void testAsciiInputWithReadlines ( ) + throws Exception + { + doInputTest("test_readlines_input_stream", "Hello\nWorld!\n", "Hello\n$World!\n", 14); + doInputTest("test_readlines_input_stream", "Hello", "Hello", 5); + } + + public void testAsciiInputWithReadlinesWithHint ( ) + throws Exception + { + // Let's leave this for now + // doInputTest("test_readlines_input_stream", "Hello\nWorld!\n", "Hello\n", 6, 5); + } + + public void testError () + throws Exception + { + // For now, just check the requisite methods exist and are callable + streamTestSetUp(); + setAppName("test_error_stream"); + createServlet(); + doGet(); + String app_output = getOutput(); + assertEquals("Application output != 'success', =='"+app_output+"'", + "success", app_output); + } + + public void testContentType() + { + // This probably needs several tests + } + + public void testContentLength() + { + // This probably needs several tests + } + + } Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWebInf.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWebInf.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWebInf.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,84 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import java.io.File; + +import org.python.core.PyDictionary; +import org.python.core.PyInteger; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyTuple; + +public class ModjyTestWebInf extends ModjyTestBase +{ + + final static String callables_dir = "test_apps"; + + protected void webInfTestSetUp() + throws Exception + { + baseSetUp(); + setAppFile("web_inf_tests.py"); + setRealPath("/WEB-INF/lib-python/some_libs", "lib_python_folder/test-lib"); + setRealPath("/WEB-INF/lib-python/some_libs/__init__.py", "lib_python_folder/test_lib/__init__.py"); + setRealPath("/WEB-INF/lib-python/some_libs/some_libs.py", "lib_python_folder/test_lib/some_libs.py"); + } + + public void testLibPythonVisible ( ) + throws Exception + { + // Check that a lib directory in lib-python is visible. + webInfTestSetUp(); + setAppName("test_import_from_lib_python"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("Factorial 10 is 3628800", result); + } + + public void testImportFromZipInLibPython ( ) + throws Exception + { + // Check that a lib directory in lib-python is visible. + webInfTestSetUp(); + setRealPath("/WEB-INF/lib-python/test_modules.zip", "lib_python_folder/test_modules.zip"); + setRealPath("/WEB-INF/lib-python/add_zips.pth", "lib_python_folder/add_zips.pth"); + setAppName("test_import_from_zip_file"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("This is a library function", result); + } + + public void testImportInPthFile ( ) + throws Exception + { + webInfTestSetUp(); + setRealPath("/WEB-INF/lib-python/do_import.pth", "lib_python_folder/do_import.pth"); + setAppName("test_execed_import_in_pth"); + createServlet(); + doGet(); + String result = getOutput(); + assertEquals("pass", result); + } + +} Added: branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWriteCallable.java =================================================================== --- branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWriteCallable.java (rev 0) +++ branches/modjy/tests/modjy/java/com/xhaus/modjy/ModjyTestWriteCallable.java 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,32 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +class ModjyTestWriteCallable extends ModjyTestBase + { + + public void testLineEndsNotTranslated ( ) + { + // Spec says no translation on output + } + + + } Added: branches/modjy/tests/modjy/lib_python_folder/add_zips.pth =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/add_zips.pth (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/add_zips.pth 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1 @@ +test_modules.zip Added: branches/modjy/tests/modjy/lib_python_folder/do_import.pth =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/do_import.pth (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/do_import.pth 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,4 @@ +# This is to test whether an import statement in a pth file is correctly exec'ed +# http://www.python.org/doc/2.5/lib/module-site.html + +import math Added: branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/wsgi_handlers.py =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/wsgi_handlers.py (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/mock_framework/web/handlers/wsgi_handlers.py 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,22 @@ +# -*- coding: windows-1252 -*- + +class WSGIHandlerClass: + + def __init__(self): + self.counter = 0 + + def _response(self, start_response_callable, called): + start_response_callable("200 OK", []) + result = "%s counter = %d" % (called, self.counter) + self.counter += 1 + return [result] + + def __call__(self, environ, start_response_callable): + return self._response(start_response_callable, "__call__") + + def handler_fn(self, environ, start_response_callable): + return self._response(start_response_callable, "handler_fn") + +def WSGIHandlerFunction(environ, start_response_callable): + start_response_callable("200 OK", []) + return ['WSGIHandlerFunction called.'] Added: branches/modjy/tests/modjy/lib_python_folder/script_name_path_info.py =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/script_name_path_info.py (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/script_name_path_info.py 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,6 @@ +# -*- coding: windows-1252 -*- + +def test_env_script_name_path_info(environ, start_response): + writer = start_response("200 OK", []) + writer("%s:::%s" % (environ['SCRIPT_NAME'], environ['PATH_INFO'])) + return [] Added: branches/modjy/tests/modjy/lib_python_folder/test_lib/__init__.py =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/test_lib/__init__.py (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/test_lib/__init__.py 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,10 @@ +# -*- coding: windows-1252 -*- + +# Specify only the some_libs module. + +#__all__ = ['some_libs', ] + +# And define a simple function in here as well. + +def stringTransform(s): + return s.upper() Added: branches/modjy/tests/modjy/lib_python_folder/test_lib/some_libs.py =================================================================== --- branches/modjy/tests/modjy/lib_python_folder/test_lib/some_libs.py (rev 0) +++ branches/modjy/tests/modjy/lib_python_folder/test_lib/some_libs.py 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,5 @@ +# -*- coding: windows-1252 -*- + +def factorial(n): + if n == 1: return 1 + return n * factorial(n-1) Added: branches/modjy/tests/modjy/lib_python_folder/test_modules.zip =================================================================== (Binary files differ) Property changes on: branches/modjy/tests/modjy/lib_python_folder/test_modules.zip ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: branches/modjy/tests/modjy/lines.txt =================================================================== --- branches/modjy/tests/modjy/lines.txt (rev 0) +++ branches/modjy/tests/modjy/lines.txt 2009-03-05 22:07:35 UTC (rev 6070) @@ -0,0 +1,4 @@ +This is line 1 +This is line 2 +This is line 3 + Added: branches/modjy/tests/modjy/readme.txt =================================================================== --- branches/modjy/tests/modjy/readme.txt (rev 0) +++ branches/modjy/tests/modjy/readme.txt 2009-03-05 22:... [truncated message content] |