xref: /unit/test/unit/applications/lang/java.py (revision 2004:ca4c5855f139)
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',
68                '8',
69                '-source',
70                '8',
71                '-nowarn',
72                '-encoding',
73                'utf-8',
74                '-d',
75                classes_path,
76                '-classpath',
77                classpath + ':' + ws_jars[0],
78            ]
79            javac.extend(src)
80
81            if option.detailed:
82                print("\n$ " + " ".join(javac))
83
84            try:
85                subprocess.check_output(javac, stderr=subprocess.STDOUT)
86
87            except KeyboardInterrupt:
88                raise
89
90            except subprocess.CalledProcessError:
91                pytest.fail('Can\'t run javac process.')
92
93    def load(self, script, **kwargs):
94        self.prepare_env(script)
95
96        self._load_conf(
97            {
98                "listeners": {"*:7080": {"pass": "applications/" + script}},
99                "applications": {
100                    script: {
101                        "unit_jars": option.current_dir + '/build',
102                        "type": self.get_application_type(),
103                        "processes": {"spare": 0},
104                        "working_directory": option.test_dir
105                        + '/java/'
106                        + script
107                        + '/',
108                        "webapp": option.temp_dir + '/java',
109                    }
110                },
111            },
112            **kwargs
113        )
114