summaryrefslogtreecommitdiffstats
path: root/sangria-core/src/test/java/com/tavianator/sangria/core/PriorityTest.java
blob: 0869565987f318b7e86f7752218d20820015fa29 (plain)
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
/****************************************************************************
 * Sangria                                                                  *
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>             *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 * http://www.apache.org/licenses/LICENSE-2.0                               *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/

package com.tavianator.sangria.core;

import java.util.*;

import org.junit.Test;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

/**
 * Tests for {@link Priority}s.
 *
 * @author Tavian Barnes (tavianator@tavianator.com)
 * @version 1.1
 * @since 1.1
 */
public class PriorityTest {
    private final Priority defaultPriority = Priority.getDefault();
    private final Priority one = Priority.create(1);
    private final Priority oneTwo = Priority.create(1, 2);
    private final Priority two = Priority.create(2);

    @Test
    public void testOrdering() {
        List<Priority> list = Arrays.asList(
                defaultPriority.next(),
                two,
                oneTwo.next(),
                oneTwo,
                two.next(),
                defaultPriority,
                defaultPriority.next().next(),
                one,
                one.next());
        Collections.sort(list);
        assertThat(list, contains(
                defaultPriority,
                defaultPriority.next(),
                defaultPriority.next().next(),
                one,
                one.next(),
                oneTwo,
                oneTwo.next(),
                two,
                two.next()));

        assertThat(defaultPriority, equalTo(Priority.getDefault()));
        assertThat(defaultPriority.next(), equalTo(Priority.getDefault().next()));
        assertThat(defaultPriority, not(equalTo(Priority.getDefault().next())));

        assertThat(one, equalTo(Priority.create(1)));
        assertThat(oneTwo, equalTo(Priority.create(1, 2)));
        assertThat(two, equalTo(Priority.create(2)));

        assertThat(oneTwo.hashCode(), equalTo(Priority.create(1, 2).hashCode()));
    }

    @Test
    public void testIsDefault() {
        assertThat(defaultPriority.isDefault(), is(true));
        assertThat(defaultPriority.next().isDefault(), is(true));

        assertThat(one.isDefault(), is(false));
        assertThat(oneTwo.isDefault(), is(false));
        assertThat(two.isDefault(), is(false));
        assertThat(two.next().isDefault(), is(false));
    }

    @Test
    public void testToString() {
        assertThat(Priority.getDefault().toString(), equalTo("default priority"));
        assertThat(Priority.getDefault().next().toString(), equalTo("default priority + 1"));
        assertThat(Priority.getDefault().next().next().toString(), equalTo("default priority + 2"));

        assertThat(Priority.create(1).toString(), equalTo("priority [1]"));
        assertThat(Priority.create(1).next().toString(), equalTo("priority [1] + 1"));
        assertThat(Priority.create(1).next().next().toString(), equalTo("priority [1] + 2"));

        assertThat(Priority.create(1, 2).toString(), equalTo("priority [1, 2]"));
        assertThat(Priority.create(1, 2).next().toString(), equalTo("priority [1, 2] + 1"));
        assertThat(Priority.create(1, 2).next().next().toString(), equalTo("priority [1, 2] + 2"));
    }
}