Skip to content

Commit 278199d

Browse files
committed
Revert "Improve allowNullValue handling when a null value is provided"
This reverts commit fd568f3.
1 parent fd568f3 commit 278199d

File tree

9 files changed

+52
-118
lines changed

9 files changed

+52
-118
lines changed

spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ public void testStaticMode() {
102102
assertEquals("value1", cache1x.get("key1").get());
103103
cache1x.put("key2", 2);
104104
assertEquals(2, cache1x.get("key2").get());
105-
105+
try {
106+
cache1x.put("key3", null);
107+
fail("Should have thrown NullPointerException");
108+
}
109+
catch (NullPointerException ex) {
110+
// expected
111+
}
112+
106113
cm.setAllowNullValues(true);
107114
Cache cache1y = cm.getCache("c1");
108115

spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import org.junit.Before;
2121
import org.junit.Test;
2222

23-
import org.springframework.cache.AbstractValueAdaptingCacheTests;
23+
import org.springframework.cache.AbstractCacheTests;
2424
import org.springframework.cache.Cache;
2525

2626
import static org.junit.Assert.*;
@@ -29,31 +29,21 @@
2929
* @author Ben Manes
3030
* @author Stephane Nicoll
3131
*/
32-
public class CaffeineCacheTests extends AbstractValueAdaptingCacheTests<CaffeineCache> {
32+
public class CaffeineCacheTests extends AbstractCacheTests<CaffeineCache> {
3333

3434
private com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache;
3535

3636
private CaffeineCache cache;
3737

38-
private CaffeineCache cacheNoNull;
39-
4038
@Before
4139
public void setUp() {
4240
nativeCache = Caffeine.newBuilder().build();
4341
cache = new CaffeineCache(CACHE_NAME, nativeCache);
44-
com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCacheNoNull
45-
= Caffeine.newBuilder().build();
46-
cacheNoNull = new CaffeineCache(CACHE_NAME_NO_NULL, nativeCacheNoNull, false);
4742
}
4843

4944
@Override
5045
protected CaffeineCache getCache() {
51-
return getCache(true);
52-
}
53-
54-
@Override
55-
protected CaffeineCache getCache(boolean allowNull) {
56-
return allowNull ? this.cache : this.cacheNoNull;
46+
return cache;
5747
}
5848

5949
@Override

spring-context-support/src/test/java/org/springframework/cache/guava/GuavaCacheManagerTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ public void testStaticMode() {
101101
assertEquals("value1", cache1x.get("key1").get());
102102
cache1x.put("key2", 2);
103103
assertEquals(2, cache1x.get("key2").get());
104-
104+
try {
105+
cache1x.put("key3", null);
106+
fail("Should have thrown NullPointerException");
107+
}
108+
catch (NullPointerException ex) {
109+
// expected
110+
}
111+
105112
cm.setAllowNullValues(true);
106113
Cache cache1y = cm.getCache("c1");
107114

spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,32 +25,26 @@
2525
import org.junit.After;
2626
import org.junit.Before;
2727

28-
import org.springframework.cache.AbstractValueAdaptingCacheTests;
28+
import org.springframework.cache.AbstractCacheTests;
2929

3030
/**
3131
* @author Stephane Nicoll
3232
*/
33-
public class JCacheEhCacheApiTests extends AbstractValueAdaptingCacheTests<JCacheCache> {
33+
public class JCacheEhCacheApiTests extends AbstractCacheTests<JCacheCache> {
3434

3535
private CacheManager cacheManager;
3636

3737
private Cache<Object, Object> nativeCache;
3838

3939
private JCacheCache cache;
4040

41-
private JCacheCache cacheNoNull;
42-
4341

4442
@Before
4543
public void setup() {
4644
this.cacheManager = getCachingProvider().getCacheManager();
4745
this.cacheManager.createCache(CACHE_NAME, new MutableConfiguration<>());
48-
this.cacheManager.createCache(CACHE_NAME_NO_NULL, new MutableConfiguration<>());
4946
this.nativeCache = this.cacheManager.getCache(CACHE_NAME);
5047
this.cache = new JCacheCache(this.nativeCache);
51-
Cache<Object, Object> nativeCacheNoNull =
52-
this.cacheManager.getCache(CACHE_NAME_NO_NULL);
53-
this.cacheNoNull = new JCacheCache(nativeCacheNoNull, false);
5448
}
5549

5650
protected CachingProvider getCachingProvider() {
@@ -64,14 +58,10 @@ public void shutdown() {
6458
}
6559
}
6660

67-
@Override
68-
protected JCacheCache getCache() {
69-
return getCache(true);
70-
}
7161

7262
@Override
73-
protected JCacheCache getCache(boolean allowNull) {
74-
return allowNull ? this.cache : this.cacheNoNull;
63+
protected JCacheCache getCache() {
64+
return this.cache;
7565
}
7666

7767
@Override

spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,13 +95,8 @@ protected Object fromStoreValue(Object storeValue) {
9595
* @return the value to store
9696
*/
9797
protected Object toStoreValue(Object userValue) {
98-
if (userValue == null) {
99-
if (this.allowNullValues) {
100-
return NullValue.INSTANCE;
101-
}
102-
throw new IllegalArgumentException(
103-
String.format("Cache '%s' is configured to not allow null " +
104-
"values but null was provided", getName()));
98+
if (this.allowNullValues && userValue == null) {
99+
return NullValue.INSTANCE;
105100
}
106101
return userValue;
107102
}

spring-context/src/test/java/org/springframework/cache/AbstractCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-context/src/test/java/org/springframework/cache/AbstractValueAdaptingCacheTests.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

spring-context/src/test/java/org/springframework/cache/concurrent/ConcurrentMapCacheManagerTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,7 +104,14 @@ public void testStaticMode() {
104104
assertEquals("value1", cache1x.get("key1").get());
105105
cache1x.put("key2", 2);
106106
assertEquals(2, cache1x.get("key2").get());
107-
107+
try {
108+
cache1x.put("key3", null);
109+
fail("Should have thrown NullPointerException");
110+
}
111+
catch (NullPointerException ex) {
112+
// expected
113+
}
114+
108115
cm.setAllowNullValues(true);
109116
Cache cache1y = cm.getCache("c1");
110117

spring-context/src/test/java/org/springframework/cache/concurrent/ConcurrentMapCacheTests.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

28-
import org.springframework.cache.AbstractValueAdaptingCacheTests;
28+
import org.springframework.cache.AbstractCacheTests;
2929
import org.springframework.core.serializer.support.SerializationDelegate;
3030

3131
import static org.junit.Assert.*;
@@ -35,35 +35,23 @@
3535
* @author Juergen Hoeller
3636
* @author Stephane Nicoll
3737
*/
38-
public class ConcurrentMapCacheTests
39-
extends AbstractValueAdaptingCacheTests<ConcurrentMapCache> {
38+
public class ConcurrentMapCacheTests extends AbstractCacheTests<ConcurrentMapCache> {
4039

4140
protected ConcurrentMap<Object, Object> nativeCache;
4241

4342
protected ConcurrentMapCache cache;
4443

45-
protected ConcurrentMap<Object, Object> nativeCacheNoNull;
46-
47-
protected ConcurrentMapCache cacheNoNull;
48-
4944

5045
@Before
5146
public void setUp() throws Exception {
52-
this.nativeCache = new ConcurrentHashMap<Object, Object>();
53-
this.cache = new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true);
54-
this.nativeCacheNoNull = new ConcurrentHashMap<Object, Object>();
55-
this.cacheNoNull = new ConcurrentMapCache(CACHE_NAME_NO_NULL,
56-
this.nativeCacheNoNull, false);
57-
this.cache.clear();
47+
nativeCache = new ConcurrentHashMap<Object, Object>();
48+
cache = new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
49+
cache.clear();
5850
}
5951

6052
@Override
6153
protected ConcurrentMapCache getCache() {
62-
return getCache(true);
63-
}
64-
65-
@Override protected ConcurrentMapCache getCache(boolean allowNull) {
66-
return allowNull ? this.cache : this.cacheNoNull;
54+
return this.cache;
6755
}
6856

6957
@Override
@@ -96,9 +84,9 @@ public void testSerializer() {
9684
public void testNonSerializableContent() {
9785
ConcurrentMapCache serializeCache = createCacheWithStoreByValue();
9886

99-
this.thrown.expect(IllegalArgumentException.class);
100-
this.thrown.expectMessage("Failed to serialize");
101-
this.thrown.expectMessage(this.cache.getClass().getName());
87+
thrown.expect(IllegalArgumentException.class);
88+
thrown.expectMessage("Failed to serialize");
89+
thrown.expectMessage(this.cache.getClass().getName());
10290
serializeCache.put(createRandomKey(), this.cache);
10391
}
10492

@@ -108,15 +96,15 @@ public void testInvalidSerializedContent() {
10896

10997
String key = createRandomKey();
11098
this.nativeCache.put(key, "Some garbage");
111-
this.thrown.expect(IllegalArgumentException.class);
112-
this.thrown.expectMessage("Failed to deserialize");
113-
this.thrown.expectMessage("Some garbage");
99+
thrown.expect(IllegalArgumentException.class);
100+
thrown.expectMessage("Failed to deserialize");
101+
thrown.expectMessage("Some garbage");
114102
serializeCache.get(key);
115103
}
116104

117105

118106
private ConcurrentMapCache createCacheWithStoreByValue() {
119-
return new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true,
107+
return new ConcurrentMapCache(CACHE_NAME, nativeCache, true,
120108
new SerializationDelegate(ConcurrentMapCacheTests.class.getClassLoader()));
121109
}
122110

0 commit comments

Comments
 (0)