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

Groovy example source code file (MindMapPrinterTest.java)

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

Java - Groovy tags/keywords

color, color, diff, exception, exception, footer, header, ident, position='right, position='right, string, string, text='an, text='int

The Groovy MindMapPrinterTest.java source code

package org.codehaus.groovy.antlr.treewalker;

import junit.framework.TestCase;

import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;

/**
 * Testcases for the antlr AST visitor that prints groovy source code nodes as Freemind compatible XML.
 */
public class MindMapPrinterTest extends TestCase implements GroovyTokenTypes{

    private static final String HEADER = "<map version='0.7.1'>";
    private static final String FOOTER = "</node>";

    protected void setUp() throws Exception {
        XMLUnit.setIgnoreWhitespace(true);
    }

    public void testAbstract() throws Exception {
        assertXmlEquals(
                HEADER +
                        "<node TEXT='MODIFIERS <" + MODIFIERS +">' POSITION='right' COLOR=\"#000000\">" +
                        "  <node TEXT='public  <" + LITERAL_public +">' POSITION='right' COLOR=\"#17178B\">" +
                        "</node>" +
                        "<node TEXT='abstract  <" + ABSTRACT +">' POSITION='right' COLOR=\"#006699\">" +
                        "<node TEXT='CLASS_DEF <" + CLASS_DEF +">' POSITION='right' COLOR=\"#17178B\">" +
                        "<node TEXT='an identifier <" + IDENT +"> : Foo' POSITION='right' COLOR=\"#006699\">" +
                        "<node TEXT='EXTENDS_CLAUSE <" + EXTENDS_CLAUSE +">' POSITION='right' COLOR=\"#17178B\">" +
                        "<node TEXT='IMPLEMENTS_CLAUSE <" + IMPLEMENTS_CLAUSE +">' POSITION='right' COLOR=\"#17178B\">" +
                        "<node TEXT='OBJBLOCK <" + OBJBLOCK +">' POSITION='right' COLOR=\"#006699\">" +
                        FOOTER,
                pretty("public abstract class Foo{}"));

    }

    public void testArrayDeclarator() throws Exception {
        assertXmlEquals(
                HEADER +
                        "<node TEXT='VARIABLE_DEF <" + VARIABLE_DEF +"> : primes' POSITION='right' COLOR=\"#000000\">" +
                        "  <node TEXT='TYPE <" + TYPE +">' POSITION='right' COLOR=\"#17178B\">" +
                        "    <node TEXT='ARRAY_DECLARATOR <" + ARRAY_DECLARATOR +"> : [' POSITION='right' COLOR=\"#000000\">" +
                        "      <node TEXT='int  <" + LITERAL_int +">' POSITION='right' COLOR=\"#17178B\">" +
                        "    </node>" +
                        "  </node>" +
                        "  <node TEXT='an identifier <" + IDENT +"> : primes' POSITION='right' COLOR=\"#006699\">" +
                        "  <node TEXT='=  <" + ASSIGN +">' POSITION='right' COLOR=\"#000000\">" +
                        "    <node TEXT='new  <" + LITERAL_new +">' POSITION='right' COLOR=\"#17178B\">" +
                        "      <node TEXT='int  <" + LITERAL_int +">' POSITION='right' COLOR=\"#17178B\">" +
                        "      <node TEXT='ARRAY_DECLARATOR <" + ARRAY_DECLARATOR +"> : [' POSITION='right' COLOR=\"#000000\">" +
                        "        <node TEXT='a numeric literal <" + NUM_INT +"> : 5' POSITION='right' COLOR=\"#006699\">" +
                        "      </node>" +
                        "    </node>" +
                        "  </node>" +
                        "</node>" +
                        FOOTER,
                pretty("int[] primes = new int[5]"));
    }

    public void testRegexMatch() throws Exception {
        assertXmlEquals(
                HEADER +
                        "<node TEXT='if  <" + LITERAL_if +">' POSITION='right' COLOR=\"#17178B\">" +
                        "  <node TEXT='an identifier <" + IDENT +"> : foo' POSITION='right' COLOR=\"#006699\">" +
                        "  <node TEXT='==~  <" + REGEX_MATCH +">' POSITION='right' COLOR=\"#000000\">" +
                        "  <node TEXT='a string literal <" + STRING_LITERAL +"> : bar' POSITION='right' COLOR=\"#008000\">" +
                        "</node>" +
                        "<node TEXT='SLIST <" + SLIST +"> : {' POSITION='right' COLOR=\"#006699\">" +
                        FOOTER,
                pretty("if (foo==~\"bar\"){}"));
    }

    private void assertXmlEquals(String expected, String actual) throws Exception {
        Diff diff = new Diff(expected, actual);
        assertTrue(diff.toString(), diff.similar());
    }

    private String pretty(String input) throws Exception {
        TraversalTestHelper traverser = new TraversalTestHelper();
        return traverser.traverse(input, MindMapPrinter.class);
    }

}

Other Groovy examples (source code examples)

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

new blog posts

 

Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.