LUCENE-3506: tests for verifying assertions enabled were no-op.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1188777 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Doron Cohen 2011-10-25 16:44:56 +00:00
parent b8d95c3a9f
commit 6b084931fd
4 changed files with 33 additions and 67 deletions

View File

@ -724,6 +724,10 @@ Test Cases
for implementing subclasses in different packages, where assertions are not for implementing subclasses in different packages, where assertions are not
enabled. (Uwe Schindler) 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 ======================= ======================= Lucene 3.4.0 =======================
Bug fixes Bug fixes

View File

@ -400,6 +400,9 @@ public abstract class LuceneTestCase extends Assert {
TimeZone.setDefault(timeZone); TimeZone.setDefault(timeZone);
similarityProvider = new RandomSimilarityProvider(random); similarityProvider = new RandomSimilarityProvider(random);
testsFailed = false; testsFailed = false;
// verify assertions are enabled (do last, for smooth cleanup)
assertTrue("assertions are not enabled!", assertionsEnabled());
} }
@AfterClass @AfterClass
@ -1442,4 +1445,15 @@ public abstract class LuceneTestCase extends Assert {
@Ignore("just a hack") @Ignore("just a hack")
public final void alwaysIgnoredTestMethod() {} 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;
}
}
} }

View File

@ -17,55 +17,14 @@ package org.apache.lucene;
* limitations under the License. * limitations under the License.
*/ */
import java.io.Reader;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
/**
* validate that assertions are enabled during tests
*/
public class TestAssertions extends LuceneTestCase { 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 { static class TestTokenStream1 extends TokenStream {
@Override @Override
public final boolean incrementToken() { return false; } public final boolean incrementToken() { return false; }
@ -82,31 +41,15 @@ public class TestAssertions extends LuceneTestCase {
} }
public void testTokenStreams() { 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 TestTokenStream1();
new TestTokenStream2(); new TestTokenStream2();
boolean doFail = false;
try { try {
new TestTokenStream3(); new TestTokenStream3();
fail("TestTokenStream3 should fail assertion"); doFail = true;
} catch (AssertionError e) { } catch (AssertionError e) {
// expected
} }
assertFalse("TestTokenStream3 should fail assertion", doFail);
} }
} }

View File

@ -146,12 +146,14 @@ public class TestSegmentMerger extends LuceneTestCase {
// Assert that SM fails if .del exists // Assert that SM fails if .del exists
SegmentMerger sm = new SegmentMerger(dir, 1, "a", null, null, null, newIOContext(random)); SegmentMerger sm = new SegmentMerger(dir, 1, "a", null, null, null, newIOContext(random));
boolean doFail = false;
try { try {
sm.createCompoundFile("b1", w.segmentInfos.info(0), newIOContext(random)); 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) { } catch (AssertionError e) {
// expected // expected
} }
assertFalse("should not have been able to create a .cfs with .del and .s* files", doFail);
// Create an index w/ .s* // Create an index w/ .s*
w = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE)); 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(); r.close();
// Assert that SM fails if .s* exists // Assert that SM fails if .s* exists
SegmentInfos sis = new SegmentInfos();
sis.read(dir);
try { try {
sm.createCompoundFile("b2", w.segmentInfos.info(0), newIOContext(random)); sm.createCompoundFile("b2", sis.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) { } catch (AssertionError e) {
// expected // expected
} }
assertFalse("should not have been able to create a .cfs with .del and .s* files", doFail);
dir.close(); dir.close();
} }