xref: /unit/test/unit/applications/lang/java.py (revision 1938:418e225f58e4)
1import glob
2import os
3import shutil
4import subprocess
5
6import pytest
7from unit.applications.proto import TestApplicationProto
8from unit.option import option
9
10
11class TestApplicationJava(TestApplicationProto):
12    application_type = "java"
13
14    def prepare_env(self, script):
15        app_path = option.temp_dir + '/java'
16        web_inf_path = app_path + '/WEB-INF/'
17        classes_path = web_inf_path + 'classes/'
18        script_path = option.test_dir + '/java/' + script + '/'
19
20        if not os.path.isdir(app_path):
21            os.makedirs(app_path)
22
23        src = []
24
25        for f in os.listdir(script_path):
26            file_path = script_path + f
27
28            if f.endswith('.java'):
29                src.append(file_path)
30                continue
31
32            if f.startswith('.') or f == 'Makefile':
33                continue
34
35            if os.path.isdir(file_path):
36                if f == 'WEB-INF':
37                    continue
38
39                shutil.copytree(file_path, app_path + '/' + f)
40                continue
41
42            if f == 'web.xml':
43                if not os.path.isdir(web_inf_path):
44                    os.makedirs(web_inf_path)
45
46                shutil.copy2(file_path, web_inf_path)
47            else:
48                shutil.copy2(file_path, app_path)
49
50        if src:
51            if not os.path.isdir(classes_path):
52                os.makedirs(classes_path)
53
54            classpath = (
55                option.current_dir + '/build/tomcat-servlet-api-9.0.52.jar'
56            )
57
58            ws_jars = glob.glob(
59                option.current_dir + '/build/websocket-api-java-*.jar'
60            )
61
62            if not ws_jars:
63                pytest.fail('websocket api jar not found.')
64
65            javac = [
66                'javac',
67                '-target', '8', '-source', '8', '-nowarn',
68                '-encoding',   'utf-8',
69                '-d',          classes_path,
70                '-classpath',  classpath + ':' + ws_jars[0],
71            ]
72            javac.extend(src)
73
74            if option.detailed:
75                print("\n$ " + " ".join(javac))
76
77            try:
78                process = subprocess.Popen(javac, stderr=subprocess.STDOUT)
79                process.communicate()
80
81            except KeyboardInterrupt:
82                raise
83
84            except:
85                pytest.fail('Can\'t run javac process.')
86
87    def load(self, script, **kwargs):
88        self.prepare_env(script)
89
90        self._load_conf(
91            {
92                "listeners": {"*:7080": {"pass": "applications/" + script}},
93                "applications": {
94                    script: {
95                        "unit_jars": option.current_dir + '/build',
96                        "type": self.get_application_type(),
97                        "processes": {"spare": 0},
98                        "working_directory": option.test_dir
99                        + '/java/'
100                        + script
101                        + '/',
102                        "webapp": option.temp_dir + '/java',
103                    }
104                },
105            },
106            **kwargs
107        )
108