Skip to content

Commit

Permalink
Unarchive
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Feb 19, 2024
1 parent bc83448 commit ee13d26
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ A home for additional useful unit testing modules for Java.
[![LGPLv3 License](http://img.shields.io/badge/license-LGPLv3-blue.svg)](https://www.gnu.org/licenses/lgpl.html)
[![Java Development Kit 17](https://img.shields.io/badge/JDK-17-green.svg)](https://openjdk.java.net/projects/jdk/17/)

> [!NOTE]
> The project is no longer maintained as most of its functionality can be found nowadays in other libraries.
> For most parts use [ArchUnit](https://www.archunit.org/). Some methods where moved to [Utils4J](https://github.com/fuinorg/utils4j)
> because they are not strictly bound to test functions.
## Versions
- 0.12.0-SNAPSHOT = LAST VERSION (Will never be released :worried:)
- 0.12.0-SNAPSHOT = Made several functions deprecated and added new [ArchUnit](https://www.archunit.org/) helper.
- 0.11.0 (or later) = **Java 17** with **junit5** / **WeldJUnit4Runner removed** in favor of [weld-junit5](https://github.com/weld/weld-testing)
- 0.10.0 = **Java 11** with new **jakarta** namespace
- 0.9.x = **Java 11** before namespace change from 'javax' to 'jakarta'
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>1.2.1</version>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.2.1</version>
</dependency>

<!-- Test -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.fuin.units4j.archunit;

import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;

import java.util.Collection;
import java.util.Set;

import static com.tngtech.archunit.lang.ConditionEvent.createMessage;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toSet;

/**
* Defines a condition that all top level classes should have a test class.
*/
public final class AllTopLevelClassesHaveATestCondition extends ArchCondition<JavaClass> {

private final String suffix;

private Set<String> testedClassNames;

/**
* Default constructor that assumes the test classes names always end on "Test".
*/
public AllTopLevelClassesHaveATestCondition() {
this("Test");
}

/**
* Defines the suffix for test classes.
*
* @param suffix Suffix for test classes like "Test".
*/
public AllTopLevelClassesHaveATestCondition(final String suffix) {
super("have a corresponding test class with suffix '" + suffix + "'");
this.suffix = suffix;
this.testedClassNames = emptySet();
}

@Override
public void init(Collection<JavaClass> allClasses) {
testedClassNames = allClasses.stream()
.map(JavaClass::getName)
.filter(className -> className.endsWith(suffix))
.map(className -> className.substring(0, className.length() - suffix.length()))
.collect(toSet());
}

@Override
public void check(JavaClass clazz, ConditionEvents events) {
if (!clazz.getName().endsWith(suffix)) {
boolean satisfied = testedClassNames.contains(clazz.getName());
String message = createMessage(clazz, "has " + (satisfied ? "a" : "no") + " corresponding test class");
events.add(new SimpleConditionEvent(clazz, satisfied, message));
}
}

/**
* Defines a condition that all classes should have a corresponding "*Test" class.
*
* @return Condition.
*/
public static ArchCondition<JavaClass> haveACorrespondingClassEndingWithTest() {
return new AllTopLevelClassesHaveATestCondition();
}

/**
* Defines a condition that all classes should have a corresponding "*SUFFIX" class.
*
* @return Condition.
*/
public static ArchCondition<JavaClass> haveACorrespondingClassEndingWith(String suffix) {
return new AllTopLevelClassesHaveATestCondition(suffix);
}

}
8 changes: 6 additions & 2 deletions src/test/java/org/fuin/units4j/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.fuin.units4j.analyzer.EmptyClassVisitor;
import org.fuin.units4j.analyzer.EmptyFieldVisitor;
import org.fuin.units4j.analyzer.EmptyMethodVisitor;
import org.fuin.units4j.archunit.AllTopLevelClassesHaveATestCondition;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand All @@ -34,8 +35,11 @@ public final void testCoverage() {
AssertCoverage.assertEveryClassHasATest(new File("src/main/java"), new ClassFilter() {
@Override
public boolean isIncludeClass(final Class<?> clasz) {
if ((clasz == EmptyClassVisitor.class) || (clasz == EmptyAnnotationVisitor.class) || (clasz == EmptyFieldVisitor.class)
|| (clasz == EmptyMethodVisitor.class)) {
if (clasz == EmptyClassVisitor.class
|| clasz == EmptyAnnotationVisitor.class
|| clasz == EmptyFieldVisitor.class
|| clasz == EmptyMethodVisitor.class
|| clasz == AllTopLevelClassesHaveATestCondition.class) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import jakarta.validation.constraints.NotNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.fuin.units4j.Units4JUtils;
import org.fuin.utils4j.JandexUtils;
import org.fuin.utils4j.jandex.JandexUtils;
import org.fuin.utils4j.Utils4J;
import org.jboss.jandex.*;
import org.jboss.jandex.Type.Kind;
Expand Down

0 comments on commit ee13d26

Please sign in to comment.