|
| 1 | +/* |
| 2 | + * Copyright 2002-2009 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.scheduling.quartz; |
| 18 | + |
| 19 | +import java.util.Date; |
| 20 | +import java.util.Calendar; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +/** @author Rob Harrop */ |
| 26 | +public class CronTriggerBeanTests { |
| 27 | + |
| 28 | + @Test(expected = IllegalStateException.class) |
| 29 | + public void testInvalidStartDelay() { |
| 30 | + createTriggerBean().setStartDelay(-1); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testStartTime() throws Exception { |
| 35 | + CronTriggerBean bean = createTriggerBean(); |
| 36 | + Date startTime = new Date(System.currentTimeMillis() + 1234L); |
| 37 | + bean.setStartTime(startTime); |
| 38 | + bean.afterPropertiesSet(); |
| 39 | + assertTimeEquals(startTime, bean.getStartTime()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testStartDelay() throws Exception { |
| 44 | + CronTriggerBean bean = createTriggerBean(); |
| 45 | + long startDelay = 1234L; |
| 46 | + Date startTime = new Date(System.currentTimeMillis() + startDelay); |
| 47 | + bean.setStartDelay(startDelay); |
| 48 | + bean.afterPropertiesSet(); |
| 49 | + assertTimeEquals(startTime, bean.getStartTime()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testStartDelayOverridesStartTime() throws Exception { |
| 54 | + CronTriggerBean bean = createTriggerBean(); |
| 55 | + long startDelay = 1234L; |
| 56 | + Date startTime = new Date(System.currentTimeMillis() + startDelay); |
| 57 | + bean.setStartTime(new Date(System.currentTimeMillis() + 123456789L)); |
| 58 | + bean.setStartDelay(startDelay); |
| 59 | + bean.afterPropertiesSet(); |
| 60 | + assertTimeEquals(startTime, bean.getStartTime()); |
| 61 | + } |
| 62 | + |
| 63 | + private CronTriggerBean createTriggerBean() { |
| 64 | + CronTriggerBean triggerBean = new CronTriggerBean(); |
| 65 | + triggerBean.setName("test"); |
| 66 | + return triggerBean; |
| 67 | + } |
| 68 | + |
| 69 | + private void assertTimeEquals(Date a, Date b) { |
| 70 | + Calendar ca = Calendar.getInstance(); |
| 71 | + ca.setTime(a); |
| 72 | + ca.set(Calendar.MILLISECOND, 0); |
| 73 | + Calendar cb = Calendar.getInstance(); |
| 74 | + cb.setTime(b); |
| 75 | + cb.set(Calendar.MILLISECOND, 0); |
| 76 | + assertEquals(ca, cb); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments