Skip to content

Commit

Permalink
Refactoring of tests to be compatible with the spock framework (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernando88to authored Mar 29, 2024
1 parent 730bb5c commit 1f78456
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 522 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Trigger> 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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading

0 comments on commit 1f78456

Please sign in to comment.