File: PyServletTest.java

package info (click to toggle)
jython 2.7.1%2Brepack1-4~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 63,416 kB
  • sloc: python: 639,274; java: 296,480; xml: 1,743; sh: 396; ansic: 126; makefile: 72
file content (88 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.python.util;

import javax.servlet.ServletException;

import org.python.core.PyFile;
import com.mockrunner.base.NestedApplicationException;
import com.mockrunner.mock.web.MockServletConfig;
import com.mockrunner.mock.web.MockServletContext;
import com.mockrunner.mock.web.WebMockObjectFactory;
import com.mockrunner.servlet.BasicServletTestCaseAdapter;

public class PyServletTest extends BasicServletTestCaseAdapter {
    public void testGet() {
        assertEquals("Basic text response", doGetAndRead("basic"));
    }

    public void testNoCallable() {
        try {
            doGetAndRead("empty");
            fail("Using an empty file for PyServlet should raise a ServletException");
        } catch (NestedApplicationException e) {
            assertTrue(e.getRootCause() instanceof ServletException);
        }
    }

    public void testReload() {
        String originalBasic = readTestFile("basic");
        try {
            testGet();
            writeToTestFile("basic", readTestFile("updated_basic"));
            assertEquals("Updated text response", doGetAndRead("basic"));
        } finally {
            writeToTestFile("basic", originalBasic);
        }
    }

    public void testInstanceCaching() {
        assertEquals("1", doGetAndRead("increment"));
        assertEquals("2", doGetAndRead("increment"));
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        MockServletConfig cfg = getWebMockObjectFactory().getMockServletConfig();
        cfg.setInitParameter("python.home", System.getProperty("JYTHON_HOME"));
        createServlet(PyServlet.class);
    }

    protected String doGetAndRead(String testName) {
        getWebMockObjectFactory().getMockRequest().setServletPath(getTestPath(testName));
        doGet();
        String result = getOutput();
        clearOutput();
        return result;
    }

    protected String getTestPath(String testName) {
        return "/test/pyservlet/" + testName + ".py";
    }

    private String readTestFile(String testName) {
        PyFile in = new PyFile(basePath + getTestPath(testName), "r", 4192);
        String result = in.read().toString();
        return result;
    }

    private void writeToTestFile(String testName, String newContents) {
        PyFile out = new PyFile(basePath + getTestPath(testName), "w", 4192);
        out.write(newContents);
        out.close();
    }

    @Override
    protected WebMockObjectFactory createWebMockObjectFactory() {
        return new WebMockObjectFactory() {
            @Override public MockServletContext createMockServletContext() {
                return new MockServletContext() {
                    @Override public synchronized String getRealPath(String path) {
                        return basePath + path;
                    }
                };
            }
        };
    }

    protected String basePath = System.getProperty("JYTHON_HOME") + "/Lib";
}