package com.googlecode.jvmvm.tests;

import com.googlecode.jvmvm.loader.Project;
import com.googlecode.jvmvm.loader.ProjectExecutionException;
import com.googlecode.jvmvm.tests.interpretable.CustomExamples;
import com.googlecode.jvmvm.tests.interpretable.MapExamples;
import com.googlecode.jvmvm.tests.interpretable.SystemExamples;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class SerializerTest {

    @Before
    public void setUp() {

    }

    List<String> bootstrap = Arrays.asList(
            Object.class.getName(),

            String.class.getName(),
            StringBuilder.class.getName(),
            CharSequence.class.getName(),

            Character.class.getName(),
            Boolean.class.getName(),
            Number.class.getName(),
            Byte.class.getName(),
            Short.class.getName(),
            Integer.class.getName(),
            Long.class.getName(),
            Float.class.getName(),
            Double.class.getName(),

            Math.class.getName(),

            Iterable.class.getName(),
            Iterator.class.getName(),
            Collection.class.getName(),

            StackTraceElement.class.getName(),
            Throwable.class.getName(),
            Exception.class.getName(),
            RuntimeException.class.getName(),
            UnsupportedOperationException.class.getName(),
            NoSuchElementException.class.getName(),
            UnsupportedEncodingException.class.getName(),

            Serializable.class.getName(),

            Arrays.class.getName(),
            Collections.class.getName(),

            List.class.getName(),
            ArrayList.class.getName(),
            LinkedList.class.getName(),

            Set.class.getName(),
            HashSet.class.getName(),

            Map.class.getName(),
            HashMap.class.getName(),
            TreeMap.class.getName()
    );

    @Test
    public void test_vm_hashmap_entry() throws Exception {
        String src1 = MapExamples.class.getCanonicalName().replace(".", "/") + ".java";

        Project project = new Project("serializer-test")
                .addFile(src1, FileUtils.readFileToString(new File("src/test/java/" + src1)))
                .addSystemClasses(bootstrap)
                .compile()
                .setupVM(MapExamples.class.getCanonicalName(), "testHashMapEntry");

        byte[] bytes = project.saveToBytes();

        Object res = project.run();

        String expected = MapExamples.testHashMapEntry();
        Assert.assertEquals("result", expected, res);

        Project project2 = Project.fromBytes(bytes);
        while (project2.isActive()) {
            project2.step(true);
        }
        Object res2 = project2.getResult();
        Assert.assertEquals("result2", expected, res2);
    }

    @Test
    public void test_vm_treemap_entry() throws Exception {
        String src1 = MapExamples.class.getCanonicalName().replace(".", "/") + ".java";

        Project project = new Project("serializer-test")
                .addFile(src1, FileUtils.readFileToString(new File("src/test/java/" + src1)))
                .addSystemClasses(bootstrap)
                .compile()
                .setupVM(MapExamples.class.getCanonicalName(), "testTreeMapEntry");

        byte[] bytes = project.saveToBytes();

        Object res = project.run();

        String expected = MapExamples.testTreeMapEntry();
        Assert.assertEquals("result", expected, res);

        Project project2 = Project.fromBytes(bytes);
        while (project2.isActive()) {
            project2.step(true);
        }
        Object res2 = project2.getResult();
        Assert.assertEquals("result2", expected, res2);
    }

    @Test
    public void test_vm_custom1() throws Exception {
        String src1 = CustomExamples.class.getCanonicalName().replace(".", "/") + ".java";

        Project project = new Project("serializer-test")
                .addFile(src1, FileUtils.readFileToString(new File("src/test/java/" + src1)))
                .addSystemClasses(bootstrap)
                .compile()
                .setupVM(CustomExamples.class.getCanonicalName(), "test");

        byte[] bytes = project.saveToBytes();

        Object res = project.run();

        String expected = CustomExamples.test();
        Assert.assertEquals("result", expected, res);

        Project project2 = Project.fromBytes(bytes);
        while (project2.isActive()) {
            project2.step(true);
        }
        Object res2 = project2.getResult();
        Assert.assertEquals("result2", expected, res2);
    }

    @Test(expected = ProjectExecutionException.class)
    public void test_vm_system() throws Exception {
        String src1 = SystemExamples.class.getCanonicalName().replace(".", "/") + ".java";

        Project project = new Project("serializer-test")
                .addFile(src1, FileUtils.readFileToString(new File("src/test/java/" + src1)))
                .addSystemClasses(bootstrap)
                .compile()
                .setupVM(SystemExamples.class.getCanonicalName(), "test");
        Object res = project.run();
    }
}

@@ -456,24 +456,52 @@
 va.io.File;%0A
+import java.io.IOException;%0A
 import java.
@@ -554,32 +554,71 @@
 odingException;%0A
+import java.net.MalformedURLException;%0A
 import java.util
@@ -1709,32 +1709,124 @@
 class.getName(),
+%0A            IOException.class.getName(),%0A            MalformedURLException.class.getName(),
 %0A%0A            Se

test fix