implemented small scheduler engine

This commit is contained in:
Sebastian Sdorra
2016-05-25 16:32:25 +02:00
parent a011bb3e8d
commit 13bea6e502
12 changed files with 943 additions and 0 deletions

View File

@@ -435,6 +435,7 @@
<!-- util libraries --> <!-- util libraries -->
<guava.version>15.0</guava.version> <guava.version>15.0</guava.version>
<quartz.version>2.2.3</quartz.version>
<!-- build properties --> <!-- build properties -->
<project.build.javaLevel>1.6</project.build.javaLevel> <project.build.javaLevel>1.6</project.build.javaLevel>

View File

@@ -0,0 +1,65 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import java.io.Closeable;
/**
* Scheduler is able to run tasks on the future in a background thread. Task can be scheduled with cron like expression.
* <strong>Note:</strong> Task are always executed in an administrative context.
* @author Sebastian Sdorra
* @since 1.47
*/
public interface Scheduler extends Closeable {
/**
* Schedule a new task for future execution.
*
* @param expression cron expression
* @param runnable action
*
* @return cancelable task
*/
public Task schedule(String expression, Runnable runnable);
/**
* Schedule a new task for future execution. The method will create a new instance of the runnable for every
* execution. The runnable can use injection.
*
* @param expression cron expression
* @param runnable action class
*
* @return cancelable task
*/
public Task schedule(String expression, Class<? extends Runnable> runnable);
}

View File

@@ -0,0 +1,47 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
/**
* Tasks are executed in the future and can be running more than once. A task execution can be canceled.
*
* @author Sebastian Sdorra
* @since 1.47
*/
public interface Task {
/**
* Cancel task execution.
*/
public void cancel();
}

View File

@@ -190,6 +190,18 @@
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
<exclusions>
<exclusion>
<artifactId>c3p0</artifactId>
<groupId>c3p0</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- fix version conflict --> <!-- fix version conflict -->
<dependency> <dependency>

View File

@@ -60,6 +60,7 @@ import java.util.List;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import sonia.scm.repository.HealthCheckContextListener; import sonia.scm.repository.HealthCheckContextListener;
import sonia.scm.schedule.Scheduler;
/** /**
* *
@@ -79,6 +80,8 @@ public class ScmContextListener extends GuiceServletContextListener
{ {
if ((globalInjector != null) &&!startupError) if ((globalInjector != null) &&!startupError)
{ {
// close Scheduler
IOUtil.close(globalInjector.getInstance(Scheduler.class));
// close RepositoryManager // close RepositoryManager
IOUtil.close(globalInjector.getInstance(RepositoryManager.class)); IOUtil.close(globalInjector.getInstance(RepositoryManager.class));

View File

@@ -164,6 +164,8 @@ import sonia.scm.net.ahc.ContentTransformer;
import sonia.scm.net.ahc.DefaultAdvancedHttpClient; import sonia.scm.net.ahc.DefaultAdvancedHttpClient;
import sonia.scm.net.ahc.JsonContentTransformer; import sonia.scm.net.ahc.JsonContentTransformer;
import sonia.scm.net.ahc.XmlContentTransformer; import sonia.scm.net.ahc.XmlContentTransformer;
import sonia.scm.schedule.QuartzScheduler;
import sonia.scm.schedule.Scheduler;
import sonia.scm.security.XsrfProtectionFilter; import sonia.scm.security.XsrfProtectionFilter;
import sonia.scm.web.UserAgentParser; import sonia.scm.web.UserAgentParser;
@@ -282,6 +284,9 @@ public class ScmServletModule extends ServletModule
bind(PluginLoader.class).toInstance(pluginLoader); bind(PluginLoader.class).toInstance(pluginLoader);
bind(PluginManager.class, DefaultPluginManager.class); bind(PluginManager.class, DefaultPluginManager.class);
// bind scheduler
bind(Scheduler.class).to(QuartzScheduler.class);
// note CipherUtil uses an other generator // note CipherUtil uses an other generator
bind(KeyGenerator.class).to(DefaultKeyGenerator.class); bind(KeyGenerator.class).to(DefaultKeyGenerator.class);
bind(CipherHandler.class).toInstance(cu.getCipherHandler()); bind(CipherHandler.class).toInstance(cu.getCipherHandler());

View File

@@ -0,0 +1,90 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import com.google.common.base.Preconditions;
import com.google.inject.Injector;
import com.google.inject.Provider;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.web.security.AdministrationContext;
import sonia.scm.web.security.PrivilegedAction;
/**
* InjectionEnabledJob allows the execution of quartz jobs and enable injection on them.
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
* @since 1.47
*/
public class InjectionEnabledJob implements Job {
private static final Logger logger = LoggerFactory.getLogger(InjectionEnabledJob.class);
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
Preconditions.checkNotNull(jec, "execution context is null");
JobDetail detail = jec.getJobDetail();
Preconditions.checkNotNull(detail, "job detail not provided");
JobDataMap dataMap = detail.getJobDataMap();
Preconditions.checkNotNull(dataMap, "job detail does not contain data map");
Injector injector = (Injector) dataMap.get(Injector.class.getName());
Preconditions.checkNotNull(injector, "data map does not contain injector");
final Provider<Runnable> runnableProvider = (Provider<Runnable>) dataMap.get(Runnable.class.getName());
if (runnableProvider == null) {
throw new JobExecutionException("could not find runnable provider");
}
AdministrationContext ctx = injector.getInstance(AdministrationContext.class);
ctx.runAsAdmin(new PrivilegedAction()
{
@Override
public void run()
{
logger.trace("create runnable from provider");
Runnable runnable = runnableProvider.get();
logger.debug("execute injection enabled job {}", runnable.getClass());
runnable.run();
}
});
}
}

