Skip to content

Commit

Permalink
Added recursion at ArcProxyBeanMetaDataClassNormalizer.normalize to fix
Browse files Browse the repository at this point in the history
quarkusio#18388

(cherry picked from commit 0c66ba5)
  • Loading branch information
tioback authored and gsmet committed Jul 12, 2021
1 parent 5f8af25 commit 728197e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@

/**
* In the case of a proxy generated by Arc, return the parent class.
* <br />
* This can have more than one level of hierarchy, in example:
* <ul>
* <li>When using @{@link io.quarkus.test.junit.mockito.InjectMock} or @{@link io.quarkus.test.junit.mockito.InjectSpy}</li>
* </ul>
*/
public class ArcProxyBeanMetaDataClassNormalizer implements BeanMetaDataClassNormalizer {

@Override
public <T> Class<? super T> normalize(Class<T> beanClass) {
if (Subclass.class.isAssignableFrom(beanClass)) {
return beanClass.getSuperclass();
Class<? super T> targetClass = beanClass;
while (Subclass.class.isAssignableFrom(targetClass)) {
targetClass = targetClass.getSuperclass();
}

return beanClass;
return targetClass;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.hibernate.validator.runtime;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.quarkus.arc.Subclass;

/**
* Tests class normalization, which translates into getting the "original" class, without all the specializations instances of
* {@link io.quarkus.arc.Subclass}. <br />
* There's no need to be a @{@link io.quarkus.test.junit.QuarkusTest} because no Quarkus related feature is required.
*/
class ArcProxyBeanMetaDataClassNormalizerTest {

@Test
@DisplayName("Normalize should return same class if beanClass isn't a Subclass.")
void normalize_simpleClass() {
Class<Original> expected = Original.class;

assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(Original.class));
}

@Test
@DisplayName("Normalize should return 'superclass' if beanClass is the only Subclass in it's class hierarchy.")
void normalize_oneSubClass() {
Class<Original> expected = Original.class;

assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(FirstSubclass.class));
}

@Test
@DisplayName("Normalize should return upmost superclass if beanClass has more than one Subclass in it's class hierarchy.")
void normalize_multipleSubClasses() {
Class<Original> expected = Original.class;

assertEquals(expected, new ArcProxyBeanMetaDataClassNormalizer().normalize(SecondSubclass.class));
}

private static class Original {
}

/**
* Simulates an object injected through @{@link javax.inject.Inject}.
*/
private static class FirstSubclass extends Original implements Subclass {
}

/**
* Simulates an object injected through @{@link io.quarkus.test.junit.mockito.InjectMock}
* or @{@link io.quarkus.test.junit.mockito.InjectSpy}.
*/
private static class SecondSubclass extends FirstSubclass {
}
}

0 comments on commit 728197e

Please sign in to comment.