-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mnhock
authored and
mnhock
committed
Oct 20, 2024
1 parent
8af2ad7
commit e1871a5
Showing
6 changed files
with
382 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/test/java/com/enofex/taikai/internal/ArchConditionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.enofex.taikai.internal; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaField; | ||
import com.tngtech.archunit.core.domain.JavaMethod; | ||
import com.tngtech.archunit.core.domain.JavaModifier; | ||
import com.tngtech.archunit.core.domain.ThrowsClause; | ||
import com.tngtech.archunit.lang.ConditionEvents; | ||
import com.tngtech.archunit.lang.SimpleConditionEvent; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ArchConditionsTest { | ||
|
||
@Mock | ||
private JavaMethod mockMethod; | ||
@Mock | ||
private JavaField mockField; | ||
@Mock | ||
private JavaClass mockClass; | ||
@Mock | ||
private ConditionEvents events; | ||
@Mock | ||
private ThrowsClause mockThrowsClause; | ||
|
||
|
||
@Test | ||
void shouldNotDeclareThrownExceptions() { | ||
when(this.mockMethod.getThrowsClause()).thenReturn(this.mockThrowsClause); | ||
when(this.mockThrowsClause.isEmpty()).thenReturn(true); | ||
|
||
ArchConditions.notDeclareThrownExceptions().check(this.mockMethod, this.events); | ||
|
||
verify(this.events, never()).add(any(SimpleConditionEvent.class)); | ||
} | ||
|
||
@Test | ||
void shouldDeclareThrownExceptions() { | ||
when(this.mockMethod.getThrowsClause()).thenReturn(this.mockThrowsClause); | ||
when(this.mockThrowsClause.isEmpty()).thenReturn(false); | ||
|
||
ArchConditions.notDeclareThrownExceptions().check(this.mockMethod, this.events); | ||
|
||
verify(this.events).add(any(SimpleConditionEvent.class)); | ||
} | ||
|
||
@Test | ||
void shouldBePublicButNotStatic() { | ||
when(this.mockField.getName()).thenReturn("publicField"); | ||
when(this.mockField.getOwner()).thenReturn(this.mockClass); | ||
when(this.mockField.getModifiers()).thenReturn(EnumSet.of(JavaModifier.PUBLIC)); | ||
|
||
ArchConditions.notBePublicUnlessStatic().check(this.mockField, this.events); | ||
|
||
ArgumentCaptor<SimpleConditionEvent> eventCaptor = ArgumentCaptor.forClass( | ||
SimpleConditionEvent.class); | ||
verify(this.events).add(eventCaptor.capture()); | ||
assertEquals("Field %s in class %s is public".formatted( | ||
this.mockField.getName(), this.mockClass.getFullName()), | ||
eventCaptor.getValue().getDescriptionLines().getFirst()); | ||
} | ||
|
||
|
||
@Test | ||
void shouldHaveRequiredModifiers() { | ||
Set<JavaModifier> requiredModifiers = EnumSet.of(JavaModifier.PRIVATE, JavaModifier.FINAL); | ||
when(this.mockField.getModifiers()).thenReturn(requiredModifiers); | ||
|
||
ArchConditions.hasFieldModifiers(requiredModifiers).check(this.mockField, this.events); | ||
|
||
verify(this.events, never()).add(any(SimpleConditionEvent.class)); | ||
} | ||
|
||
@Test | ||
void shouldNotHaveRequiredModifiers() { | ||
Set<JavaModifier> requiredModifiers = EnumSet.of(JavaModifier.PRIVATE, JavaModifier.FINAL); | ||
when(this.mockField.getModifiers()).thenReturn(EnumSet.of(JavaModifier.PUBLIC)); | ||
when(this.mockField.getName()).thenReturn("field"); | ||
when(this.mockField.getOwner()).thenReturn(this.mockClass); | ||
|
||
ArchConditions.hasFieldModifiers(requiredModifiers).check(this.mockField, this.events); | ||
|
||
ArgumentCaptor<SimpleConditionEvent> eventCaptor = ArgumentCaptor.forClass( | ||
SimpleConditionEvent.class); | ||
verify(this.events).add(eventCaptor.capture()); | ||
assertEquals("Field %s in class %s is missing one of this %s modifier".formatted( | ||
this.mockField.getName(), | ||
this.mockClass.getFullName(), | ||
"PRIVATE, FINAL"), | ||
eventCaptor.getValue().getDescriptionLines().getFirst()); | ||
} | ||
|
||
@Test | ||
void shouldHaveFieldOfType() { | ||
String typeName = "com.example.MyType"; | ||
JavaClass mockRawType = mock(JavaClass.class); | ||
|
||
when(mockRawType.getName()).thenReturn(typeName); | ||
when(this.mockField.getRawType()).thenReturn(mockRawType); | ||
when(this.mockClass.getAllFields()).thenReturn(Collections.singleton(this.mockField)); | ||
|
||
ArchConditions.haveFieldOfType(typeName).check(this.mockClass, this.events); | ||
|
||
verify(this.events, never()).add(any(SimpleConditionEvent.class)); | ||
} | ||
|
||
@Test | ||
void shouldNotHaveFieldOfType() { | ||
String typeName = "com.example.MyType"; | ||
JavaClass mockRawType = mock(JavaClass.class); | ||
|
||
when(mockRawType.getName()).thenReturn("com.example.AnotherType"); | ||
when(this.mockField.getRawType()).thenReturn(mockRawType); | ||
when(this.mockClass.getAllFields()).thenReturn(Collections.singleton(this.mockField)); | ||
|
||
ArchConditions.haveFieldOfType(typeName).check(this.mockClass, this.events); | ||
|
||
verify(this.events).add(any(SimpleConditionEvent.class)); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/com/enofex/taikai/internal/DescribedPredicatesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.enofex.taikai.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DescribedPredicatesTest { | ||
|
||
@Mock | ||
private CanBeAnnotated canBeAnnotated; | ||
@Mock | ||
private JavaClass javaClass; | ||
|
||
@Test | ||
void shouldReturnTrueWhenAnnotatedWithSpecificAnnotation() { | ||
String annotation = "MyAnnotation"; | ||
when(this.canBeAnnotated.isAnnotatedWith(annotation)).thenReturn(true); | ||
|
||
assertTrue(DescribedPredicates.annotatedWith(annotation, false).test(this.canBeAnnotated)); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenNotAnnotatedWithSpecificAnnotation() { | ||
String annotation = "MyAnnotation"; | ||
when(this.canBeAnnotated.isAnnotatedWith(annotation)).thenReturn(false); | ||
|
||
assertFalse(DescribedPredicates.annotatedWith(annotation, false).test(this.canBeAnnotated)); | ||
} | ||
|
||
@Test | ||
void shouldReturnTrueWhenAnnotatedWithAllAnnotations() { | ||
Set<String> annotations = Set.of("MyAnnotation1", "MyAnnotation2"); | ||
|
||
when(this.canBeAnnotated.isAnnotatedWith("MyAnnotation1")).thenReturn(true); | ||
when(this.canBeAnnotated.isAnnotatedWith("MyAnnotation2")).thenReturn(true); | ||
|
||
assertTrue(DescribedPredicates.annotatedWithAll(annotations, false).test(this.canBeAnnotated)); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenNotAnnotatedWithAllAnnotations() { | ||
Set<String> annotations = Set.of("MyAnnotation1", "MyAnnotation2"); | ||
|
||
when(this.canBeAnnotated.isAnnotatedWith("MyAnnotation1")).thenReturn(false); | ||
when(this.canBeAnnotated.isAnnotatedWith("MyAnnotation2")).thenReturn(true); | ||
|
||
assertFalse(DescribedPredicates.annotatedWithAll(annotations, false).test(this.canBeAnnotated)); | ||
} | ||
|
||
@Test | ||
void shouldReturnTrueWhenClassIsFinal() { | ||
when(this.javaClass.getModifiers()).thenReturn( | ||
Set.of(com.tngtech.archunit.core.domain.JavaModifier.FINAL)); | ||
|
||
assertTrue(DescribedPredicates.areFinal().test(this.javaClass)); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenClassIsNotFinal() { | ||
when(this.javaClass.getModifiers()).thenReturn(Collections.emptySet()); | ||
|
||
assertFalse(DescribedPredicates.areFinal().test(this.javaClass)); | ||
} | ||
} |
Oops, something went wrong.