Skip to content

Commit 1f78456

Browse files
authored
Refactoring of tests to be compatible with the spock framework (apache#126)
1 parent 730bb5c commit 1f78456

12 files changed

+542
-522
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package grails.plugins.quartz
2+
3+
import grails.plugins.quartz.config.TriggersConfigBuilder
4+
5+
import org.quartz.CronTrigger
6+
import org.quartz.DailyTimeIntervalTrigger
7+
import org.quartz.DateBuilder
8+
import org.quartz.SimpleTrigger
9+
import org.quartz.TimeOfDay
10+
import org.quartz.Trigger
11+
import org.quartz.impl.triggers.DailyTimeIntervalTriggerImpl
12+
import spock.lang.Specification
13+
14+
/**
15+
* Tests for CustomTriggerFactoryBean
16+
*
17+
* @author Vitalii Samolovskikh aka Kefir
18+
*/
19+
class CustomTriggerFactoryBeanSpec extends Specification {
20+
21+
private static final String CRON_EXPRESSION = '0 15 6 * * ?'
22+
private static final TimeOfDay START_TIME = new TimeOfDay(10, 0)
23+
private static final TimeOfDay END_TIME = new TimeOfDay(11, 30)
24+
25+
void 'testFactory'() {
26+
setup:
27+
def builder = new TriggersConfigBuilder('TestJob', null)
28+
def closure = {
29+
simple name: 'simple', group: 'group', startDelay: 500, repeatInterval: 1000, repeatCount: 3
30+
cron name: 'cron', group: 'group', cronExpression: CRON_EXPRESSION
31+
custom name: 'custom', group: 'group', triggerClass: DailyTimeIntervalTriggerImpl,
32+
startTimeOfDay: START_TIME, endTimeOfDay: END_TIME,
33+
repeatIntervalUnit: DateBuilder.IntervalUnit.MINUTE, repeatInterval: 5
34+
}
35+
builder.build(closure)
36+
37+
Map<String, Trigger> triggers = [:]
38+
39+
builder.triggers.values().each {
40+
CustomTriggerFactoryBean factory = new CustomTriggerFactoryBean()
41+
factory.setTriggerClass(it.triggerClass)
42+
factory.setTriggerAttributes(it.triggerAttributes)
43+
factory.afterPropertiesSet()
44+
Trigger trigger = factory.getObject() as Trigger
45+
triggers.put(trigger.key.name, trigger)
46+
}
47+
48+
expect:
49+
assert triggers['simple'] instanceof SimpleTrigger
50+
SimpleTrigger simpleTrigger = triggers['simple'] as SimpleTrigger
51+
assert 'simple' == simpleTrigger.key.name
52+
assert 'group' == simpleTrigger.key.group
53+
assert 1000 == simpleTrigger.repeatInterval
54+
assert 3 == simpleTrigger.repeatCount
55+
56+
assert triggers['cron'] instanceof CronTrigger
57+
CronTrigger cronTrigger = triggers['cron'] as CronTrigger
58+
assert 'cron' == cronTrigger.key.name
59+
assert 'group' == cronTrigger.key.group
60+
assert CRON_EXPRESSION == cronTrigger.getCronExpression()
61+
62+
assert triggers['custom'] instanceof DailyTimeIntervalTrigger
63+
DailyTimeIntervalTrigger customTrigger = triggers['custom'] as DailyTimeIntervalTrigger
64+
assert 'custom' == customTrigger.key.name
65+
assert 'group' == customTrigger.key.group
66+
assert START_TIME == customTrigger.startTimeOfDay
67+
assert END_TIME == customTrigger.endTimeOfDay
68+
assert DateBuilder.IntervalUnit.MINUTE == customTrigger.repeatIntervalUnit
69+
assert 5 == customTrigger.repeatInterval
70+
}
71+
}

Diff for: src/test/groovy/grails/plugins/quartz/CustomTriggerFactoryBeanTests.groovy

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2011 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 grails.plugins.quartz
18+
19+
import spock.lang.Specification
20+
21+
class DefaultGrailsJobClassSpec extends Specification {
22+
protected GroovyClassLoader gcl = new GroovyClassLoader()
23+
24+
def cleanup() {
25+
gcl.clearCache()
26+
}
27+
28+
void 'default properties are set correctly'() {
29+
setup:
30+
def jobClass = gcl.parseClass('class TestJob { def execute(){} }')
31+
def grailsJobClass = new DefaultGrailsJobClass(jobClass)
32+
expect:
33+
assert 'GRAILS_JOBS' == grailsJobClass.group: "Wrong default group"
34+
assert grailsJobClass.sessionRequired: "Job should require Hibernate session by default"
35+
assert grailsJobClass.concurrent: "Job should be concurrent by default"
36+
}
37+
38+
void 'job class execute method works correctly'() {
39+
setup:
40+
boolean wasExecuted = false
41+
def testClosure = { wasExecuted = true }
42+
Class jobClass = gcl.parseClass("""
43+
class TestJob {
44+
static testClosure
45+
def execute() {
46+
testClosure.call()
47+
}
48+
}
49+
""".stripIndent())
50+
GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass)
51+
grailsJobClass.referenceInstance.testClosure = testClosure
52+
when:
53+
grailsJobClass.execute()
54+
then:
55+
assert wasExecuted: "Job wasn't executed"
56+
}
57+
58+
void 'session required parameter is handled correctly'() {
59+
setup:
60+
Class jobClass = gcl.parseClass("""
61+
class TestJob {
62+
static sessionRequired = false
63+
def execute() {}
64+
}
65+
""".stripIndent())
66+
GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass)
67+
expect:
68+
assert !grailsJobClass.sessionRequired: "Hibernate Session shouldn't be required"
69+
}
70+
71+
void 'concurrent parameter is handled correctly'() {
72+
setup:
73+
Class jobClass = gcl.parseClass("""
74+
class TestJob {
75+
static concurrent = false
76+
def execute() {}
77+
}
78+
""".stripIndent())
79+
GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass)
80+
expect:
81+
assert !grailsJobClass.concurrent: "Job class shouldn't be marked as concurrent"
82+
}
83+
84+
void 'group parameter is handled correctly'() {
85+
setup:
86+
Class jobClass = gcl.parseClass("""
87+
class TestJob {
88+
static group = 'myGroup'
89+
def execute() {}
90+
}
91+
""".stripIndent())
92+
GrailsJobClass grailsJobClass = new DefaultGrailsJobClass(jobClass)
93+
expect:
94+
'myGroup' == grailsJobClass.group
95+
}
96+
}

