Skip to content

Commit

Permalink
feature: detect ArchUnit-JUnit5 tests (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtoison authored Oct 30, 2022
1 parent 80f620f commit 335fa11
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 26 deletions.
7 changes: 7 additions & 0 deletions infinitest-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5-api</artifactId>
<version>${archunit-junit5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,39 @@
package org.infinitest.parser;

import static java.util.Arrays.stream;
import static javassist.Modifier.*;
import static javassist.bytecode.AnnotationsAttribute.*;
import static org.infinitest.parser.DescriptorParser.*;

import java.io.*;
import java.util.*;
import static javassist.Modifier.isPublic;
import static javassist.bytecode.AnnotationsAttribute.invisibleTag;
import static javassist.bytecode.AnnotationsAttribute.visibleTag;
import static org.infinitest.parser.DescriptorParser.parseClassNameFromConstantPoolDescriptor;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import javassist.*;
import javassist.bytecode.*;
import javassist.bytecode.annotation.*;
import junit.framework.*;

import org.infinitest.util.InfinitestUtils;
import org.junit.platform.commons.annotation.Testable;
import org.junit.runner.*;

import com.google.common.base.*;
import org.junit.runner.RunWith;

import com.google.common.base.Predicate;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.AttributeInfo;
import javassist.bytecode.ConstPool;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.ParameterAnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;
import junit.framework.TestCase;

/**
* Be careful: instances of this class are kept in a cache
Expand Down Expand Up @@ -249,6 +264,7 @@ public String toString() {
private boolean hasTests(CtClass classReference) {
return hasJUnitTestMethods(classReference) //
|| usesCustomRunner(classReference) //
|| hasArchUnitTests(classReference)
|| hasTestNGTests(classReference);
}

Expand Down Expand Up @@ -281,6 +297,16 @@ public boolean apply(CtClass input) {
};
}

private boolean hasArchUnitTests(CtClass classReference) {
for (CtField field : classReference.getDeclaredFields()) {
if (field.hasAnnotation("com.tngtech.archunit.junit.ArchTest")) {
return true;
}
}

return false;
}

private boolean hasTestNGTests(CtClass classReference) {
if (isTestNGTestClass(classReference)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Infinitest, a Continuous Test Runner.
*
* Copyright (C) 2010-2013
* "Ben Rady" <benrady@gmail.com>,
* "Rod Coffin" <rfciii@gmail.com>,
* "Ryan Breidenbach" <ryan.breidenbach@gmail.com>
* "David Gageot" <david@gageot.net>, et al.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.fakeco.fakeproduct;

import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;

import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;

public class JUnit5ArchUnitTest {

@ArchTest
private final ArchRule no_access_to_standard_streams = NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.junit.Ignore;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.fakeco.fakeproduct.ANewClass;
import com.fakeco.fakeproduct.AnnotatedClass;
Expand All @@ -50,8 +52,11 @@
import com.fakeco.fakeproduct.FieldAnnotation;
import com.fakeco.fakeproduct.InvisibleClassAnnotation;
import com.fakeco.fakeproduct.InvisibleParameterAnnotation;
import com.fakeco.fakeproduct.JUnit5ArchUnitTest;
import com.fakeco.fakeproduct.JUnit5CompositeAnnotationTest;
import com.fakeco.fakeproduct.JUnit5ParameterizedTest;
import com.fakeco.fakeproduct.JUnit5Testable;
import com.fakeco.fakeproduct.JUnit5TestableSubclass;
import com.fakeco.fakeproduct.MethodAnnotation;
import com.fakeco.fakeproduct.ParameterAnnotation;
import com.fakeco.fakeproduct.TestJUnit5TestCase;
Expand Down Expand Up @@ -147,19 +152,17 @@ private String[] dependenciesOf(Class<?> dependingClass) {
return classPoolUtil.dependenciesOf(dependingClass);
}

@Test
void shouldSupportParameterizedTest() throws NotFoundException {
ClassPool classPool = classPoolUtil.getClassPool();
CtClass ctClass = classPool.get(JUnit5ParameterizedTest.class.getName());
JavaAssistClass javaClass = new JavaAssistClass(ctClass);

assertTrue(javaClass.isATest());
}

@Test
void shouldSupportCompositeTestAnnotation() throws NotFoundException {
@ParameterizedTest
@ValueSource(classes = {
JUnit5ParameterizedTest.class,
JUnit5CompositeAnnotationTest.class,
JUnit5Testable.class,
JUnit5TestableSubclass.class,
JUnit5ArchUnitTest.class,
})
void shouldSupportTestClasses(Class<?> testClass) throws NotFoundException {
ClassPool classPool = classPoolUtil.getClassPool();
CtClass ctClass = classPool.get(JUnit5CompositeAnnotationTest.class.getName());
CtClass ctClass = classPool.get(testClass.getName());
JavaAssistClass javaClass = new JavaAssistClass(ctClass);

assertTrue(javaClass.isATest());
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<junit.version>4.12</junit.version>
<junit5.version>5.9.1</junit5.version>
<junit5-platform.version>1.9.1</junit5-platform.version>
<archunit-junit5.version>1.0.0</archunit-junit5.version>
<guava.version>18.0</guava.version>
<hamcrest.version>1.3</hamcrest.version>
<testng.version>6.8.8</testng.version>
Expand Down

0 comments on commit 335fa11

Please sign in to comment.