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