aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/config/model/ConfigModelUtilsTest.java
blob: 3db4948c8c36790f15528efd22c2b433743eec62 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model;

import com.yahoo.config.model.application.provider.Bundle;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Ulf Lilleengen
 */
public class ConfigModelUtilsTest {

    public static final String VALID_TEST_BUNDLE = "src/test/cfg/application/app1/components/";
    public static final String INVALID_TEST_BUNDLE = "src/test/cfg/application/validation/invalidjar_app/components";

    @Test
    void all_def_files_in_correct_directory_are_handled_and_files_outside_are_ignored() {
        List<Bundle> bundles = Bundle.getBundles(new File(VALID_TEST_BUNDLE));
        assertEquals(1, bundles.size());
        assertEquals(5, bundles.get(0).getDefEntries().size());
    }

    @Test
    void def_file_with_namespace_is_handled() {
        Bundle.DefEntry defEntry = getDefEntry("test-namespace");
        assertEquals("config", defEntry.defNamespace);
    }

    @Test
    void def_file_with_namespace_and_namespace_in_filename_is_handled() {
        Bundle.DefEntry defEntry = getDefEntry("namespace-in-filename");
        assertEquals("a.b", defEntry.defNamespace);
    }

    @Test
    void def_file_with_package_is_handled() {
        Bundle.DefEntry defEntry = getDefEntry("test-package");
        assertEquals("com.mydomain.mypackage", defEntry.defNamespace);
    }

    @Test
    void def_file_with_package_and_pacakage_in_filename_is_handled() {
        Bundle.DefEntry defEntry = getDefEntry("package-in-filename");
        assertEquals("com.mydomain.mypackage", defEntry.defNamespace);
    }

    @Test
    void def_file_with_both_package_and_namespace_gets_package_as_namespace() {
        Bundle.DefEntry defEntry = getDefEntry("namespace-and-package");
        assertEquals("com.mydomain.mypackage", defEntry.defNamespace);
    }

    private static Bundle.DefEntry getDefEntry(String defName) {
        Bundle bundle = Bundle.getBundles(new File(VALID_TEST_BUNDLE)).get(0);

        for (Bundle.DefEntry defEntry : bundle.getDefEntries()) {
            if (defEntry.defName.equals(defName))
                return defEntry;
        }
        throw new IllegalArgumentException("No def file with name '" + defName + "' found in the test bundle.");
    }

    @Test
    void invalid_jar_file_fails_to_load() {
        try {
            Bundle.getBundles(new File(INVALID_TEST_BUNDLE));
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Error opening jar file 'invalid.jar'. Please check that this is a valid jar file", e.getMessage());
        }
    }

}