summaryrefslogtreecommitdiffhomepage
path: root/test/unit/applications/lang/java.py
blob: 0aa1a7af53a578325f6253655732081773ae7cb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import glob
import os
import shutil
import subprocess

import pytest
from conftest import option
from unit.applications.proto import TestApplicationProto


class TestApplicationJava(TestApplicationProto):
    def prepare_env(self, script):
        app_path = option.temp_dir + '/java'
        web_inf_path = app_path + '/WEB-INF/'
        classes_path = web_inf_path + 'classes/'
        script_path = option.test_dir + '/java/' + script + '/'

        if not os.path.isdir(app_path):
            os.makedirs(app_path)

        src = []

        for f in os.listdir(script_path):
            file_path = script_path + f

            if f.endswith('.java'):
                src.append(file_path)
                continue

            if f.startswith('.') or f == 'Makefile':
                continue

            if os.path.isdir(file_path):
                if f == 'WEB-INF':
                    continue

                shutil.copytree(file_path, app_path + '/' + f)
                continue

            if f == 'web.xml':
                if not os.path.isdir(web_inf_path):
                    os.makedirs(web_inf_path)

                shutil.copy2(file_path, web_inf_path)
            else:
                shutil.copy2(file_path, app_path)

        if src:
            if not os.path.isdir(classes_path):
                os.makedirs(classes_path)

            classpath = (
                option.current_dir + '/build/tomcat-servlet-api-9.0.13.jar'
            )

            ws_jars = glob.glob(
                option.current_dir + '/build/websocket-api-java-*.jar'
            )

            if not ws_jars:
                pytest.fail('websocket api jar not found.')

            javac = [
                'javac',
                '-encoding',   'utf-8',
                '-d',          classes_path,
                '-classpath',  classpath + ':' + ws_jars[0],
            ]
            javac.extend(src)

            try:
                process = subprocess.Popen(javac, stderr=subprocess.STDOUT)
                process.communicate()

            except:
                pytest.fail('Cann\'t run javac process.')

    def load(self, script, **kwargs):
        self.prepare_env(script)

        self._load_conf(
            {
                "listeners": {"*:7080": {"pass": "applications/" + script}},
                "applications": {
                    script: {
                        "unit_jars": option.current_dir + '/build',
                        "type": 'java',
                        "processes": {"spare": 0},
                        "working_directory": option.test_dir
                        + '/java/'
                        + script
                        + '/',
                        "webapp": option.temp_dir + '/java',
                    }
                },
            },
            **kwargs
        )