View File

@@ -0,0 +1,175 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import com.google.common.base.Throwables;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import javax.inject.Inject;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.Initable;
import sonia.scm.SCMContextProvider;
/**
* {@link Scheduler} which uses the quartz scheduler.
*
* @author Sebastian Sdorra
* @since 1.47
*
* @see <a href="http://www.quartz-scheduler.org/">Quartz Job Scheduler</a>
*/
@Singleton
public class QuartzScheduler implements Scheduler, Initable {
private static final Logger logger = LoggerFactory.getLogger(QuartzScheduler.class);
private final Injector injector;
private final org.quartz.Scheduler scheduler;
/**
* Creates a new quartz scheduler.
*
* @param injector injector
*/
@Inject
public QuartzScheduler(Injector injector)
{
this.injector = injector;
// get default scheduler
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException ex) {
throw Throwables.propagate(ex);
}
}
/**
* Creates a new quartz scheduler. This constructor is only for testing.
*
* @param injector injector
* @param scheduler quartz scheduler
*/
QuartzScheduler(Injector injector, org.quartz.Scheduler scheduler)
{
this.injector = injector;
this.scheduler = scheduler;
}
@Override
public void init(SCMContextProvider context)
{
try
{
if (!scheduler.isStarted())
{
scheduler.start();
}
}
catch (SchedulerException ex)
{
logger.error("can not start scheduler", ex);
}
}
@Override
public void close() throws IOException
{
try
{
if (scheduler.isStarted()){
scheduler.shutdown();
}
}
catch (SchedulerException ex)
{
logger.error("can not stop scheduler", ex);
}
}
@Override
public Task schedule(String expression, final Runnable runnable)
{
return schedule(expression, new Provider<Runnable>(){
@Override
public Runnable get()
{
return runnable;
}
});
}
@Override
public Task schedule(String expression, Class<? extends Runnable> runnable)
{
return schedule(expression, injector.getProvider(runnable));
}
private Task schedule(String expression, Provider<? extends Runnable> provider){
// create data map with injection provider for InjectionEnabledJob
JobDataMap map = new JobDataMap();
map.put(Runnable.class.getName(), provider);
map.put(Injector.class.getName(), injector);
// create job detail for InjectionEnabledJob with the provider for the annotated class
JobDetail detail = JobBuilder.newJob(InjectionEnabledJob.class)
.usingJobData(map)
.build();
// create a trigger with the cron expression from the annotation
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(detail)
.withSchedule(CronScheduleBuilder.cronSchedule(expression))
.build();
try {
scheduler.scheduleJob(detail, trigger);
} catch (SchedulerException ex) {
throw Throwables.propagate(ex);
}
return new QuartzTask(scheduler, trigger.getJobKey());
}
}

View File

@@ -0,0 +1,68 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import com.google.common.base.Throwables;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
/**
* Task implementation for the {@link QuartzScheduler}.
*
* @author Sebastian Sdorra
*/
public class QuartzTask implements Task {
private final org.quartz.Scheduler scheduler;
private final JobKey jobKey;
QuartzTask(Scheduler scheduler, JobKey jobKey)
{
this.scheduler = scheduler;
this.jobKey = jobKey;
}
@Override
public void cancel()
{
try
{
scheduler.deleteJob(jobKey);
}
catch (SchedulerException ex)
{
throw Throwables.propagate(ex);
}
}
}

View File

