// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import com.yahoo.config.FileReference;

import com.yahoo.filedistribution.fileacquirer.*;

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;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
 * Test of public API of MockFileAcquirer, since it is intended to be used by 3rd parties.
 * Do not place it in the same package as MockFileAcquirer.
 * @author tonytv
 */
public class MockFileAcquirerTest {
    @org.junit.Test
    public void testReturnFile() throws Exception {
        File file = new File("/test");
        assertThat(waitFor(MockFileAcquirer.returnFile(file)),
                equalTo(file));
    }

    @org.junit.Test
    public void testReturnFiles() throws Exception {
        File file1 = new File("/test1");
        File file2 = new File("/test2");

        HashMap<String, File> map = new HashMap<String, File>();
        map.put("1", file1);
        map.put("2", file2);

        FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(map);

        assertThat(waitFor(fileAcquirer, createFileReference("1")),
                equalTo(file1));
        assertThat(waitFor(fileAcquirer, createFileReference("2")),
                equalTo(file2));
    }

    @org.junit.Test(expected = TimeoutException.class)
    public void testThrowTimeoutException() throws Exception {
        waitFor(MockFileAcquirer.throwTimeoutException());
    }

    @org.junit.Test(expected = FileReferenceDoesNotExistException.class)
    public void testThrowFileReferenceDoesNotExistException() throws Exception {
        waitFor(MockFileAcquirer.throwFileReferenceDoesNotExistException());
    }

    private File waitFor(FileAcquirer fileAcquirer) throws InterruptedException {
        return waitFor(fileAcquirer, null);
    }

    private File waitFor(FileAcquirer fileAcquirer, FileReference reference)
            throws InterruptedException {
        return fileAcquirer.waitFor(reference, 100, TimeUnit.SECONDS);
    }

    private FileReference createFileReference(String value) {
        Constructor constructors = FileReference.class.getDeclaredConstructors()[0];
        constructors.setAccessible(true);
        try {
            return (FileReference)constructors.newInstance(value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -2298,27 +2298,60 @@
         
-Constructor
+try %7B%0A            Constructor%3CFileReference%3E
  constru
@@ -2404,16 +2404,28 @@
 ctor
-s()%5B0%5D;%0A
+(String.class);%0A    
     
@@ -2474,48 +2474,19 @@
     
-try %7B%0A            return (FileReference)
+    return 
 cons

rewrite to avoid warning