-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first tests for the dynamics framework (fix #74)
- Loading branch information
1 parent
dc80bb0
commit 64530fc
Showing
2 changed files
with
380 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package de.tum.in.test.api; | ||
|
||
import static de.tum.in.test.testutilities.CustomConditions.*; | ||
import static org.junit.platform.testkit.engine.EventConditions.*; | ||
|
||
import org.junit.platform.testkit.engine.Events; | ||
import org.opentest4j.AssertionFailedError; | ||
|
||
import de.tum.in.test.testutilities.TestTest; | ||
import de.tum.in.test.testutilities.UserBased; | ||
import de.tum.in.test.testutilities.UserTestResults; | ||
import de.tum.in.testuser.DynamicsUser; | ||
|
||
@UserBased(DynamicsUser.class) | ||
class DynamicsTest { | ||
|
||
@UserTestResults | ||
private static Events tests; | ||
|
||
private final String class_check = "class_check"; | ||
private final String class_isClass = "class_isClass"; | ||
private final String class_notFound = "class_notFound"; | ||
private final String class_searchNonFinalFields = "class_searchNonFinalFields"; | ||
private final String class_searchNonPrivateFields = "class_searchNonPrivateFields"; | ||
private final String class_searchPublicOrProtectedMethods = "class_searchPublicOrProtectedMethods"; | ||
private final String constructor_abstract = "constructor_abstract"; | ||
private final String constructor_check = "constructor_check"; | ||
private final String constructor_illegalArguments = "constructor_illegalArguments"; | ||
private final String constructor_newInstance = "constructor_newInstance"; | ||
private final String constructor_notFound = "constructor_notFound"; | ||
private final String constructor_throwing = "constructor_throwing"; | ||
private final String field_check = "field_check"; | ||
private final String field_getInstance = "field_getInstance"; | ||
private final String field_getStatic = "field_getStatic"; | ||
private final String field_noSuchField = "field_noSuchField"; | ||
private final String field_wrongType = "field_wrongType"; | ||
private final String method_badCast = "method_badCast"; | ||
private final String method_check = "method_check"; | ||
private final String method_illegalArguments = "method_illegalArguments"; | ||
private final String method_invoke = "method_invoke"; | ||
private final String method_notFound = "method_notFound"; | ||
private final String method_notStatic = "method_notStatic"; | ||
private final String method_null = "method_null"; | ||
private final String method_throwing = "method_throwing"; | ||
|
||
@TestTest | ||
void test_class_check() { | ||
tests.assertThatEvents().haveExactly(1, event(test(class_check), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_class_isClass() { | ||
tests.assertThatEvents().haveExactly(1, event(test(class_isClass), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_class_notFound() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(class_notFound, AssertionFailedError.class, | ||
"Klasse/Interface DoesNotExist nicht gefunden")); | ||
} | ||
|
||
@TestTest | ||
void test_class_searchNonFinalFields() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(class_searchNonFinalFields, AssertionFailedError.class, | ||
"Attribut private java.lang.String de.tum.in.testuser.subject.structural.SomeClass.someAttribute muss final sein.")); | ||
} | ||
|
||
@TestTest | ||
void test_class_searchNonPrivateFields() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(class_searchNonPrivateFields, AssertionFailedError.class, | ||
"Attribut public static final int de.tum.in.testuser.subject.structural.SomeClass.SOME_CONSTANT muss private sein.")); | ||
} | ||
|
||
@TestTest | ||
void test_class_searchPublicOrProtectedMethods() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(class_searchPublicOrProtectedMethods, | ||
AssertionFailedError.class, "Methode throwException() ist public, sollte sie aber nicht")); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_abstract() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(constructor_abstract, AssertionFailedError.class, | ||
"Objekt der Klasse de.tum.in.testuser.subject.structural.SomeAbstractClass konnte nicht erzeugt werden, ist die Klasse abstract?")); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_check() { | ||
tests.assertThatEvents().haveExactly(1, event(test(constructor_check), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_illegalArguments() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(constructor_illegalArguments, AssertionFailedError.class, | ||
"Konstruktor de.tum.in.testuser.subject.structural.SomeClass(java.lang.Integer) konnte Parametertypen (java.lang.String) nicht entgegennehmen")); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_newInstance() { | ||
tests.assertThatEvents().haveExactly(1, event(test(constructor_newInstance), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_notFound() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(constructor_notFound, AssertionFailedError.class, | ||
"Kein Konstruktor für de.tum.in.testuser.subject.structural.SomeClass mit Parametern (java.lang.Boolean, java.lang.Boolean) gefunden.")); | ||
} | ||
|
||
@TestTest | ||
void test_constructor_throwing() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(constructor_throwing, RuntimeException.class, | ||
"\n/// Mögliche Problemstelle: de.tum.in.testuser.subject.structural.SomeClass.<init>(SomeClass.java:20) ///")); | ||
} | ||
|
||
@TestTest | ||
void test_field_check() { | ||
tests.assertThatEvents().haveExactly(1, event(test(field_check), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_field_getInstance() { | ||
tests.assertThatEvents().haveExactly(1, event(test(field_getInstance), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_field_getStatic() { | ||
tests.assertThatEvents().haveExactly(1, event(test(field_getStatic), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_field_noSuchField() { | ||
tests.assertThatEvents().haveExactly(1, | ||
testFailedWith(field_noSuchField, AssertionFailedError.class, "Feld [a] konnte nicht gefunden werden")); | ||
} | ||
|
||
@TestTest | ||
void test_field_wrongType() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(field_wrongType, AssertionFailedError.class, | ||
"Feld [some_constant] konnte nicht gefunden werden")); | ||
} | ||
|
||
@TestTest | ||
void test_method_badCast() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_badCast, AssertionFailedError.class, | ||
"Methode de.tum.in.testuser.subject.structural.SomeClass.doSomethingElse(int) gibt nicht double zurück")); | ||
} | ||
|
||
@TestTest | ||
void test_method_check() { | ||
tests.assertThatEvents().haveExactly(1, event(test(method_check), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_method_illegalArguments() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_illegalArguments, AssertionFailedError.class, | ||
"Methode de.tum.in.testuser.subject.structural.SomeClass.doSomethingElse(int) konnte Parametertypen (java.lang.String) für Objekt der Klasse de.tum.in.testuser.subject.structural.SomeClass nicht entgegennehmen")); | ||
} | ||
|
||
@TestTest | ||
void test_method_invoke() { | ||
tests.assertThatEvents().haveExactly(1, event(test(method_invoke), finishedSuccessfullyRep())); | ||
} | ||
|
||
@TestTest | ||
void test_method_notFound() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_notFound, AssertionFailedError.class, | ||
"Keine Methode void de.tum.in.testuser.subject.structural.SomeClass.abc() gefunden.")); | ||
} | ||
|
||
@TestTest | ||
void test_method_notStatic() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_notStatic, AssertionFailedError.class, | ||
"Methode de.tum.in.testuser.subject.structural.SomeClass.throwException() ist nicht statisch")); | ||
} | ||
|
||
@TestTest | ||
void test_method_null() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_null, AssertionFailedError.class, | ||
"Methode de.tum.in.testuser.subject.structural.SomeClass.throwException() konnte nicht aufgerufen werden, das Objekt ist null")); | ||
} | ||
|
||
@TestTest | ||
void test_method_throwing() { | ||
tests.assertThatEvents().haveExactly(1, testFailedWith(method_throwing, RuntimeException.class, | ||
"\n/// Mögliche Problemstelle: de.tum.in.testuser.subject.structural.SomeClass.throwException(SomeClass.java:56) ///")); | ||
} | ||
} |
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,194 @@ | ||
package de.tum.in.testuser; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.MethodOrderer.MethodName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
|
||
import de.tum.in.test.api.StrictTimeout; | ||
import de.tum.in.test.api.WhitelistPath; | ||
import de.tum.in.test.api.dynamic.Check; | ||
import de.tum.in.test.api.dynamic.DynamicClass; | ||
import de.tum.in.test.api.dynamic.DynamicConstructor; | ||
import de.tum.in.test.api.dynamic.DynamicField; | ||
import de.tum.in.test.api.dynamic.DynamicMethod; | ||
import de.tum.in.test.api.jupiter.Public; | ||
import de.tum.in.test.api.localization.UseLocale; | ||
import de.tum.in.testuser.subject.structural.SomeClass; | ||
|
||
@Public | ||
@UseLocale("de") | ||
@StrictTimeout(10) | ||
@WhitelistPath("") | ||
@TestMethodOrder(MethodName.class) | ||
public class DynamicsUser { | ||
|
||
private static final String SUBJECT_PACKAGE = "de.tum.in.testuser.subject.structural"; | ||
|
||
private static final DynamicClass<?> SomeClass = DynamicClass.toDynamic(SUBJECT_PACKAGE + ".SomeClass"); | ||
private static final DynamicClass<?> SomeAbstractClass = DynamicClass | ||
.toDynamic(SUBJECT_PACKAGE + ".SomeAbstractClass"); | ||
|
||
private static final DynamicConstructor<?> SomeClass_new = SomeClass.constructor(); | ||
private static final DynamicConstructor<?> SomeClass_newBool = SomeClass.constructor(Boolean.class); | ||
private static final DynamicConstructor<?> SomeClass_newInt = SomeClass.constructor(Integer.class); | ||
private static final DynamicConstructor<?> SomeClass_newString = SomeClass.constructor(String.class); | ||
|
||
private static final DynamicField<Integer> SomeClass_SOME_CONSTANT = SomeClass.field(int.class, "SOME_CONSTANT"); | ||
private static final DynamicField<String> SomeClass_someAttribute = SomeClass.field(String.class, "someAttribute"); | ||
|
||
private static final DynamicMethod<String> SomeClass_getSomeAttribute = SomeClass.method(String.class, | ||
"getSomeAttribute"); | ||
private static final DynamicMethod<Integer> SomeClass_doSomethingElse = SomeClass.method(int.class, | ||
"doSomethingElse", int.class); | ||
private static final DynamicMethod<Void> SomeClass_throwException = SomeClass.method(void.class, "throwException"); | ||
|
||
@Test | ||
void class_check() { | ||
assertThat(SomeClass.exists()).isTrue(); | ||
SomeClass.check(Check.PUBLIC, Check.NOT_FINAL, Check.NOT_STATIC); | ||
assertThat(SomeClass.toClass()).isEqualTo(SomeClass.class); | ||
} | ||
|
||
@Test | ||
void class_isClass() { | ||
assertThrows(NullPointerException.class, () -> SomeClass.isClass(null)); | ||
assertThrows(IllegalArgumentException.class, () -> SomeClass.isClass(new Object())); | ||
assertThat(SomeClass.isClass(SomeClass.class)).isTrue(); | ||
assertThat(SomeClass.isClass(SUBJECT_PACKAGE + ".SomeClass")).isTrue(); | ||
assertThat(SomeClass.isClass(DynamicClass.toDynamic(SomeClass.class))).isTrue(); | ||
assertThat(DynamicClass.toDynamic("java.lang.String").isClass(String.class)).isTrue(); | ||
} | ||
|
||
@Test | ||
void class_notFound() { | ||
DynamicClass<?> notFound = DynamicClass.toDynamic("DoesNotExist"); | ||
assertThat(notFound.exists()).isFalse(); | ||
notFound.check(); | ||
} | ||
|
||
@Test | ||
void class_searchNonFinalFields() { | ||
SomeClass.checkForNonFinalFields(); | ||
} | ||
|
||
@Test | ||
void class_searchNonPrivateFields() { | ||
SomeClass.checkForNonPrivateFields(); | ||
} | ||
|
||
@Test | ||
void class_searchPublicOrProtectedMethods() { | ||
SomeClass.checkForPublicOrProtectedMethods(); | ||
} | ||
|
||
@Test | ||
void constructor_abstract() { | ||
SomeAbstractClass.constructor().newInstance(); | ||
} | ||
|
||
@Test | ||
void constructor_check() throws NoSuchMethodException { | ||
assertThat(SomeClass_newString.exists()).isTrue(); | ||
SomeClass_newString.check(Check.PACKAGE_PRIVATE, Check.NOT_PUBLIC); | ||
assertThat(SomeClass_new.toConstructor()).isEqualTo(SomeClass.class.getConstructor()); | ||
} | ||
|
||
@Test | ||
void constructor_illegalArguments() { | ||
SomeClass_newInt.newInstance(""); | ||
} | ||
|
||
@Test | ||
void constructor_newInstance() { | ||
var instance = SomeClass_newInt.newInstance(1); | ||
assertThat(instance).isInstanceOf(SomeClass.class); | ||
} | ||
|
||
@Test | ||
void constructor_notFound() { | ||
SomeClass.constructor(Boolean.class, Boolean.class).newInstance(true, true); | ||
} | ||
|
||
@Test | ||
void constructor_throwing() { | ||
SomeClass_newBool.newInstance(true); | ||
} | ||
|
||
@Test | ||
void field_check() throws NoSuchFieldException { | ||
assertThat(SomeClass_SOME_CONSTANT.exists()).isTrue(); | ||
SomeClass_SOME_CONSTANT.check(Check.PUBLIC, Check.STATIC, Check.FINAL); | ||
assertThat(SomeClass_SOME_CONSTANT.toField()).isEqualTo(SomeClass.class.getField("SOME_CONSTANT")); | ||
} | ||
|
||
@Test | ||
void field_getInstance() { | ||
var instance = new SomeClass(); | ||
assertThat(SomeClass_someAttribute.getOf(instance)).isNull(); | ||
} | ||
|
||
@Test | ||
void field_getStatic() { | ||
assertThat(SomeClass_SOME_CONSTANT.getStatic()).isEqualTo(42); | ||
} | ||
|
||
@Test | ||
void field_noSuchField() { | ||
SomeClass.field(double.class, "a").getStatic(); | ||
} | ||
|
||
@Test | ||
void field_wrongType() { | ||
SomeClass.field(byte.class, "SOME_CONSTANT").getStatic(); | ||
} | ||
|
||
@Test | ||
void method_badCast() { | ||
var instance = new SomeClass(); | ||
SomeClass.method(double.class, "doSomethingElse", int.class).invokeOn(instance, 10); | ||
} | ||
|
||
@Test | ||
void method_check() throws NoSuchMethodException { | ||
assertThat(SomeClass_getSomeAttribute.exists()).isTrue(); | ||
SomeClass_getSomeAttribute.check(Check.PUBLIC, Check.NOT_STATIC, Check.FINAL); | ||
assertThat(SomeClass_getSomeAttribute.toMethod()).isEqualTo(SomeClass.class.getMethod("getSomeAttribute")); | ||
} | ||
|
||
@Test | ||
void method_illegalArguments() { | ||
var instance = new SomeClass(); | ||
SomeClass_doSomethingElse.invokeOn(instance, "abc"); | ||
} | ||
|
||
@Test | ||
void method_invoke() { | ||
var instance = new SomeClass(); | ||
var value = SomeClass_doSomethingElse.invokeOn(instance, 10); | ||
assertThat(value).isEqualTo(42); | ||
} | ||
|
||
@Test | ||
void method_notFound() { | ||
SomeClass.method(void.class, "abc").invokeStatic(); | ||
} | ||
|
||
@Test | ||
void method_notStatic() { | ||
SomeClass_throwException.invokeStatic(); | ||
} | ||
|
||
@Test | ||
void method_null() { | ||
SomeClass_throwException.invokeOn(null); | ||
} | ||
|
||
@Test | ||
void method_throwing() { | ||
var instance = new SomeClass(); | ||
SomeClass_throwException.invokeOn(instance); | ||
} | ||
} |