summaryrefslogtreecommitdiffstats
path: root/container-di/src/test/java/com/yahoo/container/di/componentgraph/cycle/CycleFinderTest.java
blob: 219aa6b5e8b30b0d65f487566f6ec5824d6c445e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
 */

package com.yahoo.container.di.componentgraph.cycle;

import org.junit.Test;

import static com.yahoo.container.di.componentgraph.cycle.CycleFinderTest.Vertices.A;
import static com.yahoo.container.di.componentgraph.cycle.CycleFinderTest.Vertices.B;
import static com.yahoo.container.di.componentgraph.cycle.CycleFinderTest.Vertices.C;
import static com.yahoo.container.di.componentgraph.cycle.CycleFinderTest.Vertices.D;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertThat;

/**
 * @author gjoranv
 */
public class CycleFinderTest {

    enum Vertices {A, B, C, D}

    @Test
    public void graph_without_cycles_returns_no_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, B);
        graph.edge(B, C);
        graph.edge(A, C);
        graph.edge(D, A);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), empty());
    }

    @Test
    public void graph_with_cycle_returns_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, B);
        graph.edge(B, C);
        graph.edge(C, A);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), contains(A, B, C, A));
    }

    @Test
    public void graph_with_self_referencing_vertex_returns_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, A);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), contains(A, A));
    }

    @Test
    public void leading_nodes_are_stripped_from_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, B);
        graph.edge(B, C);
        graph.edge(C, B);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), contains(B, C, B));
    }

    @Test
    public void findCycle_is_idempotent_with_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, A);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), contains(A, A));
        assertThat(cycleFinder.findCycle(), contains(A, A));
    }

    @Test
    public void findCycle_is_idempotent_without_cycle() {
        var graph = new Graph<Vertices>();
        graph.edge(A, B);

        var cycleFinder = new CycleFinder<>(graph);
        assertThat(cycleFinder.findCycle(), empty());
        assertThat(cycleFinder.findCycle(), empty());
    }

}