View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.framework.engine.expression;
17  
18  import java.math.BigInteger;
19  
20  import org.junit.Test;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  /**
25   *
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  
30  public class ComparisonOperatorTest {
31      @Test
32      public void testEquals() {
33          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.EQUALS.toString());
34          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
35          assertTrue(op.compare("StringOne", "StringOne"));
36          assertTrue(op.compare(123, "123"));
37          assertTrue(op.compare(BigInteger.TEN, "10"));
38  //        assertFalse(op.compare(11, "elf"));  // throws:
39  // org.kuali.rice.krms.api.engine.IncompatibleTypeException: Could not coerce String to Integer = -> Type should have been one of [java.lang.Integer] but was java.lang.String
40  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.coerceRhsHelper
41  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.coerceRhs
42  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.compare
43      }
44  
45      @Test
46      public void testNotEquals() {
47          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.NOT_EQUALS.toString());
48          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
49          assertTrue(op.compare("StringOne", "StringTwo"));
50          assertTrue(op.compare(122, "123"));
51          assertTrue(op.compare(BigInteger.TEN, "11"));
52          assertTrue(op.compare(null, "124"));
53          assertFalse(op.compare(null, null));
54      }
55  
56      @Test
57      public void testLess() {
58          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.LESS_THAN.toString());
59          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
60          assertTrue(op.compare(123, "124"));
61          assertTrue(op.compare(new Double(123.2), "124"));
62          assertTrue(op.compare(null, "124"));
63          assertFalse(op.compare(124, null));
64          assertFalse(op.compare(null, null));
65          assertFalse(op.compare(123, "123"));
66          assertFalse(op.compare(new Integer(123), "122"));
67          assertFalse(op.compare(122.1, "122"));
68      }
69  
70      @Test
71      public void testLessThanEqual() {
72          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.LESS_THAN_EQUAL.toString());
73          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
74          assertTrue(op.compare(123, "124"));
75          assertTrue(op.compare(123.1, "123.1"));
76          assertTrue(op.compare(null, null));
77          assertFalse(op.compare(new Double(123.1), "123.01"));
78          assertFalse(op.compare(123, "122"));
79      }
80  
81      @Test
82      public void testGreaterThanEqual() {
83          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.GREATER_THAN_EQUAL.toString());
84          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
85          assertFalse(op.compare(123, "124"));
86          assertTrue(op.compare(123.1, "123.1"));
87          assertFalse(op.compare(null, "124"));
88          assertTrue(op.compare(124, null));
89          assertTrue(op.compare(null, null));
90          assertTrue(op.compare(new Double(123.1), "123.01"));
91          assertTrue(op.compare(123, "122"));
92      }
93  
94  
95      @Test
96      public void testExists() {
97          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.EXISTS.toString());
98          op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
99          assertTrue(op.compare("123", "0"));
100         assertFalse(op.compare("123", null));
101     }
102 
103     @Test
104     public void testDoesNotExists() {
105         ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.DOES_NOT_EXIST.toString());
106         op.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
107         assertFalse(op.compare("123", "0"));
108         assertTrue(op.compare("123", null));
109     }
110 }