package com.redheap.selenium.components;

import com.redheap.selenium.component.AdfInputFile;
import com.redheap.selenium.pages.InputFileDemoPage;

import java.io.File;

import java.net.URISyntaxException;
import java.net.URL;

import static org.hamcrest.Matchers.*;

import static org.junit.Assert.*;
import org.junit.Test;

public class InputFileTest extends PageTestBase<InputFileDemoPage> {

    @Test
    public void testPlainInputFile() throws URISyntaxException {
        InputFileDemoPage page = pages.goHome();
        AdfInputFile inputFile = page.findInputFile();
        URL url = Thread.currentThread().getContextClassLoader().getResource("build.xml");
        File f = new File(url.toURI());
        System.out.println(f.getAbsolutePath());
        inputFile.typeFileName(f);
        assertThat(inputFile.getSubmittedValue(), endsWith("build.xml"));
        assertThat(inputFile.getValue(), nullValue());
        assertThat(inputFile.getUpdateValue(), nullValue());
        page.findPartialPostbackButton().click();
        assertThat(inputFile.getSubmittedValue(), hasToString(""));
        assertThat(inputFile.getValue(), nullValue());
        assertThat(inputFile.getUpdateValue(), hasToString("build.xml"));
    }

    public static void main(String[] args) {
        org.junit.runner.JUnitCore.main(InputFileTest.class.getName());
    }

    @Override
    protected Class<InputFileDemoPage> getPageClass() {
        return InputFileDemoPage.class;
    }

    @Override
    protected String getJspxName() {
        return "inputFile.jspx";
    }
}

@@ -156,20 +156,27 @@
 java.io.
-File
+IOException
 ;%0A%0Aimpor
@@ -206,16 +206,17 @@
 eption;%0A
+%0A
 import j
@@ -224,14 +224,48 @@
 va.n
-et.URL
+io.file.Files;%0Aimport java.nio.file.Path
 ;%0A%0Ai
@@ -503,16 +503,29 @@
 xception
+, IOException
  %7B%0A     
@@ -635,215 +635,156 @@
     
-URL url = Thread.currentThread().getContextClassLoader().getResource(%22build.xml%22);%0A        File f = new File(url.toURI());%0A        System.out.println(f.getAbsolutePath());%0A        inputFile.typeFileName(f);%0A
+Path tmp = Files.createTempFile(getClass().getName(), %22.tmp%22);%0A        try %7B%0A            inputFile.typeFileName(tmp.toAbsolutePath().toFile());%0A    
     
@@ -838,27 +838,44 @@
 ndsWith(
-%22build.xml%22
+tmp.getFileName().toString()
 ));%0A    
@@ -870,32 +870,36 @@
 ng()));%0A        
+    
 assertThat(input
@@ -921,32 +921,36 @@
 , nullValue());%0A
+    
         assertTh
@@ -1002,16 +1002,20 @@
         
+   
+ 
 page.fin
@@ -1044,24 +1044,28 @@
 ().click();%0A
+    
         asse
@@ -1120,32 +1120,36 @@
 g(%22%22));%0A        
+    
 assertThat(input
@@ -1179,32 +1179,36 @@
 lue());%0A        
+ 
+   
 assertThat(input
@@ -1246,22 +1246,108 @@
 ing(
-%22build.xml%22));
+tmp.getFileName().toString()));%0A        %7D finally %7B%0A            Files.deleteIfExists(tmp);%0A        %7D
 %0A   

create temporary file for testing upload