summaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/test/java/com/yahoo/container/plugin/osgi/ImportPackageTest.java
blob: 23960323a316be259f5b24f62740126c0ad59429 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.osgi;

import com.yahoo.container.plugin.osgi.ExportPackages.Export;
import com.yahoo.container.plugin.osgi.ExportPackages.Parameter;
import com.yahoo.container.plugin.osgi.ImportPackages.Import;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class ImportPackageTest {
    private static final String PACKAGE_NAME = "com.yahoo.exported";
    private Set<String> referencedPackages = Collections.singleton(PACKAGE_NAME);
    private Map<String, Export> exports = exportByPackageName(new Export(singletonList(PACKAGE_NAME), Collections.emptyList()));
    private Map<String, Export> exportsWithVersion = exportByPackageName(
            new Export(singletonList(PACKAGE_NAME), singletonList(new Parameter("version", "1.3"))));

    private static Map<String, Export> exportByPackageName(Export export) {
        return ExportPackages.exportsByPackageName(singletonList(export));
    }

    @Test
    public void require_that_non_implemented_import_with_matching_export_is_included() {
        Set<Import> imports = calculateImports(referencedPackages, emptySet(), exports);
        assertThat(imports, contains(importMatching(PACKAGE_NAME, "")));
    }

    @Test
    public void require_that_non_implemented_import_without_matching_export_is_excluded() {
        Set<Import> imports = calculateImports(referencedPackages, emptySet(), emptyMap());
        assertThat(imports, empty());
    }

    @Test
    public void require_that_implemented_import_with_matching_export_is_excluded() {
        Set<Import> imports = calculateImports(referencedPackages, referencedPackages, exports);

        assertThat(imports, empty());
    }

    @Test
    public void require_that_version_is_included() {
        Set<Import> imports = calculateImports(referencedPackages, emptySet(), exportsWithVersion);

        assertThat(imports, contains(importMatching(PACKAGE_NAME, "1.3")));
    }

    @Test
    public void require_that_all_versions_up_to_the_next_major_version_is_in_range() {
        assertThat(new Import("foo", Optional.of("1.2")).importVersionRange().get(), is("[1.2,2)"));
    }

    // TODO: Detecting guava packages should be based on bundle-symbolicName, not package name.
    @Test
    public void require_that_for_guava_all_future_major_versions_are_in_range() {
        Optional<String> rangeWithInfiniteUpperLimit = Optional.of("[18.1," + ImportPackages.INFINITE_VERSION + ")");
        assertThat(new Import("com.google.common", Optional.of("18.1")).importVersionRange(), is(rangeWithInfiniteUpperLimit));
        assertThat(new Import("com.google.common.foo", Optional.of("18.1")).importVersionRange(), is(rangeWithInfiniteUpperLimit));
        assertThat(new Import("com.google.commonality", Optional.of("18.1")).importVersionRange(), is(Optional.of("[18.1,19)")));
    }

    @Test
    public void require_that_none_version_gives_non_version_range() {
        assertThat(new Import("foo", Optional.empty()).importVersionRange(), is(Optional.empty()));
    }

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void require_that_exception_is_thrown_when_major_component_is_non_numeric() {
        expectedException.expect(IllegalArgumentException.class);
        new Import("foo", Optional.of("1notValid.2"));
    }

    @Test
    public void require_that_osgi_import_supports_missing_version() {
        assertThat(new Import("com.yahoo.exported", Optional.empty()).asOsgiImport(), is("com.yahoo.exported"));
    }

    @Test
    public void require_that_osgi_import_version_range_includes_all_versions_from_the_current_up_to_the_next_major_version() {
        assertThat(new Import("com.yahoo.exported", Optional.of("1.2")).asOsgiImport(), is("com.yahoo.exported;version=\"[1.2,2)\""));
    }

    @Test
    public void require_that_osgi_import_version_range_ignores_qualifier() {
        assertThat(new Import("com.yahoo.exported", Optional.of("1.2.3.qualifier")).asOsgiImport(),
                is("com.yahoo.exported;version=\"[1.2.3,2)\""));
    }

    private static Set<Import> calculateImports(Set<String> referencedPackages, Set<String> implementedPackages,
            Map<String, Export> exportedPackages) {
        return new HashSet<>(ImportPackages.calculateImports(referencedPackages, implementedPackages, exportedPackages).values());
    }

    private static TypeSafeMatcher<Import> importMatching(String packageName, String version) {
        return new TypeSafeMatcher<Import>() {
            @Override
            protected boolean matchesSafely(Import anImport) {
                return anImport.packageName().equals(packageName) && anImport.version().equals(version);
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("an Import of package ").appendValue(packageName).appendText(" with version ").appendValue(version);
            }
        };
    }
}