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'): 10 11 app_path = self.testdir + '/java' 12 web_inf_path = app_path + '/WEB-INF/' 13 classes_path = web_inf_path + 'classes/' 14 15 script_path = self.current_dir + '/java/' + script + '/' 16 17 if not os.path.isdir(app_path): 18 os.makedirs(app_path) 19 20 src = [] 21 22 for f in os.listdir(script_path): 23 if f.endswith('.java'): 24 src.append(script_path + f) 25 continue 26 27 if f.startswith('.') or f == 'Makefile': 28 continue 29 30 if os.path.isdir(script_path + f): 31 if f == 'WEB-INF': 32 continue 33 34 shutil.copytree(script_path + f, 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(script_path + f, web_inf_path) 42 else: 43 shutil.copy2(script_path + f, 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 process = Popen(javac) 67 process.communicate() 68 69 self._load_conf( 70 { 71 "listeners": {"*:7080": {"pass": "applications/" + script}}, 72 "applications": { 73 script: { 74 "unit_jars": self.pardir + '/build', 75 "type": 'java', 76 "processes": {"spare": 0}, 77 "working_directory": script_path, 78 "webapp": app_path, 79 } 80 }, 81 } 82 ) 83