package com.redhat.ceylon.compiler.js;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.util.Arrays;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/** To run this you need to execute the nodetest ant target first. */
public class RunJsTest {

    static File tmpModules;

    static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        try (FileInputStream fIn = new FileInputStream(sourceFile);
                FileChannel source = fIn.getChannel();
                FileOutputStream fOut = new FileOutputStream(destFile);
                FileChannel destination = fOut.getChannel()) {
            long transfered = 0;
            long bytes = source.size();
            while (transfered < bytes) {
                transfered += destination.transferFrom(source, 0, source.size());
                destination.position(transfered);
            }
        } finally {
        }
    }

    @BeforeClass
    public static void setup() throws IOException {
        tmpModules = File.createTempFile("ceylon", "runjstest");
        tmpModules.delete();
        tmpModules.mkdir();
        File sub = new File(tmpModules, "check/0.1");
        sub.mkdirs();
        File src = new File("build/test/proto/check/0.1");
        for (File f : src.listFiles()) {
            copyFile(f, new File(sub, f.getName()));
        }
    }

    @Test
    public void testModuleLoading() throws Exception {
        CeylonRunJsTool runner = new CeylonRunJsTool();
        runner.setModuleVersion("misc/0.1");
        runner.setRun("test");
        runner.setRepository(Arrays.asList(new URI(tmpModules.getAbsolutePath()), new URI("build/runtime"), new URI("build/test/proto")));
        runner.run();
    }

    @AfterClass
    public static void cleanup() {
        tmpModules.delete();
    }
}

@@ -58,73 +58,8 @@
 le;%0A
-import java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0A
 impo
@@ -123,28 +123,18 @@
 nio.
-channels.FileChannel
+file.Files
 ;%0Aim
@@ -182,24 +182,49 @@
 AfterClass;%0A
+import org.junit.Assert;%0A
 import org.j
@@ -394,723 +394,8 @@
 s;%0A%0A
-    static void copyFile(File sourceFile, File destFile) throws IOException %7B%0A        if (!destFile.exists()) %7B%0A            destFile.createNewFile();%0A        %7D%0A        try (FileInputStream fIn = new FileInputStream(sourceFile);%0A                FileChannel source = fIn.getChannel();%0A                FileOutputStream fOut = new FileOutputStream(destFile);%0A                FileChannel destination = fOut.getChannel()) %7B%0A            long transfered = 0;%0A            long bytes = source.size();%0A            while (transfered %3C bytes) %7B%0A                transfered += destination.transferFrom(source, 0, source.size());%0A                destination.position(transfered);%0A            %7D%0A        %7D finally %7B%0A        %7D%0A    %7D%0A%0A
     
@@ -773,18 +773,29 @@
     
-copyFile(f
+Files.copy(f.toPath()
 , ne
@@ -818,16 +818,25 @@
 tName())
+.toPath()
 );%0A     
@@ -1298,10 +1298,1120 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void testResources() throws Exception %7B%0A        //Compile a module with resources%0A        CeylonCompileJsTool compiler = new CeylonCompileJsTool();%0A        compiler.setRepositoryAsStrings(Arrays.asList(%22build/runtime%22));%0A        compiler.setSource(Arrays.asList(new File(%22src/test/resources/doc/highers.ceylon%22)));%0A        compiler.setSkipSrcArchive(true);%0A        compiler.setResource(Arrays.asList(new File(%22src/test/resources/res_test%22)));%0A        compiler.run();%0A        //Run it, just to make sure the resources were exploded%0A        CeylonRunJsTool runner = new CeylonRunJsTool();%0A        runner.setModuleVersion(%22default%22);%0A        runner.setRun(%22run%22);%0A        runner.setRepositoryAsStrings(Arrays.asList(%22build/runtime%22, %22modules%22));%0A        runner.run();%0A        Assert.assertTrue(%22test.txt is missing%22, new File(%22modules/default/test.txt%22).exists());%0A        Assert.assertTrue(%22another_test.txt is missing%22, new File(%22modules/default/another_test.txt%22).exists());%0A        Assert.assertTrue(%22third.txt is missing%22, new File(%22modules/default/subdir/third.txt%22).exists());%0A    %7D%0A%0A
 %7D%0A

Add runtime tests for #389