View Javadoc

1   /**
2    * Copyright 2005-2012 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          assertTrue(op.compare("StringOne", "StringOne"));
35          assertTrue(op.compare(123, "123"));
36          assertTrue(op.compare(BigInteger.TEN, "10"));
37  //        assertFalse(op.compare(11, "elf"));  // throws:
38  // 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
39  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.coerceRhsHelper
40  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.coerceRhs
41  //        at org.kuali.rice.krms.framework.engine.expression.ComparisonOperator.compare
42      }
43  
44      @Test
45      public void testNotEquals() {
46          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.NOT_EQUALS.toString());
47          assertTrue(op.compare("StringOne", "StringTwo"));
48          assertTrue(op.compare(122, "123"));
49          assertTrue(op.compare(BigInteger.TEN, "11"));
50          assertTrue(op.compare(null, "124"));
51          assertFalse(op.compare(null, null));
52      }
53  
54      @Test
55      public void testLess() {
56          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.LESS_THAN.toString());
57          assertTrue(op.compare(123, "124"));
58          assertTrue(op.compare(new Double(123.2), "124"));
59          assertTrue(op.compare(null, "124"));
60          assertFalse(op.compare(124, null));
61          assertFalse(op.compare(null, null));
62          assertFalse(op.compare(123, "123"));
63          assertFalse(op.compare(new Integer(123), "122"));
64          assertFalse(op.compare(122.1, "122"));
65      }
66  
67      @Test
68      public void testLessThanEqual() {
69          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.LESS_THAN_EQUAL.toString());
70          assertTrue(op.compare(123, "124"));
71          assertTrue(op.compare(123.1, "123.1"));
72          assertTrue(op.compare(null, null));
73          assertFalse(op.compare(new Double(123.1), "123.01"));
74          assertFalse(op.compare(123, "122"));
75      }
76  
77      @Test
78      public void testGreaterThanEqual() {
79          ComparisonOperator op = ComparisonOperator.fromCode(ComparisonOperator.GREATER_THAN_EQUAL.toString());
80          assertFalse(op.compare(123, "124"));
81          assertTrue(op.compare(123.1, "123.1"));
82          assertFalse(op.compare(null, "124"));
83          assertTrue(op.compare(124, null));
84          assertTrue(op.compare(null, null));
85          assertTrue(op.compare(new Double(123.1), "123.01"));
86          assertTrue(op.compare(123, "122"));
87      }
88  }