diff --git a/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanSpec.groovy b/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanSpec.groovy new file mode 100644 index 00000000..8663fc32 --- /dev/null +++ b/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanSpec.groovy @@ -0,0 +1,71 @@ +package grails.plugins.quartz + +import grails.plugins.quartz.config.TriggersConfigBuilder + +import org.quartz.CronTrigger +import org.quartz.DailyTimeIntervalTrigger +import org.quartz.DateBuilder +import org.quartz.SimpleTrigger +import org.quartz.TimeOfDay +import org.quartz.Trigger +import org.quartz.impl.triggers.DailyTimeIntervalTriggerImpl +import spock.lang.Specification + +/** + * Tests for CustomTriggerFactoryBean + * + * @author Vitalii Samolovskikh aka Kefir + */ +class CustomTriggerFactoryBeanSpec extends Specification { + + private static final String CRON_EXPRESSION = '0 15 6 * * ?' + private static final TimeOfDay START_TIME = new TimeOfDay(10, 0) + private static final TimeOfDay END_TIME = new TimeOfDay(11, 30) + + void 'testFactory'() { + setup: + def builder = new TriggersConfigBuilder('TestJob', null) + def closure = { + simple name: 'simple', group: 'group', startDelay: 500, repeatInterval: 1000, repeatCount: 3 + cron name: 'cron', group: 'group', cronExpression: CRON_EXPRESSION + custom name: 'custom', group: 'group', triggerClass: DailyTimeIntervalTriggerImpl, + startTimeOfDay: START_TIME, endTimeOfDay: END_TIME, + repeatIntervalUnit: DateBuilder.IntervalUnit.MINUTE, repeatInterval: 5 + } + builder.build(closure) + + Map triggers = [:] + + builder.triggers.values().each { + CustomTriggerFactoryBean factory = new CustomTriggerFactoryBean() + factory.setTriggerClass(it.triggerClass) + factory.setTriggerAttributes(it.triggerAttributes) + factory.afterPropertiesSet() + Trigger trigger = factory.getObject() as Trigger + triggers.put(trigger.key.name, trigger) + } + + expect: + assert triggers['simple'] instanceof SimpleTrigger + SimpleTrigger simpleTrigger = triggers['simple'] as SimpleTrigger + assert 'simple' == simpleTrigger.key.name + assert 'group' == simpleTrigger.key.group + assert 1000 == simpleTrigger.repeatInterval + assert 3 == simpleTrigger.repeatCount + + assert triggers['cron'] instanceof CronTrigger + CronTrigger cronTrigger = triggers['cron'] as CronTrigger + assert 'cron' == cronTrigger.key.name + assert 'group' == cronTrigger.key.group + assert CRON_EXPRESSION == cronTrigger.getCronExpression() + + assert triggers['custom'] instanceof DailyTimeIntervalTrigger + DailyTimeIntervalTrigger customTrigger = triggers['custom'] as DailyTimeIntervalTrigger + assert 'custom' == customTrigger.key.name + assert 'group' == customTrigger.key.group + assert START_TIME == customTrigger.startTimeOfDay + assert END_TIME == customTrigger.endTimeOfDay + assert DateBuilder.IntervalUnit.MINUTE == customTrigger.repeatIntervalUnit + assert 5 == customTrigger.repeatInterval + } +} diff --git a/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanTests.groovy b/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanTests.groovy deleted file mode 100644 index 25810ec6..00000000 --- a/src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanTests.groovy +++ /dev/null @@ -1,69 +0,0 @@ -package grails.plugins.quartz - -import grails.plugins.quartz.config.TriggersConfigBuilder - -import org.junit.Test -import org.quartz.CronTrigger -import org.quartz.DailyTimeIntervalTrigger -import org.quartz.DateBuilder -import org.quartz.SimpleTrigger -import org.quartz.TimeOfDay -import org.quartz.Trigger -import org.quartz.impl.triggers.DailyTimeIntervalTriggerImpl - -/** - * Tests for CustomTriggerFactoryBean - * - * @author Vitalii Samolovskikh aka Kefir - */ -class CustomTriggerFactoryBeanTests { - private static final String CRON_EXPRESSION = '0 15 6 * * ?' - private static final TimeOfDay START_TIME = new TimeOfDay(10, 0) - private static final TimeOfDay END_TIME = new TimeOfDay(11, 30) - - @Test - void testFactory() { - def builder = new TriggersConfigBuilder('TestJob', null) - def closure = { - simple name: 'simple', group:'group', startDelay: 500, repeatInterval: 1000, repeatCount: 3 - cron name: 'cron', group: 'group', cronExpression: CRON_EXPRESSION - custom name: 'custom', group: 'group', triggerClass: DailyTimeIntervalTriggerImpl, - startTimeOfDay: START_TIME, endTimeOfDay: END_TIME, - repeatIntervalUnit: DateBuilder.IntervalUnit.MINUTE, repeatInterval: 5 - } - builder.build(closure) - - Map triggers = [:] - - builder.triggers.values().each { - CustomTriggerFactoryBean factory = new CustomTriggerFactoryBean() - factory.setTriggerClass(it.triggerClass) - factory.setTriggerAttributes(it.triggerAttributes) - factory.afterPropertiesSet() - Trigger trigger = factory.getObject() as Trigger - triggers.put(trigger.key.name, trigger) - } - - assert triggers['simple'] instanceof SimpleTrigger - SimpleTrigger simpleTrigger = triggers['simple'] as SimpleTrigger - assert 'simple' == simpleTrigger.key.name - assert 'group' == simpleTrigger.key.group - assert 1000 == simpleTrigger.repeatInterval - assert 3 == simpleTrigger.repeatCount - - assert triggers['cron'] instanceof CronTrigger - CronTrigger cronTrigger = triggers['cron'] as CronTrigger - assert 'cron' == cronTrigger.key.name - assert 'group' == cronTrigger.key.group - assert CRON_EXPRESSION == cronTrigger.getCronExpression() - - assert triggers['custom'] instanceof DailyTimeIntervalTrigger - DailyTimeIntervalTrigger customTrigger = triggers['custom'] as DailyTimeIntervalTrigger - assert 'custom' == customTrigger.key.name - assert 'group' == customTrigger.key.group - assert START_TIME == customTrigger.startTimeOfDay - assert END_TIME == customTrigger.endTimeOfDay - assert DateBuilder.IntervalUnit.MINUTE == customTrigger.repeatIntervalUnit - assert 5 == customTrigger.repeatInterval - } -} diff --git a/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassSpec.groovy b/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassSpec.groovy new file mode 100644 index 00000000..9a6d3901 --- /dev/null +++ b/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassSpec.groovy @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2011 the original author or authors. + * + * 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 grails.plugins.quartz + +import spock.lang.Specification + +class DefaultGrailsJobClassSpec extends Specification { + protected GroovyClassLoader gcl = new GroovyClassLoader() + + def cleanup() { + gcl.clearCache() + } + + void 'default properties are set correctly'() { + setup: + def jobClass = gcl.parseClass('class TestJob { def execute(){} }') + def grailsJobClass = new DefaultGrailsJobClass(jobClass) + expect: + assert 'GRAILS_JOBS' == grailsJobClass.group: "Wrong default group" + assert grailsJobClass.sessionRequired: "Job should require Hibernate session by default" + assert grailsJobClass.concurrent: "Job should be concurrent by default" + } + + void 'job class execute method works correctly'() { + setup: + boolean wasExecuted = false + def testClosure = { wasExecuted = true } + Class jobClass = gcl.parseClass(""" + class TestJob { + static testClosure + def execute() { + testClosure.call() + } + } + """.stripIndent()) + GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) + grailsJobClass.referenceInstance.testClosure = testClosure + when: + grailsJobClass.execute() + then: + assert wasExecuted: "Job wasn't executed" + } + + void 'session required parameter is handled correctly'() { + setup: + Class jobClass = gcl.parseClass(""" + class TestJob { + static sessionRequired = false + def execute() {} + } + """.stripIndent()) + GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) + expect: + assert !grailsJobClass.sessionRequired: "Hibernate Session shouldn't be required" + } + + void 'concurrent parameter is handled correctly'() { + setup: + Class jobClass = gcl.parseClass(""" + class TestJob { + static concurrent = false + def execute() {} + } + """.stripIndent()) + GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) + expect: + assert !grailsJobClass.concurrent: "Job class shouldn't be marked as concurrent" + } + + void 'group parameter is handled correctly'() { + setup: + Class jobClass = gcl.parseClass(""" + class TestJob { + static group = 'myGroup' + def execute() {} + } + """.stripIndent()) + GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) + expect: + 'myGroup' == grailsJobClass.group + } +} diff --git a/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassTests.groovy b/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassTests.groovy deleted file mode 100644 index 24359507..00000000 --- a/src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassTests.groovy +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2011 the original author or authors. - * - * 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 grails.plugins.quartz - -class DefaultGrailsJobClassTests extends GroovyTestCase { - protected GroovyClassLoader gcl = new GroovyClassLoader() - - protected void tearDown() { - super.tearDown() - gcl.clearCache() - } - - void testDefaultProperties() { - def jobClass = gcl.parseClass('class TestJob { def execute(){} }') - def grailsJobClass = new DefaultGrailsJobClass(jobClass) - - assertEquals "Wrong default group", 'GRAILS_JOBS', grailsJobClass.group - assertTrue "Job should require Hibernate session by default", grailsJobClass.sessionRequired - assertTrue "Job should be concurrent by default", grailsJobClass.concurrent - } - - void testJobClassExecute() { - boolean wasExecuted = false - def testClosure = { wasExecuted = true } - Class jobClass = gcl.parseClass(""" - class TestJob { - static testClosure - def execute() { - testClosure.call() - } - } - """.stripIndent()) - GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) - grailsJobClass.referenceInstance.testClosure = testClosure - grailsJobClass.execute() - assertTrue "Job wasn't executed", wasExecuted - } - - void testSessionRequiredParameter() { - Class jobClass = gcl.parseClass(""" - class TestJob { - static sessionRequired = false - def execute() {} - } - """.stripIndent()) - GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) - assertFalse "Hibernate Session shouldn't be required", grailsJobClass.sessionRequired - } - - void testConcurrentParameter() { - Class jobClass = gcl.parseClass(""" - class TestJob { - static concurrent = false - def execute() {} - } - """.stripIndent()) - GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) - assertFalse "Job class shouldn't be marked as concurrent", grailsJobClass.concurrent - } - - void testGroupParameter() { - Class jobClass = gcl.parseClass(""" - class TestJob { - static group = 'myGroup' - def execute() {} - } - """.stripIndent()) - GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass) - assertEquals 'myGroup', grailsJobClass.group - } -} diff --git a/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerSpec.groovy b/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerSpec.groovy new file mode 100644 index 00000000..fbb38363 --- /dev/null +++ b/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerSpec.groovy @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2011 the original author or authors. + * + * 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 grails.plugins.quartz + +import grails.core.ArtefactHandler +import spock.lang.Specification + +/** + * Test case for Job artefact handler. + * + * @author Sergey Nebolsin + * @since 0.2 + */ +class JobArtefactHandlerSpec extends Specification { + + private ArtefactHandler handler = new JobArtefactHandler() + protected GroovyClassLoader gcl = new GroovyClassLoader() + + void 'class with execute() method should be recognized as a Job class'() { + setup: + Class c = gcl.parseClass("class TestJob { def execute() { }}\n") + expect: + assert handler.isArtefact(c): "Class *Job which defines execute() method should be recognized as a Job class" + } + + void 'class with execute(param) method should be recognized as a Job class'() { + setup: + Class c = gcl.parseClass("class TestJob { def execute(param) { }}\n") + expect: + assert handler.isArtefact(c): "Class *Job which defines execute(param) method should be recognized as a Job class" + } + + void 'class with wrong name should not be recognized as a Job class'() { + setup: + Class c = gcl.parseClass("class TestController { def execute() { }}\n") + expect: + assert !handler.isArtefact(c): "Class which name doesn't end with 'Job' shouldn't be recognized as a Job class" + } + + void 'class without execute() method should not be recognized as a Job class'() { + setup: + Class c = gcl.parseClass("class TestJob { def execute1() { }}\n") + expect: + assert !handler.isArtefact(c): "Class which doesn't declare 'execute' method shouldn't be recognized as a Job class" + } + +} diff --git a/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerTests.groovy b/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerTests.groovy deleted file mode 100644 index 47e2da52..00000000 --- a/src/test/groovy/grails/plugins/quartz/JobArtefactHandlerTests.groovy +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2011 the original author or authors. - * - * 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 grails.plugins.quartz - -import grails.core.ArtefactHandler - -/** - * Test case for Job artefact handler. - * - * @author Sergey Nebolsin - * @since 0.2 - */ -class JobArtefactHandlerTests extends GroovyTestCase { - private ArtefactHandler handler = new JobArtefactHandler() - protected GroovyClassLoader gcl = new GroovyClassLoader() - - void testJobClassWithExecuteMethod() { - Class c = gcl.parseClass("class TestJob { def execute() { }}\n") - assertTrue "Class *Job which defines execute() method should be recognized as a Job class", handler.isArtefact(c) - } - - void testJobClassWithExecuteMethodWithParam() { - Class c = gcl.parseClass("class TestJob { def execute(param) { }}\n") - assertTrue "Class *Job which defines execute(param) method should be recognized as a Job class", handler.isArtefact(c) - } - - void testJobClassWithWrongName() { - Class c = gcl.parseClass("class TestController { def execute() { }}\n") - assertFalse "Class which name doesn't end with 'Job' shouldn't be recognized as a Job class", handler.isArtefact(c) - } - - void testJobClassWithoutExecuteMethod() { - Class c = gcl.parseClass("class TestJob { def execute1() { }}\n") - assertFalse "Class which doesn't declare 'execute' method shouldn't be recognized as a Job class", handler.isArtefact(c) - } -} diff --git a/src/test/groovy/grails/plugins/quartz/JobDescriptorTests.groovy b/src/test/groovy/grails/plugins/quartz/JobDescriptorSpec.groovy similarity index 53% rename from src/test/groovy/grails/plugins/quartz/JobDescriptorTests.groovy rename to src/test/groovy/grails/plugins/quartz/JobDescriptorSpec.groovy index de4abfd4..fc2c1775 100644 --- a/src/test/groovy/grails/plugins/quartz/JobDescriptorTests.groovy +++ b/src/test/groovy/grails/plugins/quartz/JobDescriptorSpec.groovy @@ -1,55 +1,45 @@ package grails.plugins.quartz -import org.junit.After -import org.junit.Before -import org.junit.Test -import org.quartz.JobBuilder -import org.quartz.JobDetail -import org.quartz.JobKey -import org.quartz.Scheduler -import org.quartz.SimpleScheduleBuilder -import org.quartz.Trigger -import org.quartz.TriggerBuilder -import org.quartz.TriggerKey +import org.quartz.* import org.quartz.impl.StdSchedulerFactory +import spock.lang.Specification /** * Unit tests for JobDescriptor. * * @author Vitalii Samolovskikh aka Kefir */ -class JobDescriptorTests { +class JobDescriptorSpec extends Specification { + private Scheduler scheduler private JobDetail job private Trigger trigger - @Before - void prepare(){ - scheduler = StdSchedulerFactory.getDefaultScheduler() + def setup() { + scheduler = StdSchedulerFactory.getDefaultScheduler() scheduler.start() - job = JobBuilder.newJob(TestQuartzJob).withIdentity(new JobKey("job", "group")).build() trigger = TriggerBuilder.newTrigger() .withIdentity(new TriggerKey("trigger", "group")) .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(2).repeatForever()) .startNow() .build() - scheduler.scheduleJob(job, trigger) } - @After - void dispose(){ + def cleanup() { scheduler.shutdown() } - @Test - void testBuild(){ - JobDescriptor descriptor = JobDescriptor.build(job, scheduler) - assert descriptor.name == 'job' - assert descriptor.group == 'group' - assert descriptor.triggerDescriptors.size() == 1 - assert descriptor.triggerDescriptors[0].name == 'trigger' + void 'JobDescriptor builds correctly from a job and scheduler'() { + when: + JobDescriptor descriptor = JobDescriptor.build(job, scheduler) + then: + descriptor.name == 'job' + descriptor.group == 'group' + descriptor.triggerDescriptors.size() == 1 + descriptor.triggerDescriptors[0].name == 'trigger' } + } diff --git a/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanSpec.groovy b/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanSpec.groovy new file mode 100644 index 00000000..fc149698 --- /dev/null +++ b/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanSpec.groovy @@ -0,0 +1,118 @@ +package grails.plugins.quartz + +import grails.core.GrailsApplication +import org.quartz.JobDetail +import org.quartz.JobKey +import org.springframework.beans.BeanWrapper +import spock.lang.Specification + +/** + * Tests for the JobDetailFactoryBean + * + * @author Vitalii Samolovskikh aka Kefir + */ +class JobDetailFactoryBeanSpec extends Specification { + + private static final String JOB_NAME = 'jobName' + private static final String JOB_GROUP = 'jobGroup' + private static final String JOB_DESCRIPTION = 'The job description' + JobDetailFactoryBean factory = new JobDetailFactoryBean() + + + void 'testFactory1'() { + setup: + factory.jobClass = new GrailsJobClassMock( + [ + fullName : JOB_NAME, + group : JOB_GROUP, + concurrent : true, + durability : true, + sessionRequired : true, + requestsRecovery: true, + description : JOB_DESCRIPTION + ] + ) + factory.afterPropertiesSet() + when: + JobDetail jobDetail = factory.object + then: + new JobKey(JOB_NAME, JOB_GROUP) == jobDetail.key + JOB_NAME == jobDetail.getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER) + jobDetail.durable + !jobDetail.concurrentExectionDisallowed + !jobDetail.persistJobDataAfterExecution + jobDetail.requestsRecovery() + JOB_DESCRIPTION == jobDetail.description + } + + void 'testFactory2'() { + setup: + factory.jobClass = new GrailsJobClassMock( + [ + fullName : JOB_NAME, + group : JOB_GROUP, + concurrent : false, + durability : false, + sessionRequired : false, + requestsRecovery: false + ] + ) + factory.afterPropertiesSet() + when: + JobDetail jobDetail = factory.object + then: + new JobKey(JOB_NAME, JOB_GROUP) == jobDetail.key + JOB_NAME == jobDetail.getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER) + !jobDetail.durable + jobDetail.concurrentExectionDisallowed + jobDetail.persistJobDataAfterExecution + !jobDetail.requestsRecovery() + jobDetail.description == null + } +} + +class GrailsJobClassMock implements GrailsJobClass { + String group + String fullName + boolean concurrent + boolean jobEnabled + boolean durability + boolean sessionRequired + boolean requestsRecovery + String description + + void execute() {} + Map getTriggers() {} + boolean byName() { false } + boolean byType() { false } + boolean getAvailable() { false } + boolean isAbstract() { false } + boolean isEnabled() { true } + GrailsApplication getGrailsApplication() {} + + @Override + grails.core.GrailsApplication getApplication() { + return null + } + + Object getPropertyValue(String name) {} + boolean hasProperty(String name) { false } + Object newInstance() {} + String getName() {} + String getShortName() {} + String getPropertyName() {} + String getLogicalPropertyName() {} + String getNaturalName() {} + String getPackageName() {} + Class getClazz() {} + BeanWrapper getReference() {} + Object getReferenceInstance() {} + def T getPropertyValue(String name, Class type) {} + + @Override + String getPluginName() { + return null + } + + void setGrailsApplication(GrailsApplication grailsApplication) {} +} diff --git a/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanTests.groovy b/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanTests.groovy deleted file mode 100644 index 21c0e444..00000000 --- a/src/test/groovy/grails/plugins/quartz/JobDetailFactoryBeanTests.groovy +++ /dev/null @@ -1,117 +0,0 @@ -package grails.plugins.quartz - -import static junit.framework.Assert.assertEquals -import static junit.framework.Assert.assertFalse -import static junit.framework.Assert.assertNull -import static junit.framework.Assert.assertTrue - -import grails.core.GrailsApplication -import org.junit.Test -import org.quartz.JobDetail -import org.quartz.JobKey -import org.springframework.beans.BeanWrapper - -/** - * Tests for the JobDetailFactoryBean - * - * @author Vitalii Samolovskikh aka Kefir - */ -class JobDetailFactoryBeanTests { - private static final String JOB_NAME = 'jobName' - private static final String JOB_GROUP = 'jobGroup' - private static final String JOB_DESCRIPTION = 'The job description' - JobDetailFactoryBean factory = new JobDetailFactoryBean() - - @Test - void testFactory1(){ - factory.jobClass = new GrailsJobClassMock( - [ - fullName:JOB_NAME, - group:JOB_GROUP, - concurrent:true, - durability:true, - sessionRequired:true, - requestsRecovery:true, - description: JOB_DESCRIPTION - ] - ) - factory.afterPropertiesSet() - JobDetail jobDetail = factory.object - assertEquals(new JobKey(JOB_NAME, JOB_GROUP), jobDetail.key) - assertEquals(JOB_NAME, jobDetail.getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER)) - assertTrue(jobDetail.durable) - assertFalse(jobDetail.concurrentExectionDisallowed) - assertFalse(jobDetail.persistJobDataAfterExecution) - assertTrue(jobDetail.requestsRecovery()) - assertEquals(JOB_DESCRIPTION, jobDetail.description) - } - - @Test - void testFactory2(){ - factory.jobClass = new GrailsJobClassMock( - [ - fullName:JOB_NAME, - group:JOB_GROUP, - concurrent:false, - durability:false, - sessionRequired:false, - requestsRecovery:false - ] - ) - factory.afterPropertiesSet() - JobDetail jobDetail = factory.object - assertEquals(new JobKey(JOB_NAME, JOB_GROUP), jobDetail.key) - assertEquals(JOB_NAME, jobDetail.getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER)) - assertFalse(jobDetail.durable) - assertTrue(jobDetail.concurrentExectionDisallowed) - assertTrue(jobDetail.persistJobDataAfterExecution) - assertFalse(jobDetail.requestsRecovery()) - assertNull(jobDetail.description) - } -} - -class GrailsJobClassMock implements GrailsJobClass { - String group - String fullName - boolean concurrent - boolean jobEnabled - boolean durability - boolean sessionRequired - boolean requestsRecovery - String description - - void execute() {} - Map getTriggers() {} - boolean byName() { false } - boolean byType() { false } - boolean getAvailable() { false } - boolean isAbstract() { false } - boolean isEnabled() { true } - GrailsApplication getGrailsApplication() {} - - @Override - grails.core.GrailsApplication getApplication() { - return null - } - - Object getPropertyValue(String name) {} - boolean hasProperty(String name) { false } - Object newInstance() {} - String getName() {} - String getShortName() {} - String getPropertyName() {} - String getLogicalPropertyName() {} - String getNaturalName() {} - String getPackageName() {} - Class getClazz() {} - BeanWrapper getReference() {} - Object getReferenceInstance() {} - def T getPropertyValue(String name, Class type) {} - - @Override - String getPluginName() { - return null - } - - void setGrailsApplication(GrailsApplication grailsApplication) {} -} diff --git a/src/test/groovy/grails/plugins/quartz/TriggerDescriptorTests.groovy b/src/test/groovy/grails/plugins/quartz/TriggerDescriptorSpec.groovy similarity index 68% rename from src/test/groovy/grails/plugins/quartz/TriggerDescriptorTests.groovy rename to src/test/groovy/grails/plugins/quartz/TriggerDescriptorSpec.groovy index 6adc26b4..39266f5c 100644 --- a/src/test/groovy/grails/plugins/quartz/TriggerDescriptorTests.groovy +++ b/src/test/groovy/grails/plugins/quartz/TriggerDescriptorSpec.groovy @@ -1,8 +1,5 @@ package grails.plugins.quartz -import org.junit.After -import org.junit.Before -import org.junit.Test import org.quartz.JobBuilder import org.quartz.JobDetail import org.quartz.JobKey @@ -12,19 +9,20 @@ import org.quartz.Trigger import org.quartz.TriggerBuilder import org.quartz.TriggerKey import org.quartz.impl.StdSchedulerFactory +import spock.lang.Specification /** * Unit tests for TriggerDescriptor * * @author Vitalii Samolovskikh aka Kefir */ -class TriggerDescriptorTests { +class TriggerDescriptorSpec extends Specification { + private Scheduler scheduler private JobDetail job private Trigger trigger - @Before - void prepare(){ + def setup() { scheduler = StdSchedulerFactory.getDefaultScheduler() scheduler.start() @@ -39,17 +37,18 @@ class TriggerDescriptorTests { scheduler.scheduleJob(job, trigger) } - @After - void dispose(){ + def cleanup() { scheduler.shutdown() } - @Test - void testBuild(){ - TriggerDescriptor descriptor = - TriggerDescriptor.build(JobDescriptor.build(job, scheduler), trigger, scheduler) - assert descriptor.name == 'trigger' - assert descriptor.group == 'group' - assert descriptor.state == Trigger.TriggerState.NORMAL + + void 'build TriggerDescriptor correctly'() { + when: + TriggerDescriptor descriptor = + TriggerDescriptor.build(JobDescriptor.build(job, scheduler), trigger, scheduler) + then: + descriptor.name == 'trigger' + descriptor.group == 'group' + descriptor.state == Trigger.TriggerState.NORMAL } } diff --git a/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderSpec.groovy b/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderSpec.groovy new file mode 100644 index 00000000..c73f2901 --- /dev/null +++ b/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderSpec.groovy @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2011 the original author or authors. + * + * 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 grails.plugins.quartz.config + +import grails.plugins.quartz.CustomTriggerFactoryBean +import grails.plugins.quartz.GrailsJobClassConstants as Constants +import spock.lang.Specification + +/** + * Create 10 triggers with different attributes. + * + * @author Sergey Nebolsin (nebolsin@gmail.com) + */ +class TriggersConfigBuilderSpec extends Specification { + + + void 'testConfigBuilder'() { + setup: + def builder = new TriggersConfigBuilder('TestJob', null) + def closure = { + simple() + simple timeout: 1000 + simple startDelay: 500 + simple startDelay: 500, timeout: 1000 + simple startDelay: 500, timeout: 1000, repeatCount: 3 + simple name: 'everySecond', timeout: 1000 + cron() + cron cronExpression: '0 15 6 * * ?' + cron name: 'myTrigger', cronExpression: '0 15 6 * * ?' + simple startDelay: 500, timeout: 1000, repeatCount: 0 + } + builder.build(closure) + expect: + assert 10 == builder.triggers.size(): 'Invalid triggers count' + when: 'TestJob0' + def triggerName = 'TestJob0' + then: 'Verify attributes for TestJob0' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: Constants.DEFAULT_START_DELAY, + repeatInterval: Constants.DEFAULT_REPEAT_INTERVAL, + repeatCount: Constants.DEFAULT_REPEAT_COUNT, + ), builder.triggers[triggerName].triggerAttributes) + when: 'TestJob1' + triggerName = 'TestJob1' + then: 'Verify attributes for TestJob1' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: Constants.DEFAULT_START_DELAY, + repeatInterval: 1000, + repeatCount: Constants.DEFAULT_REPEAT_COUNT, + ), builder.triggers[triggerName].triggerAttributes) + when: 'TestJob2' + triggerName = 'TestJob2' + then: 'Verify attributes for TestJob2' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: 500, + repeatInterval: Constants.DEFAULT_REPEAT_INTERVAL, + repeatCount: Constants.DEFAULT_REPEAT_COUNT, + ), builder.triggers[triggerName].triggerAttributes) + when: 'TestJob3' + triggerName = 'TestJob3' + then: 'Verify attributes for TestJob3' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: 500, + repeatInterval: 1000, + repeatCount: Constants.DEFAULT_REPEAT_COUNT, + ), builder.triggers[triggerName].triggerAttributes) + when: 'TestJob4' + triggerName = 'TestJob4' + then: 'Verify attributes for TestJob4' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: 500, + repeatInterval: 1000, + repeatCount: 3, + ), builder.triggers[triggerName].triggerAttributes) + when: 'everySecond' + triggerName = 'everySecond' + then: 'Verify attribute everySecond' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: Constants.DEFAULT_START_DELAY, + repeatInterval: 1000, + repeatCount: Constants.DEFAULT_REPEAT_COUNT, + ), builder.triggers[triggerName].triggerAttributes + ) + when: 'TestJob5' + triggerName = 'TestJob5' + then: 'Verify attributes TestJob5' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: Constants.DEFAULT_START_DELAY, + cronExpression: Constants.DEFAULT_CRON_EXPRESSION, + ), builder.triggers[triggerName].triggerAttributes + ) + when: 'TestJob6' + triggerName = 'TestJob6' + then: 'Verify attributes TestJob6' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + cronExpression: '0 15 6 * * ?', + startDelay: Constants.DEFAULT_START_DELAY, + ), builder.triggers[triggerName].triggerAttributes) + when: 'myTrigger' + triggerName = 'myTrigger' + then: 'Verify attribute myTrigger' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: Constants.DEFAULT_START_DELAY, + cronExpression: '0 15 6 * * ?', + ), builder.triggers[triggerName].triggerAttributes) + when: 'TestJob7' + triggerName = 'TestJob7' + then: 'Verify attribute TestJob7' + assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean + assertPropertiesEquals(new Expando( + name: triggerName, + group: Constants.DEFAULT_TRIGGERS_GROUP, + startDelay: 500, + repeatInterval: 1000, + repeatCount: 0, + ), builder.triggers[triggerName].triggerAttributes) + } + + private static void assertPropertiesEquals(expected, actual) { + expected.properties.each { entry -> + assert actual[entry.key] == entry.value, "Unexpected value for property: ${entry.key}" + } + assert actual.size() == expected.properties?.size(), 'Different number of properties' + } +} diff --git a/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderTests.groovy b/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderTests.groovy deleted file mode 100644 index eb895eb2..00000000 --- a/src/test/groovy/grails/plugins/quartz/config/TriggersConfigBuilderTests.groovy +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (c) 2011 the original author or authors. - * - * 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 grails.plugins.quartz.config - -import grails.plugins.quartz.CustomTriggerFactoryBean -import grails.plugins.quartz.GrailsJobClassConstants as Constants - -/** - * Create 10 triggers with different attributes. - * - * @author Sergey Nebolsin (nebolsin@gmail.com) - */ -class TriggersConfigBuilderTests extends GroovyTestCase { - void testConfigBuilder() { - def builder = new TriggersConfigBuilder('TestJob', null) - def closure = { - simple() - simple timeout:1000 - simple startDelay:500 - simple startDelay:500, timeout: 1000 - simple startDelay:500, timeout: 1000, repeatCount: 3 - simple name: 'everySecond', timeout:1000 - cron() - cron cronExpression:'0 15 6 * * ?' - cron name: 'myTrigger', cronExpression:'0 15 6 * * ?' - simple startDelay:500, timeout: 1000, repeatCount: 0 - } - builder.build(closure) - - assertEquals 'Invalid triggers count', 10, builder.triggers.size() - - def triggerName = 'TestJob0' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: Constants.DEFAULT_START_DELAY, - repeatInterval: Constants.DEFAULT_REPEAT_INTERVAL, - repeatCount: Constants.DEFAULT_REPEAT_COUNT, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob1' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: Constants.DEFAULT_START_DELAY, - repeatInterval: 1000, - repeatCount: Constants.DEFAULT_REPEAT_COUNT, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob2' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: 500, - repeatInterval: Constants.DEFAULT_REPEAT_INTERVAL, - repeatCount: Constants.DEFAULT_REPEAT_COUNT, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob3' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: 500, - repeatInterval: 1000, - repeatCount: Constants.DEFAULT_REPEAT_COUNT, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob4' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: 500, - repeatInterval: 1000, - repeatCount: 3, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'everySecond' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: Constants.DEFAULT_START_DELAY, - repeatInterval: 1000, - repeatCount: Constants.DEFAULT_REPEAT_COUNT, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob5' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay:Constants.DEFAULT_START_DELAY, - cronExpression: Constants.DEFAULT_CRON_EXPRESSION, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob6' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - cronExpression: '0 15 6 * * ?', - startDelay: Constants.DEFAULT_START_DELAY, - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'myTrigger' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: Constants.DEFAULT_START_DELAY, - cronExpression: '0 15 6 * * ?', - ), builder.triggers[triggerName].triggerAttributes - ) - - triggerName = 'TestJob7' - assert builder.triggers[triggerName]?.clazz == CustomTriggerFactoryBean - assertPropertiesEquals(new Expando( - name:triggerName, - group: Constants.DEFAULT_TRIGGERS_GROUP, - startDelay: 500, - repeatInterval: 1000, - repeatCount: 0, - ), builder.triggers[triggerName].triggerAttributes - ) - } - - private static assertPropertiesEquals(expected, actual) { - expected.properties.each { entry -> - assert actual[entry.key] == entry.value, "Unexpected value for property: ${entry.key}" - } - assert actual.size() == expected.properties?.size(), 'Different number of properties' - } -}