Package org.apache.hivemind.impl

Source Code of org.apache.hivemind.impl.TestSymbolExpander$SymbolSourceFixture

// Copyright 2004, 2005 The Apache Software Foundation
//
// 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.hivemind.impl;

import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ErrorHandler;
import org.apache.hivemind.Location;
import org.apache.hivemind.SymbolSource;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;

/**
* Tests for {@link org.apache.hivemind.impl.SymbolExpanderImpl}.
*
* @author Howard Lewis Ship
* @since 1.1
*/
public class TestSymbolExpander extends HiveMindTestCase
{
    private class SymbolSourceFixture implements SymbolSource
    {
        public String valueForSymbol(String name)
        {
            return name.toUpperCase();
        }
    }

    private void attempt(String expected, String text)
    {
        SymbolExpanderImpl e = new SymbolExpanderImpl(null, new SymbolSource[] {new SymbolSourceFixture()});

        String actual = e.expandSymbols(text, null);

        assertEquals(expected, actual);
    }

    public void testSimple()
    {
        attempt("Now is the TIME", "Now is the ${time}");
    }

    public void testNoSymbols()
    {
        attempt("No symbols in here", "No symbols in here");
    }

    public void testFalseStart()
    {
        attempt("The cost of the ITEM is $1,000.", "The cost of the ${item} is $1,000.");
    }

    public void testNestedBraces()
    {
        attempt("Nested {BRACES}", "Nested ${{braces}}");
    }

    public void testEmptySymbol()
    {
        attempt("An empty ${} symbol", "An empty ${} symbol");
    }

    public void testTrailingDollar()
    {
        attempt("SYMBOL Ends with $", "${symbol} Ends with $");
    }

    public void testEndsWithPartialSymbol()
    {
        attempt("SYMBOL Ends with ${partial", "${symbol} Ends with ${partial");
    }

    public void testMissingSymbol()
    {
        ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
        Location l = newLocation();

        MockControl control = newControl(SymbolSource.class);
        SymbolSource source = (SymbolSource) control.getMock();

        // Training

        source.valueForSymbol("symbol");
        control.setReturnValue(null);

        eh.error(
            LogFactory.getLog(SymbolExpanderImpl.class),
            XmlImplMessages.noSuchSymbol("symbol"),
            l,
            null);

        replayControls();

        SymbolExpanderImpl e = new SymbolExpanderImpl(eh, new SymbolSource[] {source});

        String actual = e.expandSymbols("Unknown ${symbol}", l);

        assertEquals("Unknown ${symbol}", actual);

        verifyControls();
    }

    public void testEscaped()
    {
        attempt("This is a SYMBOL, this is ${not}.", "This is a ${symbol}, this is $${not}.");
    }
   
    public void testEscapedAtStart()
    {
      attempt("${not-a-symbol}", "$${not-a-symbol}");
    }

    public void testSystemPropertiesSymbolSource()
    {
        SymbolSource s = new SystemPropertiesSymbolSource();

        assertEquals(System.getProperty("user.home"), s.valueForSymbol("user.home"));
    }
   
}
TOP

Related Classes of org.apache.hivemind.impl.TestSymbolExpander$SymbolSourceFixture

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.