aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/exception_classes/exception_classes_test.cpp
blob: ffe0f21ceb8a1bbdfdc3fc563eb6e94667e7be93 (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
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/stringfmt.h>

using namespace vespalib;

TEST("require that PortListenException retains relevant information") {
    PortListenException error(80, "HTTP", "details", VESPA_STRLOC, 0);
    try {
        error.throwSelf();
        ASSERT_TRUE(false);
    } catch(PortListenException & e) {
        fprintf(stderr, "what: %s\n", e.what());
        EXPECT_EQUAL(80, e.get_port());
        EXPECT_EQUAL("HTTP", e.get_protocol());
        EXPECT_TRUE(e.getCause() == nullptr);
    }
}

TEST("require that PortListenException with cause retains relevant information") {
    Exception root("root");
    PortListenException error(1337, "RPC", root, "details", VESPA_STRLOC, 0);
    try {
        error.throwSelf();
        ASSERT_TRUE(false);
    } catch(PortListenException & e) {
        fprintf(stderr, "what: %s\n", e.what());
        EXPECT_EQUAL(1337, e.get_port());
        EXPECT_EQUAL("RPC", e.get_protocol());
        EXPECT_TRUE(e.getCause() != nullptr);
        EXPECT_TRUE(e.getCause() != &root);
        EXPECT_EQUAL("root", e.getCause()->getMessage());
    }
}

TEST("test that OOMException carries message forward.") {
    const char * M = "This is the simple message.";
    bool caught(false);
    try {
        throw OOMException(M);
        ASSERT_TRUE(false);
    } catch (OOMException & e) {
        EXPECT_EQUAL(0, strcmp(M, e.what()));
        caught = true;
    }
    EXPECT_TRUE(caught);
}

TEST("require that rethrow_if_unsafe will rethrow unsafe exception") {
    try {
        try {
            throw OOMException("my message");
        } catch (const std::exception &e) {
            rethrow_if_unsafe(e);
            TEST_ERROR("should not be reached");
        }
    } catch (const OOMException &) {}
}

TEST("require that rethrow_if_unsafe will not rethrow safe exception") {
    try {
        throw IllegalArgumentException("my message");
    } catch (const std::exception &e) {
        rethrow_if_unsafe(e);
    }
}

TEST_MAIN() { TEST_RUN_ALL(); }