1   /*
2    $Id: InvokerTest.java,v 1.12 2004/03/06 15:52:00 jstrachan Exp $
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.util.GroovyTestCase;
51  
52  import java.util.ArrayList;
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Map;
58  
59  
60  /***
61   * Test the Invoker class
62   * 
63   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
64   * @version $Revision: 1.12 $
65   */
66  public class InvokerTest extends GroovyTestCase {
67  
68      protected Invoker invoker = new Invoker();
69  
70      public void testAsCollectionWithArray() {
71          Object[] array = { "A", "B", "C" };
72          assertAsCollection(array, 3);
73      }
74  
75      public void testAsCollectionWithMap() {
76          Map map = new HashMap();
77          map.put("A", "abc");
78          map.put("B", "def");
79          map.put("C", "xyz");
80          assertAsCollection(map, 3);
81      }
82  
83      public void testAsCollectionWithList() {
84          List list = new ArrayList();
85          list.add("A");
86          list.add("B");
87          list.add("C");
88          assertAsCollection(list, 3);
89      }
90  
91      public void testInvokerException() throws Throwable {
92          try {
93              throw new GroovyRuntimeException("message", new NullPointerException());
94          }
95          catch (GroovyRuntimeException e) {
96              // worked
97              assertEquals("message", e.getMessage());
98              assertTrue(e.getCause() instanceof NullPointerException);
99          }
100     }
101 
102     public void testAsBoolean() {
103         assertAsBoolean(true, Boolean.TRUE);
104         assertAsBoolean(true, "true");
105         assertAsBoolean(true, "TRUE");
106         assertAsBoolean(true, "false");
107         assertAsBoolean(false, Boolean.FALSE);
108         assertAsBoolean(false, (String) null);
109         assertAsBoolean(true, new Integer(1234));
110         assertAsBoolean(false, new Integer(0));
111        }
112     
113     public void testLessThan() {
114         assertTrue(InvokerHelper.compareLessThan(new Integer(1), new Integer(2)));
115         assertTrue(InvokerHelper.compareLessThanEqual(new Integer(2), new Integer(2)));
116     }
117     
118     public void testGreaterThan() {
119         assertTrue(InvokerHelper.compareGreaterThan(new Integer(3), new Integer(2)));
120         assertTrue(InvokerHelper.compareGreaterThanEqual(new Integer(2), new Integer(2)));
121     }
122     
123     public void testCompareTo() {
124         assertTrue(InvokerHelper.compareEqual("x", new Integer('x')));
125     }
126     
127     // Implementation methods
128     //-------------------------------------------------------------------------
129 
130     /***
131      * Asserts the asBoolean method returns the given flag
132      */
133     protected void assertAsBoolean(boolean expected, Object value) {
134         boolean answer = InvokerHelper.asBool(value);
135         assertEquals("value: " + value + " asBoolean()", expected, answer);
136     }
137 
138     /***
139      * Asserts that the given object can be converted into a collection and iterator
140      * of the given size
141      */
142     protected void assertAsCollection(Object collectionObject, int count) {
143         Collection collection = invoker.asCollection(collectionObject);
144         assertTrue("Collection is not null", collection != null);
145         assertEquals("Collection size", count, collection.size());
146 
147         assertIterator("collections iterator", collection.iterator(), count);
148         assertIterator("invoker.asIterator", invoker.asIterator(collectionObject), count);
149         assertIterator("invoker.asIterator(invoker.asCollection)", invoker.asIterator(collection), count);
150         assertIterator("invoker.asIterator(invoker.asIterator)", invoker.asIterator(invoker.asIterator(collectionObject)), count);
151     }
152 
153     /***
154      * Asserts that the iterator is valid and of the right size
155      */
156     protected void assertIterator(String message, Iterator iterator, int count) {
157         for (int i = 0; i < count; i++) {
158             assertTrue(message + ": should have item: " + i, iterator.hasNext());
159             assertTrue(message + ": item: " + i + " should not be null", iterator.next() != null);
160         }
161 
162         assertFalse(
163             message + ": should not have item after iterating through: " + count + " items",
164             iterator.hasNext());
165     }
166 
167    
168 }