import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Scanner; import java.io.PrintStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.File; import java.io.FileNotFoundException; /** * The test class MoyenneTest. * * @author DB * @version 2012/04 */ public class MoyenneTest extends junit.framework.TestCase { public static final double EPS = 1E-6; // precision pour les comparaisons private static PrintStream out /*= null*/; private static PrintStream console /*= null*/; public void testfonctionMoyenne() { Class vCla = Moyenne.class; // le nom de la classe doit etre : Moyenne Method vMet; // si existe moyenne(double[],int) vMet = hasMethodP( vCla, "p", "double", "moyenne", new String[]{"String[]","int"} ); if ( vMet != null ) { try { String[] vTab3 = { "9.5", "10.0", "15.0" }; assertEquals( "3) Mauvais calcul de moyenne", 11.5, (Double)vMet.invoke( null, vTab3, 3 ), EPS ); assertEquals( "3) Non prise en compte du nombre de valeurs", 9.75, (Double)vMet.invoke( null, vTab3, 2 ), EPS ); } catch ( Exception e ) { fail( "moyenne3 : "+e ); } } // if moyenne(double[],int) // sinon si existe moyenne(String[],int) else { vMet = hasMethodP( vCla, "p", "double", "moyenne", new String[]{"double[]","int"} ); if ( vMet != null ) { try { double[] vTab2 = { 9.5, 10.0, 15.0 }; assertEquals( "2) Mauvais calcul de moyenne", 11.5, (Double)vMet.invoke( null, vTab2, 3 ), EPS ); assertEquals( "2) Non prise en compte du nombre de valeurs", 9.75, (Double)vMet.invoke( null, vTab2, 2 ), EPS ); } catch ( Exception e ) { fail( "moyenne2 : "+e ); } } // if moyenne(String[],int)f // sinon else fail( "pas de fonction moyenne(.,.) !" ); } // else pas double[] } // fonctionMoyenne() public void testprocedureMoyenne() { Class vCla = Moyenne.class; // le nom de la classe doit etre : Moyenne Method vMet; vMet = hasMethodP( vCla, "p", "void", "moyenne", new String[]{"String[]","int"} ); assertNull( "Le second parametre n'est plus utile !", vMet ); vMet = hasMethodP( vCla, "p", "void", "moyenne", new String[]{"String[]"} ); if ( vMet != null ) { try { if ( ! redirectTerminal() ) fail( "Probleme avec la fenetre Terminal !" ); Object[] vArgList = new Object[1]; String[] vTab4 = { "9.5", "10.0", "15.0" }; vArgList[0] = vTab4; vMet.invoke( null, vArgList ); String[] vTab0 = { }; vArgList[0] = vTab0; vMet.invoke( null, vArgList ); String res = compareTerminal( "moyenne4.in" ); if ( ! res.equals( "OK" ) ) fail( res ); } catch ( Exception e ) { fail( "moyenne4 : "+e ); } } // if moyenne(String[]) // sinon else fail( "pas de procedure moyenne(.) !" ); } // procedureMoyenne() public void testprocedureMain() { Class vCla = Moyenne.class; // le nom de la classe doit etre : Moyenne Method vMet; vMet = hasMethod( vCla, "p", "main", 0 ); assertNull( "Il manque le parametre obligatoire !", vMet ); vMet = hasMethod( vCla, "p", "main", 2 ); assertNull( "On ne peut pas ajouter de parametre !", vMet ); vMet = hasMethod( vCla, "p", "main", 1 ); assertNotNull( "Le nb de parametres est impose par Java !", vMet ); vMet = hasMethodP( vCla, "p", "void", "main", new String[]{"String[]"} ); assertNotNull( "La signature est imposee par Java !"+vMet, vMet ); vMet = hasMethodP( vCla, "u", "void", "main", new String[]{"String[]"} ); assertNotNull( "main() doit pouvoir etre appelee par une classe 'etrangere' !", vMet ); vMet = hasMethodP( vCla, "us", "void", "main", new String[]{"String[]"} ); assertNotNull( "main() doit pouvoir etre appelee directement sur la classe !", vMet ); try { if ( ! redirectTerminal() ) fail( "Probleme avec la fenetre Terminal !" ); Object[] vArgList = new Object[1]; String[] vTab4 = { "9.5", "10.0", "15.0" }; vArgList[0] = vTab4; vMet.invoke( null, vArgList ); String[] vTab0 = { }; vArgList[0] = vTab0; vMet.invoke( null, vArgList ); String res = compareTerminal( "moyenne4.in" ); if ( ! res.equals( "OK" ) ) fail( res ); } catch ( Exception e ) { fail( "main : "+e ); } } // procedureMain() /** * Default constructor for test class MoyenneTest */ public MoyenneTest() { } /** * Sets up the test fixture. * * Called before every test case method. */ public void setUp() { } /** * Tears down the test fixture. * * Called after every test case method. */ public void tearDown() { } public static boolean redirectTerminal() { boolean ok = false; try { console = System.out; out = new PrintStream( new BufferedOutputStream( new FileOutputStream( "test.out" ) )); System.setOut( out ); ok = true; } catch( Exception e ) { e.printStackTrace(); } return ok; } // redirectTerminal() public static String compareTerminal( String refName ) { try { out.flush(); out.close(); Scanner ref = new Scanner( new File( refName ) ); Scanner res = new Scanner( new File( "test.out" ) ); while ( ref.hasNextLine() ) { String liRef = ref.nextLine(); if ( ! res.hasNextLine() ) return "Il manque au moins une ligne d'affichage !" ; String liRes = res.nextLine(); if ( ! liRef.equals( liRes ) ) return "Display expected <" + liRef + "> but was <" + liRes + "> !" ; } // while } catch( FileNotFoundException fnfe ) { return "Fichier " + refName + " introuvable !" ; } catch( Exception e ) { e.printStackTrace(); } finally { System.setOut( console ); } return "OK"; } // compareTerminal(.) public static Method hasMethod( Class pC, String pL, String pN, int pP ) { // pUblic prOtected pAckage prIvate ou anyProtection for ( Method vM : pC.getDeclaredMethods() ) { if ( isMethod( vM, pL, pN, pP ) ) return vM; } // for return null; } // hasMethod() public static Method hasMethodP( Class pC, String pL, String pT, String pN, String[] pP ) { // pUblic prOtected pAckage prIvate ou anyProtection for ( Method vM : pC.getDeclaredMethods() ) { if ( isMethodP( vM, pL, pT, pN, pP ) ) return vM; } // for return null; } // hasMethodP() public static boolean isMethod( Method pM, String pL, String pN, int pP ) { // pUblic prOtected pAckage prIvate ou anyProtection return hasModifiers( pM, pL ) && pM.getName().equals( pN ) && pM.getParameterTypes().length == pP; } // isMethod() public static boolean isMethodP( Method pM, String pL, String pT, String pN, String[] pP ) { // pUblic prOtected pAckage prIvate ou anyProtection boolean vR; vR = pM.getName().equals( pN ); if ( pL.contains( "r" ) ) { // vR = vR && (pM.getAnnotation( Override.class ) != null); // System.out.println(java.util.Arrays.toString(pM.getDeclaredAnnotations())); pL = pL.replace( "r", "" ); } // r vR = vR && hasModifiers( pM, pL ); vR = vR && pM.getReturnType().getName().equals( pT ); Class[] vP = pM.getParameterTypes(); vR = vR && (vP.length == pP.length); for ( int vI=0; vI