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