xref: /unit/test/unit/applications/lang/java.py (revision 2004:ca4c5855f139)
11477Szelenkov@nginx.comimport glob
21019Szelenkov@nginx.comimport os
31019Szelenkov@nginx.comimport shutil
41388Szelenkov@nginx.comimport subprocess
51477Szelenkov@nginx.com
61635Szelenkov@nginx.comimport pytest
71019Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
81730Szelenkov@nginx.comfrom unit.option import option
91019Szelenkov@nginx.com
101019Szelenkov@nginx.com
111019Szelenkov@nginx.comclass TestApplicationJava(TestApplicationProto):
121670Smax.romanov@nginx.com    application_type = "java"
131670Smax.romanov@nginx.com
141657Szelenkov@nginx.com    def prepare_env(self, script):
151654Szelenkov@nginx.com        app_path = option.temp_dir + '/java'
161019Szelenkov@nginx.com        web_inf_path = app_path + '/WEB-INF/'
171019Szelenkov@nginx.com        classes_path = web_inf_path + 'classes/'
181596Szelenkov@nginx.com        script_path = option.test_dir + '/java/' + script + '/'
191019Szelenkov@nginx.com
201019Szelenkov@nginx.com        if not os.path.isdir(app_path):
211019Szelenkov@nginx.com            os.makedirs(app_path)
221019Szelenkov@nginx.com
231019Szelenkov@nginx.com        src = []
241019Szelenkov@nginx.com
251019Szelenkov@nginx.com        for f in os.listdir(script_path):
261163Szelenkov@nginx.com            file_path = script_path + f
271163Szelenkov@nginx.com
281019Szelenkov@nginx.com            if f.endswith('.java'):
291163Szelenkov@nginx.com                src.append(file_path)
301019Szelenkov@nginx.com                continue
311019Szelenkov@nginx.com
321019Szelenkov@nginx.com            if f.startswith('.') or f == 'Makefile':
331019Szelenkov@nginx.com                continue
341019Szelenkov@nginx.com
351163Szelenkov@nginx.com            if os.path.isdir(file_path):
361019Szelenkov@nginx.com                if f == 'WEB-INF':
371019Szelenkov@nginx.com                    continue
381019Szelenkov@nginx.com
391163Szelenkov@nginx.com                shutil.copytree(file_path, app_path + '/' + f)
401019Szelenkov@nginx.com                continue
411019Szelenkov@nginx.com
421019Szelenkov@nginx.com            if f == 'web.xml':
431019Szelenkov@nginx.com                if not os.path.isdir(web_inf_path):
441019Szelenkov@nginx.com                    os.makedirs(web_inf_path)
451019Szelenkov@nginx.com
461163Szelenkov@nginx.com                shutil.copy2(file_path, web_inf_path)
471019Szelenkov@nginx.com            else:
481163Szelenkov@nginx.com                shutil.copy2(file_path, app_path)
491019Szelenkov@nginx.com
501019Szelenkov@nginx.com        if src:
511019Szelenkov@nginx.com            if not os.path.isdir(classes_path):
521019Szelenkov@nginx.com                os.makedirs(classes_path)
531019Szelenkov@nginx.com
541621Szelenkov@nginx.com            classpath = (
551938Smax.romanov@nginx.com                option.current_dir + '/build/tomcat-servlet-api-9.0.52.jar'
561621Szelenkov@nginx.com            )
571157Smax.romanov@nginx.com
581157Smax.romanov@nginx.com            ws_jars = glob.glob(
591621Szelenkov@nginx.com                option.current_dir + '/build/websocket-api-java-*.jar'
601157Smax.romanov@nginx.com            )
611157Smax.romanov@nginx.com
621157Smax.romanov@nginx.com            if not ws_jars:
631596Szelenkov@nginx.com                pytest.fail('websocket api jar not found.')
641019Szelenkov@nginx.com
651019Szelenkov@nginx.com            javac = [
661019Szelenkov@nginx.com                'javac',
67*2004Szelenkov@nginx.com                '-target',
68*2004Szelenkov@nginx.com                '8',
69*2004Szelenkov@nginx.com                '-source',
70*2004Szelenkov@nginx.com                '8',
71*2004Szelenkov@nginx.com                '-nowarn',
72*2004Szelenkov@nginx.com                '-encoding',
73*2004Szelenkov@nginx.com                'utf-8',
74*2004Szelenkov@nginx.com                '-d',
75*2004Szelenkov@nginx.com                classes_path,
76*2004Szelenkov@nginx.com                '-classpath',
77*2004Szelenkov@nginx.com                classpath + ':' + ws_jars[0],
781019Szelenkov@nginx.com            ]
791019Szelenkov@nginx.com            javac.extend(src)
801019Szelenkov@nginx.com
811706Smax.romanov@nginx.com            if option.detailed:
821706Smax.romanov@nginx.com                print("\n$ " + " ".join(javac))
831706Smax.romanov@nginx.com
841208Szelenkov@nginx.com            try:
85*2004Szelenkov@nginx.com                subprocess.check_output(javac, stderr=subprocess.STDOUT)
861208Szelenkov@nginx.com
871706Smax.romanov@nginx.com            except KeyboardInterrupt:
881706Smax.romanov@nginx.com                raise
891706Smax.romanov@nginx.com
90*2004Szelenkov@nginx.com            except subprocess.CalledProcessError:
911706Smax.romanov@nginx.com                pytest.fail('Can\'t run javac process.')
921019Szelenkov@nginx.com
931657Szelenkov@nginx.com    def load(self, script, **kwargs):
941657Szelenkov@nginx.com        self.prepare_env(script)
951657Szelenkov@nginx.com
961050Szelenkov@nginx.com        self._load_conf(
971019Szelenkov@nginx.com            {
981041Svbart@nginx.com                "listeners": {"*:7080": {"pass": "applications/" + script}},
991019Szelenkov@nginx.com                "applications": {
1001019Szelenkov@nginx.com                    script: {
1011621Szelenkov@nginx.com                        "unit_jars": option.current_dir + '/build',
1021670Smax.romanov@nginx.com                        "type": self.get_application_type(),
1031019Szelenkov@nginx.com                        "processes": {"spare": 0},
1041657Szelenkov@nginx.com                        "working_directory": option.test_dir
1051657Szelenkov@nginx.com                        + '/java/'
1061657Szelenkov@nginx.com                        + script
1071657Szelenkov@nginx.com                        + '/',
1081657Szelenkov@nginx.com                        "webapp": option.temp_dir + '/java',
1091019Szelenkov@nginx.com                    }
1101019Szelenkov@nginx.com                },
1111303St.nateldemoura@f5.com            },
1121303St.nateldemoura@f5.com            **kwargs
1131019Szelenkov@nginx.com        )
114