001: /*
002: $Id: DefaultGroovyMethodsTest.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.Closure;
050: import groovy.util.GroovyTestCase;
051:
052: import java.math.BigDecimal;
053: import java.math.BigInteger;
054: import java.net.URI;
055: import java.net.URL;
056: import java.util.ArrayList;
057: import java.util.HashMap;
058: import java.util.List;
059: import java.util.Map;
060:
061: /**
062: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
063: * @author Marc Guillemot
064: * @version $Revision: 4099 $
065: */
066: public class DefaultGroovyMethodsTest extends GroovyTestCase {
067:
068: public void testPrint() throws Exception {
069: Map map = new HashMap();
070: map.put("bob", "drools");
071: map.put("james", "geronimo");
072: List list = new ArrayList();
073: list.add(map);
074:
075: /** @todo fix this! */
076: //assertConsoleOutput(list, "[['bob':'drools', 'james':'geronimo']]");
077: }
078:
079: public void testIncrementString() throws Exception {
080: String original = "z";
081: String answer = DefaultGroovyMethods.next(original);
082:
083: System.out.println(answer);
084: assertTrue(answer.compareTo(original) > 0);
085: }
086:
087: public void testDecrementString() throws Exception {
088: String original = "a";
089: String answer = DefaultGroovyMethods.previous(original);
090:
091: System.out.println(answer);
092: assertTrue(ScriptBytecodeAdapter.compareLessThan(answer,
093: original));
094: }
095:
096: public void testToMethods() throws Exception {
097: Number n = new Long(7);
098:
099: assertEquals(DefaultGroovyMethods.toInteger("1"),
100: new Integer(1));
101: assertEquals(DefaultGroovyMethods.toInteger(n), new Integer(7));
102: assertEquals(DefaultGroovyMethods.toLong("1"), new Long(1));
103: assertEquals(DefaultGroovyMethods.toLong(n), new Long(7));
104: assertEquals(DefaultGroovyMethods.toFloat("1"), new Float(1));
105: assertEquals(DefaultGroovyMethods.toFloat(n), new Float(7));
106: assertEquals(DefaultGroovyMethods.toDouble("1"), new Double(1));
107: assertEquals(DefaultGroovyMethods.toDouble(n), new Double(7));
108: assertEquals(DefaultGroovyMethods.toBigInteger("1"),
109: new BigInteger("1"));
110: assertEquals(DefaultGroovyMethods.toBigInteger(n),
111: new BigInteger("7"));
112: assertEquals(DefaultGroovyMethods.toBigDecimal("1"),
113: new BigDecimal("1"));
114: assertEquals(DefaultGroovyMethods.toBigDecimal(n),
115: new BigDecimal("7"));
116: assertEquals(DefaultGroovyMethods.toURL("http://example.org/"),
117: new URL("http://example.org/"));
118: assertEquals(DefaultGroovyMethods.toURI("http://example.org/"),
119: new URI("http://example.org/"));
120: assertEquals(DefaultGroovyMethods.toBoolean("True"),
121: Boolean.TRUE);
122: assertEquals(DefaultGroovyMethods.toBoolean("Y"), Boolean.TRUE);
123: assertEquals(DefaultGroovyMethods.toBoolean(" y "),
124: Boolean.TRUE);
125: assertEquals(DefaultGroovyMethods.toBoolean("1"), Boolean.TRUE);
126: assertEquals(DefaultGroovyMethods.toBoolean("false"),
127: Boolean.FALSE);
128: assertEquals(DefaultGroovyMethods.toBoolean("n"), Boolean.FALSE);
129: assertEquals(DefaultGroovyMethods.toBoolean("0"), Boolean.FALSE);
130: }
131:
132: public void testDownto() {
133: final int[] count = new int[] { 0 };
134: final Closure closure = new Closure(null) {
135: public Object doCall(final Object params) {
136: count[0]++;
137: return null;
138: }
139: };
140:
141: DefaultGroovyMethods.downto(new BigInteger("1"),
142: new BigDecimal("0"), closure);
143: assertEquals(count[0], 2);
144:
145: count[0] = 0;
146:
147: DefaultGroovyMethods.downto(new BigInteger("1"),
148: new BigDecimal("0.123"), closure);
149: assertEquals(count[0], 1);
150: }
151: }
|