File: PySystemStateTest.java

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (218 lines) | stat: -rw-r--r-- 10,715 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package org.python.core;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.python.util.PythonInterpreter;

import jnr.posix.util.Platform;
import junit.framework.TestCase;

public class PySystemStateTest extends TestCase {

    /**
     * A class to hold examples of URLs (just the path and class noise) and the reference answer.
     * Provide the reference answer like a Un*x path (forward slash).
     */
    private static class JarExample {

        final String urlJarPath;
        final String urlClassPath;
        final String filePath;

        /** This constructor adapts unixPath to Windows when on Windows. */
        JarExample(String urlJarPath, String urlClassPath, String unixPath) {
            this(urlJarPath, urlClassPath,
                    Platform.IS_WINDOWS ? new File(unixPath).toString() : unixPath, true);
        }

        /** This constructor accepts filePath exactly as given. */
        JarExample(String urlJarPath, String urlClassPath, String filePath, boolean ignored) {
            this.urlJarPath = urlJarPath;
            this.urlClassPath = urlClassPath;
            this.filePath = filePath;
        }
    }

    /**
     * Examples of URLs (just the path and class noise) and the reference answer. Provide the
     * reference answer like a Un*x path (forward slash).
     */
    private static List<JarExample> jarExamples = Arrays.asList(//
            // simple jar-file url
            new JarExample("/some_dir/some.jar", "a/package/with/A.class", "/some_dir/some.jar"),
            // jar-file url to decode
            new JarExample("/some%20dir/some.jar", "a/package/with/A.class", "/some dir/some.jar"),
            // In an old implementation using URLDecoder "+" needed special treatment
            new JarExample("/some+dir/some.jar", "a/package/with/A.class", "/some+dir/some.jar"),
            // Some characters should be encoded in the URL, but emerge as themselves in the path.
            new JarExample("/n%c3%a5gon/katalog/r%c3%a4tt.jar", "en/f%c3%b6rpackning/med/En.class",
                    "/någon/katalog/rätt.jar") //
    );

    /* Check drive-letter  and UNC path handling if on Windows. */
    static {
        if (Platform.IS_WINDOWS) {
            // Add some examples to the list (must be made mutable for that).
            jarExamples = new ArrayList<JarExample>(jarExamples);

            // Drive-letter examples
            jarExamples.add(new JarExample("/C:/some_dir/some.jar", "a/package/with/A.class",
                    "C:\\some_dir\\some.jar", true));
            jarExamples.add(new JarExample("/E:/n%c3%a5gon/katalog/r%c3%a4tt.jar", "med/En.class",
                    "E:\\någon\\katalog\\rätt.jar", true));

            // Simple network file path (UNC path without controversial characters)
            String p = "/org/python/version.properies";
            String r = "\\\\localhost\\shared\\jython-dev.jar";
            // JAR UNC file resource URL as produced by File.getURL or getURI
            jarExamples.add(new JarExample("////localhost/shared/jython-dev.jar", p, r, true));
            // JAR UNC file resource URL as produced by URLClassLoader.getResource
            jarExamples.add(new JarExample("//localhost/shared/jython-dev.jar", p, r, true));

            // Network file path (UNC path with a controversial characters)
            r = "\\\\localhost\\shared\\jy thon%dev.jar";
            // JAR UNC file resource URL based on (deprecated) File.getURL is invalid
            // jarExamples.add(new JarExample("//localhost/shared/jy thon%dev.jar", p, r, true));
            // JAR UNC file resource URL based on File.getURI
            jarExamples.add(new JarExample("////localhost/shared/jy%20thon%25dev.jar", p, r, true));
            // JAR UNC file resource URL as produced by URLClassLoader.getResource
            jarExamples.add(new JarExample("//localhost/shared/jy%20thon%25dev.jar", p, r, true));
        }
    }

    /**
     * Test case for finding the path in the local file system of the file located by a JAR-file
     * URL. A URL is a sequence of characters (from a limited set) that encodes a sequence of octets
     * that may (if the protocol intends it) represent characters in some encoding. In the case of a
     * JAR-file URL, these octets encode the file path elements in UTF-8.
     */
    public void testGetJarFileNameFromURL() throws Exception {
        // null
        assertNull(Py.getJarFileNameFromURL(null));
        // Examples from the table
        for (JarExample ex : jarExamples) {
            // Something like jar:file:/some_dir/some.jar!/a/package/with/A.class
            URL url = new URL("jar:file:" + ex.urlJarPath + "!/" + ex.urlClassPath);
            assertEquals(ex.filePath, Py.getJarFileNameFromURL(url));
        }
    }

