package org.voltdb;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import junit.framework.TestCase;

import org.junit.Test;
import org.voltdb.VoltDB.Configuration;
import org.voltdb.client.Client;
import org.voltdb.client.ClientFactory;
import org.voltdb.client.ProcCallException;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb.utils.MiscUtils;

public class TestRoutingEdgeCases extends TestCase {

    /**
     * Insert into a <8byte integer column using byte[] to send
     * the int. The column is also the partition column. Make sure
     * TheHashinator doesn't screw up.
     */
    @Test
    public void testPartitionKeyAsBytes() throws Exception {

        ServerThread localServer = null;
        Client client = null;

        try {
            String simpleSchema =
                    "create table blah (" +
                    "ival integer default 0 not null, " +
                    "PRIMARY KEY(ival)); " +
                    "PARTITION TABLE blah ON COLUMN ival;";

            VoltProjectBuilder builder = new VoltProjectBuilder();
            builder.addLiteralSchema(simpleSchema);
            builder.addStmtProcedure("Insert", "insert into blah values (?);", null);
            boolean success = builder.compile(Configuration.getPathToCatalogForTest("edgecases.jar"), 7, 1, 0);
            assert(success);
            MiscUtils.copyFile(builder.getPathToDeployment(), Configuration.getPathToCatalogForTest("edgecases.xml"));

            VoltDB.Configuration config = new VoltDB.Configuration();
            config.m_pathToCatalog = Configuration.getPathToCatalogForTest("edgecases.jar");
            config.m_pathToDeployment = Configuration.getPathToCatalogForTest("edgecases.xml");
            localServer = new ServerThread(config);
            localServer.start();
            localServer.waitForInitialization();

            client = ClientFactory.createClient();
            client.createConnection("localhost");

            try {
                ByteBuffer buf = ByteBuffer.allocate(4);
                buf.order(ByteOrder.LITTLE_ENDIAN);
                buf.putInt(7);
                byte[] value = buf.array();

                client.callProcedure("Insert", value);
                fail();
            }
            catch (ProcCallException pce) {
                assertTrue(pce.getMessage().contains("Array / Scalar parameter mismatch"));
            }

            // For now, @LoadSinglepartitionTable assumes 8 byte integers, even if type is < 8 bytes
            // This is ok because we don't expose this functionality.
            // The code below will throw a constraint violation, but it really shouldn't. There's
            // Another comment about this in ProcedureRunner about a reasonable fix for this.

            //VoltTable t = new VoltTable(new VoltTable.ColumnInfo("foo", VoltType.INTEGER));
            //t.addRow(13);
            //
            //ByteBuffer buf = ByteBuffer.allocate(4);
            //buf.order(ByteOrder.LITTLE_ENDIAN);
            //buf.putInt(13);
            //byte[] value = buf.array();

            //client.callProcedure("@LoadSinglepartitionTable", value, "blah", t);
        }
        finally {
            if (client != null) client.close();
            if (localServer != null) localServer.shutdown();
        }
    }
}

@@ -1,8 +1,1141 @@
+/* This file is part of VoltDB.%0A * Copyright (C) 2008-2013 VoltDB Inc.%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining%0A * a copy of this software and associated documentation files (the%0A * %22Software%22), to deal in the Software without restriction, including%0A * without limitation the rights to use, copy, modify, merge, publish,%0A * distribute, sublicense, and/or sell copies of the Software, and to%0A * permit persons to whom the Software is furnished to do so, subject to%0A * the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be%0A * included in all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND,%0A * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF%0A * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.%0A * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR%0A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,%0A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR%0A * OTHER DEALINGS IN THE SOFTWARE.%0A */%0A%0A
 package 

Fix licensecheck.