aboutsummaryrefslogtreecommitdiffstats
path: root/component/src/test/java/com/yahoo/component/VersionCompatibilityTest.java
blob: 45c94591b1191b7515802d6f1dffe8e9f8d14a2c (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author jonmv
 */
public class VersionCompatibilityTest {

    @Test
    void testNoIncompatibilities() {
        List<String> versions = List.of();
        VersionCompatibility compatibility = VersionCompatibility.fromVersionList(versions);
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(0, 0, 0)));
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(0, 0, 1)));
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(0, 1, 0)));
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(1, 0, 0)));
    }

    @Test
    void testValidIncompatibilities() {
        List<String> versions = List.of("1.2.*", "2", "3.*", "4.0.0", "4.0.1", "4.0.3", "4.1.0", "5");
        VersionCompatibility compatibility = VersionCompatibility.fromVersionList(versions);
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(0, 0, 0)));
        assertTrue(compatibility.accept(new Version(0, 0, 0), new Version(1, 1, 1)));
        assertFalse(compatibility.accept(new Version(0, 0, 0), new Version(1, 2, 3)));
        assertFalse(compatibility.accept(new Version(1, 1, 0), new Version(1, 2, 0)));
        assertFalse(compatibility.accept(new Version(1, 2, 1), new Version(1, 2, 0)));
        assertFalse(compatibility.accept(new Version(1, 1, 0), new Version(1, 3, 0)));
        assertTrue(compatibility.accept(new Version(1, 2, 3), new Version(1, 2, 3)));
        assertTrue(compatibility.accept(new Version(1, 3, 0), new Version(1, 9, 9)));
        assertFalse(compatibility.accept(new Version(1, 3, 0), new Version(2, 0, 0)));
        assertTrue(compatibility.accept(new Version(2, 0, 0), new Version(2, 2, 2)));
        assertFalse(compatibility.accept(new Version(2, 0, 0), new Version(3, 0, 0)));
        assertTrue(compatibility.accept(new Version(3, 0, 0), new Version(3, 0, 0)));
        assertFalse(compatibility.accept(new Version(3, 0, 0), new Version(3, 1, 0)));
        assertTrue(compatibility.accept(new Version(3, 0, 0), new Version(3, 0, 1)));
        assertFalse(compatibility.accept(new Version(3, 0, 0), new Version(4, 0, 0)));
        assertFalse(compatibility.accept(new Version(4, 0, 0), new Version(4, 0, 1)));
        assertTrue(compatibility.accept(new Version(4, 0, 1), new Version(4, 0, 2)));
        assertFalse(compatibility.accept(new Version(4, 0, 2), new Version(4, 0, 3)));
        assertFalse(compatibility.accept(new Version(4, 0, 3), new Version(4, 1, 0)));
        assertFalse(compatibility.accept(new Version(4, 1, 0), new Version(5, 0, 0)));
        assertTrue(compatibility.accept(new Version(5, 0, 0), new Version(6, 0, 0)));
        assertFalse(compatibility.accept(new Version(0, 0, 0), new Version(2, 0, 0)));
        assertFalse(compatibility.accept(new Version(0, 0, 0), new Version(6, 0, 0)));
    }

    @Test
    void testIllegalIncompatibilities() {
        assertThrows(List.of("1", "*"),         IllegalArgumentException.class, "may not have siblings");
        assertThrows(List.of("*", "*.*"),       IllegalArgumentException.class, "may not have siblings");
        assertThrows(List.of("*", "*"),         IllegalArgumentException.class, "may not have siblings");
        assertThrows(List.of("*.1"),            IllegalArgumentException.class, "may only have wildcard children");
        assertThrows(List.of("0", "0"),         IllegalArgumentException.class, "duplicate element");
        assertThrows(List.of("0", "0.0.0"),     IllegalArgumentException.class, "duplicate element");
        assertThrows(List.of("0.0.0", "0.0.0"), IllegalArgumentException.class, "duplicate element");
        assertThrows(List.of("."),              IllegalArgumentException.class, "1 to 3 parts");
        assertThrows(List.of("0.0.0.0"),        IllegalArgumentException.class, "1 to 3 parts");
        assertThrows(List.of("-1"),             IllegalArgumentException.class, "must be non-negative");
        assertThrows(List.of(""),               NumberFormatException.class,    "input string: \"\"");
        assertThrows(List.of("x"),              NumberFormatException.class,    "input string: \"x\"");
    }

    static void assertThrows(List<String> spec, Class<? extends IllegalArgumentException> clazz, String fragment) {
        IllegalArgumentException thrown = null;
        try {
            VersionCompatibility.fromVersionList(spec);
        }
        catch (IllegalArgumentException e) {
            if (clazz.isInstance(e) && e.getMessage().toLowerCase(Locale.ROOT).contains(fragment.toLowerCase(Locale.ROOT)))
                return;
            thrown = e;
        }
        fail("Should fail with " + clazz.getSimpleName() + " containing '" + fragment + "' in its message, but got " +
             (thrown == null ? "no exception" : "'" + thrown + "'"));
    }

}