xref: /unit/test/unit/applications/lang/java.py (revision 1041:9bdd46610ea9)
1import os
2import shutil
3from subprocess import Popen
4from unit.applications.proto import TestApplicationProto
5
6
7class TestApplicationJava(TestApplicationProto):
8    def load(self, script, name='app'):
9
10        app_path = self.testdir + '/java'
11        web_inf_path = app_path + '/WEB-INF/'
12        classes_path = web_inf_path + 'classes/'
13
14        script_path = self.current_dir + '/java/' + script + '/'
15
16        if not os.path.isdir(app_path):
17            os.makedirs(app_path)
18
19        src = []
20
21        for f in os.listdir(script_path):
22            if f.endswith('.java'):
23                src.append(script_path + f)
24                continue
25
26            if f.startswith('.') or f == 'Makefile':
27                continue
28
29            if os.path.isdir(script_path + f):
30                if f == 'WEB-INF':
31                    continue
32
33                shutil.copytree(script_path + f, app_path + '/' + f)
34                continue
35
36            if f == 'web.xml':
37                if not os.path.isdir(web_inf_path):
38                    os.makedirs(web_inf_path)
39
40                shutil.copy2(script_path + f, web_inf_path)
41            else:
42                shutil.copy2(script_path + f, app_path)
43
44        if src:
45            if not os.path.isdir(classes_path):
46                os.makedirs(classes_path)
47
48            tomcat_jar = self.pardir + '/build/tomcat-servlet-api-9.0.13.jar'
49
50            javac = [
51                'javac',
52                '-encoding',   'utf-8',
53                '-d',          classes_path,
54                '-classpath',  tomcat_jar,
55            ]
56            javac.extend(src)
57
58            process = Popen(javac)
59            process.communicate()
60
61        self.conf(
62            {
63                "listeners": {"*:7080": {"pass": "applications/" + script}},
64                "applications": {
65                    script: {
66                        "unit_jars": self.pardir + '/build',
67                        "type": "java",
68                        "processes": {"spare": 0},
69                        "working_directory": script_path,
70                        "webapp": app_path,
71                    }
72                },
73            }
74        )
75