summaryrefslogtreecommitdiffstats
path: root/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
blob: 31e27fa2675a0bde035615b838b6d07c846c607e (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
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean;

import org.junit.Test;

import java.io.IOException;
import java.io.UncheckedIOException;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class ExceptionsTestCase {

    @Test
    public void testToMessageStrings() {
        assertEquals("Blah",Exceptions.toMessageString(new Exception("Blah")));
        assertEquals("Blah", Exceptions.toMessageString(new Exception(new Exception("Blah"))));
        assertEquals("Exception",Exceptions.toMessageString(new Exception()));
        assertEquals("Foo: Blah",Exceptions.toMessageString(new Exception("Foo",new Exception(new IllegalArgumentException("Blah")))));
        assertEquals("Foo",Exceptions.toMessageString(new Exception("Foo",new Exception("Foo"))));
        assertEquals("Foo: Exception",Exceptions.toMessageString(new Exception("Foo",new Exception())));
        assertEquals("Foo",Exceptions.toMessageString(new Exception(new Exception("Foo"))));
    }

    @Test
    public void testUnchecks() {
        try {
            Exceptions.uncheck(this::throwIO);
        } catch (UncheckedIOException e) {
            assertEquals("root cause", e.getCause().getMessage());
        }

        try {
            Exceptions.uncheck(this::throwIO, "additional %s", "info");
        } catch (UncheckedIOException e) {
            assertEquals("additional info", e.getMessage());
        }

        try {
            int i = Exceptions.uncheck(this::throwIOWithReturnValue);
        } catch (UncheckedIOException e) {
            assertEquals("root cause", e.getCause().getMessage());
        }

        try {
            int i = Exceptions.uncheck(this::throwIOWithReturnValue, "additional %s", "info");
        } catch (UncheckedIOException e) {
            assertEquals("additional info", e.getMessage());
        }
    }

    private void throwIO() throws IOException {
        throw new IOException("root cause");
    }

    private int throwIOWithReturnValue() throws IOException {
        throw new IOException("root cause");
    }
}