From 9eaf1f6c4f21c1acd29ef3c14889876f5f0ac485 Mon Sep 17 00:00:00 2001 From: Bruce Hyslop Date: Fri, 19 Nov 2021 19:14:11 +1100 Subject: [PATCH] unit test for key generator --- .../cache/CustomCacheKeyGeneratorSpec.groovy | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/groovy/grails/plugin/cache/CustomCacheKeyGeneratorSpec.groovy diff --git a/src/test/groovy/grails/plugin/cache/CustomCacheKeyGeneratorSpec.groovy b/src/test/groovy/grails/plugin/cache/CustomCacheKeyGeneratorSpec.groovy new file mode 100644 index 00000000..a3703c19 --- /dev/null +++ b/src/test/groovy/grails/plugin/cache/CustomCacheKeyGeneratorSpec.groovy @@ -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 ] + } +}