Skip to content

Commit

Permalink
Issue #424 passivation/activation and Annotated equality (#430)
Browse files Browse the repository at this point in the history
* Issue #424 - Annotated does not necessarily implement equals/hashCode
* Extract the assert to compare Annotated instance so it can be reused.
  • Loading branch information
jeanouii committed Feb 21, 2023
1 parent 1eda01e commit 65ae5af
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

import java.io.IOException;

import jakarta.enterprise.inject.spi.Annotated;
import jakarta.enterprise.inject.spi.AnnotatedMember;
import jakarta.enterprise.inject.spi.AnnotatedParameter;
import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.cdi.tck.util.Assert;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecVersion;
Expand Down Expand Up @@ -97,7 +102,12 @@ public void testInjectionPoint() throws IOException, ClassNotFoundException {
assertEquals(inspectorCopy.getInjectionPoint().getQualifiers(), inspector.getInjectionPoint().getQualifiers());
assertEquals(inspectorCopy.getInjectionPoint().getBean(), inspector.getInjectionPoint().getBean());
assertEquals(inspectorCopy.getInjectionPoint().getMember(), inspector.getInjectionPoint().getMember());
assertEquals(inspectorCopy.getInjectionPoint().getAnnotated(), inspector.getInjectionPoint().getAnnotated());

// Annotated does not necessarily implement equals()/hashcode() so we need to test the underlying Java reflection object
Assert.assertAnnotated(inspectorCopy.getInjectionPoint().getAnnotated(), inspector.getInjectionPoint().getAnnotated());
assertEquals(inspectorCopy.getInjectionPoint().getAnnotated().getBaseType(), inspector.getInjectionPoint().getAnnotated().getBaseType());
assertEquals(inspectorCopy.getInjectionPoint().getAnnotated().getAnnotations(), inspector.getInjectionPoint().getAnnotated().getAnnotations());

assertEquals(inspectorCopy.getInjectionPoint().isDelegate(), inspector.getInjectionPoint().isDelegate());
assertEquals(inspectorCopy.getInjectionPoint().isTransient(), inspector.getInjectionPoint().isTransient());
}
Expand Down
29 changes: 29 additions & 0 deletions impl/src/main/java/org/jboss/cdi/tck/util/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package org.jboss.cdi.tck.util;

import jakarta.enterprise.inject.spi.Annotated;
import jakarta.enterprise.inject.spi.AnnotatedMember;
import jakarta.enterprise.inject.spi.AnnotatedParameter;
import jakarta.enterprise.inject.spi.AnnotatedType;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -101,4 +107,27 @@ public static void assertTypeListMatches(List<? extends Type> types, Type... req
}
}

/**
* Helper method to compare 2 Annotated. They don't necessarily implement equals()/hashcode() so we need to
* compare the underlying java.lang.reflect objects.
*
* @param expected The expected Annotated instance
* @param actual The actual Annotated instance to compare
*/
public static void assertAnnotated(final Annotated expected, final Annotated actual) {
assertEquals(unwrap(expected), unwrap(actual));
}

private static Object unwrap(final Annotated annotated) {
if (annotated instanceof AnnotatedMember) {
return ((AnnotatedMember) annotated).getJavaMember();
} else if (annotated instanceof AnnotatedParameter) {
return ((AnnotatedParameter) annotated).getJavaParameter();
} else if (annotated instanceof AnnotatedType) {
return ((AnnotatedType) annotated).getJavaClass();
} else {
throw new UnsupportedOperationException("Unknown Annotated instance: " + annotated);
}
}

}

0 comments on commit 65ae5af

Please sign in to comment.