Project

General

Profile

Download (3.02 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.model.metadata;
10

    
11
import java.util.Arrays;
12
import java.util.List;
13

    
14
import org.junit.Assert;
15
import org.junit.Before;
16
import org.junit.Test;
17

    
18
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
19

    
20
/**
21
 * @author a.mueller
22
 * @since 29.11.2018
23
 *
24
 */
25
public class PreferenceResolverTest {
26

    
27
    CdmPreference pref1;
28
    CdmPreference pref2;
29
    CdmPreference pref3;
30
    CdmPreference pref3b;
31
    CdmPreference pref4;
32
    PrefKey key;
33

    
34
    /**
35
     * @throws java.lang.Exception
36
     */
37
    @Before
38
    public void setUp() throws Exception {
39
        pref1 = CdmPreference.NewInstance(PreferenceSubject.NewInstance("/taxeditor/distributionEditor/"),
40
                PreferencePredicate.AvailableDistributionAreaVocabularies,
41
                "abc");
42
        pref2 = CdmPreference.NewInstance(PreferenceSubject.NewInstance("/"),
43
                PreferencePredicate.AvailableDistributionAreaVocabularies,
44
                "abc");
45
        pref3 = CdmPreference.NewInstance(PreferenceSubject.NewInstance("/distributionEditor/"),
46
                PreferencePredicate.AvailableDistributionAreaVocabularies,
47
                "abc");
48
        pref3b = CdmPreference.NewInstance(PreferenceSubject.NewInstance("/distributionEditor/"),
49
                PreferencePredicate.AvailableDistributionAreaVocabularies,
50
                "def");
51
        pref4 = CdmPreference.NewInstance(PreferenceSubject.NewInstance("/vaadin/distributionEditor/areas/"),
52
                PreferencePredicate.AvailableDistributionAreaVocabularies,
53
                "abc");
54
        key = CdmPreference.NewKey(PreferenceSubject.NewInstance("/taxeditor/distributionEditor/areas/"),
55
                PreferencePredicate.AvailableDistributionAreaVocabularies);
56
    }
57

    
58
    @Test
59
    public void test() {
60
        List<CdmPreference> list = Arrays.asList(new CdmPreference[]{pref2, pref1, pref3, pref4}) ;
61
        CdmPreference result = PreferenceResolver.resolve(list, key);
62
        Assert.assertSame(pref1, result);
63

    
64
        list = Arrays.asList(new CdmPreference[]{pref2, pref3, pref4}) ;
65
        result = PreferenceResolver.resolve(list, key);
66
        Assert.assertSame(pref3, result);
67
    }
68

    
69
    @Test
70
    public void testException() {
71
        List<CdmPreference> list;
72
        CdmPreference result;
73
        //assure pref3 is best matching
74
        list = Arrays.asList(new CdmPreference[]{pref2, pref3, pref4}) ;
75
        result = PreferenceResolver.resolve(list, key);
76
        Assert.assertSame(pref3, result);
77

    
78
        list = Arrays.asList(new CdmPreference[]{pref2, pref3, pref3b, pref4}) ;
79
        try {
80
            result = PreferenceResolver.resolve(list, key);
81
            Assert.fail();
82
        } catch (IllegalArgumentException e) {
83
            Assert.assertEquals(PreferenceResolver.MULTI_BEST_MATCHING, e.getMessage());
84
        }
85
    }
86

    
87

    
88

    
89
}
(3-3/4)