Skip to content

Commit

Permalink
unit test for key generator
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehyslop committed Nov 19, 2021
1 parent c442583 commit 9eaf1f6
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package grails.plugin.cache

import spock.lang.Specification
import spock.lang.Unroll

class CustomCacheKeyGeneratorSpec extends Specification {

void 'test matching keys'() {

given:
CustomCacheKeyGenerator keyGenerator = new CustomCacheKeyGenerator()

when:
Serializable key1 = keyGenerator.generate('TestService', 'method', 0, [ arg1: 1, arg2: 2 ])
Serializable key2 = keyGenerator.generate('TestService', 'method', 0, [ arg1: 1, arg2: 2 ])

then:
key1.hashCode() == key2.hashCode()
}

@Unroll('#className::#methodName(#params) should not match TestService::method([arg1: 1, arg2: 2])')
void 'test differing keys'() {

given:
CustomCacheKeyGenerator keyGenerator = new CustomCacheKeyGenerator()
Serializable key1 = keyGenerator.generate('TestService', 'method', 0, [ arg1: 1, arg2: 2 ])

when:
Serializable key2 = keyGenerator.generate(className, methodName, 0, params)

then:
key1.hashCode() != key2.hashCode()

where:
className | methodName | params
'TestService' | 'method' | [ arg1: 1, arg2: 3 ]
'TestService' | 'method' | [ arg1: 1, _arg2: 2 ]
'TestService' | '_method' | [ arg1: 1, arg2: 2 ]
'_TestService' | 'method' | [ arg1: 1, arg2: 2 ]
}
}

0 comments on commit 9eaf1f6

Please sign in to comment.