Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6051: don't use trappy Iterable<Path> in helper functions

Robert Muir497e64e2b904
diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
index 838b1c4c8b4..f903bc49447 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
@@ -37,6 +37,7 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
+import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -215,7 +216,7 @@ public final class IOUtils {
* <p>
* Some of the files may be null, if so they are ignored.
*/
- public static void deleteFilesIgnoringExceptions(Iterable<? extends Path> files) {
+ public static void deleteFilesIgnoringExceptions(Collection<? extends Path> files) {
for (Path name : files) {
if (name != null) {
try {
@@ -249,7 +250,7 @@ public final class IOUtils {
*
* @param files files to delete
*/
- public static void deleteFilesIfExist(Iterable<? extends Path> files) throws IOException {
+ public static void deleteFilesIfExist(Collection<? extends Path> files) throws IOException {
Throwable th = null;
for (Path file : files) {
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java b/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
new file mode 100644
index 00000000000..2952d9f5f16
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
@@ -0,0 +1,81 @@
+package org.apache.lucene.util;
+
+/*
+ * 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.
+ */
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+/** Simple test methods for IOUtils */
+public class TestIOUtils extends LuceneTestCase {
+
+ public void testDeleteFileIgnoringExceptions() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ Files.createFile(file1);
+ IOUtils.deleteFilesIgnoringExceptions(file1);
+ assertFalse(Files.exists(file1));
+ // actually deletes
+ }
+
+ public void testDontDeleteFileIgnoringExceptions() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ IOUtils.deleteFilesIgnoringExceptions(file1);
+ // no exception
+ }
+
+ public void testDeleteTwoFilesIgnoringExceptions() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ Path file2 = dir.resolve("file2");
+ // only create file2
+ Files.createFile(file2);
+ IOUtils.deleteFilesIgnoringExceptions(file1, file2);
+ assertFalse(Files.exists(file2));
+ // no exception
+ // actually deletes file2
+ }
+
+ public void testDeleteFileIfExists() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ Files.createFile(file1);
+ IOUtils.deleteFilesIfExist(file1);
+ assertFalse(Files.exists(file1));
+ // actually deletes
+ }
+
+ public void testDontDeleteDoesntExist() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ IOUtils.deleteFilesIfExist(file1);
+ // no exception
+ }
+
+ public void testDeleteTwoFilesIfExist() throws Exception {
+ Path dir = createTempDir();
+ Path file1 = dir.resolve("file1");
+ Path file2 = dir.resolve("file2");
+ // only create file2
+ Files.createFile(file2);
+ IOUtils.deleteFilesIfExist(file1, file2);
+ assertFalse(Files.exists(file2));
+ // no exception
+ // actually deletes file2
+ }
+}