| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (

This example Groovy source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

bob, comparable, dummygstring, dummygstring, groovytestcase, gstring, gstringtest, hello, hello, james, james, object, object, string

The Groovy source code

 * Copyright 2003-2011 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package groovy.lang;

import groovy.util.GroovyTestCase;
import org.codehaus.groovy.runtime.InvokerHelper;

 * Tests the use of the structured Attribute type
 * @author <a href="">James Strachan
 * @version $Revision: 21515 $
public class GStringTest extends GroovyTestCase {

    public void testIterateOverText() {
        DummyGString compString = new DummyGString(new Object[]{"James"});
        assertArrayEquals(new String[]{"Hello ", "!"}, compString.getStrings());
        assertArrayEquals(new Object[]{"James"}, compString.getValues());
        assertEquals("Hello James!", compString.toString());

    public void testAppendString() {
        DummyGString a = new DummyGString(new Object[]{"James"});
        GString result =" how are you?");
        assertEquals("Hello James! how are you?", result.toString());
        assertEquals('J', a.charAt(6));
        assertEquals("o J", a.subSequence(4, 7));

    public void testAppendString2() {
        DummyGString a = new DummyGString(new Object[]{"James"}, new String[]{"Hello "});
        GString result =" how are you?");
        System.out.println("Strings: " + InvokerHelper.toString(result.getStrings()));
        System.out.println("Values: " + InvokerHelper.toString(result.getValues()));
        assertEquals("Hello James how are you?", result.toString());

    public void testAppendGString() {
        DummyGString a = new DummyGString(new Object[]{"James"});
        DummyGString b = new DummyGString(new Object[]{"Bob"});
        GString result =;
        assertEquals("Hello James!Hello Bob!", result.toString());

    public void testAppendGString2() {
        DummyGString a = new DummyGString(new Object[]{"James"}, new String[]{"Hello "});
        DummyGString b = new DummyGString(new Object[]{"Bob"}, new String[]{"Hello "});
        GString result =;
        assertEquals("Hello JamesHello Bob", result.toString());

    public void testEqualsAndHashCode() {
        DummyGString a = new DummyGString(new Object[]{new Integer(1)});
        DummyGString b = new DummyGString(new Object[]{new Long(1)});
        Comparable c = new DummyGString(new Object[]{new Double(2.3)});

        assertTrue("a == b", a.equals(b));
        assertEquals("hashcode a == b", a.hashCode(), b.hashCode());
        assertFalse("a != c", a.equals(c));
        assertTrue("hashcode a != c", a.hashCode() != c.hashCode());
        assertEquals("a <=> b", 0, a.compareTo(b));
        assertEquals("a <=> b", -1, a.compareTo(c));

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.