/*
 * Test case for bug #22290.
 */
package com.mysql.jdbc.util;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class TruncationBugReport extends BaseBugReport {

    private static final String CREATE_TABLE_SQL = //
    "CREATE TABLE trunctest (" + "id int(11) NOT NULL default '1', "
            + "cost decimal(10,2) NOT NULL, "
            + "PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

    private static final String DROP_TABLE_SQL = //
    "DROP TABLE IF EXISTS trunctest;";

    private static final String INSERT_SQL = //
    "INSERT INTO trunctest (id, cost) VALUES (1, 1.00);";

    public static void main(String[] args) throws Exception {
        new TruncationBugReport().run();
    }

    /**
     * @see com.mysql.jdbc.util.BaseBugReport#runTest()
     */
    public void runTest() throws Exception {
        Connection conn = getConnection();
        try {
            PreparedStatement ps = conn.prepareStatement(//
                    "update trunctest set cost = cost + ? where id = 1");
            ps.setBigDecimal(1, new BigDecimal("1.11"));
            ps.executeUpdate();
            ps.close();
        } finally {
            conn.close();
        }
    }

    /**
     * @see com.mysql.jdbc.util.BaseBugReport#setUp()
     */
    public void setUp() throws Exception {
        Connection conn = getConnection();
        Statement stat = conn.createStatement();
        stat.executeUpdate(CREATE_TABLE_SQL);
        stat.executeUpdate(INSERT_SQL);
        stat.close();
        conn.close();
    }

    /**
     * @see com.mysql.jdbc.util.BaseBugReport#tearDown()
     */
    public void tearDown() throws Exception {
        Connection conn = getConnection();
        Statement stat = conn.createStatement();
        stat.executeUpdate(DROP_TABLE_SQL);
        stat.close();
        conn.close();
    }

}
