/** * Initializes the specified context in the provided classloader, given the classloader is a different one than * of the specified context * @param ctx * the context that should be transferred to the other classloader * @param cl * the classloader whose context should be initialized */ public static void init(final TestExecutionContext ctx, final ClassLoader cl) { //avoid overwriting the context in the same classloader if (!Objects.equals(ctx.getClass().getClassLoader(), cl)) { //NOSONAR runUnchecked(() -> cl.loadClass(ctx.getClass().getName()) .getMethod("init", Properties.class, Properties.class) .invoke(null, ctx.getInput(), ctx.getEnv())); } }
private static Result invokeRun(final String className, final ClassLoader cl) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IOException, InvocationTargetException { TestExecutionContext.current().ifPresent(ctx -> { System.getProperties().putAll(ctx.getEnv()); init(ctx, cl); }); try { final Class<?> testClass = cl.loadClass(className); final Object runner = cl.loadClass(JUnitRunner.class.getName()).newInstance(); final Method run = runner.getClass().getDeclaredMethod("run", Class.class); return new ResultHelper().deserialize((byte[]) run.invoke(runner, testClass)); } finally { TestExecutionContext.current().ifPresent(ctx -> destroy(ctx, cl)); } }