Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.testng.internal.objects;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
Expand All @@ -23,15 +26,13 @@ public static <T> T newInstance(String className, Object... parameters) {

public static <T> T newInstance(Class<T> clazz) {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (IllegalAccessException
| InstantiationException
| ExceptionInInitializerError
| NoSuchMethodException
| SecurityException e) {
MethodHandle constructor =
MethodHandles.lookup().findConstructor(clazz, MethodType.methodType(void.class));
return (T) constructor.invoke();
} catch (IllegalAccessException | NoSuchMethodException | SecurityException e) {
throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e);
} catch (Throwable e) {
throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e);
} catch (InvocationTargetException e) {
throw new TestNGException(CANNOT_INSTANTIATE_CLASS + clazz.getName(), e.getCause());
}
}

Expand All @@ -45,6 +46,9 @@ public static <T> T newInstance(Constructor<T> constructor, Object... parameters
}

public static <T> T newInstance(Class<T> cls, Object... parameters) {
if (parameters.length == 0) {
return newInstance(cls);
}
Constructor<T> ctor = null;
for (Constructor<?> c : cls.getConstructors()) {
// Just comparing parameter array sizes. Comparing the parameter types
Expand Down
18 changes: 18 additions & 0 deletions testng-core/src/test/java/test/listeners/ListenersTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package test.listeners;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -23,6 +25,8 @@
import test.listeners.issue2381.FactoryTestClassSample;
import test.listeners.issue2381.SampleGlobalListener;
import test.listeners.issue2381.SampleTransformer;
import test.listeners.issue2578.ListenerWithMissingConstructorDependency;
import test.listeners.issue2578.MissingDependencyClassLoader;
import test.listeners.issue2638.DummyInvokedMethodListener;
import test.listeners.issue2638.TestClassASample;
import test.listeners.issue2638.TestClassBSample;
Expand Down Expand Up @@ -685,6 +689,20 @@ public void ensureInheritanceIsHandledWhenDealingWithListeners() {
assertThat(testng.getStatus()).isZero();
}

@Test(description = "GITHUB-2578")
public void ensureListenerWithUnavailableConstructorDependencyDoesNotFail()
throws ClassNotFoundException {
MissingDependencyClassLoader classLoader = new MissingDependencyClassLoader();
Class<?> listenerClass = classLoader.loadClass(ListenerWithMissingConstructorDependency.class);
Class<?> testClass = classLoader.loadClass(test.listeners.issue2578.TestClassSample.class);
TestNG testng = create(testClass);

assertThatThrownBy(listenerClass::getConstructors)
.isInstanceOf(NoClassDefFoundError.class)
.hasCauseInstanceOf(ClassNotFoundException.class);
assertThatCode(testng::run).doesNotThrowAnyException();
}

private void setupTest(boolean addExplicitListener) {
TestNG testng = new TestNG();
XmlSuite xmlSuite = createXmlSuite("Xml_Suite");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package test.listeners.issue2578;

import org.testng.ITestListener;

public class ListenerWithMissingConstructorDependency implements ITestListener {

public ListenerWithMissingConstructorDependency() {}

public ListenerWithMissingConstructorDependency(MissingConstructorDependency dependency) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test.listeners.issue2578;

public class MissingConstructorDependency {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package test.listeners.issue2578;

import java.io.IOException;
import java.io.InputStream;

public class MissingDependencyClassLoader extends ClassLoader {

private static final String SAMPLE_PACKAGE = "test.listeners.issue2578.";
private static final String MISSING_DEPENDENCY = SAMPLE_PACKAGE + "MissingConstructorDependency";

public MissingDependencyClassLoader() {
super(MissingDependencyClassLoader.class.getClassLoader());
}

public Class<?> loadClass(Class<?> clazz) throws ClassNotFoundException {
return loadClass(clazz.getName());
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (MISSING_DEPENDENCY.equals(name)) {
throw new ClassNotFoundException(name);
}
if (name.startsWith(SAMPLE_PACKAGE)) {
Class<?> loadedClass = findLoadedClass(name);
if (loadedClass == null) {
loadedClass = findClass(name);
}
if (resolve) {
resolveClass(loadedClass);
}
return loadedClass;
}
return super.loadClass(name, resolve);
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String resourceName = name.replace('.', '/') + ".class";
try (InputStream inputStream = getParent().getResourceAsStream(resourceName)) {
if (inputStream == null) {
throw new ClassNotFoundException(name);
}
byte[] bytes = inputStream.readAllBytes();
return defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
throw new ClassNotFoundException(name, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.listeners.issue2578;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(ListenerWithMissingConstructorDependency.class)
public class TestClassSample {

@Test
public void testMethod() {}
}
Loading