16
16
17
17
package org .springframework .security .acls .jdbc ;
18
18
19
+ import java .util .ArrayList ;
19
20
import java .util .Arrays ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .UUID ;
23
24
24
25
import javax .sql .DataSource ;
25
26
26
- import net .sf .ehcache .Cache ;
27
- import net .sf .ehcache .CacheManager ;
28
- import net .sf .ehcache .Ehcache ;
29
27
import org .junit .jupiter .api .AfterAll ;
30
28
import org .junit .jupiter .api .AfterEach ;
31
29
import org .junit .jupiter .api .BeforeAll ;
32
30
import org .junit .jupiter .api .BeforeEach ;
33
31
import org .junit .jupiter .api .Test ;
34
32
33
+ import org .springframework .cache .Cache ;
34
+ import org .springframework .cache .CacheManager ;
35
+ import org .springframework .cache .concurrent .ConcurrentMapCache ;
35
36
import org .springframework .jdbc .core .JdbcTemplate ;
36
37
import org .springframework .security .acls .TargetObject ;
37
38
import org .springframework .security .acls .TargetObjectWithUUID ;
41
42
import org .springframework .security .acls .domain .ConsoleAuditLogger ;
42
43
import org .springframework .security .acls .domain .DefaultPermissionFactory ;
43
44
import org .springframework .security .acls .domain .DefaultPermissionGrantingStrategy ;
44
- import org .springframework .security .acls .domain .EhCacheBasedAclCache ;
45
45
import org .springframework .security .acls .domain .GrantedAuthoritySid ;
46
46
import org .springframework .security .acls .domain .ObjectIdentityImpl ;
47
47
import org .springframework .security .acls .domain .PrincipalSid ;
48
+ import org .springframework .security .acls .domain .SpringCacheBasedAclCache ;
48
49
import org .springframework .security .acls .model .Acl ;
49
50
import org .springframework .security .acls .model .AuditableAccessControlEntry ;
50
51
import org .springframework .security .acls .model .MutableAcl ;
55
56
56
57
import static org .assertj .core .api .Assertions .assertThat ;
57
58
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
59
+ import static org .mockito .BDDMockito .given ;
60
+ import static org .mockito .Mockito .mock ;
58
61
59
62
/**
60
63
* Tests {@link BasicLookupStrategy}
@@ -75,22 +78,21 @@ public abstract class AbstractBasicLookupStrategyTests {
75
78
76
79
private BasicLookupStrategy strategy ;
77
80
78
- private static CacheManager cacheManager ;
81
+ private static CacheManagerMock cacheManager ;
79
82
80
83
public abstract JdbcTemplate getJdbcTemplate ();
81
84
82
85
public abstract DataSource getDataSource ();
83
86
84
87
@ BeforeAll
85
88
public static void initCacheManaer () {
86
- cacheManager = CacheManager . create ();
87
- cacheManager .addCache (new Cache ( "basiclookuptestcache" , 500 , false , false , 30 , 30 ) );
89
+ cacheManager = new CacheManagerMock ();
90
+ cacheManager .addCache ("basiclookuptestcache" );
88
91
}
89
92
90
93
@ AfterAll
91
94
public static void shutdownCacheManager () {
92
- cacheManager .removalAll ();
93
- cacheManager .shutdown ();
95
+ cacheManager .clear ();
94
96
}
95
97
96
98
@ BeforeEach
@@ -118,11 +120,17 @@ protected AclAuthorizationStrategy aclAuthStrategy() {
118
120
return new AclAuthorizationStrategyImpl (new SimpleGrantedAuthority ("ROLE_ADMINISTRATOR" ));
119
121
}
120
122
121
- protected EhCacheBasedAclCache aclCache () {
122
- return new EhCacheBasedAclCache (getCache (), new DefaultPermissionGrantingStrategy (new ConsoleAuditLogger ()),
123
+ protected SpringCacheBasedAclCache aclCache () {
124
+ return new SpringCacheBasedAclCache (getCache (), new DefaultPermissionGrantingStrategy (new ConsoleAuditLogger ()),
123
125
new AclAuthorizationStrategyImpl (new SimpleGrantedAuthority ("ROLE_USER" )));
124
126
}
125
127
128
+ protected Cache getCache () {
129
+ Cache cache = cacheManager .getCacheManager ().getCache ("basiclookuptestcache" );
130
+ cache .clear ();
131
+ return cache ;
132
+ }
133
+
126
134
@ AfterEach
127
135
public void emptyDatabase () {
128
136
String query = "DELETE FROM acl_entry;" + "DELETE FROM acl_object_identity WHERE ID = 9;"
@@ -134,12 +142,6 @@ public void emptyDatabase() {
134
142
getJdbcTemplate ().execute (query );
135
143
}
136
144
137
- protected Ehcache getCache () {
138
- Ehcache cache = cacheManager .getCache ("basiclookuptestcache" );
139
- cache .removeAll ();
140
- return cache ;
141
- }
142
-
143
145
@ Test
144
146
public void testAclsRetrievalWithDefaultBatchSize () throws Exception {
145
147
ObjectIdentity topParentOid = new ObjectIdentityImpl (TARGET_CLASS , 100L );
@@ -318,4 +320,32 @@ public void testCreateGrantedAuthority() {
318
320
assertThat (((GrantedAuthoritySid ) result ).getGrantedAuthority ()).isEqualTo ("sid" );
319
321
}
320
322
323
+ private static final class CacheManagerMock {
324
+
325
+ private final List <String > cacheNames ;
326
+
327
+ private final CacheManager cacheManager ;
328
+
329
+ private CacheManagerMock () {
330
+ this .cacheNames = new ArrayList <>();
331
+ this .cacheManager = mock (CacheManager .class );
332
+ given (this .cacheManager .getCacheNames ()).willReturn (this .cacheNames );
333
+ }
334
+
335
+ private CacheManager getCacheManager () {
336
+ return this .cacheManager ;
337
+ }
338
+
339
+ private void addCache (String name ) {
340
+ this .cacheNames .add (name );
341
+ Cache cache = new ConcurrentMapCache (name );
342
+ given (this .cacheManager .getCache (name )).willReturn (cache );
343
+ }
344
+
345
+ private void clear () {
346
+ this .cacheNames .clear ();
347
+ }
348
+
349
+ }
350
+
321
351
}
0 commit comments