aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/TaggableItemsTestCase.java
blob: ec2a69fb9b03e94b18e9b48c63143de7edda7a4d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import static org.junit.jupiter.api.Assertions.*;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * Keep CompositeTaggableItem, SimpleTaggableItem and TaggableSegmentItem in
 * lockstep.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class TaggableItemsTestCase {

    @BeforeEach
    public void setUp() throws Exception {
    }

    @AfterEach
    public void tearDown() throws Exception {
    }

    private static class ApiMethod {
        @Override
        public int hashCode() {
            return name.hashCode();
        }

        @Override
        public boolean equals(final Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final ApiMethod other = (ApiMethod) obj;
            if (!name.equals(other.name)) {
                return false;
            }
            if (parameterTypes.length != other.parameterTypes.length) {
                return false;
            }
            for (int i = 0; i < parameterTypes.length; ++i) {
                if (parameterTypes[i] != other.parameterTypes[i]) {
                    return false;
                }
            }
            if (returnType != other.returnType) {
                return false;
            }
            return true;
        }

        public ApiMethod(final Method method) {
            if (method == null) {
                throw new IllegalArgumentException();
            }
            name = method.getName();
            returnType = method.getReturnType();
            parameterTypes = method.getParameterTypes();
        }

        @Override
        public String toString() {
            final StringBuilder s = new StringBuilder();
            s.append(returnType.getSimpleName()).append(' ').append(name)
                    .append('(');
            final int initLen = s.length();
            for (final Class<?> c : parameterTypes) {
                if (s.length() != initLen) {
                    s.append(", ");
                }
                s.append(c.getSimpleName());
            }
            s.append(')');
            return s.toString();
        }

        private final String name;
        private final Class<?> returnType;
        private final Class<?>[] parameterTypes;

    }

    @Test
    void requireSimilarAPIs() {
        final Method[] composite = CompositeTaggableItem.class
                .getDeclaredMethods();
        final Method[] simple = SimpleTaggableItem.class.getDeclaredMethods();
        final Method[] segment = TaggableSegmentItem.class.getDeclaredMethods();
        final int numberOfMethods = 10;
        assertEquals(numberOfMethods, composite.length);
        assertEquals(numberOfMethods, simple.length);
        assertEquals(numberOfMethods, segment.length);
        final Set<ApiMethod> compositeSet = methodSet(composite);
        final Set<ApiMethod> simpleSet = methodSet(simple);
        final Set<ApiMethod> segmentSet = methodSet(segment);
        assertEquals(compositeSet, simpleSet);
        assertEquals(simpleSet, segmentSet);

    }

    public Set<ApiMethod> methodSet(final Method[] methods) {
        final Set<ApiMethod> methodSet = new HashSet<>();
        for (final Method m : methods) {
            methodSet.add(new ApiMethod(m));
        }
        return methodSet;
    }

    @Test
    final void testSetUniqueID() {
        final PhraseSegmentItem p = new PhraseSegmentItem("farmyards", false,
                false);
        assertFalse(p.hasUniqueID());
        p.setUniqueID(10);
        assertEquals(10, p.getUniqueID());
        assertTrue(p.hasUniqueID());
    }

    @Test
    final void testSetConnectivity() {
        final PhraseSegmentItem p = new PhraseSegmentItem("farmyards", false,
                false);
        assertEquals(0.0d, p.getConnectivity(), 1e-9);
        final WordItem w = new WordItem("nalle");
        final double expectedConnectivity = 37e9;
        p.setConnectivity(w, expectedConnectivity);
        assertSame(w, p.getConnectedItem());
        assertEquals(expectedConnectivity, p.getConnectivity(), 1e0);
    }

    @Test
    final void testSetSignificance() {
        final PhraseSegmentItem p = new PhraseSegmentItem("farmyards", false,
                false);
        // unset
        assertEquals(0.0d, p.getSignificance(), 1e-9);
        assertFalse(p.hasExplicitSignificance());
        p.setSignificance(500.0d);
        assertTrue(p.hasExplicitSignificance());
    }

}