私人定制注释

来源:互联网 发布:iphone淘宝新版换旧版 编辑:程序博客网 时间:2024/06/10 00:25
package com.mkyong.test.core; import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD) //can use in method only.public @interface Test { //should ignore this test?public boolean enabled() default true; }
package com.mkyong.test.core; import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE) //on class levelpublic @interface TesterInfo { public enum Priority {   LOW, MEDIUM, HIGH} Priority priority() default Priority.MEDIUM; String[] tags() default ""; String createdBy() default "Mkyong"; String lastModified() default "03/01/2014"; }
package com.mkyong.test;import com.mkyong.test.core.Test;import com.mkyong.test.core.TesterInfo;import com.mkyong.test.core.TesterInfo.Priority;@TesterInfo(priority = Priority.HIGH, createdBy = "mkyong.com", tags = {"sales", "test" })public class TestExample {@Testvoid testA() {if (true)throw new RuntimeException("This test always failed");}@Test(enabled = false)void testB() {if (false)throw new RuntimeException("This test always passed");}@Test(enabled = true)void testC() {if (10 > 1) {// do nothing, this test always passed.}}}
package com.mkyong.test;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import com.mkyong.test.core.Test;import com.mkyong.test.core.TesterInfo;public class RunTest {public static void main(String[] args) throws Exception {System.out.println("Testing...");int passed = 0, failed = 0, count = 0, ignore = 0;Class<TestExample> obj = TestExample.class;// Process @TesterInfoif (obj.isAnnotationPresent(TesterInfo.class)) {Annotation annotation = obj.getAnnotation(TesterInfo.class);TesterInfo testerInfo = (TesterInfo) annotation;System.out.printf("%nPriority :%s", testerInfo.priority());System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());System.out.printf("%nTags :");int tagLength = testerInfo.tags().length;for (String tag : testerInfo.tags()) {if (tagLength > 1) {System.out.print(tag + ", ");} else {System.out.print(tag);}tagLength--;}System.out.printf("%nLastModified :%s%n%n",testerInfo.lastModified());}// Process @Testfor (Method method : obj.getDeclaredMethods()) {// if method is annotated with @Testif (method.isAnnotationPresent(Test.class)) {Annotation annotation = method.getAnnotation(Test.class);Test test = (Test) annotation;// if enabled = true (default)if (test.enabled()) {try {method.invoke(obj.newInstance());System.out.printf("%s - Test '%s' - passed %n",++count, method.getName());passed++;} catch (Throwable ex) {System.out.printf("%s - Test '%s' - failed: %s %n",++count, method.getName(), ex.getCause());failed++;}} else {System.out.printf("%s - Test '%s' - ignored%n", ++count,method.getName());ignore++;}}}System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n",count, passed, failed, ignore);}}
Testing...


Priority :HIGH
CreatedBy :mkyong.com
Tags :sales, test
LastModified :03/01/2014


1 - Test 'testA' - failed: java.lang.RuntimeException: This test always failed 
2 - Test 'testB' - ignored
3 - Test 'testC' - passed 


Result : Total : 3, Passed: 1, Failed 1, Ignore 1

http://www.mkyong.com/java/java-custom-annotations-example/

0 0