Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 803896) +++ CHANGES.txt (working copy) @@ -470,6 +470,10 @@ of the instances are correct. (Mike McCandless, Mark Miller, Michael Busch) +22. LUCENE-1805: CloseableThreadLocal did not allow a null Object in get(), + although it does allow it in set(Object). Fix get() to not assert the object + is not null. (Shai Erera via Mike McCandless) + New features 1. LUCENE-1411: Added expert API to open an IndexWriter on a prior Index: src/test/org/apache/lucene/index/TestCloseableThreadLocal.java =================================================================== --- src/test/org/apache/lucene/index/TestCloseableThreadLocal.java (revision 803896) +++ src/test/org/apache/lucene/index/TestCloseableThreadLocal.java (working copy) @@ -11,7 +11,23 @@ String str = (String)tl.get(); assertEquals(TEST_VALUE, str); } - + + public void testNullValue() throws Exception { + // Tests that null can be set as a valid value (LUCENE-1805). This + // previously failed in get(). + CloseableThreadLocal ctl = new CloseableThreadLocal(); + ctl.set(null); + assertNull(ctl.get()); + } + + public void testDefaultValueWithoutSetting() throws Exception { + // LUCENE-1805: make sure default get returns null, + // twice in a row + CloseableThreadLocal ctl = new CloseableThreadLocal(); + assertNull(ctl.get()); + assertNull(ctl.get()); + } + public class InitValueThreadLocal extends CloseableThreadLocal { protected Object initialValue() { return TEST_VALUE; Index: src/java/org/apache/lucene/util/CloseableThreadLocal.java =================================================================== --- src/java/org/apache/lucene/util/CloseableThreadLocal.java (revision 803896) +++ src/java/org/apache/lucene/util/CloseableThreadLocal.java (working copy) @@ -62,11 +62,7 @@ } else return null; } else { - Object v = weakRef.get(); - // This can never be null, because we hold a hard - // reference to the underlying object: - assert v != null; - return v; + return weakRef.get(); } }