    /**
     * Test case for finding the path in the local file system of the file located by a JBoss vfszip
     * URL. This is problematic as an objective because a path in the VFS does not necessarily have
     * a counterpart in the local file system. However, the implementation and test are based on
     * behaviour observed when this is the case.
     */
    public void testGetJarFileNameFromURL_jboss() throws Exception {
        final String protocol = "vfszip";
        final String host = "";
        final int port = -1;
        final URLStreamHandler handler = new TestJBossURLStreamHandler();
        // Test with any class file in org.python.core
        final String classPart = "/org/python/core/PySystemState.class";
        String file;
        URL url;
        if (Platform.IS_WINDOWS) {
            // plain jboss url
            file = "/C:/some_dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            // tests with jboss on windows gave URL's like this:
            assertEquals("vfszip:/C:/some_dir/some.jar" + classPart, url.toString());
            String result = Py.getJarFileNameFromURL(url);
            assertEquals("C:\\some_dir\\some.jar", result);
            // jboss url to decode
            file = "/C:/some%20dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/C:/some%20dir/some.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("C:\\some dir\\some.jar", result);
            // jboss url with + to escape
            file = "/C:/some+dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/C:/some+dir/some.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("C:\\some+dir\\some.jar", result);
            // jboss url with challenging JAR name (assume will be provided RFC-2396 encoded)
            file = "/C:/n%c3%a5gon/katalog/r%c3%a4tt.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/C:/n%c3%a5gon/katalog/r%c3%a4tt.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("C:\\någon\\katalog\\rätt.jar", result);
        } else {
            // plain jboss url
            file = "/some_dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/some_dir/some.jar" + classPart, url.toString());
            String result = Py.getJarFileNameFromURL(url);
            assertEquals("/some_dir/some.jar", result);
            // jboss url to decode
            file = "/some%20dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/some%20dir/some.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("/some dir/some.jar", result);
            // jboss url with + to escape
            file = "/some+dir/some.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/some+dir/some.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("/some+dir/some.jar", result);
            // jboss url with challenging JAR name (assume will be provided RFC-2396 encoded)
            file = "/n%c3%a5gon/katalog/r%c3%a4tt.jar" + classPart;
            url = new URL(protocol, host, port, file, handler);
            assertEquals("vfszip:/n%c3%a5gon/katalog/r%c3%a4tt.jar" + classPart, url.toString());
            result = Py.getJarFileNameFromURL(url);
            assertEquals("/någon/katalog/rätt.jar", result);
        }
    }

    /**
     * Test case for finding the path in the local file system of the file located by a JAR-file URL
     * as reported by a JavaWebStart application. As the path points to a HTTP resource, there is no
     * way to find the jar in the local file system: in this case, the method must return null.
     */
    public void testGetJarFileNameFromURL_javaWebStart() throws MalformedURLException {
        // Examples from the table
        for (JarExample ex : jarExamples) {
            // Something like jar:file:/some_dir/some.jar!/a/package/with/A.class
            URL url = new URL("jar:http://server:8080/signedWebApp/" + ex.urlJarPath + "!/"
                    + ex.urlClassPath);
            assertNull(PrePy.getJarFileNameFromURL(url));
        }
    }

    public void testImport() throws Exception {
        Options.importSite = false;
        try {
            PySystemState pySystemState = new PySystemState();
            PySystemState.initialize();
            PythonInterpreter interpreter = new PythonInterpreter(null, pySystemState);
            interpreter.exec("import os");
            assertTrue(interpreter.getSystemState().modules.__contains__(new PyString("os")));
        } finally {
            Options.importSite = true;
        }
    }

    /**
     * A URL handler that emulates the behaviour (as far as we're concerned) of
     * {@code org.jboss.virtual.protocol.vfs.Handler}, that we can use to make URLs that behave the
     * same way as JBoss ones.
     *
     */
    protected static class TestJBossURLStreamHandler extends URLStreamHandler {

        @Override
        protected URLConnection openConnection(URL u) throws IOException {
            throw new RuntimeException("unexpected call to openConnection " + u.toString());
        }
    }
}