001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.el.misc;
017:
018: import java.math.BigDecimal;
019: import java.math.BigInteger;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022:
023: import junit.framework.TestCase;
024:
025: public class BooleanOperationsTest extends TestCase {
026:
027: /**
028: * Test enum type
029: */
030: static enum Foo {
031: BAR, BAZ
032: };
033:
034: /*
035: * Test method for 'de.odysseus.el.lang.BooleanOperations.lt(Object, Object)'
036: */
037: public void testLt() {
038: assertFalse(BooleanOperations.lt(Boolean.TRUE, Boolean.TRUE));
039: assertFalse(BooleanOperations.lt(null, Boolean.TRUE));
040: assertFalse(BooleanOperations.lt(Boolean.TRUE, null));
041: assertTrue(BooleanOperations.lt("1", new BigDecimal("2")));
042: assertFalse(BooleanOperations.lt(new BigDecimal("1"), "1"));
043: assertFalse(BooleanOperations.lt(new BigDecimal("2"), "1"));
044: assertTrue(BooleanOperations.lt("1", new Float("2")));
045: assertFalse(BooleanOperations.lt(new Float("1"), "1"));
046: assertFalse(BooleanOperations.lt(new Float("2"), "1"));
047: assertTrue(BooleanOperations.lt("1", new Double("2")));
048: assertFalse(BooleanOperations.lt(new Double("1"), "1"));
049: assertFalse(BooleanOperations.lt(new Double("2"), "1"));
050: assertTrue(BooleanOperations.lt("1", new BigInteger("2")));
051: assertFalse(BooleanOperations.lt(new BigInteger("1"), "1"));
052: assertFalse(BooleanOperations.lt(new BigInteger("2"), "1"));
053: assertTrue(BooleanOperations.lt("1", new Byte("2")));
054: assertFalse(BooleanOperations.lt(new Byte("1"), "1"));
055: assertFalse(BooleanOperations.lt(new Byte("2"), "1"));
056: assertTrue(BooleanOperations.lt("1", new Short("2")));
057: assertFalse(BooleanOperations.lt(new Short("1"), "1"));
058: assertFalse(BooleanOperations.lt(new Short("2"), "1"));
059: assertTrue(BooleanOperations.lt(new Character('a'),
060: new Character('b')));
061: assertFalse(BooleanOperations.lt(new Character('a'),
062: new Character('a')));
063: assertFalse(BooleanOperations.lt(new Character('b'),
064: new Character('a')));
065: assertTrue(BooleanOperations.lt("1", new Integer("2")));
066: assertFalse(BooleanOperations.lt(new Integer("1"), "1"));
067: assertFalse(BooleanOperations.lt(new Integer("2"), "1"));
068: assertTrue(BooleanOperations.lt("1", new Long("2")));
069: assertFalse(BooleanOperations.lt(new Long("1"), "1"));
070: assertFalse(BooleanOperations.lt(new Long("2"), "1"));
071: assertTrue(BooleanOperations.lt("a", "b"));
072: assertFalse(BooleanOperations.lt("a", "a"));
073: assertFalse(BooleanOperations.lt("b", "a"));
074: try {
075: BooleanOperations.lt(getClass(), new Character('a'));
076: fail();
077: } catch (Exception e) {
078: }
079: try {
080: BooleanOperations.lt(new Character('a'), getClass());
081: fail();
082: } catch (Exception e) {
083: }
084: try {
085: BooleanOperations.lt(getClass(), new Long(0));
086: fail();
087: } catch (Exception e) {
088: }
089: }
090:
091: /*
092: * Test method for 'de.odysseus.el.lang.BooleanOperations.gt(Object, Object)'
093: */
094: public void testGt() {
095: assertFalse(BooleanOperations.gt(Boolean.TRUE, Boolean.TRUE));
096: assertFalse(BooleanOperations.gt(null, Boolean.TRUE));
097: assertFalse(BooleanOperations.gt(Boolean.TRUE, null));
098: assertFalse(BooleanOperations.gt("1", new BigDecimal("2")));
099: assertFalse(BooleanOperations.gt(new BigDecimal("1"), "1"));
100: assertTrue(BooleanOperations.gt(new BigDecimal("2"), "1"));
101: assertFalse(BooleanOperations.gt("1", new Float("2")));
102: assertFalse(BooleanOperations.gt(new Float("1"), "1"));
103: assertTrue(BooleanOperations.gt(new Float("2"), "1"));
104: assertFalse(BooleanOperations.gt("1", new Double("2")));
105: assertFalse(BooleanOperations.gt(new Double("1"), "1"));
106: assertTrue(BooleanOperations.gt(new Double("2"), "1"));
107: assertFalse(BooleanOperations.gt("1", new BigInteger("2")));
108: assertFalse(BooleanOperations.gt(new BigInteger("1"), "1"));
109: assertTrue(BooleanOperations.gt(new BigInteger("2"), "1"));
110: assertFalse(BooleanOperations.gt("1", new Byte("2")));
111: assertFalse(BooleanOperations.gt(new Byte("1"), "1"));
112: assertTrue(BooleanOperations.gt(new Byte("2"), "1"));
113: assertFalse(BooleanOperations.gt("1", new Short("2")));
114: assertFalse(BooleanOperations.gt(new Short("1"), "1"));
115: assertTrue(BooleanOperations.gt(new Short("2"), "1"));
116: assertFalse(BooleanOperations.gt(new Character('a'),
117: new Character('b')));
118: assertFalse(BooleanOperations.gt(new Character('a'),
119: new Character('a')));
120: assertTrue(BooleanOperations.gt(new Character('b'),
121: new Character('a')));
122: assertFalse(BooleanOperations.gt("1", new Integer("2")));
123: assertFalse(BooleanOperations.gt(new Integer("1"), "1"));
124: assertTrue(BooleanOperations.gt(new Integer("2"), "1"));
125: assertFalse(BooleanOperations.gt("1", new Long("2")));
126: assertFalse(BooleanOperations.gt(new Long("1"), "1"));
127: assertTrue(BooleanOperations.gt(new Long("2"), "1"));
128: assertFalse(BooleanOperations.gt("a", "b"));
129: assertFalse(BooleanOperations.gt("a", "a"));
130: assertTrue(BooleanOperations.gt("b", "a"));
131: try {
132: BooleanOperations.gt(getClass(), new Character('a'));
133: fail();
134: } catch (Exception e) {
135: }
136: try {
137: BooleanOperations.gt(new Character('a'), getClass());
138: fail();
139: } catch (Exception e) {
140: }
141: try {
142: BooleanOperations.gt(getClass(), new Long(0));
143: fail();
144: } catch (Exception e) {
145: }
146: }
147:
148: /*
149: * Test method for 'de.odysseus.el.lang.BooleanOperations.ge(Object, Object)'
150: */
151: public void testGe() {
152: assertTrue(BooleanOperations.ge(Boolean.TRUE, Boolean.TRUE));
153: assertFalse(BooleanOperations.ge(null, Boolean.TRUE));
154: assertFalse(BooleanOperations.ge(Boolean.TRUE, null));
155: assertFalse(BooleanOperations.ge("1", new BigDecimal("2")));
156: assertTrue(BooleanOperations.ge(new BigDecimal("1"), "1"));
157: assertTrue(BooleanOperations.ge(new BigDecimal("2"), "1"));
158: assertFalse(BooleanOperations.ge("1", new Float("2")));
159: assertTrue(BooleanOperations.ge(new Float("1"), "1"));
160: assertTrue(BooleanOperations.ge(new Float("2"), "1"));
161: assertFalse(BooleanOperations.ge("1", new Double("2")));
162: assertTrue(BooleanOperations.ge(new Double("1"), "1"));
163: assertTrue(BooleanOperations.ge(new Double("2"), "1"));
164: assertFalse(BooleanOperations.ge("1", new BigInteger("2")));
165: assertTrue(BooleanOperations.ge(new BigInteger("1"), "1"));
166: assertTrue(BooleanOperations.ge(new BigInteger("2"), "1"));
167: assertFalse(BooleanOperations.ge("1", new Byte("2")));
168: assertTrue(BooleanOperations.ge(new Byte("1"), "1"));
169: assertTrue(BooleanOperations.ge(new Byte("2"), "1"));
170: assertFalse(BooleanOperations.ge("1", new Short("2")));
171: assertTrue(BooleanOperations.ge(new Short("1"), "1"));
172: assertTrue(BooleanOperations.ge(new Short("2"), "1"));
173: assertFalse(BooleanOperations.ge(new Character('a'),
174: new Character('b')));
175: assertTrue(BooleanOperations.ge(new Character('a'),
176: new Character('a')));
177: assertTrue(BooleanOperations.ge(new Character('b'),
178: new Character('a')));
179: assertFalse(BooleanOperations.ge("1", new Integer("2")));
180: assertTrue(BooleanOperations.ge(new Integer("1"), "1"));
181: assertTrue(BooleanOperations.ge(new Integer("2"), "1"));
182: assertFalse(BooleanOperations.ge("1", new Long("2")));
183: assertTrue(BooleanOperations.ge(new Long("1"), "1"));
184: assertTrue(BooleanOperations.ge(new Long("2"), "1"));
185: assertFalse(BooleanOperations.ge("a", "b"));
186: assertTrue(BooleanOperations.ge("a", "a"));
187: assertTrue(BooleanOperations.ge("b", "a"));
188: try {
189: BooleanOperations.ge(getClass(), new Character('a'));
190: fail();
191: } catch (Exception e) {
192: }
193: try {
194: BooleanOperations.ge(new Character('a'), getClass());
195: fail();
196: } catch (Exception e) {
197: }
198: try {
199: BooleanOperations.ge(getClass(), new Long(0));
200: fail();
201: } catch (Exception e) {
202: }
203: }
204:
205: /*
206: * Test method for 'de.odysseus.el.lang.BooleanOperations.le(Object, Object)'
207: */
208: public void testLe() {
209: assertTrue(BooleanOperations.le(Boolean.TRUE, Boolean.TRUE));
210: assertFalse(BooleanOperations.le(null, Boolean.TRUE));
211: assertFalse(BooleanOperations.le(Boolean.TRUE, null));
212: assertTrue(BooleanOperations.le("1", new BigDecimal("2")));
213: assertTrue(BooleanOperations.le(new BigDecimal("1"), "1"));
214: assertFalse(BooleanOperations.le(new BigDecimal("2"), "1"));
215: assertTrue(BooleanOperations.le("1", new Float("2")));
216: assertTrue(BooleanOperations.le(new Float("1"), "1"));
217: assertFalse(BooleanOperations.le(new Float("2"), "1"));
218: assertTrue(BooleanOperations.le("1", new Double("2")));
219: assertTrue(BooleanOperations.le(new Double("1"), "1"));
220: assertFalse(BooleanOperations.le(new Double("2"), "1"));
221: assertTrue(BooleanOperations.le("1", new BigInteger("2")));
222: assertTrue(BooleanOperations.le(new BigInteger("1"), "1"));
223: assertFalse(BooleanOperations.le(new BigInteger("2"), "1"));
224: assertTrue(BooleanOperations.le("1", new Byte("2")));
225: assertTrue(BooleanOperations.le(new Byte("1"), "1"));
226: assertFalse(BooleanOperations.le(new Byte("2"), "1"));
227: assertTrue(BooleanOperations.le("1", new Short("2")));
228: assertTrue(BooleanOperations.le(new Short("1"), "1"));
229: assertFalse(BooleanOperations.le(new Short("2"), "1"));
230: assertTrue(BooleanOperations.le(new Character('a'),
231: new Character('b')));
232: assertTrue(BooleanOperations.le(new Character('a'),
233: new Character('a')));
234: assertFalse(BooleanOperations.le(new Character('b'),
235: new Character('a')));
236: assertTrue(BooleanOperations.le("1", new Integer("2")));
237: assertTrue(BooleanOperations.le(new Integer("1"), "1"));
238: assertFalse(BooleanOperations.le(new Integer("2"), "1"));
239: assertTrue(BooleanOperations.le("1", new Long("2")));
240: assertTrue(BooleanOperations.le(new Long("1"), "1"));
241: assertFalse(BooleanOperations.le(new Long("2"), "1"));
242: assertTrue(BooleanOperations.le("a", "b"));
243: assertTrue(BooleanOperations.le("a", "a"));
244: assertFalse(BooleanOperations.le("b", "a"));
245: try {
246: BooleanOperations.le(getClass(), new Character('a'));
247: fail();
248: } catch (Exception e) {
249: }
250: try {
251: BooleanOperations.le(new Character('a'), getClass());
252: fail();
253: } catch (Exception e) {
254: }
255: try {
256: BooleanOperations.le(getClass(), new Long(0));
257: fail();
258: } catch (Exception e) {
259: }
260: }
261:
262: /*
263: * Test method for 'de.odysseus.el.lang.BooleanOperations.eq(Object, Object)'
264: */
265: public void testEq() {
266: assertTrue(BooleanOperations.eq(Boolean.TRUE, Boolean.TRUE));
267: assertFalse(BooleanOperations.eq(null, Boolean.TRUE));
268: assertFalse(BooleanOperations.eq(Boolean.TRUE, null));
269: assertFalse(BooleanOperations.eq("1", new BigDecimal("2")));
270: assertTrue(BooleanOperations.eq(new BigDecimal("1"), "1"));
271: assertFalse(BooleanOperations.eq(new BigDecimal("2"), "1"));
272: assertFalse(BooleanOperations.eq("1", new Float("2")));
273: assertTrue(BooleanOperations.eq(new Float("1"), "1"));
274: assertFalse(BooleanOperations.eq(new Float("2"), "1"));
275: assertFalse(BooleanOperations.eq("1", new Double("2")));
276: assertTrue(BooleanOperations.eq(new Double("1"), "1"));
277: assertFalse(BooleanOperations.eq(new Double("2"), "1"));
278: assertFalse(BooleanOperations.eq("1", new BigInteger("2")));
279: assertTrue(BooleanOperations.eq(new BigInteger("1"), "1"));
280: assertFalse(BooleanOperations.eq(new BigInteger("2"), "1"));
281: assertFalse(BooleanOperations.eq("1", new Byte("2")));
282: assertTrue(BooleanOperations.eq(new Byte("1"), "1"));
283: assertFalse(BooleanOperations.eq(new Byte("2"), "1"));
284: assertFalse(BooleanOperations.eq("1", new Short("2")));
285: assertTrue(BooleanOperations.eq(new Short("1"), "1"));
286: assertFalse(BooleanOperations.eq(new Short("2"), "1"));
287: assertFalse(BooleanOperations.eq(new Character('a'),
288: new Character('b')));
289: assertTrue(BooleanOperations.eq(new Character('a'),
290: new Character('a')));
291: assertFalse(BooleanOperations.eq(new Character('b'),
292: new Character('a')));
293: assertFalse(BooleanOperations.eq("1", new Integer("2")));
294: assertTrue(BooleanOperations.eq(new Integer("1"), "1"));
295: assertFalse(BooleanOperations.eq(new Integer("2"), "1"));
296: assertFalse(BooleanOperations.eq("1", new Long("2")));
297: assertTrue(BooleanOperations.eq(new Long("1"), "1"));
298: assertFalse(BooleanOperations.eq(new Long("2"), "1"));
299: assertFalse(BooleanOperations.eq(new Boolean(false),
300: new Boolean(true)));
301: assertTrue(BooleanOperations.eq(new Boolean(true), new Boolean(
302: true)));
303: assertTrue(BooleanOperations.eq(new Boolean(false),
304: new Boolean(false)));
305: assertTrue(BooleanOperations.eq(Foo.BAR, "BAR"));
306: assertTrue(BooleanOperations.eq("BAR", Foo.BAR));
307: assertFalse(BooleanOperations.eq(Foo.BAR, "BAZ"));
308: try {
309: BooleanOperations.eq(Foo.BAR, "FOO"); // coercion fails
310: fail();
311: } catch (Exception e) {
312: }
313: assertFalse(BooleanOperations.eq("a", "b"));
314: assertTrue(BooleanOperations.eq("a", "a"));
315: assertFalse(BooleanOperations.eq("b", "a"));
316: assertFalse(BooleanOperations
317: .eq(getClass(), new Character('a')));
318: assertFalse(BooleanOperations
319: .eq(new Character('a'), getClass()));
320: try {
321: BooleanOperations.eq(getClass(), new Long(0)); // coercion fails
322: fail();
323: } catch (Exception e) {
324: }
325: }
326:
327: /*
328: * Test method for 'de.odysseus.el.lang.BooleanOperations.ne(Object, Object)'
329: */
330: public void testNe() {
331: assertFalse(BooleanOperations.ne(Boolean.TRUE, Boolean.TRUE));
332: assertTrue(BooleanOperations.ne(null, Boolean.TRUE));
333: assertTrue(BooleanOperations.ne(Boolean.TRUE, null));
334: assertTrue(BooleanOperations.ne("1", new BigDecimal("2")));
335: assertFalse(BooleanOperations.ne(new BigDecimal("1"), "1"));
336: assertTrue(BooleanOperations.ne(new BigDecimal("2"), "1"));
337: assertTrue(BooleanOperations.ne("1", new Float("2")));
338: assertFalse(BooleanOperations.ne(new Float("1"), "1"));
339: assertTrue(BooleanOperations.ne(new Float("2"), "1"));
340: assertTrue(BooleanOperations.ne("1", new Double("2")));
341: assertFalse(BooleanOperations.ne(new Double("1"), "1"));
342: assertTrue(BooleanOperations.ne(new Double("2"), "1"));
343: assertTrue(BooleanOperations.ne("1", new BigInteger("2")));
344: assertFalse(BooleanOperations.ne(new BigInteger("1"), "1"));
345: assertTrue(BooleanOperations.ne(new BigInteger("2"), "1"));
346: assertTrue(BooleanOperations.ne("1", new Byte("2")));
347: assertFalse(BooleanOperations.ne(new Byte("1"), "1"));
348: assertTrue(BooleanOperations.ne(new Byte("2"), "1"));
349: assertTrue(BooleanOperations.ne("1", new Short("2")));
350: assertFalse(BooleanOperations.ne(new Short("1"), "1"));
351: assertTrue(BooleanOperations.ne(new Short("2"), "1"));
352: assertTrue(BooleanOperations.ne(new Character('a'),
353: new Character('b')));
354: assertFalse(BooleanOperations.ne(new Character('a'),
355: new Character('a')));
356: assertTrue(BooleanOperations.ne(new Character('b'),
357: new Character('a')));
358: assertTrue(BooleanOperations.ne("1", new Integer("2")));
359: assertFalse(BooleanOperations.ne(new Integer("1"), "1"));
360: assertTrue(BooleanOperations.ne(new Integer("2"), "1"));
361: assertTrue(BooleanOperations.ne("1", new Long("2")));
362: assertFalse(BooleanOperations.ne(new Long("1"), "1"));
363: assertTrue(BooleanOperations.ne(new Long("2"), "1"));
364: assertTrue(BooleanOperations.ne(new Boolean(false),
365: new Boolean(true)));
366: assertFalse(BooleanOperations.ne(new Boolean(true),
367: new Boolean(true)));
368: assertFalse(BooleanOperations.ne(new Boolean(false),
369: new Boolean(false)));
370: assertFalse(BooleanOperations.ne(Foo.BAR, "BAR"));
371: assertFalse(BooleanOperations.ne("BAR", Foo.BAR));
372: assertTrue(BooleanOperations.ne(Foo.BAR, "BAZ"));
373: try {
374: BooleanOperations.ne(Foo.BAR, "FOO"); // coercion fails
375: fail();
376: } catch (Exception e) {
377: }
378: assertTrue(BooleanOperations.ne("a", "b"));
379: assertFalse(BooleanOperations.ne("a", "a"));
380: assertTrue(BooleanOperations.ne("b", "a"));
381: assertTrue(BooleanOperations.ne(getClass(), new Character('a')));
382: assertTrue(BooleanOperations.ne(new Character('a'), getClass()));
383: try {
384: BooleanOperations.ne(getClass(), new Long(0)); // coercion fails
385: fail();
386: } catch (Exception e) {
387: }
388: }
389:
390: /*
391: * Test method for 'de.odysseus.el.lang.BooleanOperations.and(Object, Object)'
392: */
393: public void testAnd() {
394: assertFalse(BooleanOperations.and(Boolean.FALSE, Boolean.FALSE));
395: assertFalse(BooleanOperations.and(Boolean.FALSE, Boolean.TRUE));
396: assertFalse(BooleanOperations.and(Boolean.TRUE, Boolean.FALSE));
397: assertTrue(BooleanOperations.and(Boolean.TRUE, Boolean.TRUE));
398: }
399:
400: /*
401: * Test method for 'de.odysseus.el.lang.BooleanOperations.or(Object, Object)'
402: */
403: public void testOr() {
404: assertFalse(BooleanOperations.or(Boolean.FALSE, Boolean.FALSE));
405: assertTrue(BooleanOperations.or(Boolean.FALSE, Boolean.TRUE));
406: assertTrue(BooleanOperations.or(Boolean.TRUE, Boolean.FALSE));
407: assertTrue(BooleanOperations.or(Boolean.TRUE, Boolean.TRUE));
408: }
409:
410: /*
411: * Test method for 'de.odysseus.el.lang.BooleanOperations.not(Object)'
412: */
413: public void testNot() {
414: assertTrue(BooleanOperations.not(Boolean.FALSE));
415: assertFalse(BooleanOperations.not(Boolean.TRUE));
416: }
417:
418: /*
419: * Test method for 'de.odysseus.el.lang.BooleanOperations.empty(Object)'
420: */
421: public void testEmpty() {
422: assertTrue(BooleanOperations.empty(null));
423: assertTrue(BooleanOperations.empty(""));
424: assertTrue(BooleanOperations.empty(new Object[0]));
425: assertTrue(BooleanOperations.empty(new HashMap()));
426: assertTrue(BooleanOperations.empty(new ArrayList()));
427: assertFalse(BooleanOperations.empty("foo"));
428: }
429: }
|