net.kenblair.scheduler.jpa.JpaScheduledJobRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for net.kenblair.scheduler.jpa.JpaScheduledJobRepositoryTest.java

Source

/*
 * Copyright 2014 Ken Blair
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kenblair.scheduler.jpa;

import net.kenblair.scheduler.entity.ScheduledJob;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class JpaScheduledJobRepositoryTest {

    @Autowired
    private JpaScheduledJobRepository repository;

    @Before
    public void setUp() throws Exception {
        JpaScheduledJob job1 = new JpaScheduledJob();
        job1.setBean("foo");
        job1.setCronExpression("1 1 * * * *");
        job1.setEnabled(true);
        job1.setProperty("foo", "bar");
        repository.save(job1);

        JpaScheduledJob job2 = new JpaScheduledJob();
        job2.setBean("bar");
        job2.setCronExpression("2 2 * * * *");
        job2.setEnabled(false);
        job2.setProperty("bar", "foo");
        repository.save(job2);
    }

    @Test
    public void testFindEnabledJobs() throws Exception {
        final List<ScheduledJob> jobs = repository.findEnabledJobs();
        assertEquals(1, jobs.size());
        assertEquals("foo", jobs.get(0).getBean());
    }

    @Test
    public void testUpdateLastRun() throws Exception {
        ScheduledJob job = repository.findEnabledJobs().get(0);
        final Date lastDateRun = job.getLastRun();
        repository.updateLastRun(job.getId());
        job = repository.findOne(new Long(job.getId()));
        assertTrue(lastDateRun.before(job.getLastRun()));
    }

}