summaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/test/java/com/yahoo/container/plugin/bundle/AnalyzeBundleTest.java
blob: 133cca164f8a0f9d0c170c87ba6868d240661a61 (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
// 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.bundle;

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

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import static com.yahoo.container.plugin.classanalysis.TestUtilities.throwableMessage;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class AnalyzeBundleTest {
    private final List<Export> exports;
    private final Map<String, Export> exportsByPackageName;

    File jarDir = new File("src/test/resources/jar");

    public AnalyzeBundleTest() {
        File notOsgi = new File(jarDir, "notAOsgiBundle.jar");
        File simple = new File(jarDir, "simple1.jar");
        exports = AnalyzeBundle.exportedPackagesAggregated(Arrays.asList(notOsgi, simple));
        exportsByPackageName = ExportPackages.exportsByPackageName(this.exports);
    }

    private File jarFile(String name) {
        return new File(jarDir, name);
    }

    @Test
    public void require_that_non_osgi_bundles_are_ignored() {
        assertThat(exportsByPackageName.keySet(), not(hasItem("com.yahoo.sample.exported.package.ignored")));
    }

    @Test
    public void require_that_exports_are_retrieved_from_manifest_in_jars() {
        assertThat(exportsByPackageName.keySet().size(), is(1));
        assertThat(exportsByPackageName.keySet(), hasItem("com.yahoo.sample.exported.package"));
    }

    @Test
    public void exported_class_names_can_be_retrieved() {
        assertThat(ExportPackages.packageNames(exports), is(new HashSet<>(exports.get(0).getPackageNames())));
    }

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

    @Test
    public void require_that_invalid_exports_throws_exception() {
        exception.expect(Exception.class);

        exception.expectMessage(containsString("Invalid manifest in bundle"));
        exception.expectMessage(matchesPattern("Invalid manifest in bundle '.*errorExport.jar'"));
        exception.expectCause(throwableMessage(startsWith("Failed parsing Export-Package")));

        AnalyzeBundle.exportedPackages(jarFile("errorExport.jar"));
    }

    private TypeSafeMatcher<String> matchesPattern(String pattern) {
        return new TypeSafeMatcher<String>() {
            @Override
            protected boolean matchesSafely(String s) {
                return s.matches(pattern);
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("expects String that matches the pattern ").appendValue(pattern);
            }
        };
    }
}