Skip to content

Commit

Permalink
Add test for class with no canonical name
Browse files Browse the repository at this point in the history
  • Loading branch information
pzygielo committed Nov 6, 2022
1 parent 7817755 commit f066947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void initializeMethods() {
*
* @return true if the class should not be processed
*/
private boolean isExcludedFromAnnotationProcessing(Class<?> clazz) {
static boolean isExcludedFromAnnotationProcessing(Class<?> clazz) {
if (clazz.getPackage() == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -82,10 +83,10 @@ public void testServlet() {
public void testComponentDefinitionTest() {
ComponentDefinition component = new ComponentDefinition(ComponentDefinitionTest.class);
assertAll(
() -> assertThat(component.getFields(), emptyArray()),
() -> assertThat(component.getFields(), arrayWithSize(1)),
() -> assertThat(component.getConstructors(), arrayWithSize(1)),
() -> assertThat(component.getConstructors()[0].getName(), equalTo(ComponentDefinitionTest.class.getName())),
() -> assertThat(getMethodNames(component), arrayWithSize(equalTo(36)))
() -> assertThat(getMethodNames(component), arrayWithSize(equalTo(37)))
);
}

Expand Down Expand Up @@ -142,6 +143,19 @@ public void testDifferentPackages() {
}


@Test
public void testExclusionOfClassWithNoCanonicalName() {
// getCanonicalName():
// ...
// Returns null if the underlying class does not have a canonical name
// (i.e., if it is a local or anonymous class ...).
class Local {};
assert Local.class.getCanonicalName() == null; // This is test-testing assertion

assertThat(ComponentDefinition.isExcludedFromAnnotationProcessing(Local.class), is(false));
}


private String[] getMethodNames(ComponentDefinition component) {
return Stream.of(component.getMethods()).map(Method::getName).filter(m -> !"$jacocoInit".equals(m))
.toArray(String[]::new);
Expand Down

0 comments on commit f066947

Please sign in to comment.