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