001: /*
002: $Id: InvokerTest.java 4099 2006-10-10 18:06:52Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046:
047: package org.codehaus.groovy.runtime;
048:
049: import groovy.lang.GroovyRuntimeException;
050: import groovy.lang.GString;
051: import groovy.util.GroovyTestCase;
052:
053: import java.util.ArrayList;
054: import java.util.Collection;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Map;
059: import java.util.Collections;
060: import java.util.Arrays;
061:
062: import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
063:
064: /**
065: * Test the Invoker class
066: *
067: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
068: * @version $Revision: 4099 $
069: */
070: public class InvokerTest extends GroovyTestCase {
071:
072: protected Invoker invoker = new Invoker();
073:
074: public void testAsCollectionWithArray() {
075: Object[] array = { "A", "B", "C" };
076: assertAsCollection(array, 3);
077: }
078:
079: public void testAsCollectionWithMap() {
080: Map map = new HashMap();
081: map.put("A", "abc");
082: map.put("B", "def");
083: map.put("C", "xyz");
084: assertAsCollection(map, 3);
085: }
086:
087: public void testAsCollectionWithList() {
088: List list = new ArrayList();
089: list.add("A");
090: list.add("B");
091: list.add("C");
092: assertAsCollection(list, 3);
093: }
094:
095: public void testInvokerException() throws Throwable {
096: try {
097: throw new GroovyRuntimeException("message",
098: new NullPointerException());
099: } catch (GroovyRuntimeException e) {
100: // worked
101: assertEquals("message", e.getMessage());
102: assertTrue(e.getCause() instanceof NullPointerException);
103: }
104: }
105:
106: public void testAsBoolean() {
107: assertAsBoolean(true, Boolean.TRUE);
108: assertAsBoolean(true, "true");
109: assertAsBoolean(true, "TRUE");
110: assertAsBoolean(true, "false");
111: assertAsBoolean(false, Boolean.FALSE);
112: assertAsBoolean(false, (String) null);
113: assertAsBoolean(false, "");
114: GString emptyGString = new GString(new Object[] { "" }) {
115: public String[] getStrings() {
116: return new String[] { "" };
117: }
118: };
119: assertAsBoolean(false, emptyGString);
120: GString nonEmptyGString = new GString(new Object[] { "x" }) {
121: public String[] getStrings() {
122: return new String[] { "x" };
123: }
124: };
125: assertAsBoolean(true, nonEmptyGString);
126: assertAsBoolean(true, new Integer(1234));
127: assertAsBoolean(false, new Integer(0));
128: assertAsBoolean(true, new Float(0.3f));
129: assertAsBoolean(true, new Double(3.0f));
130: assertAsBoolean(false, new Float(0.0f));
131: assertAsBoolean(true, new Character((char) 1));
132: assertAsBoolean(false, new Character((char) 0));
133: assertAsBoolean(false, Collections.EMPTY_LIST);
134: assertAsBoolean(true, Arrays
135: .asList(new Integer[] { new Integer(1) }));
136: }
137:
138: public void testLessThan() {
139: assertTrue(ScriptBytecodeAdapter.compareLessThan(
140: new Integer(1), new Integer(2)));
141: assertTrue(ScriptBytecodeAdapter.compareLessThanEqual(
142: new Integer(2), new Integer(2)));
143: }
144:
145: public void testGreaterThan() {
146: assertTrue(ScriptBytecodeAdapter.compareGreaterThan(
147: new Integer(3), new Integer(2)));
148: assertTrue(ScriptBytecodeAdapter.compareGreaterThanEqual(
149: new Integer(2), new Integer(2)));
150: }
151:
152: public void testCompareTo() {
153: assertTrue(DefaultTypeTransformation.compareEqual("x",
154: new Integer('x')));
155: }
156:
157: // Implementation methods
158: //-------------------------------------------------------------------------
159:
160: /**
161: * Asserts the asBoolean method returns the given flag
162: */
163: protected void assertAsBoolean(boolean expected, Object value) {
164: boolean answer = DefaultTypeTransformation.castToBoolean(value);
165: assertEquals("value: " + value + " asBoolean()", expected,
166: answer);
167: }
168:
169: /**
170: * Asserts that the given object can be converted into a collection and iterator
171: * of the given size
172: */
173: protected void assertAsCollection(Object collectionObject, int count) {
174: Collection collection = DefaultTypeTransformation
175: .asCollection(collectionObject);
176: assertTrue("Collection is not null", collection != null);
177: assertEquals("Collection size", count, collection.size());
178:
179: assertIterator("collections iterator", collection.iterator(),
180: count);
181: assertIterator("InvokerHelper.asIterator", InvokerHelper
182: .asIterator(collectionObject), count);
183: assertIterator(
184: "InvokerHelper.asIterator(InvokerHelper.asCollection)",
185: InvokerHelper.asIterator(collection), count);
186: assertIterator(
187: "InvokerHelper.asIterator(InvokerHelper.asIterator)",
188: InvokerHelper.asIterator(InvokerHelper
189: .asIterator(collectionObject)), count);
190: }
191:
192: /**
193: * Asserts that the iterator is valid and of the right size
194: */
195: protected void assertIterator(String message, Iterator iterator,
196: int count) {
197: for (int i = 0; i < count; i++) {
198: assertTrue(message + ": should have item: " + i, iterator
199: .hasNext());
200: assertTrue(
201: message + ": item: " + i + " should not be null",
202: iterator.next() != null);
203: }
204:
205: assertFalse(message
206: + ": should not have item after iterating through: "
207: + count + " items", iterator.hasNext());
208: }
209:
210: }
|