1   /*
2    $Id: InvokerTest.java 4099 2006-10-10 18:06:52Z blackdrag $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45   */
46  
47  package org.codehaus.groovy.runtime;
48  
49  import groovy.lang.GroovyRuntimeException;
50  import groovy.lang.GString;
51  import groovy.util.GroovyTestCase;
52  
53  import java.util.ArrayList;
54  import java.util.Collection;
55  import java.util.HashMap;
56  import java.util.Iterator;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.Collections;
60  import java.util.Arrays;
61  
62  import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
63  
64  
65  /***
66   * Test the Invoker class
67   * 
68   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
69   * @version $Revision: 4099 $
70   */
71  public class InvokerTest extends GroovyTestCase {
72  
73      protected Invoker invoker = new Invoker();
74  
75      public void testAsCollectionWithArray() {
76          Object[] array = { "A", "B", "C" };
77          assertAsCollection(array, 3);
78      }
79  
80      public void testAsCollectionWithMap() {
81          Map map = new HashMap();
82          map.put("A", "abc");
83          map.put("B", "def");
84          map.put("C", "xyz");
85          assertAsCollection(map, 3);
86      }
87  
88      public void testAsCollectionWithList() {
89          List list = new ArrayList();
90          list.add("A");
91          list.add("B");
92          list.add("C");
93          assertAsCollection(list, 3);
94      }
95  
96      public void testInvokerException() throws Throwable {
97          try {
98              throw new GroovyRuntimeException("message", new NullPointerException());
99          }
100         catch (GroovyRuntimeException e) {
101             // worked
102             assertEquals("message", e.getMessage());
103             assertTrue(e.getCause() instanceof NullPointerException);
104         }
105     }
106 
107     public void testAsBoolean() {
108         assertAsBoolean(true, Boolean.TRUE);
109         assertAsBoolean(true, "true");
110         assertAsBoolean(true, "TRUE");
111         assertAsBoolean(true, "false");
112         assertAsBoolean(false, Boolean.FALSE);
113         assertAsBoolean(false, (String) null);
114         assertAsBoolean(false, "");
115         GString emptyGString = new GString(new Object[] { "" }) {
116             public String[] getStrings() {
117                 return new String[] { "" };
118             }
119         };
120         assertAsBoolean(false, emptyGString);
121         GString nonEmptyGString = new GString(new Object[] { "x" }) {
122             public String[] getStrings() {
123                 return new String[] { "x" };
124             }
125         };
126         assertAsBoolean(true, nonEmptyGString);
127         assertAsBoolean(true, new Integer(1234));
128         assertAsBoolean(false, new Integer(0));
129         assertAsBoolean(true, new Float(0.3f));
130         assertAsBoolean(true, new Double(3.0f));
131         assertAsBoolean(false, new Float(0.0f));
132         assertAsBoolean(true, new Character((char) 1));
133         assertAsBoolean(false, new Character((char) 0));
134         assertAsBoolean(false, Collections.EMPTY_LIST);
135 		assertAsBoolean(true, Arrays.asList(new Integer[] { new Integer(1) }));
136        }
137     
138     public void testLessThan() {
139         assertTrue(ScriptBytecodeAdapter.compareLessThan(new Integer(1), new Integer(2)));
140         assertTrue(ScriptBytecodeAdapter.compareLessThanEqual(new Integer(2), new Integer(2)));
141     }
142     
143     public void testGreaterThan() {
144         assertTrue(ScriptBytecodeAdapter.compareGreaterThan(new Integer(3), new Integer(2)));
145         assertTrue(ScriptBytecodeAdapter.compareGreaterThanEqual(new Integer(2), new Integer(2)));
146     }
147     
148     public void testCompareTo() {
149         assertTrue(DefaultTypeTransformation.compareEqual("x", new Integer('x')));
150     }
151     
152     // Implementation methods
153     //-------------------------------------------------------------------------
154 
155     /***
156      * Asserts the asBoolean method returns the given flag
157      */
158     protected void assertAsBoolean(boolean expected, Object value) {
159         boolean answer = DefaultTypeTransformation.castToBoolean(value);
160         assertEquals("value: " + value + " asBoolean()", expected, answer);
161     }
162 
163     /***
164      * Asserts that the given object can be converted into a collection and iterator
165      * of the given size
166      */
167     protected void assertAsCollection(Object collectionObject, int count) {
168         Collection collection = DefaultTypeTransformation.asCollection(collectionObject);
169         assertTrue("Collection is not null", collection != null);
170         assertEquals("Collection size", count, collection.size());
171 
172         assertIterator("collections iterator", collection.iterator(), count);
173         assertIterator("InvokerHelper.asIterator", InvokerHelper.asIterator(collectionObject), count);
174         assertIterator("InvokerHelper.asIterator(InvokerHelper.asCollection)", InvokerHelper.asIterator(collection), count);
175         assertIterator("InvokerHelper.asIterator(InvokerHelper.asIterator)", InvokerHelper.asIterator(InvokerHelper.asIterator(collectionObject)), count);
176     }
177 
178     /***
179      * Asserts that the iterator is valid and of the right size
180      */
181     protected void assertIterator(String message, Iterator iterator, int count) {
182         for (int i = 0; i < count; i++) {
183             assertTrue(message + ": should have item: " + i, iterator.hasNext());
184             assertTrue(message + ": item: " + i + " should not be null", iterator.next() != null);
185         }
186 
187         assertFalse(
188             message + ": should not have item after iterating through: " + count + " items",
189             iterator.hasNext());
190     }
191 
192    
193 }