/**
 * Copyright (C) 2011 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package com.akiban.server.expression.std;

import com.akiban.server.expression.Expression;
import com.akiban.server.expression.ExpressionEvaluation;
import com.akiban.server.types.AkType;
import com.akiban.server.types.NullValueSource;
import com.akiban.server.types.ValueSource;
import com.akiban.server.types.util.ValueHolder;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.math.BigDecimal;

public final class CastExpressionTest {
    protected ValueSource cast(ValueSource source, AkType to) {
        Expression expression = new CastExpression(to, new LiteralExpression(source));
        return expression.evaluation().eval();
    }

    @Test
    public void testNull() {
        assertTrue("result is null", 
                   cast(NullValueSource.only(), AkType.VARCHAR).isNull());
    }

    @Test
    public void testSame() {
        ValueSource value;

        value = new ValueHolder(AkType.INT, 1);
        assertEquals(value, cast(value, AkType.INT));

        value = new ValueHolder(AkType.VARCHAR, "test");
        assertEquals(value, cast(value, AkType.VARCHAR));
    }

    @Test
    public void testConvert() {
        ValueSource value, expected;

        value = new ValueHolder(AkType.INT, 20);
        expected = new ValueHolder(AkType.VARCHAR, "20");
        assertEquals(expected, cast(value, AkType.VARCHAR));

        value = new ValueHolder(AkType.VARCHAR, "-123");
        expected = new ValueHolder(AkType.LONG, -123L);
        assertEquals(expected, cast(value, AkType.LONG));

        value = new ValueHolder(AkType.VARCHAR, "98.76");
        expected = new ValueHolder(AkType.DECIMAL, new BigDecimal("98.76"));
        assertEquals(expected, cast(value, AkType.DECIMAL));
    }
}

Add unit test for that.