Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the allEntries, key, and condition arguments to CacheEvict.
- Loading branch information
1 parent
9602b02
commit 1e1c374
Showing
7 changed files
with
232 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/test/groovy/grails/plugin/cache/CacheEvictParseSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package grails.plugin.cache | ||
|
||
import org.grails.plugin.cache.GrailsCacheManager | ||
import org.springframework.cache.Cache | ||
import spock.lang.Specification | ||
|
||
class CacheEvictParseSpec extends Specification { | ||
|
||
void "test simple usage"() { | ||
given: | ||
GrailsCacheManager cacheManager = Mock(GrailsCacheManager) | ||
GrailsCacheKeyGenerator keyGenerator = Mock(GrailsCacheKeyGenerator) | ||
def cache = Mock(Cache) | ||
Class testService = new GroovyShell().evaluate(''' | ||
import grails.plugin.cache.* | ||
import groovy.transform.CompileStatic | ||
@CompileStatic | ||
class TestService { | ||
@CacheEvict('sum') | ||
def evict(String foo) { | ||
} | ||
} | ||
return TestService | ||
''') | ||
when: | ||
def instance = testService.newInstance() | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__grailsCacheManager" = cacheManager | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__customCacheKeyGenerator" = keyGenerator | ||
instance.evict("a") | ||
|
||
then: | ||
1 * keyGenerator.generate("TestService", "evict", _, [foo:"a"]) >> "a" | ||
1 * cacheManager.getCache("sum") >> cache | ||
1 * cache.evict("a") | ||
} | ||
|
||
void "test evict with key closure"() { | ||
given: | ||
GrailsCacheManager cacheManager = Mock(GrailsCacheManager) | ||
GrailsCacheKeyGenerator keyGenerator = Mock(GrailsCacheKeyGenerator) | ||
def cache = Mock(Cache) | ||
Class testService = new GroovyShell().evaluate(''' | ||
import grails.plugin.cache.* | ||
import groovy.transform.CompileStatic | ||
@CompileStatic | ||
class TestService { | ||
@CacheEvict(value = 'sum', key = { foo }) | ||
def evict(String foo) { | ||
} | ||
} | ||
return TestService | ||
''') | ||
when: | ||
def instance = testService.newInstance() | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__grailsCacheManager" = cacheManager | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__customCacheKeyGenerator" = keyGenerator | ||
instance.evict("a") | ||
|
||
then: | ||
1 * keyGenerator.generate("TestService", "evict", _, _ as Closure) >> "a" | ||
1 * cacheManager.getCache("sum") >> cache | ||
1 * cache.evict("a") | ||
} | ||
|
||
void "test evict all entries"() { | ||
given: | ||
GrailsCacheManager cacheManager = Mock(GrailsCacheManager) | ||
def cache = Mock(Cache) | ||
Class testService = new GroovyShell().evaluate(''' | ||
import grails.plugin.cache.* | ||
import groovy.transform.CompileStatic | ||
@CompileStatic | ||
class TestService { | ||
@CacheEvict(value = 'sum', allEntries = true) | ||
def evict(String foo) { | ||
} | ||
} | ||
return TestService | ||
''') | ||
when: | ||
def instance = testService.newInstance() | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__grailsCacheManager" = cacheManager | ||
instance.evict("a") | ||
|
||
then: | ||
1 * cacheManager.getCache("sum") >> cache | ||
1 * cache.clear() | ||
} | ||
|
||
void "test evict with condition that evaluates to false"() { | ||
given: | ||
GrailsCacheManager cacheManager = Mock(GrailsCacheManager) | ||
def cache = Mock(Cache) | ||
Class testService = new GroovyShell().evaluate(''' | ||
import grails.plugin.cache.* | ||
import groovy.transform.CompileStatic | ||
@CompileStatic | ||
class TestService { | ||
@CacheEvict(value = 'sum', allEntries = true, condition = { false }) | ||
def evict(String foo) { | ||
} | ||
} | ||
return TestService | ||
''') | ||
when: | ||
def instance = testService.newInstance() | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__grailsCacheManager" = cacheManager | ||
instance.evict("a") | ||
|
||
then: | ||
0 * cacheManager.getCache("sum") | ||
0 * cache.clear() | ||
} | ||
|
||
void "test evict with condition that evaluates to true"() { | ||
given: | ||
GrailsCacheManager cacheManager = Mock(GrailsCacheManager) | ||
def cache = Mock(Cache) | ||
Class testService = new GroovyShell().evaluate(''' | ||
import grails.plugin.cache.* | ||
import groovy.transform.CompileStatic | ||
@CompileStatic | ||
class TestService { | ||
@CacheEvict(value = 'sum', allEntries = true, condition = { true }) | ||
def evict(String foo) { | ||
} | ||
} | ||
return TestService | ||
''') | ||
when: | ||
def instance = testService.newInstance() | ||
instance.@"org_grails_plugin_cache_GrailsCacheManagerAware__grailsCacheManager" = cacheManager | ||
instance.evict("a") | ||
|
||
then: | ||
1 * cacheManager.getCache("sum") >> cache | ||
1 * cache.clear() | ||
} | ||
} |