diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index baff37d8477..ef994910a2f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -724,6 +724,10 @@ Test Cases for implementing subclasses in different packages, where assertions are not enabled. (Uwe Schindler) +* LUCENE-3506: tests relying on assertions being enabled were no-op because + they ignored AssertionError. With this fix now entire test framework + (all tests) fail if assertions are disabled. (Doron Cohen) + ======================= Lucene 3.4.0 ======================= Bug fixes diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java index 2a895e72a85..5fe5c7bfbb4 100644 --- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java @@ -400,6 +400,9 @@ public abstract class LuceneTestCase extends Assert { TimeZone.setDefault(timeZone); similarityProvider = new RandomSimilarityProvider(random); testsFailed = false; + + // verify assertions are enabled (do last, for smooth cleanup) + assertTrue("assertions are not enabled!", assertionsEnabled()); } @AfterClass @@ -1442,4 +1445,15 @@ public abstract class LuceneTestCase extends Assert { @Ignore("just a hack") public final void alwaysIgnoredTestMethod() {} + + /** check if assertions are enabled */ + private static boolean assertionsEnabled() { + try { + assert Boolean.FALSE.booleanValue(); + return false; // should never get here + } catch (AssertionError e) { + return true; + } + } + } diff --git a/lucene/src/test/org/apache/lucene/TestAssertions.java b/lucene/src/test/org/apache/lucene/TestAssertions.java index 34138ae83f1..eceae88f4c2 100644 --- a/lucene/src/test/org/apache/lucene/TestAssertions.java +++ b/lucene/src/test/org/apache/lucene/TestAssertions.java @@ -17,55 +17,14 @@ package org.apache.lucene; * limitations under the License. */ -import java.io.Reader; - import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; +/** + * validate that assertions are enabled during tests + */ public class TestAssertions extends LuceneTestCase { - public void testBasics() { - try { - assert Boolean.FALSE.booleanValue(); - fail("assertions are not enabled!"); - } catch (AssertionError e) { - assert Boolean.TRUE.booleanValue(); - } - } - - static class TestAnalyzer1 extends Analyzer { - - @Override - protected TokenStreamComponents createComponents(String fieldName, Reader aReader) { - return null; - } - } - - static final class TestAnalyzer2 extends Analyzer { - - @Override - protected TokenStreamComponents createComponents(String fieldName, Reader aReader) { - return null; - } - } - - static class TestAnalyzer3 extends Analyzer { - - @Override - protected TokenStreamComponents createComponents(String fieldName, Reader aReader) { - return null; - } - } - - static class TestAnalyzer4 extends Analyzer { - - @Override - protected TokenStreamComponents createComponents(String fieldName, Reader aReader) { - return null; - } - } - static class TestTokenStream1 extends TokenStream { @Override public final boolean incrementToken() { return false; } @@ -82,31 +41,15 @@ public class TestAssertions extends LuceneTestCase { } public void testTokenStreams() { - new TestAnalyzer1(); - - new TestAnalyzer2(); - - try { - new TestAnalyzer3(); - fail("TestAnalyzer3 should fail assertion"); - } catch (AssertionError e) { - } - - try { - new TestAnalyzer4(); - fail("TestAnalyzer4 should fail assertion"); - } catch (AssertionError e) { - } - new TestTokenStream1(); - new TestTokenStream2(); - + boolean doFail = false; try { new TestTokenStream3(); - fail("TestTokenStream3 should fail assertion"); + doFail = true; } catch (AssertionError e) { + // expected } + assertFalse("TestTokenStream3 should fail assertion", doFail); } - } diff --git a/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java b/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java index a84a96d4cca..0348bd8dcf1 100644 --- a/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java +++ b/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java @@ -146,12 +146,14 @@ public class TestSegmentMerger extends LuceneTestCase { // Assert that SM fails if .del exists SegmentMerger sm = new SegmentMerger(dir, 1, "a", null, null, null, newIOContext(random)); + boolean doFail = false; try { sm.createCompoundFile("b1", w.segmentInfos.info(0), newIOContext(random)); - fail("should not have been able to create a .cfs with .del and .s* files"); + doFail = true; // should never get here } catch (AssertionError e) { // expected } + assertFalse("should not have been able to create a .cfs with .del and .s* files", doFail); // Create an index w/ .s* w = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE)); @@ -164,12 +166,15 @@ public class TestSegmentMerger extends LuceneTestCase { r.close(); // Assert that SM fails if .s* exists + SegmentInfos sis = new SegmentInfos(); + sis.read(dir); try { - sm.createCompoundFile("b2", w.segmentInfos.info(0), newIOContext(random)); - fail("should not have been able to create a .cfs with .del and .s* files"); + sm.createCompoundFile("b2", sis.info(0), newIOContext(random)); + doFail = true; // should never get here } catch (AssertionError e) { // expected } + assertFalse("should not have been able to create a .cfs with .del and .s* files", doFail); dir.close(); }