You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2012/08/16 14:26:32 UTC
svn commit: r1373801 - in /lucene/dev/trunk/lucene:
core/src/test/org/apache/lucene/util/junitcompat/
test-framework/src/java/org/apache/lucene/util/
Author: rmuir
Date: Thu Aug 16 12:26:32 2012
New Revision: 1373801
URL: http://svn.apache.org/viewvc?rev=1373801&view=rev
Log:
LUCENE-4308: don't delete a test's files if the test fails
Added:
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java (with props)
Modified:
lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java
lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
Added: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java?rev=1373801&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java (added)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java Thu Aug 16 12:26:32 2012
@@ -0,0 +1,51 @@
+package org.apache.lucene.util.junitcompat;
+
+import java.io.File;
+
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util._TestUtil;
+import org.apache.lucene.util.junitcompat.TestFailIfDirectoryNotClosed.Nested1;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.JUnitCore;
+import org.junit.runner.Result;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class TestLeaveFilesIfTestFails extends WithNestedTests {
+ public TestLeaveFilesIfTestFails() {
+ super(true);
+ }
+
+ public static class Nested1 extends WithNestedTests.AbstractNestedTest {
+ static File file;
+ public void testDummy() {
+ file = _TestUtil.getTempDir("leftover");
+ file.mkdirs();
+ fail();
+ }
+ }
+
+ @Test
+ public void testLeaveFilesIfTestFails() {
+ Result r = JUnitCore.runClasses(Nested1.class);
+ Assert.assertEquals(1, r.getFailureCount());
+ Assert.assertTrue(Nested1.file.exists());
+ Nested1.file.delete();
+ }
+}
Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java?rev=1373801&r1=1373800&r2=1373801&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java Thu Aug 16 12:26:32 2012
@@ -24,25 +24,30 @@ import java.io.*;
*/
final class CloseableFile implements Closeable {
private final File file;
+ private final TestRuleMarkFailure failureMarker;
- public CloseableFile(File file) {
+ public CloseableFile(File file, TestRuleMarkFailure failureMarker) {
this.file = file;
+ this.failureMarker = failureMarker;
}
@Override
public void close() throws IOException {
- if (file.exists()) {
- try {
- _TestUtil.rmDir(file);
- } catch (IOException e) {
- // Ignore the exception from rmDir.
- }
-
- // Re-check.
+ // only if there were no other test failures.
+ if (failureMarker.wasSuccessful()) {
if (file.exists()) {
- throw new IOException(
+ try {
+ _TestUtil.rmDir(file);
+ } catch (IOException e) {
+ // Ignore the exception from rmDir.
+ }
+
+ // Re-check.
+ if (file.exists()) {
+ throw new IOException(
"Could not remove: " + file.getAbsolutePath());
- }
+ }
+ }
}
}
}
\ No newline at end of file
Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java?rev=1373801&r1=1373800&r2=1373801&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java Thu Aug 16 12:26:32 2012
@@ -94,7 +94,7 @@ public class _TestUtil {
try {
File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR);
f.delete();
- LuceneTestCase.closeAfterSuite(new CloseableFile(f));
+ LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker));
return f;
} catch (IOException e) {
throw new RuntimeException(e);
@@ -136,7 +136,7 @@ public class _TestUtil {
rmDir(destDir);
destDir.mkdir();
- LuceneTestCase.closeAfterSuite(new CloseableFile(destDir));
+ LuceneTestCase.closeAfterSuite(new CloseableFile(destDir, LuceneTestCase.suiteFailureMarker));
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();