Guest User

Untitled

a guest
May 1st, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.28 KB | None | 0 0
  1. package it.anas.testloader.controller;
  2.  
  3.  
  4. @RestController
  5. public class Controller {
  6.  
  7.  
  8.     @ResponseStatus(value = HttpStatus.OK)
  9.     @RequestMapping(value = "/getTestList", method = RequestMethod.GET)
  10.     public ResponseEntity<JarClassMethod> getTestList() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  11.         if (TestLoaderApplication.changed){
  12.             ServiceUtil.getAllTests();
  13.             TestLoaderApplication.changed=false;
  14.         }
  15.         return new ResponseEntity<>(TestLoaderApplication.testClassObjMap, HttpStatus.OK);
  16.     }
  17.  
  18.     @RequestMapping(value = {"/startTest/{jar}/{class_name}/{test_name}","/startTest/{jar}/{class_name}"},
  19.             method = RequestMethod.GET)
  20.     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 {
  21.         ReportInnerTests listener = new ReportInnerTests();
  22.         ServiceUtil.runOne(jar,class_name,test_name,listener);
  23.         Map<String, String> summary= listener.getSummary();
  24.  
  25.         HttpHeaders responseHeaders = new HttpHeaders();
  26.         Esito esito=new Esito();
  27.         esito.setSummary(summary);
  28.         return new ResponseEntity<Esito>(esito, responseHeaders, HttpStatus.OK);
  29.     }
  30. }
  31.  
  32.  
  33. @Component
  34. public class ServiceUtil {
  35.     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceUtil.class);
  36.     public static void getAllTests() throws IllegalArgumentException,  NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException,LinkageError, ClassNotFoundException {
  37.         TestLoaderApplication.testClassObjMap.clear();
  38.         LoadLibrary loadLibrary=new LoadLibrary();
  39.         List<JarFile> jarList= loadLibrary.getListJar(pathJars).stream().map(f -> {
  40.             try {
  41.                 return new JarFile(f);
  42.             } catch (IOException e) {
  43.                 e.printStackTrace();
  44.             }
  45.             return null;
  46.         })
  47.                 .collect(Collectors.toList());
  48.  
  49.         for (JarFile j : jarList) {
  50.             URLClassLoader child = new URLClassLoader(
  51.                     new URL[] {new File(j.getName()).toURI().toURL()},
  52.                     ServiceUtil.class.getClassLoader()
  53.             );
  54.  
  55.             for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
  56.                 JarEntry entry = entries.nextElement();
  57.                 String file = entry.getName();
  58.                 if (file.endsWith(".class")) {
  59.                     String classname = file.replaceAll("/", ".")
  60.                             .substring(0, file.lastIndexOf("."));
  61.                     try {
  62.                         Class<?> currentClass = Class.forName(classname,true,child);
  63.                         List<String> testMethods = new ArrayList<>();
  64.                         for (int i = 0; i < currentClass.getMethods().length; i++) {
  65.                             Method method = currentClass.getMethods()[i];
  66.                             Annotation annTest = method.getAnnotation(Test.class);
  67.                             Annotation annTestFactory = method.getAnnotation(TestFactory.class);
  68.                             if (annTest != null || annTestFactory != null) {
  69.                                 testMethods.add(method.getName());
  70.                             }
  71.                         }//fine for metodi
  72.                         if (testMethods.size() >=1) {
  73.                             //if windows: testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\")+1),classname,testMethods);
  74.                             TestLoaderApplication.testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("/")+1),classname,testMethods);
  75.                             LOGGER.info(String.format("%s %s %s",j.toString(),classname,testMethods));
  76.                         }
  77.                     }catch (NoClassDefFoundError e) {
  78.                         LOGGER.warn("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
  79.                     }
  80.                     catch (Throwable e) {
  81.                         LOGGER.warn("WARNING: failed to instantiate " + classname + " from " + file);
  82.                     }
  83.  
  84.                 }//if .class
  85.  
  86.             }//chiudo jarentry for
  87.             j.close();
  88.             child.close();
  89.  
  90.         }//chiudo jarfile for
  91.  
  92.         LOGGER.info("Test Loader Console Back-End:\tFine Reflection Scan");
  93.  
  94.     }
  95.  
  96.     public static void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener) throws ClassNotFoundException, NoSuchMethodException, MalformedURLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  97.         Launcher launcher = LauncherFactory.create();
  98.         /*URLClassLoader child = new URLClassLoader(
  99.                 new URL[] {new File(pathJars+"/"+jar).toURI().toURL()},
  100.                 this.getClass().getClassLoader()
  101.         );*/
  102.         LOGGER.info(String.format("Test Loader Console Back-End:\tRunOne Method %s, %s, %s",jar,class_name,test_name));
  103.         ClassLoader loader = URLClassLoader.newInstance(
  104.                 new URL[] { new File(pathJars+"/"+jar).toURI().toURL() },
  105.                 ServiceUtil.class.getClassLoader()
  106.         );
  107.  
  108.         loader.getClass();
  109.         Class cls=Class.forName(class_name,true,loader);
  110.         Constructor constructor = cls.getConstructor();
  111.         constructor.newInstance();
  112.  
  113.         LauncherDiscoveryRequest request;
  114.         if (test_name.isPresent()) {
  115.             Method m = cls.getMethod(test_name.get());
  116.             request = LauncherDiscoveryRequestBuilder.request()
  117.                     .selectors(selectMethod(cls,m))
  118.                     .build();
  119.         }
  120.         else{
  121.             request = LauncherDiscoveryRequestBuilder.request()
  122.                     .selectors(selectClass(cls))
  123.                     .build();
  124.         }
  125.  
  126.         TestPlan testPlan = launcher.discover(request);
  127.         launcher.registerTestExecutionListeners(listener);
  128.         launcher.execute(request);
  129.  
  130.     }
  131. }
Add Comment
Please, Sign In to add comment