1import glob 2import os 3import shutil 4import subprocess 5 6import pytest 7from unit.applications.proto import ApplicationProto 8from unit.option import option 9 10 11class ApplicationJava(ApplicationProto): 12 def __init__(self, application_type='java'): 13 self.application_type = application_type 14 15 def prepare_env(self, script): 16 app_path = f'{option.temp_dir}/java' 17 web_inf_path = f'{app_path}/WEB-INF/' 18 classes_path = f'{web_inf_path}classes/' 19 script_path = f'{option.test_dir}/java/{script}/' 20 21 if not os.path.isdir(app_path): 22 os.makedirs(app_path) 23 24 src = [] 25 26 for f in os.listdir(script_path): 27 file_path = f'{script_path}{f}' 28 29 if f.endswith('.java'): 30 src.append(file_path) 31 continue 32 33 if f.startswith('.') or f == 'Makefile': 34 continue 35 36 if os.path.isdir(file_path): 37 if f == 'WEB-INF': 38 continue 39 40 shutil.copytree(file_path, f'{app_path}/{f}') 41 continue 42 43 if f == 'web.xml': 44 if not os.path.isdir(web_inf_path): 45 os.makedirs(web_inf_path) 46 47 shutil.copy2(file_path, web_inf_path) 48 else: 49 shutil.copy2(file_path, app_path) 50 51 if src: 52 if not os.path.isdir(classes_path): 53 os.makedirs(classes_path) 54 55 classpath = ( 56 f'{option.current_dir}/build/tomcat-servlet-api-9.0.86.jar' 57 ) 58 59 ws_jars = glob.glob( 60 f'{option.current_dir}/build/websocket-api-java*.jar' 61 ) 62 63 if not ws_jars: 64 pytest.fail('websocket api jar not found.') 65 66 javac = [ 67 'javac', 68 '-target', 69 '8', 70 '-source', 71 '8', 72 '-nowarn', 73 '-encoding', 74 'utf-8', 75 '-d', 76 classes_path, 77 '-classpath', 78 f'{classpath}:{ws_jars[0]}', 79 ] 80 javac.extend(src) 81 82 if option.detailed: 83 print(f'\n$ {" ".join(javac)}') 84 85 try: 86 subprocess.check_output(javac, stderr=subprocess.STDOUT) 87 88 except KeyboardInterrupt: 89 raise 90 91 except subprocess.CalledProcessError: 92 pytest.fail("Can't run javac process.") 93 94 def load(self, script, **kwargs): 95 self.prepare_env(script) 96 97 script_path = f'{option.test_dir}/java/{script}/' 98 self._load_conf( 99 { 100 "listeners": {"*:8080": {"pass": f"applications/{script}"}}, 101 "applications": { 102 script: { 103 "unit_jars": f'{option.current_dir}/build', 104 "type": self.get_application_type(), 105 "processes": {"spare": 0}, 106 "working_directory": script_path, 107 "webapp": f'{option.temp_dir}/java', 108 } 109 }, 110 }, 111 **kwargs, 112 ) 113