KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > runtime > InvokerTest


1 /*
2  $Id: InvokerTest.java,v 1.13 2004/09/30 21:23:38 glaforge 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 JavaDoc;
53 import java.util.Collection JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57 import java.util.Map JavaDoc;
58 import java.util.Collections JavaDoc;
59 import java.util.Arrays JavaDoc;
60
61
62 /**
63  * Test the Invoker class
64  *
65  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
66  * @version $Revision: 1.13 $
67  */

68 public class InvokerTest extends GroovyTestCase {
69
70     protected Invoker invoker = new Invoker();
71
72     public void testAsCollectionWithArray() {
73         Object JavaDoc[] array = { "A", "B", "C" };
74         assertAsCollection(array, 3);
75     }
76
77     public void testAsCollectionWithMap() {
78         Map JavaDoc map = new HashMap JavaDoc();
79         map.put("A", "abc");
80         map.put("B", "def");
81         map.put("C", "xyz");
82         assertAsCollection(map, 3);
83     }
84
85     public void testAsCollectionWithList() {
86         List JavaDoc list = new ArrayList JavaDoc();
87         list.add("A");
88         list.add("B");
89         list.add("C");
90         assertAsCollection(list, 3);
91     }
92
93     public void testInvokerException() throws Throwable JavaDoc {
94         try {
95             throw new GroovyRuntimeException("message", new NullPointerException JavaDoc());
96         }
97         catch (GroovyRuntimeException e) {
98             // worked
99
assertEquals("message", e.getMessage());
100             assertTrue(e.getCause() instanceof NullPointerException JavaDoc);
101         }
102     }
103
104     public void testAsBoolean() {
105         assertAsBoolean(true, Boolean.TRUE);
106         assertAsBoolean(true, "true");
107         assertAsBoolean(true, "TRUE");
108         assertAsBoolean(true, "false");
109         assertAsBoolean(false, Boolean.FALSE);
110         assertAsBoolean(false, (String JavaDoc) null);
111         assertAsBoolean(true, new Integer JavaDoc(1234));
112         assertAsBoolean(false, new Integer JavaDoc(0));
113         assertAsBoolean(true, new Float JavaDoc(0.3f));
114         assertAsBoolean(true, new Double JavaDoc(3.0f));
115         assertAsBoolean(false, new Float JavaDoc(0.0f));
116         assertAsBoolean(false, Collections.EMPTY_LIST);
117         assertAsBoolean(true, Arrays.asList(new Integer JavaDoc[] { new Integer JavaDoc(1) }));
118        }
119     
120     public void testLessThan() {
121         assertTrue(InvokerHelper.compareLessThan(new Integer JavaDoc(1), new Integer JavaDoc(2)));
122         assertTrue(InvokerHelper.compareLessThanEqual(new Integer JavaDoc(2), new Integer JavaDoc(2)));
123     }
124     
125     public void testGreaterThan() {
126         assertTrue(InvokerHelper.compareGreaterThan(new Integer JavaDoc(3), new Integer JavaDoc(2)));
127         assertTrue(InvokerHelper.compareGreaterThanEqual(new Integer JavaDoc(2), new Integer JavaDoc(2)));
128     }
129     
130     public void testCompareTo() {
131         assertTrue(InvokerHelper.compareEqual("x", new Integer JavaDoc('x')));
132     }
133     
134     // Implementation methods
135
//-------------------------------------------------------------------------
136

137     /**
138      * Asserts the asBoolean method returns the given flag
139      */

140     protected void assertAsBoolean(boolean expected, Object JavaDoc value) {
141         boolean answer = InvokerHelper.asBool(value);
142         assertEquals("value: " + value + " asBoolean()", expected, answer);
143     }
144
145     /**
146      * Asserts that the given object can be converted into a collection and iterator
147      * of the given size
148      */

149     protected void assertAsCollection(Object JavaDoc collectionObject, int count) {
150         Collection JavaDoc collection = invoker.asCollection(collectionObject);
151         assertTrue("Collection is not null", collection != null);
152         assertEquals("Collection size", count, collection.size());
153
154         assertIterator("collections iterator", collection.iterator(), count);
155         assertIterator("invoker.asIterator", invoker.asIterator(collectionObject), count);
156         assertIterator("invoker.asIterator(invoker.asCollection)", invoker.asIterator(collection), count);
157         assertIterator("invoker.asIterator(invoker.asIterator)", invoker.asIterator(invoker.asIterator(collectionObject)), count);
158     }
159
160     /**
161      * Asserts that the iterator is valid and of the right size
162      */

163     protected void assertIterator(String JavaDoc message, Iterator JavaDoc iterator, int count) {
164         for (int i = 0; i < count; i++) {
165             assertTrue(message + ": should have item: " + i, iterator.hasNext());
166             assertTrue(message + ": item: " + i + " should not be null", iterator.next() != null);
167         }
168
169         assertFalse(
170             message + ": should not have item after iterating through: " + count + " items",
171             iterator.hasNext());
172     }
173
174    
175 }
176
Popular Tags