1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.composite;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import org.apache.commons.functor.BaseFunctorTest;
24 import org.apache.commons.functor.core.Constant;
25 import org.junit.Test;
26
27
28
29
30 public class TestCompositePredicate extends BaseFunctorTest {
31
32
33
34
35 @Override
36 protected Object makeFunctor() {
37 return Composite.predicate(Constant.TRUE);
38 }
39
40
41
42
43 @Test
44 public void testTest() throws Exception {
45 assertTrue(Composite.predicate(Constant.TRUE).test(null));
46 assertTrue(Composite.predicate(Constant.TRUE, Constant.of(3)).test("xyzzy"));
47 assertFalse(Composite.predicate(Constant.FALSE, Constant.of(4)).test("xyzzy"));
48 }
49
50 @Test(expected = NullPointerException.class)
51 public void testNullNotAllowed() throws Exception {
52 new CompositePredicate<Object>(null);
53 }
54
55 @Test(expected = NullPointerException.class)
56 public void testNullNotAllowed2() throws Exception {
57 Composite.function(Constant.TRUE, null);
58 }
59
60 @Test
61 public void testOf() throws Exception {
62 CompositePredicate<Object> f = new CompositePredicate<Object>(Constant.TRUE);
63 assertTrue(f.test(null));
64 for (int i=0;i<10;i++) {
65 f = f.of(Constant.FALSE);
66 assertTrue(f.test(null));
67 }
68 }
69
70 @Test
71 public void testEquals() throws Exception {
72 CompositePredicate<Object> f = new CompositePredicate<Object>(Constant.TRUE);
73 assertEquals(f,f);
74 CompositePredicate<Object> g = new CompositePredicate<Object>(Constant.TRUE);
75 assertObjectsAreEqual(f,g);
76
77 for (int i=0;i<3;i++) {
78 f = f.of(Constant.of("x"));
79 assertObjectsAreNotEqual(f,g);
80 g = g.of(Constant.of("x"));
81 assertObjectsAreEqual(f,g);
82 f = f.of(Constant.of("y")).of(Constant.of("z"));
83 assertObjectsAreNotEqual(f,g);
84 g = g.of(Constant.of("y")).of(Constant.of("z"));
85 assertObjectsAreEqual(f,g);
86 }
87
88 assertObjectsAreNotEqual(f,Constant.FALSE);
89 assertTrue(!f.equals(null));
90 }
91
92 }