@Retention(SOURCE) @Target({ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD,PARAMETER}) public@interface RequiresPermission { /** * The name of the permission that is required, if precisely one permission * is required. If more than one permission is required, specify either * {@link #allOf()} or {@link #anyOf()} instead. */ String value()default"";
/** * Specifies a list of permission names that are all required. */ String[] allOf() default {};
/** * Specifies a list of permission names where at least one is required */ String[] anyOf() default {};
/** * If true, the permission may not be required in all cases (e.g. it may only be * enforced on certain platforms, or for certain call parameters, etc. */ booleanconditional()defaultfalse;
/** * Specifies that the given permission is required for read operations. * * When specified on a parameter, the annotation indicates that the method requires * a permission which depends on the value of the parameter (and typically * the corresponding field passed in will be one of a set of constants which have * been annotated with a <code>@RequiresPermission</code> annotation.) */ @Target({FIELD, METHOD, PARAMETER}) @interface Read { RequiresPermission value()default@RequiresPermission; }
/** * Specifies that the given permission is required for write operations. * * When specified on a parameter, the annotation indicates that the method requires * a permission which depends on the value of the parameter (and typically * the corresponding field passed in will be one of a set of constants which have been annotated with a <code>@RequiresPermission</code> annotation.) */ @Target({FIELD, METHOD, PARAMETER}) @interface Write { RequiresPermission value()default@RequiresPermission; } }
@Documented @Inherited @Target (value=METHOD) @Retention (RetentionPolicy.RUNTIME) public@interface Test { /** * This annotation attribute is used whether method throw assigned custom *exception * or not. If it throws custom exception, it means test case pass * @return */ public Class<? extendsException> expected() default java.lang.Exception.class; }
/** * The class <code>InvalidParameterException</code> is a subclass of <code>Exception</code> class; * This class is used to throw exception when method arguments are invalid * @author Jason * */ @SuppressWarnings("serial") publicclassInvalidParameterExceptionextendsException{
/** * math operation class, which support two math operations * @author Jason * */ publicclassMathsOperation { publicintsum(int a, int b){ return a + b; } publicfloatdivide(int a, int b)throws InvalidParameterException{ if(b == 0){ thrownewInvalidParameterException(); } return (float)(a/b); } }
/** * All test cases are written here with @Test annotation * */ publicclassMathUnitTests { @Test(expected = InvalidParameterException.class) publicvoidtestDivide()throws InvalidParameterException{ MathsOperationmp=newMathsOperation(); mp.divide(10, 0); } @Test publicvoidtestSum(){ MathsOperationmp=newMathsOperation(); ints= mp.sum(10, 3); if(s == 13){ System.out.println("Sum Test Case Pass, output value is " + s); } } }
/** * This class is test suite runner, which will runs all test cases * provided in given test class. This class particularly check custom exception thrown by method. * If thrown exception match with expected exception mentioned on attributes of @Test annotation, that means * test case is pass. This same scenario is used in JUnit as well. * */ publicclassUnitTestRunner { publicvoidrunUnitTests(String className){ try{ Class<?> testClass = Class.forName(className); Objectobj= testClass.newInstance(); Method[] methods = testClass.getMethods(); for(Method method : methods){ if(method.isAnnotationPresent(com.examples.jason.javecore.annotation.Test.class)){ Testannotation= method.getAnnotation(Test.class); Class<? extendsException> expectedClass = annotation.expected(); if(expectedClass != null){ try{ method.invoke(obj); } catch(InvocationTargetException e){ if(e.getTargetException().getClass() == expectedClass){ System.out.println("Test Case Pass with InvalidParameterException"); } else{ System.out.println("Test Case Fail with an exception: " + e.getMessage()); } } catch(Exception e){ System.out.println("Test Case Fail with an exception: " + e.getMessage()); } } } } } catch (ClassNotFoundException cnfe){ cnfe.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } publicstaticvoidmain(String[] args){ UnitTestRunnerutRunner=newUnitTestRunner(); utRunner.runUnitTests("com.examples.jason.javecore.annotation.MathUnitTests"); }