Skip to content

Commit

Permalink
Fix deepClone issue with Supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and johnaohara committed Jun 29, 2020
1 parent 4820549 commit 211300f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.Collections;
import java.util.function.Supplier;

import javax.inject.Inject;

Expand All @@ -18,15 +20,19 @@

@QuarkusTest
@ExtendWith(ParameterResolverTest.UnusedBeanDummyInputResolver.class)
@ExtendWith(ParameterResolverTest.SupplierParameterResolver.class)
public class ParameterResolverTest {

@Inject
UnusedBean unusedBean;

@Test
public void testParameterResolver(UnusedBean.DummyInput dummyInput) {
public void testParameterResolver(UnusedBean.DummyInput dummyInput, Supplier<UnusedBean.DummyInput> supplier) {
UnusedBean.DummyResult dummyResult = unusedBean.dummy(dummyInput);
assertEquals("whatever/6", dummyResult.getResult());

dummyResult = unusedBean.dummy(supplier.get());
assertEquals("fromSupplier/0", dummyResult.getResult());
}

public static class UnusedBeanDummyInputResolver implements ParameterResolver {
Expand All @@ -44,4 +50,20 @@ public Object resolveParameter(ParameterContext parameterContext,
}
}

public static class SupplierParameterResolver implements ParameterResolver {

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return Supplier.class.getName().equals(parameterContext.getParameter().getType().getName());
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return (Supplier<UnusedBean.DummyInput>) () -> new UnusedBean.DummyInput("fromSupplier",
new UnusedBean.NestedDummyInput(Collections.emptyList()));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.test.junit.internal;

import java.util.function.Supplier;

import com.thoughtworks.xstream.XStream;

/**
Expand All @@ -17,7 +19,33 @@ public XStreamDeepClone(ClassLoader classLoader) {
}

public Object clone(Object objectToClone) {
if (objectToClone == null) {
return null;
}

if (objectToClone instanceof Supplier) {
return handleSupplier((Supplier<?>) objectToClone);
}

return doClone(objectToClone);
}

private Supplier<Object> handleSupplier(final Supplier<?> supplier) {
return new Supplier<Object>() {
@Override
public Object get() {
return doClone(supplier.get());
}
};
}

private Object doClone(Object objectToClone) {
final String serialized = xStream.toXML(objectToClone);
return xStream.fromXML(serialized);
final Object result = xStream.fromXML(serialized);
if (result == null) {
throw new IllegalStateException("Unable to deep clone object of type '" + objectToClone.getClass().getName()
+ "'. Please report the issue on the Quarkus issue tracker.");
}
return result;
}
}

0 comments on commit 211300f

Please sign in to comment.