package systems.crigges.jmpq3test;

import org.junit.Assert;
import org.junit.Test;
import systems.crigges.jmpq3.JMpqEditor;
import systems.crigges.jmpq3.JMpqException;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

/**
 * Created by Frotty on 06.03.2017.
 */
public class MpqTests {

    private static File[] getMpqs() {
        return new File(MpqTests.class.getClassLoader().getResource("./mpqs/").getFile()).listFiles((dir, name) -> name.endsWith(".w3x"));
    }

    private static File getFile(String name) {
        return new File(MpqTests.class.getClassLoader().getResource(name).getFile());
    }

    @Test
    public void testHasScriptFile() throws JMpqException {
        File[] mpqs = getMpqs();
        for (File mpq : mpqs) {
            JMpqEditor mpqEditor = new JMpqEditor(mpq);
            Assert.assertTrue(mpqEditor.hasFile("war3map.j"));
        }
    }

    @Test
    public void testExtractScriptFile() throws IOException {
        File[] mpqs = getMpqs();
        for (File mpq : mpqs) {
            JMpqEditor mpqEditor = new JMpqEditor(mpq);
            File temp = File.createTempFile("war3mapj", "extracted");
            temp.deleteOnExit();
            mpqEditor.extractFile("war3map.j", temp);
            Assert.assertTrue(Arrays.equals(Files.readAllBytes(temp.toPath()), Files.readAllBytes(getFile("war3map.j").toPath())));
        }
    }

    @Test
    public void testInsertDeleteFile() throws IOException {
        File[] mpqs = getMpqs();
        for (File mpq : mpqs) {
            insertAndDelete(mpq, "Example.txt");
        }
    }

    private void insertAndDelete(File mpq, String filename) throws IOException {
        JMpqEditor mpqEditor = new JMpqEditor(mpq);
        Assert.assertFalse(mpqEditor.hasFile(filename));

        mpqEditor.insertFile(filename, getFile(filename), false);
        mpqEditor.close();
        mpqEditor = new JMpqEditor(mpq);
        Assert.assertTrue(mpqEditor.hasFile(filename));

        mpqEditor.deleteFile(filename);
        mpqEditor.close();
        mpqEditor = new JMpqEditor(mpq);
        Assert.assertFalse(mpqEditor.hasFile(filename));
    }


}

@@ -215,61 +215,8 @@
 ion;
-%0Aimport java.nio.file.Files;%0Aimport java.util.Arrays;
 %0A%0A/*
@@ -868,32 +868,34 @@
        %7D%0A    %7D%0A%0A
+//
     @Test%0A    pu
@@ -880,32 +880,34 @@
  %7D%0A%0A//    @Test%0A
+//
     public void 
@@ -943,32 +943,34 @@
 s IOException %7B%0A
+//
         File%5B%5D m
@@ -978,32 +978,34 @@
 qs = getMpqs();%0A
+//
         for (Fil
@@ -1012,32 +1012,34 @@
 e mpq : mpqs) %7B%0A
+//
             JMpq
@@ -1070,32 +1070,34 @@
 MpqEditor(mpq);%0A
+//
             File
@@ -1146,24 +1146,26 @@
 xtracted%22);%0A
+//
             
@@ -1181,24 +1181,26 @@
 teOnExit();%0A
+//
             
@@ -1241,16 +1241,18 @@
  temp);%0A
+//
         
@@ -1371,24 +1371,26 @@
 oPath())));%0A
+//
         %7D%0A  
@@ -1379,32 +1379,34 @@
 ));%0A//        %7D%0A
+//
     %7D%0A%0A    @Test

disable test