Diff for: src/test/groovy/grails/plugins/quartz/DefaultGrailsJobClassTests.groovy

-85
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2011 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 grails.plugins.quartz
18+
19+
import grails.core.ArtefactHandler
20+
import spock.lang.Specification
21+
22+
/**
23+
* Test case for Job artefact handler.
24+
*
25+
* @author Sergey Nebolsin
26+
* @since 0.2
27+
*/
28+
class JobArtefactHandlerSpec extends Specification {
29+
30+
private ArtefactHandler handler = new JobArtefactHandler()
31+
protected GroovyClassLoader gcl = new GroovyClassLoader()
32+
33+
void 'class with execute() method should be recognized as a Job class'() {
34+
setup:
35+
Class c = gcl.parseClass("class TestJob { def execute() { }}\n")
36+
expect:
37+
assert handler.isArtefact(c): "Class *Job which defines execute() method should be recognized as a Job class"
38+
}
39+
40+
void 'class with execute(param) method should be recognized as a Job class'() {
41+
setup:
42+
Class c = gcl.parseClass("class TestJob { def execute(param) { }}\n")
43+
expect:
44+
assert handler.isArtefact(c): "Class *Job which defines execute(param) method should be recognized as a Job class"
45+
}
46+
47+
void 'class with wrong name should not be recognized as a Job class'() {
48+
setup:
49+
Class c = gcl.parseClass("class TestController { def execute() { }}\n")
50+
expect:
51+
assert !handler.isArtefact(c): "Class which name doesn't end with 'Job' shouldn't be recognized as a Job class"
52+
}
53+
54+
void 'class without execute() method should not be recognized as a Job class'() {
55+
setup:
56+
Class c = gcl.parseClass("class TestJob { def execute1() { }}\n")
57+
expect:
58+
assert !handler.isArtefact(c): "Class which doesn't declare 'execute' method shouldn't be recognized as a Job class"
59+
}
60+
61+
}

0 commit comments

Comments
 (0)