Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.anas.testloader.controller;
- @RestController
- public class Controller {
- @ResponseStatus(value = HttpStatus.OK)
- @RequestMapping(value = "/getTestList", method = RequestMethod.GET)
- public ResponseEntity<JarClassMethod> getTestList() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- if (TestLoaderApplication.changed){
- ServiceUtil.getAllTests();
- TestLoaderApplication.changed=false;
- }
- return new ResponseEntity<>(TestLoaderApplication.testClassObjMap, HttpStatus.OK);
- }
- @RequestMapping(value = {"/startTest/{jar}/{class_name}/{test_name}","/startTest/{jar}/{class_name}"},
- method = RequestMethod.GET)
- public ResponseEntity<Esito> runTest(@PathVariable String jar, @PathVariable String class_name, @PathVariable Optional<String> test_name ) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, MalformedURLException, InstantiationException, IllegalArgumentException, InvocationTargetException {
- ReportInnerTests listener = new ReportInnerTests();
- ServiceUtil.runOne(jar,class_name,test_name,listener);
- Map<String, String> summary= listener.getSummary();
- HttpHeaders responseHeaders = new HttpHeaders();
- Esito esito=new Esito();
- esito.setSummary(summary);
- return new ResponseEntity<Esito>(esito, responseHeaders, HttpStatus.OK);
- }
- }
- @Component
- public class ServiceUtil {
- private static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtil.class);
- public static void getAllTests() throws IllegalArgumentException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException,LinkageError, ClassNotFoundException {
- TestLoaderApplication.testClassObjMap.clear();
- LoadLibrary loadLibrary=new LoadLibrary();
- List<JarFile> jarList= loadLibrary.getListJar(pathJars).stream().map(f -> {
- try {
- return new JarFile(f);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- })
- .collect(Collectors.toList());
- for (JarFile j : jarList) {
- URLClassLoader child = new URLClassLoader(
- new URL[] {new File(j.getName()).toURI().toURL()},
- ServiceUtil.class.getClassLoader()
- );
- for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
- JarEntry entry = entries.nextElement();
- String file = entry.getName();
- if (file.endsWith(".class")) {
- String classname = file.replaceAll("/", ".")
- .substring(0, file.lastIndexOf("."));
- try {
- Class<?> currentClass = Class.forName(classname,true,child);
- List<String> testMethods = new ArrayList<>();
- for (int i = 0; i < currentClass.getMethods().length; i++) {
- Method method = currentClass.getMethods()[i];
- Annotation annTest = method.getAnnotation(Test.class);
- Annotation annTestFactory = method.getAnnotation(TestFactory.class);
- if (annTest != null || annTestFactory != null) {
- testMethods.add(method.getName());
- }
- }//fine for metodi
- if (testMethods.size() >=1) {
- //if windows: testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\")+1),classname,testMethods);
- TestLoaderApplication.testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("/")+1),classname,testMethods);
- LOGGER.info(String.format("%s %s %s",j.toString(),classname,testMethods));
- }
- }catch (NoClassDefFoundError e) {
- LOGGER.warn("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
- }
- catch (Throwable e) {
- LOGGER.warn("WARNING: failed to instantiate " + classname + " from " + file);
- }
- }//if .class
- }//chiudo jarentry for
- j.close();
- child.close();
- }//chiudo jarfile for
- LOGGER.info("Test Loader Console Back-End:\tFine Reflection Scan");
- }
- public static void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener) throws ClassNotFoundException, NoSuchMethodException, MalformedURLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- Launcher launcher = LauncherFactory.create();
- /*URLClassLoader child = new URLClassLoader(
- new URL[] {new File(pathJars+"/"+jar).toURI().toURL()},
- this.getClass().getClassLoader()
- );*/
- LOGGER.info(String.format("Test Loader Console Back-End:\tRunOne Method %s, %s, %s",jar,class_name,test_name));
- ClassLoader loader = URLClassLoader.newInstance(
- new URL[] { new File(pathJars+"/"+jar).toURI().toURL() },
- ServiceUtil.class.getClassLoader()
- );
- loader.getClass();
- Class cls=Class.forName(class_name,true,loader);
- Constructor constructor = cls.getConstructor();
- constructor.newInstance();
- LauncherDiscoveryRequest request;
- if (test_name.isPresent()) {
- Method m = cls.getMethod(test_name.get());
- request = LauncherDiscoveryRequestBuilder.request()
- .selectors(selectMethod(cls,m))
- .build();
- }
- else{
- request = LauncherDiscoveryRequestBuilder.request()
- .selectors(selectClass(cls))
- .build();
- }
- TestPlan testPlan = launcher.discover(request);
- launcher.registerTestExecutionListeners(listener);
- launcher.execute(request);
- }
- }
Add Comment
Please, Sign In to add comment