aboutsummaryrefslogtreecommitdiff
path: root/src/test/persistance/JsonUtilsTest.java
blob: 9ab9b1cfd6488000adbe695fc321d80c730964a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package persistance;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;

import static org.junit.jupiter.api.Assertions.*;

public class JsonUtilsTest {
    private static final String testPath = "data/test.cache";

    @Test
    void testWritingEmptyFile() {
        ArrayDeque<String> test = new ArrayDeque<>();
        JsonUtils.writeToFile(new JSONArray(test), testPath);

        JSONArray dugUp = JsonUtils.readFromFile(testPath);
        assertTrue(dugUp.isEmpty());
    }

    @Test
    void testWritingLists() {
        ArrayDeque<String> test = new ArrayDeque<>();
        test.add("/home/apropos/foo/bar"); // absolute path
        test.add("data"); // relative path
        test.add("/home/apropos/baz/bar"); // another path
        JsonUtils.writeToFile(new JSONArray(test), testPath);

        JSONArray dugUp = JsonUtils.readFromFile(testPath);
        for (int i = 0; i < dugUp.length(); i++) {
            assertTrue(test.removeFirst().equals(dugUp.get(i)));
        }
    }
}