    1   /*
    2    $Id: DefaultGroovyMethodsTest.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.Closure;
    50  import groovy.util.GroovyTestCase;
    51
    52  import java.math.BigDecimal;
    53  import java.math.BigInteger;
    54  import java.net.URI;
    55  import java.net.URL;
    56  import java.util.ArrayList;
    57  import java.util.HashMap;
    58  import java.util.List;
    59  import java.util.Map;
    60
    61  /***
    62   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    63   * @author Marc Guillemot
    64   * @version $Revision: 4099 $
    65   */
    66  public class DefaultGroovyMethodsTest extends GroovyTestCase {
    67
    68      public void testPrint() throws Exception {
    69          Map map = new HashMap();
    70          map.put("bob", "drools");
    71          map.put("james", "geronimo");
    72          List list = new ArrayList();
    73          list.add(map);
    74
    75          /*** @todo fix this! */
    76          //assertConsoleOutput(list, "[['bob':'drools', 'james':'geronimo']]");
    77      }
    78
    79      public void testIncrementString() throws Exception {
    80          String original = "z";
    81          String answer = DefaultGroovyMethods.next(original);
    82
    83          System.out.println(answer);
    84          assertTrue(answer.compareTo(original) > 0);
    85      }
    86
    87      public void testDecrementString() throws Exception {
    88          String original = "a";
    89          String answer = DefaultGroovyMethods.previous(original);
    90
    91          System.out.println(answer);
    92          assertTrue(ScriptBytecodeAdapter.compareLessThan(answer, original));
    93      }
    94
    95      public void testToMethods() throws Exception {
    96          Number n = new Long(7);
    97
    98          assertEquals(DefaultGroovyMethods.toInteger("1"), new Integer(1));
    99          assertEquals(DefaultGroovyMethods.toInteger(n), new Integer(7));
    100         assertEquals(DefaultGroovyMethods.toLong("1"), new Long(1));
    101         assertEquals(DefaultGroovyMethods.toLong(n), new Long(7));
    102         assertEquals(DefaultGroovyMethods.toFloat("1"), new Float(1));
    103         assertEquals(DefaultGroovyMethods.toFloat(n), new Float(7));
    104         assertEquals(DefaultGroovyMethods.toDouble("1"), new Double(1));
    105         assertEquals(DefaultGroovyMethods.toDouble(n), new Double(7));
    106         assertEquals(DefaultGroovyMethods.toBigInteger("1"), new BigInteger("1"));
    107         assertEquals(DefaultGroovyMethods.toBigInteger(n), new BigInteger("7"));
    108         assertEquals(DefaultGroovyMethods.toBigDecimal("1"), new BigDecimal("1"));
    109         assertEquals(DefaultGroovyMethods.toBigDecimal(n), new BigDecimal("7"));
    110         assertEquals(DefaultGroovyMethods.toURL("http://example.org/"), new URL("http://example.org/"));
    111         assertEquals(DefaultGroovyMethods.toURI("http://example.org/"), new URI("http://example.org/"));
    112         assertEquals(DefaultGroovyMethods.toBoolean("True"), Boolean.TRUE);
    113         assertEquals(DefaultGroovyMethods.toBoolean("Y"), Boolean.TRUE);
    114         assertEquals(DefaultGroovyMethods.toBoolean(" y "), Boolean.TRUE);
    115         assertEquals(DefaultGroovyMethods.toBoolean("1"), Boolean.TRUE);
    116         assertEquals(DefaultGroovyMethods.toBoolean("false"), Boolean.FALSE);
    117         assertEquals(DefaultGroovyMethods.toBoolean("n"), Boolean.FALSE);
    118         assertEquals(DefaultGroovyMethods.toBoolean("0"), Boolean.FALSE);
    119     }
    120
    121
    122     public void testDownto() {
    123         final int[] count = new int[]{0};
    124         final Closure closure = new Closure(null) {
    125             public Object doCall(final Object params) {
    126                 count[0]++;
    127                 return null;
    128             }
    129         };
    130
    131         DefaultGroovyMethods.downto(new BigInteger("1"), new BigDecimal("0"), closure);
    132         assertEquals(count[0], 2);
    133
    134         count[0] = 0;
    135
    136         DefaultGroovyMethods.downto(new BigInteger("1"), new BigDecimal("0.123"), closure);
    137         assertEquals(count[0], 1);
    138     }
    139 }