@@ -0,0 +1,168 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import com.google.inject.Injector;
import com.google.inject.Provider;
import org.junit.Test;
import static org.mockito.Mockito.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import sonia.scm.web.security.AdministrationContext;
import sonia.scm.web.security.PrivilegedAction;
/**
* Unit tests for {@link InjectionEnabledJob}.
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class InjectionEnabledJobTest {
@Mock
private Injector injector;
@Mock
private JobDataMap dataMap;
@Mock
private JobDetail detail;
@Mock
private JobExecutionContext jec;
@Mock
private Provider<Runnable> runnable;
@Mock
private AdministrationContext context;
@Rule
public ExpectedException expected = ExpectedException.none();
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)} without context.
*
* @throws JobExecutionException
*/
@Test
public void testExecuteWithoutContext() throws JobExecutionException
{
expected.expect(NullPointerException.class);
expected.expectMessage("execution context");
new InjectionEnabledJob().execute(null);
}
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)} without job detail.
*
* @throws JobExecutionException
*/
@Test
public void testExecuteWithoutJobDetail() throws JobExecutionException
{
expected.expect(NullPointerException.class);
expected.expectMessage("detail");
new InjectionEnabledJob().execute(jec);
}
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)} without data map.
*
* @throws JobExecutionException
*/
@Test
public void testExecuteWithoutDataMap() throws JobExecutionException
{
when(jec.getJobDetail()).thenReturn(detail);
expected.expect(NullPointerException.class);
expected.expectMessage("data map");
new InjectionEnabledJob().execute(jec);
}
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)} without injector.
*
* @throws JobExecutionException
*/
@Test
public void testExecuteWithoutInjector() throws JobExecutionException
{
when(jec.getJobDetail()).thenReturn(detail);
when(detail.getJobDataMap()).thenReturn(dataMap);
expected.expect(NullPointerException.class);
expected.expectMessage("injector");
new InjectionEnabledJob().execute(jec);
}
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)} without runnable.
*
* @throws JobExecutionException
*/
@Test
public void testExecuteWithoutRunnable() throws JobExecutionException
{
when(jec.getJobDetail()).thenReturn(detail);
when(detail.getJobDataMap()).thenReturn(dataMap);
when(dataMap.get(Injector.class.getName())).thenReturn(injector);
expected.expect(JobExecutionException.class);
expected.expectMessage("runnable");
new InjectionEnabledJob().execute(jec);
}
/**
* Tests {@link InjectionEnabledJob#execute(org.quartz.JobExecutionContext)}.
*
* @throws JobExecutionException
*/
@Test
public void testExecute() throws JobExecutionException
{
when(jec.getJobDetail()).thenReturn(detail);
when(detail.getJobDataMap()).thenReturn(dataMap);
when(dataMap.get(Injector.class.getName())).thenReturn(injector);
when(dataMap.get(Runnable.class.getName())).thenReturn(runnable);
when(injector.getInstance(AdministrationContext.class)).thenReturn(context);
new InjectionEnabledJob().execute(jec);
verify(context).runAsAdmin(Mockito.any(PrivilegedAction.class));
}
}

View File

