aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/test/java/com/yahoo/document/predicate/NegationTest.java
blob: 904dce795c3b854b383594bf3766dc6566d0981e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class NegationTest {

    @Test
    public void requireThatNegationIsAnOperator() {
        assertTrue(PredicateOperator.class.isAssignableFrom(Negation.class));
    }

    @Test
    public void requireThatAccessorsWork() {
        Predicate foo = SimplePredicates.newString("foo");
        Negation node = new Negation(foo);
        assertSame(foo, node.getOperand());

        Predicate bar = SimplePredicates.newString("bar");
        node.setOperand(bar);
        assertSame(bar, node.getOperand());
    }

    @Test
    public void requireThatCloneIsImplemented() throws CloneNotSupportedException {
        Negation node1 = new Negation(SimplePredicates.newString("a"));
        Negation node2 = node1.clone();
        assertEquals(node1, node2);
        assertNotSame(node1, node2);
        assertNotSame(node1.getOperand(), node2.getOperand());
    }

    @Test
    public void requireThatHashCodeIsImplemented() {
        Predicate predicate = SimplePredicates.newPredicate();
        assertEquals(new Negation(predicate).hashCode(), new Negation(predicate).hashCode());
    }

    @Test
    public void requireThatEqualsIsImplemented() {
        Negation lhs = new Negation(SimplePredicates.newString("foo"));
        assertTrue(lhs.equals(lhs));
        assertFalse(lhs.equals(new Object()));

        Negation rhs = new Negation(SimplePredicates.newString("bar"));
        assertFalse(lhs.equals(rhs));
        rhs.setOperand(SimplePredicates.newString("foo"));
        assertTrue(lhs.equals(rhs));
    }

    @Test
    public void requireThatChildIsMandatoryInConstructor() {
        try {
            new Negation(null);
            fail();
        } catch (NullPointerException e) {
            assertEquals("operand", e.getMessage());
        }
    }

    @Test
    public void requireThatChildIsMandatoryInSetter() {
        Predicate operand = SimplePredicates.newPredicate();
        Negation negation = new Negation(operand);
        try {
            negation.setOperand(null);
            fail();
        } catch (NullPointerException e) {
            assertEquals("operand", e.getMessage());
        }
        assertSame(operand, negation.getOperand());
    }

    @Test
    public void requireThatChildIsIncludedInToString() {
        assertEquals("not (foo)", new Negation(SimplePredicates.newString("foo")).toString());
    }

}