IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tests et Performance Java Discussion :

Tester un catch interne (cobertura)


Sujet :

Tests et Performance Java

  1. #1
    Rédacteur
    Avatar de thierryler
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    4 078
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 4 078
    Points : 12 815
    Points
    12 815
    Par défaut Tester un catch interne (cobertura)
    Bonjour à tous,

    J'ai écris une méthode avec un catch :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     
    	protected final String doEncrypt(final String message, final String algo) {
    		// REGLE RG024.2
    		if (message == null || message.length() == 0) {
    			throw new IllegalArgumentException("Le param 'message' ne peut pas etre vide.");
    		}
     
    		// REGLE RG024.1, RG024.3
    		try {
    			final byte[] uniqueKey = message.getBytes();
    			final byte[] hash = MessageDigest.getInstance(algo).digest(uniqueKey);
     
    			final StringBuilder sb = new StringBuilder();
    			for (int i = 0; i < hash.length; i++) {
    				String hex = Integer.toHexString(hash[i]);
    				if (hex.length() == 1) {
    					sb.append('0');
    					sb.append(hex.charAt(hex.length() - 1));
    				} else {
    					sb.append(hex.substring(hex.length() - 2));
    				}
    			}
    			final String encrypt = sb.toString();
    			return encrypt;
    		} catch (NoSuchAlgorithmException e) {
    			return null;
    		}
    	}
    Mais cobertura (lancé depuis maven 3) me dit que le catch n'est pas couvert. Comment je peux le tester ?
    Thierry Leriche-Dessirier
    Consultant Java JEE Web Agile freelance
    Rédacteur pour Developpez
    Professeur de Génie Logiciel à l'ESIEA

    Site : http://www.icauda.com / Linked'in : http://www.linkedin.com/in/thierryler / Twitter : @ThierryLeriche

  2. #2
    Membre averti
    Homme Profil pro
    Dev
    Inscrit en
    Novembre 2006
    Messages
    112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev

    Informations forums :
    Inscription : Novembre 2006
    Messages : 112
    Points : 350
    Points
    350
    Par défaut
    Bonjour
    Peut tu nous dire comment tu appelles la méthodes
    protected final String doEncrypt(final String message, final String algo) ?

    Si l'argument algo est une constante , alors ton outil doit réussir à détecter que l' appel à MessageDigest.getInstance(algo) ne lèveras jamais d’exception .

  3. #3
    Rédacteur
    Avatar de thierryler
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    4 078
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 4 078
    Points : 12 815
    Points
    12 815
    Par défaut
    C'est une classe qui en hérite qui l'appelle.

    Edit : plus précisément, avec un petit changement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
     
    public abstract class AbstractSimpleEncryptor implements Encryptor {
     
    	/**
             * REGLE RG024
             * 
             * {@inheritDoc}
             */
    	protected final String doEncrypt(final String message, final String algo) {
    		// REGLE RG024.2
    		if (message == null || message.length() == 0) {
    			throw new IllegalArgumentException("Le param 'message' ne peut pas etre vide.");
    		}
     
    		// REGLE RG024.1, RG024.3
    		try {
    			final byte[] uniqueKey = message.getBytes();
    			final byte[] hash = MessageDigest.getInstance(algo).digest(uniqueKey);
     
    			final StringBuilder sb = new StringBuilder();
    			for (int i = 0; i < hash.length; i++) {
    				String hex = Integer.toHexString(hash[i]);
    				if (hex.length() == 1) {
    					sb.append('0');
    					sb.append(hex.charAt(hex.length() - 1));
    				} else {
    					sb.append(hex.substring(hex.length() - 2));
    				}
    			}
    			final String encrypt = sb.toString();
    			return encrypt;
    		} catch (NoSuchAlgorithmException e) {
    			throw new IllegalArgumentException("L'algo choisi n'est pas valide.");
    		}
    	}
    }
    avec la sous classe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    public class MD5Encryptor extends AbstractSimpleEncryptor implements Encryptor {
     
    	/**
             * MD5
             * 
             * REGLE RG024.1
             */
    	public static final String ALGO_CRYPTAGE = ALGO_MD5;
     
    	/**
             * REGLE RG024, RG024.1
             * 
             * {@inheritDoc}
             */
    	public final String encrypt(final String message) {
     
    		return doEncrypt(message, ALGO_CRYPTAGE);
    	}
     
    }
    Thierry Leriche-Dessirier
    Consultant Java JEE Web Agile freelance
    Rédacteur pour Developpez
    Professeur de Génie Logiciel à l'ESIEA

    Site : http://www.icauda.com / Linked'in : http://www.linkedin.com/in/thierryler / Twitter : @ThierryLeriche

  4. #4
    Rédacteur
    Avatar de thierryler
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    4 078
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 4 078
    Points : 12 815
    Points
    12 815
    Par défaut
    personne ?
    Thierry Leriche-Dessirier
    Consultant Java JEE Web Agile freelance
    Rédacteur pour Developpez
    Professeur de Génie Logiciel à l'ESIEA

    Site : http://www.icauda.com / Linked'in : http://www.linkedin.com/in/thierryler / Twitter : @ThierryLeriche

Discussions similaires

  1. Logiciel pour tester le réseau interne
    Par pierrot67 dans le forum Supervision
    Réponses: 3
    Dernier message: 07/02/2014, 08h47
  2. Tester plusieurs calculs avec try-catch
    Par saddamtohmto dans le forum MATLAB
    Réponses: 2
    Dernier message: 09/08/2007, 11h15
  3. Tester connexion Internet active sous Windows
    Par Altau dans le forum Développement
    Réponses: 3
    Dernier message: 12/08/2002, 12h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo