编写自己的annotation,当前日期在指定的日期后,则执行

package testng;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;

@Retention(RUNTIME)	//希望该annotation存在于类文件中,可以利用反射来查询他的属性值
@Target({METHOD, TYPE, CONSTRUCTOR})	//该annotation可以用于:普通方法,类,构造方法
public @interface ScheduledTest {
	/**
	 * 属性 - 如果指定了activeAfter,就取出他的值,并转换为Date对象,
	 * 然后与当前日期进行比较,如果当前日期在activeAfter之后,就返回true
	 * @return
	 */
	String activeAfter() default "";
}

package testng;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;

public class ScheduledTestSelector implements IMethodSelector {

	private Date parse(String dateString) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d = null;
		try {
			d = formatter.parse(dateString);
		} catch (ParseException e) {
			System.err.println(dateString + " is not correct!");
			throw new RuntimeException();
		}
		
		return d;
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod m, boolean isTestMethod) {
		Method method = m.getMethod();
		Annotation a = method.getAnnotation(ScheduledTest.class);
		if(a != null) {
			Date now = Calendar.getInstance().getTime();
			ScheduledTest st = (ScheduledTest) a;
			Date activeAfter = null;
			String activeAfterString = st.activeAfter();
			if(activeAfterString != null && !"".equals(activeAfterString)) {
				activeAfter = parse(activeAfterString);
				if(now.before(activeAfter)) {
					return false;
				}
			}
		}
		
		return true;
	}

	@Override
	public void setTestMethods(List<ITestNGMethod> testMethods) {
		//do something
	}

}

package testng;

import org.testng.annotations.Test;

public class ScheduledTest_Test {
	@Test
	@ScheduledTest
	public void test1() {
		System.out.println("test1");
	}
	
	@Test
	@ScheduledTest(activeAfter = "2010-05-24 18:23")
	public void test2() {
		System.out.println("test2");
	}
	
	@Test
	@ScheduledTest(activeAfter = "2014-05-24 18:23")
	public void test3() {
		System.out.println("test3");
	}
	
	@Test(expectedExceptions = RuntimeException.class)
	@ScheduledTest(activeAfter = "2014")
	public void test4() {
		System.out.println("test4");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值