package io.rouz.task;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class SerializationTest {

  @Test
  public void shouldJavaUtilSerialize() throws Exception {
    File tempFile = File.createTempFile("tempdata", ".bin");
    tempFile.deleteOnExit();

    TaskId id;

    try {
      Task<Long> task1 = Task.named("Foo", "Bar", 39)
          .process(() -> 9999L);
      Task<String> task2 = Task.named("Baz", 40)
          .in(() -> task1)
          .ins(() -> Stream.of(task1))
          .process((t1, t1l) -> t1l + " hello " + (t1 + 5));

      id = task1.id();

      FileOutputStream fos = new FileOutputStream(tempFile);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(task2);
      oos.close();
    } catch (Exception ex) {
      fail("Exception thrown during serialization: " + ex.toString());
      return;
    }

    try {
      FileInputStream fis = new FileInputStream(tempFile);
      ObjectInputStream ois = new ObjectInputStream(fis);
      Task<?> task = (Task<?>) ois.readObject();
      ois.close();

      List<Task<?>> deps = task.inputs().collect(toList());

      assertEquals(task.id().name(), "Baz");
      assertEquals(task.out(), "[9999] hello 10004");
      assertEquals(deps.get(0).id(), id);
    } catch (Exception ex) {
      fail("Exception thrown during deserialization: " + ex.toString());
    }
  }
}

@@ -118,32 +118,60 @@
 leOutputStream;%0A
+import java.io.IOException;%0A
 import java.io.O
@@ -537,107 +537,21 @@
 e = 
-File.createTempFile(%22tempdata%22, %22.bin%22);%0A    tempFile.deleteOnExit();%0A%0A    TaskId id;%0A%0A    try %7B%0A  
+tempFile();%0A%0A
     
@@ -598,18 +598,16 @@
 r%22, 39)%0A
-  
         
@@ -629,26 +629,24 @@
 9999L);%0A    
-  
 Task%3CString%3E
@@ -684,18 +684,16 @@
         
- 
- 
 .in(() -
@@ -701,18 +701,16 @@
  task1)%0A
-  
         
@@ -738,18 +738,16 @@
 task1))%0A
-  
         
@@ -806,14 +806,263 @@
     
-  id =
+serialize(task2, tempFile);%0A    Task%3C?%3E des = deserialize(tempFile);%0A%0A    List%3CTask%3C?%3E%3E deps = des.inputs().collect(toList());%0A%0A    assertEquals(des.id().name(), %22Baz%22);%0A    assertEquals(des.out(), %22%5B9999%5D hello 10004%22);%0A    assertEquals(deps.get(0).id(),
  tas
@@ -1068,18 +1068,487 @@
 sk1.id()
+)
 ;%0A
+  %7D%0A%0A  @Test%0A  public void shouldWorkWithEnclosedValues() throws Exception %7B%0A    File tempFile = tempFile();%0A%0A    Task%3CString%3E task = closure(%22woop%22);%0A%0A    serialize(task, tempFile);%0A    Task%3C?%3E des = deserialize(tempFile);%0A%0A    assertEquals(des.out(), %22woop is enclosed%22);%0A  %7D%0A%0A  private Task%3CString%3E closure(String arg) %7B%0A    return Task.named(%22Closed%22)%0A        .process(() -%3E arg + %22 is enclosed%22);%0A  %7D%0A%0A  private void serialize(Task%3C?%3E task, File file) %7B%0A    try %7B
 %0A      F
@@ -1582,37 +1582,33 @@
 ileOutputStream(
-tempF
+f
 ile);%0A      Obje
@@ -1683,17 +1683,16 @@
 ect(task
-2
 );%0A     
@@ -1813,24 +1813,57 @@
     
-  return;%0A    %7D%0A
+%7D%0A  %7D%0A%0A  private Task%3C?%3E deserialize(File file) %7B
 %0A   
@@ -1917,21 +1917,17 @@
 tStream(
-tempF
+f
 ile);%0A  
@@ -2061,217 +2061,227 @@
     
-List%3CTask%3C?%3E%3E deps = task.inputs().collect(toList());%0A%0A      assertEquals(task.id().name(), %22Baz%22);%0A      assertEquals(task.out(), %22%5B9999%5D hello 10004%22);%0A      assertEquals(deps.get(0).id(), id);%0A    %7D catch (
+return task;%0A    %7D catch (Exception ex) %7B%0A      fail(%22Exception thrown during deserialization: %22 + ex.toString());%0A    %7D%0A%0A    throw new IllegalStateException(%22should not reach%22);%0A  %7D%0A%0A  private File tempFile() throws IO
 Exce
@@ -2289,93 +2289,121 @@
 tion
- ex)
  %7B%0A    
-  fail(%22Exception thrown during deserialization: %22 + ex.toString
+File tempFile = File.createTempFile(%22tempdata%22, %22.bin%22);%0A    tempFile.deleteOnExit
 ()
-)
 ;%0A    
-%7D
+return tempFile;
 %0A  %7D

refactor serialization tests