@@ -0,0 +1,220 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import com.google.inject.Injector;
import com.google.inject.Provider;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
/**
* Unit tests for {@link QuartzScheduler}.
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class QuartzSchedulerTest {
@Mock
private Injector injector;
@Mock
private org.quartz.Scheduler quartzScheduler;
private QuartzScheduler scheduler;
@Before
public void setUp()
{
scheduler = new QuartzScheduler(injector, quartzScheduler);
}
/**
* Tests {@link QuartzScheduler#schedule(java.lang.String, java.lang.Runnable)}.
*
* @throws SchedulerException
*/
@Test
public void testSchedule() throws SchedulerException
{
DummyRunnable dr = new DummyRunnable();
Task task = scheduler.schedule("42 2 * * * ?", dr);
assertNotNull(task);
ArgumentCaptor<JobDetail> detailCaptor = ArgumentCaptor.forClass(JobDetail.class);
ArgumentCaptor<Trigger> triggerCaptor = ArgumentCaptor.forClass(Trigger.class);
verify(quartzScheduler).scheduleJob(detailCaptor.capture(), triggerCaptor.capture());
Trigger trigger = triggerCaptor.getValue();
assertThat(trigger, is(instanceOf(CronTrigger.class)));
CronTrigger cron = (CronTrigger) trigger;
assertEquals("42 2 * * * ?", cron.getCronExpression());
JobDetail detail = detailCaptor.getValue();
assertEquals(InjectionEnabledJob.class, detail.getJobClass());
Provider<Runnable> runnable = (Provider<Runnable>) detail.getJobDataMap().get(Runnable.class.getName());
assertNotNull(runnable);
assertSame(dr, runnable.get());
assertEquals(injector, detail.getJobDataMap().get(Injector.class.getName()));
}
/**
* Tests {@link QuartzScheduler#schedule(java.lang.String, java.lang.Class)}.
*
* @throws SchedulerException
*/
@Test
public void testScheduleWithClass() throws SchedulerException
{
scheduler.schedule("42 * * * * ?", DummyRunnable.class);
verify(injector).getProvider(DummyRunnable.class);
ArgumentCaptor<JobDetail> detailCaptor = ArgumentCaptor.forClass(JobDetail.class);
ArgumentCaptor<Trigger> triggerCaptor = ArgumentCaptor.forClass(Trigger.class);
verify(quartzScheduler).scheduleJob(detailCaptor.capture(), triggerCaptor.capture());
Trigger trigger = triggerCaptor.getValue();
assertThat(trigger, is(instanceOf(CronTrigger.class)));
CronTrigger cron = (CronTrigger) trigger;
assertEquals("42 * * * * ?", cron.getCronExpression());
JobDetail detail = detailCaptor.getValue();
assertEquals(InjectionEnabledJob.class, detail.getJobClass());
assertEquals(injector, detail.getJobDataMap().get(Injector.class.getName()));
}
/**
* Tests {@link QuartzScheduler#init(sonia.scm.SCMContextProvider)}.
*
* @throws SchedulerException
*/
@Test
public void testInit() throws SchedulerException
{
when(quartzScheduler.isStarted()).thenReturn(Boolean.FALSE);
scheduler.init(null);
verify(quartzScheduler).start();
}
/**
* Tests {@link QuartzScheduler#init(sonia.scm.SCMContextProvider)} when the underlying scheduler is already started.
*
* @throws SchedulerException
*/
@Test
public void testInitAlreadyRunning() throws SchedulerException
{
when(quartzScheduler.isStarted()).thenReturn(Boolean.TRUE);
scheduler.init(null);
verify(quartzScheduler, never()).start();
}
/**
* Tests {@link QuartzScheduler#init(sonia.scm.SCMContextProvider)} when the underlying scheduler throws an exception.
*
* @throws SchedulerException
*/
@Test
public void testInitException() throws SchedulerException
{
when(quartzScheduler.isStarted()).thenThrow(SchedulerException.class);
scheduler.init(null);
verify(quartzScheduler, never()).start();
}
/**
* Tests {@link QuartzScheduler#close()}.
*
* @throws IOException
* @throws SchedulerException
*/
@Test
public void testClose() throws IOException, SchedulerException
{
when(quartzScheduler.isStarted()).thenReturn(Boolean.TRUE);
scheduler.close();
verify(quartzScheduler).shutdown();
}
/**
* Tests {@link QuartzScheduler#close()} when the underlying scheduler is not running.
*
* @throws IOException
* @throws SchedulerException
*/
@Test
public void testCloseNotRunning() throws IOException, SchedulerException
{
when(quartzScheduler.isStarted()).thenReturn(Boolean.FALSE);
scheduler.close();
verify(quartzScheduler, never()).shutdown();
}
/**
* Tests {@link QuartzScheduler#close()} when the underlying scheduler throws an exception.
*
* @throws IOException
* @throws SchedulerException
*/
@Test
public void testCloseException() throws IOException, SchedulerException
{
when(quartzScheduler.isStarted()).thenThrow(SchedulerException.class);
scheduler.close();
verify(quartzScheduler, never()).shutdown();
}
public static class DummyRunnable implements Runnable {
@Override
public void run()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}

View File

@@ -0,0 +1,89 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.schedule;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
/**
* Unit tests for {@link QuartzTask}.
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
*/
@RunWith(MockitoJUnitRunner.class)
public class QuartzTaskTest {
@Mock
private org.quartz.Scheduler scheduler;
private final JobKey jobKey = new JobKey("sample");
private QuartzTask task;
@Before
public void setUp(){
task = new QuartzTask(scheduler, jobKey);
}
/**
* Tests {@link QuartzTask#cancel()}.
*
* @throws SchedulerException
*/
@Test
public void testCancel() throws SchedulerException
{
task.cancel();
verify(scheduler).deleteJob(jobKey);
}
/**
* Tests {@link QuartzTask#cancel()} when the scheduler throws an exception.
* @throws org.quartz.SchedulerException
*/
@Test(expected = RuntimeException.class)
public void testCancelWithException() throws SchedulerException
{
when(scheduler.deleteJob(jobKey)).thenThrow(SchedulerException.class);
task.cancel();
}
}