TP-metaprogrammation-test/TP4/LanceurIndependant.java
2023-04-22 17:31:23 +02:00

148 lines
4 KiB
Java

import java.lang.reflect.*;
import java.util.*;
import java.lang.annotation.*;
/**
* L'objectif est de faire un lanceur simple sans utiliser toutes les clases de
* notre architecture JUnit. Il permet juste de valider la compréhension de
* l'introspection en Java.
*/
public class LanceurIndependant {
private int nbTestsLances;
private int nbErreurs;
private int nbEchecs;
private List<Throwable> erreurs = new ArrayList<>();
public LanceurIndependant(String... nomsClasses) {
System.out.println();
// Lancer les tests pour chaque classe
for (String nom : nomsClasses) {
try {
System.out.println(nom + " : ");
this.testerUneClasse(nom);
System.out.println();
} catch (ClassNotFoundException e) {
System.out.println("Classe inconnue !");
} catch (Exception e) {
System.out.println("Problème : " + e);
e.printStackTrace();
}
}
// Afficher les erreurs
for (Throwable e : erreurs) {
System.out.println();
e.printStackTrace();
}
// Afficher un bilan
System.out.printf("%d tests lancés dont %d échecs et %d erreurs.", nbTestsLances, nbEchecs, nbErreurs);
}
public int getNbTests() {
return this.nbTestsLances;
}
public int getNbErreurs() {
return this.nbErreurs;
}
public int getNbEchecs() {
return this.nbEchecs;
}
private void testerUneClasse(String nomClasse)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException, SecurityException {
// Récupérer la classe
Class<?> classe = Class.forName(nomClasse);
// Récupérer les méthodes "preparer" et "nettoyer"
Method preparer = null;
Method nettoyer = null;
for (Method method : classe.getMethods()) {
if (method.isAnnotationPresent(Avant.class)) {
preparer = method;
break;
}
}
for (Method method : classe.getMethods()) {
if (method.isAnnotationPresent(Apres.class)) {
nettoyer = method;
break;
}
}
// Instancier l'objet qui sera le récepteur des tests
// Object objet = classe.newInstance();
Object objet = classe.getDeclaredConstructor().newInstance();
// Exécuter les méthods de test
Method[] methods = classe.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(UnTest.class)) {
if (method.getAnnotation(UnTest.class).enabled()) {
this.nbTestsLances++;
if (preparer != null) {
preparer.invoke(objet);
}
Class<? extends Throwable> exep = method.getAnnotation(UnTest.class).expected();
try {
if (exep != FausseException.class) {
System.out.println("exep != FausseException.class");
try {
method.invoke(objet);
System.out.println(method.getName() + " : echec");
this.nbEchecs++;
} catch (InvocationTargetException e) {
Class<? extends Throwable> exep2 = method.getAnnotation(UnTest.class).expected();
if (e.getCause().getClass().isAssignableFrom(exep)) {
System.out.println(method.getName() + " : success");
} else {
System.out.println(method.getName() + " : erreur");
this.erreurs.add(e);
this.nbErreurs++;
}
}
} else {
try {
method.invoke(objet);
System.out.println(method.getName() + " : success");
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Echec) {
System.out.println(method.getName() + " : echec");
this.erreurs.add(e);
this.nbEchecs++;
} else {
System.out.println(method.getName() + " : erreur");
this.erreurs.add(e);
this.nbErreurs++;
}
}
}
} catch (java.lang.IllegalArgumentException e) {
System.out.println(method.getName() + " : erreur, pas de test avec arguments");
this.nbErreurs++;
System.out.println("d");
}
if (nettoyer != null) {
nettoyer.invoke(objet);
}
}
}
}
}
public static void main(String... args) {
new LanceurIndependant(args);
}
}