Bug 534803 - Migration org.eclipse.e4.ui.tests.css.core to JUnit5 

Change-Id: If140a058065be4d4edd7eedfff9fee9d5e0829c0
Signed-off-by: Vikas Chandra <Vikas.Chandra@in.ibm.com>
diff --git a/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/MultiBundleClassLoader2.java b/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/MultiBundleClassLoader2.java
new file mode 100644
index 0000000..3454625
--- /dev/null
+++ b/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/MultiBundleClassLoader2.java
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ *  Copyright (c) 2018 IBM Corporation and others.
+ *  All rights reserved. This program and the accompanying materials
+ *  are made available under the terms of the Eclipse Public License v1.0
+ *  which accompanies this distribution, and is available at
+ *  http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.pde.internal.junit.runtime;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.*;
+import org.osgi.framework.Bundle;
+
+class MultiBundleClassLoader2 extends ClassLoader {
+	private List<Bundle> bundleList;
+
+	public MultiBundleClassLoader2(List<Bundle> platformEngineBundles) {
+		this.bundleList = platformEngineBundles;
+
+	}
+
+	@Override
+	protected Class<?> findClass(String name) throws ClassNotFoundException {
+		Class<?> c = null;
+		for (Bundle temp : bundleList) {
+			try {
+				c = temp.loadClass(name);
+				if (c != null)
+					return c;
+			} catch (ClassNotFoundException e) {
+			}
+		}
+		return c;
+	}
+
+	@Override
+	protected URL findResource(String name) {
+		URL url = null;
+		for (Bundle temp : bundleList) {
+			url = temp.getResource(name);
+			if (url != null)
+				return url;
+		}
+		return url;
+	}
+
+	@Override
+	protected Enumeration<URL> findResources(String name) throws IOException {
+		Enumeration<URL> enumFinal = null;
+		for (int i = 0; i < bundleList.size(); i++) {
+			if (i == 0) {
+				enumFinal = bundleList.get(i).getResources(name);
+				continue;
+			}
+			Enumeration<URL> e2 = bundleList.get(i).getResources(name);
+			Vector<URL> temp = new Vector<>();
+			while (enumFinal != null && enumFinal.hasMoreElements()) {
+				temp.add(enumFinal.nextElement());
+			}
+			while (e2 != null && e2.hasMoreElements()) {
+				temp.add(e2.nextElement());
+			}
+			enumFinal = temp.elements();
+		}
+		return enumFinal;
+	}
+}
\ No newline at end of file
diff --git a/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/RemotePluginTestRunner.java b/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/RemotePluginTestRunner.java
index dbc1748..dcfbd9b 100644
--- a/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/RemotePluginTestRunner.java
+++ b/ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/RemotePluginTestRunner.java
@@ -221,7 +221,10 @@
 		if (isJUnit5(args)) {
 			//change the classloader so that the test classes in testplugin are discoverable
 			//by junit5 framework  see bug 520811
-			Thread.currentThread().setContextClassLoader(getPluginClassLoader(testRunner.getfTestPluginName()));
+			if (runAsJUnit5(args))
+				Thread.currentThread().setContextClassLoader(getPluginClassLoader2(testRunner.getfTestPluginName()));
+			else
+				Thread.currentThread().setContextClassLoader(getPluginClassLoader(testRunner.getfTestPluginName()));
 		}
 		testRunner.run();
 		if (isJUnit5(args)) {
@@ -229,6 +232,36 @@
 		}
 	}
 
+	private static ClassLoader getPluginClassLoader2(String getfTestPluginName) {
+		Bundle bundle = Platform.getBundle(getfTestPluginName);
+		if (bundle == null) {
+			throw new IllegalArgumentException("Bundle \"" + getfTestPluginName + "\" not found. Possible causes include missing dependencies, too restrictive version ranges, or a non-matching required execution environment."); //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		final String pluginId = getfTestPluginName;
+		List<String> engines = new ArrayList<String>();
+		Bundle bund = FrameworkUtil.getBundle(RemotePluginTestRunner.class);
+		Bundle[] bundles = bund.getBundleContext().getBundles();
+		for (Bundle iBundle : bundles) {
+			try {
+				BundleWiring bundleWiring = Platform.getBundle(iBundle.getSymbolicName()).adapt(BundleWiring.class);
+				Collection<String> listResources = bundleWiring.listResources("META-INF/services", "org.junit.platform.engine.TestEngine", BundleWiring.LISTRESOURCES_LOCAL); //$NON-NLS-1$//$NON-NLS-2$
+				if (!listResources.isEmpty())
+					engines.add(iBundle.getSymbolicName());
+			} catch (Exception e) {
+				// check the next bundle
+			}
+		}
+		engines.add(pluginId);
+		List<Bundle> platformEngineBundles = new ArrayList<Bundle>();
+		for (Iterator<String> iterator = engines.iterator(); iterator.hasNext();) {
+			String string = iterator.next();
+			Bundle bundle2 = Platform.getBundle(string);
+			platformEngineBundles.add(bundle2);
+		}
+
+		return new MultiBundleClassLoader2(platformEngineBundles);
+	}
+
 	private static ClassLoader getPluginClassLoader(String getfTestPluginName) {
 		Bundle bundle = Platform.getBundle(getfTestPluginName);
 		if (bundle == null) {
@@ -299,7 +332,19 @@
 		return new MultiBundleClassLoader(platformEngineBundles);
 	}
 
+	private static boolean runAsJUnit5(String[] args) {
+		for (String string : args) {
+			if (string.equalsIgnoreCase("-runasjunit5")) { //$NON-NLS-1$
+				return true;
+			}
+		}
+		return false;
+	}
+	
 	private static boolean isJUnit5(String[] args) {
+		if (runAsJUnit5(args) == true)
+			return true;
+		
 		for (int i = 0; i < args.length; i++) {
 			if (args[i].equals("org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader")) //$NON-NLS-1$
 				return true;