summaryrefslogtreecommitdiffstats
path: root/fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java
diff options
context:
space:
mode:
Diffstat (limited to 'fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java')
-rw-r--r--fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java b/fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java
new file mode 100644
index 00000000000..b2ffa2c3f88
--- /dev/null
+++ b/fileacquirer/src/main/java/com/yahoo/filedistribution/fileacquirer/MockFileAcquirer.java
@@ -0,0 +1,63 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.filedistribution.fileacquirer;
+
+import com.yahoo.config.FileReference;
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * For use when testing searchers that uses file distribution.
+ * @author tonytv
+ */
+public abstract class MockFileAcquirer implements FileAcquirer {
+ /** Creates a FileAcquirer that always returns the given file. **/
+ public static FileAcquirer returnFile(final File file) {
+ return new MockFileAcquirer() {
+ @Override
+ public File waitFor(FileReference fileReference,
+ long timeout, TimeUnit timeUnit) throws InterruptedException {
+ return file;
+ }
+ };
+ }
+
+ /** Creates a FileAcquirer that maps from fileReference.value to a file. **/
+ public static FileAcquirer returnFiles(final Map<String, File> files) {
+ return new MockFileAcquirer() {
+ @Override
+ public File waitFor(FileReference fileReference,
+ long timeout, TimeUnit timeUnit) throws InterruptedException {
+ return files.get(fileReference.value());
+ }
+ };
+ }
+
+ /** Creates a FileAcquirer that throws TimeoutException **/
+ public static FileAcquirer throwTimeoutException() {
+ return new MockFileAcquirer() {
+ @Override
+ public File waitFor(FileReference fileReference,
+ long timeout, TimeUnit timeUnit) throws InterruptedException {
+ throw new TimeoutException("Timed out");
+ }
+ };
+ }
+
+ /** Creates a FileAcquirer that throws FileReferenceDoesNotExistException **/
+ public static FileAcquirer throwFileReferenceDoesNotExistException() {
+ return new MockFileAcquirer() {
+ @Override
+ public File waitFor(FileReference fileReference,
+ long timeout, TimeUnit timeUnit) throws InterruptedException {
+ throw new FileReferenceDoesNotExistException(null);
+ }
+ };
+ }
+
+ @Override
+ public void shutdown() {}
+}