Coverage Report - pt.digitalis.dif.sanitycheck.manager.TestMethodDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
TestMethodDefinition
0%
0/35
0%
0/20
3,167
 
 1  0
 /**
 2  
  * 2009, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
 3  
  * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
 4  
  */
 5  
 
 6  
 package pt.digitalis.dif.sanitycheck.manager;
 7  
 
 8  
 import java.lang.reflect.Method;
 9  
 
 10  
 import pt.digitalis.dif.controller.interfaces.IDIFContext;
 11  
 import pt.digitalis.dif.dem.annotations.AnnotationTags;
 12  
 import pt.digitalis.dif.sanitycheck.ExecutionResult;
 13  
 import pt.digitalis.dif.sanitycheck.ISanityCheckTestSuite;
 14  
 import pt.digitalis.dif.sanitycheck.TestResult;
 15  
 import pt.digitalis.dif.sanitycheck.annotations.SanityCheckTest;
 16  
 import pt.digitalis.dif.sanitycheck.exceptions.WrongTestMethodSignature;
 17  
 import pt.digitalis.dif.utils.DIFConstants;
 18  
 import pt.digitalis.utils.common.StringUtils;
 19  
 
 20  
 /**
 21  
  * Defines a Sanity Check Test Method form a {@link ISanityCheckTestSuite}
 22  
  * 
 23  
  * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
 24  
  * @created 15 de Jul de 2011
 25  
  */
 26  
 public class TestMethodDefinition {
 27  
 
 28  
     /** the name of the test */
 29  
     private String name;
 30  
 
 31  
     /** if true the test can only be ran if an administrator is logged in */
 32  
     private boolean onlyAdministrators;
 33  
 
 34  
     /** if T will pass the DIF context to the test method invocation */
 35  
     private boolean passContext;
 36  
 
 37  
     /** the method host test suite instance */
 38  
     private ISanityCheckTestSuite testSuiteInstance;
 39  
 
 40  
     /** the method to run */
 41  
     private Method theMethod;
 42  
 
 43  
     /**
 44  
      * @param testSuiteInstance
 45  
      * @param theMethod
 46  
      * @throws WrongTestMethodSignature
 47  
      *             the methods can only have a single {@link IDIFContext} parameter, or none at all
 48  
      */
 49  0
     public TestMethodDefinition(ISanityCheckTestSuite testSuiteInstance, Method theMethod)
 50  
             throws WrongTestMethodSignature
 51  
     {
 52  0
         SanityCheckTest declarationAnnotation = theMethod.getAnnotation(SanityCheckTest.class);
 53  
 
 54  0
         this.theMethod = theMethod;
 55  0
         this.onlyAdministrators = declarationAnnotation.onlyAdministrators();
 56  0
         this.testSuiteInstance = testSuiteInstance;
 57  
 
 58  0
         if (declarationAnnotation.name().equals(AnnotationTags.NONE))
 59  0
             this.name = StringUtils.camelCaseToString(theMethod.getName());
 60  
         else
 61  0
             this.name = StringUtils.camelCaseToString(declarationAnnotation.name());
 62  
 
 63  
         // Method has parameters... analise them...
 64  0
         if (theMethod.getParameterTypes().length > 0)
 65  
         {
 66  0
             if (theMethod.getParameterTypes().length > 1)
 67  
                 // More than 1 parameters... invalid!
 68  0
                 throw new WrongTestMethodSignature(theMethod);
 69  
 
 70  0
             else if (theMethod.getParameterTypes()[0] != IDIFContext.class)
 71  
                 // The single parameter is not of type IDIFContext... invalid!
 72  0
                 throw new WrongTestMethodSignature(theMethod);
 73  
 
 74  
             else
 75  
                 // A single parameter of the type IDIFContext... Valid!
 76  0
                 this.passContext = true;
 77  
         }
 78  
 
 79  
         // Check the return type, as TestResult...
 80  0
         if (theMethod.getReturnType() != TestResult.class)
 81  0
             throw new WrongTestMethodSignature(theMethod);
 82  0
     }
 83  
 
 84  
     /**
 85  
      * Inspector for the 'name' attribute.
 86  
      * 
 87  
      * @return the name value
 88  
      */
 89  
     public String getName()
 90  
     {
 91  0
         return name;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Inspector for the 'onlyAdministrators' attribute.
 96  
      * 
 97  
      * @return the onlyAdministrators value
 98  
      */
 99  
     public boolean isOnlyAdministrators()
 100  
     {
 101  0
         return onlyAdministrators;
 102  
     }
 103  
 
 104  
     /**
 105  
      * Runs the test
 106  
      * 
 107  
      * @param context
 108  
      *            the current DIF context
 109  
      * @return the result of the test execution
 110  
      */
 111  
     public TestResult run(IDIFContext context)
 112  
     {
 113  
         TestResult result;
 114  
 
 115  
         try
 116  
         {
 117  0
             if (onlyAdministrators
 118  0
                     && (!context.getSession().isLogged() || !context.getSession().getUser().getGroupIDs()
 119  0
                             .contains(DIFConstants.ADMINISTRATORS_GROUP.toLowerCase())))
 120  
             {
 121  0
                 result = new TestResult(ExecutionResult.SKIPPED);
 122  
             }
 123  
             else
 124  
             {
 125  0
                 if (passContext)
 126  0
                     result = (TestResult) theMethod.invoke(testSuiteInstance, context);
 127  
                 else
 128  0
                     result = (TestResult) theMethod.invoke(testSuiteInstance);
 129  
             }
 130  
         }
 131  0
         catch (Exception e)
 132  
         {
 133  0
             result = new TestResult(ExecutionResult.FAILED);
 134  0
             result.setErrorMessage(e.getMessage());
 135  
         }
 136  
 
 137  0
         return result;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Modifier for the 'name' attribute.
 142  
      * 
 143  
      * @param name
 144  
      *            the new name value to set
 145  
      */
 146  
     public void setName(String name)
 147  
     {
 148  0
         this.name = name;
 149  0
     }
 150  
 
 151  
     /**
 152  
      * Modifier for the 'onlyAdministrators' attribute.
 153  
      * 
 154  
      * @param onlyAdministrators
 155  
      *            the new onlyAdministrators value to set
 156  
      */
 157  
     public void setOnlyAdministrators(boolean onlyAdministrators)
 158  
     {
 159  0
         this.onlyAdministrators = onlyAdministrators;
 160  0
     }
 161  
 }