From 5d2115a33b16a2a7323e264d42bb0d9c7e6323c4 Mon Sep 17 00:00:00 2001 From: Bruce Hyslop Date: Fri, 19 Nov 2021 19:14:11 +1100 Subject: [PATCH 1/2] 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 ] + } +} From 9fe25549060431452d8c3c7f9cd4b5b9be55d49d Mon Sep 17 00:00:00 2001 From: Bruce Hyslop Date: Fri, 19 Nov 2021 19:16:29 +1100 Subject: [PATCH 2/2] fixed formatting of groovy code causing bytecode compilation errors --- .../cache/CustomCacheKeyGenerator.groovy | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/ast/groovy/grails/plugin/cache/CustomCacheKeyGenerator.groovy b/src/ast/groovy/grails/plugin/cache/CustomCacheKeyGenerator.groovy index 97ce2f1e..c506ac5f 100644 --- a/src/ast/groovy/grails/plugin/cache/CustomCacheKeyGenerator.groovy +++ b/src/ast/groovy/grails/plugin/cache/CustomCacheKeyGenerator.groovy @@ -55,14 +55,9 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator { int hashCode() { final int prime = 31 int result = 1 - result = prime * result - + ((simpleKey == null) ? 0 : simpleKey.hashCode()) - result = prime * result - + ((targetClassName == null) ? 0 : targetClassName - .hashCode()) - result = prime * result - + ((targetMethodName == null) ? 0 : targetMethodName - .hashCode()) + result = prime * result + ((simpleKey == null) ? 0 : simpleKey.hashCode()) + result = prime * result + ((targetClassName == null) ? 0 : targetClassName.hashCode()) + result = prime * result + ((targetMethodName == null) ? 0 : targetMethodName.hashCode()) result = prime * result + targetObjectHashCode return result } @@ -140,14 +135,9 @@ class CustomCacheKeyGenerator implements KeyGenerator, GrailsCacheKeyGenerator { int hashCode() { final int prime = 31 int result = 1 - result = prime * result - + ((simpleKey == null) ? 0 : simpleKey.hashCode()) - result = prime * result - + ((targetClassName == null) ? 0 : targetClassName - .hashCode()) - result = prime * result - + ((targetMethodName == null) ? 0 : targetMethodName - .hashCode()) + result = prime * result + ((simpleKey == null) ? 0 : simpleKey.hashCode()) + result = prime * result + ((targetClassName == null) ? 0 : targetClassName.hashCode()) + result = prime * result + ((targetMethodName == null) ? 0 : targetMethodName.hashCode()) result = prime * result + targetObjectHashCode return result }