summaryrefslogtreecommitdiff
path: root/support/latex-dependency-grapher/source/test/java/InteractionHandlerTest.java
blob: 2489c139b904faec09db657c0207e8c6adf93694 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import ch.bfh.lpdg.InteractionHandler;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.ParseException;
import org.junit.jupiter.api.Test;

import java.security.InvalidParameterException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class InteractionHandlerTest {
    @Test
    public void testValidPathSingleFile() {
        var handler1 = InteractionHandler.getInstance();
        var handler2 = InteractionHandler.getInstance();
        var handler3 = InteractionHandler.getInstance();
        try {
            handler1.handleOptions(new String[]{"--debug", "-p", "src/test/resources/LaTexFolder/file1.cls"});
            handler2.handleOptions(new String[]{"--debug", "-p", "src/test/resources/LaTexFolder/file2.tex"});
            handler3.handleOptions(new String[]{"--debug", "-p", "src/test/resources/LaTexFolder/file3.sty"});
        } catch (Exception e) {
            fail("This should have worked.");
        }
    }

    @Test
    public void testValidPathSingleInvalidFile() {
        var handler = InteractionHandler.getInstance();
        try {
            handler.handleOptions(new String[]{"--debug", "-p", "src/test/resources/test.txt"});
            fail("The file is not a LaTeX-File and this should have failed.");
        } catch (Exception e) {
            assertEquals(IllegalArgumentException.class, e.getClass());
        }
    }

    @Test
    public void testValidPathMultipleFiles() throws ParseException {
        var handler = InteractionHandler.getInstance();
        handler.handleOptions(new String[]{"--debug", "-p", "src/test/resources/LaTexFolder/"});

        // Depending on thest order, there should be at least 3 scannable files.
        assertThat(handler.getFiles().size()).isGreaterThan(3);
    }

    @Test
    public void testInvalidPath() {
        var handler = InteractionHandler.getInstance();

        try {
            handler.handleOptions(new String[]{"--debug", "-p", "/TEST/INVALID"});
        } catch (Exception e) {
            assertEquals(InvalidParameterException.class, e.getClass());
            assertEquals(e.getMessage(), "The given path is invalid: /TEST/INVALID");
            return;
        }
        fail("The path is invalid and should have thrown an exception.");
    }

    @Test
    public void testNullPath() {
        var handler = InteractionHandler.getInstance();

        try {
            handler.handleOptions(new String[]{"--debug", "-p"});
            fail("The path is null and should have thrown an exception.");
        } catch (Exception e) {
            assertEquals(MissingArgumentException.class, e.getClass());
        }
    }

    @Test
    public void testEmptyPath() {
        var handler = InteractionHandler.getInstance();

        try {
            handler.handleOptions(new String[]{"--debug", "-p", ""});
            fail("The path is empty and should have thrown an exception.");
        } catch (Exception e) {
            assertEquals(MissingArgumentException.class, e.getClass());
        }
    }
}