gt:         Client client = getClient();
context: /* This file is part of VoltDB.
   * Copyright (C) 2008-2013 VoltDB Inc.
   *
   * Permission is hereby granted, free of charge, to any person obtaining
   * a copy of this software and associated documentation files (the
   * "Software"), to deal in the Software without restriction, including
   * without limitation the rights to use, copy, modify, merge, publish,
   * distribute, sublicense, and/or sell copies of the Software, and to
   * permit persons to whom the Software is furnished to do so, subject to
   * the following conditions:
   *
   * The above copyright notice and this permission notice shall be
   * included in all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   */
  
  package org.voltdb.regressionsuites;
  
  import java.io.IOException;
  
  import junit.framework.Test;
  
  import org.voltdb.BackendTarget;
  import org.voltdb.VoltTable;
  import org.voltdb.client.Client;
  import org.voltdb.client.ClientResponse;
  import org.voltdb.client.ProcCallException;
  import org.voltdb.compiler.VoltProjectBuilder;
  import org.voltdb.exceptions.ConstraintFailureException;
  import org.voltdb_testprocs.regressionsuites.failureprocs.BadDecimalToVarcharCompare;
  import org.voltdb_testprocs.regressionsuites.failureprocs.BadFloatToVarcharCompare;
  import org.voltdb_testprocs.regressionsuites.failureprocs.BadVarcharCompare;
  import org.voltdb_testprocs.regressionsuites.failureprocs.BatchTooBig;
  import org.voltdb_testprocs.regressionsuites.failureprocs.CleanupFail;
  import org.voltdb_testprocs.regressionsuites.failureprocs.DivideByZero;
  import org.voltdb_testprocs.regressionsuites.failureprocs.FetchTooMuch;
  import org.voltdb_testprocs.regressionsuites.failureprocs.InsertBigString;
  import org.voltdb_testprocs.regressionsuites.failureprocs.InsertLotsOfData;
  import org.voltdb_testprocs.regressionsuites.failureprocs.ReturnAppStatus;
  import org.voltdb_testprocs.regressionsuites.failureprocs.SelectBigString;
  import org.voltdb_testprocs.regressionsuites.failureprocs.TooFewParams;
  import org.voltdb_testprocs.regressionsuites.failureprocs.ViolateUniqueness;
  import org.voltdb_testprocs.regressionsuites.failureprocs.ViolateUniquenessAndCatchException;
  import org.voltdb_testprocs.regressionsuites.sqlfeatureprocs.WorkWithBigString;
  
  public class TestFailuresSuite extends RegressionSuite {
  
      // procedures used by these tests
      static final Class<?>[] PROCEDURES = {
          BadVarcharCompare.class, BadFloatToVarcharCompare.class,
          BadDecimalToVarcharCompare.class,
          ViolateUniqueness.class, ViolateUniquenessAndCatchException.class,
          DivideByZero.class, WorkWithBigString.class, InsertBigString.class,
          InsertLotsOfData.class, FetchTooMuch.class, CleanupFail.class, TooFewParams.class,
          ReturnAppStatus.class, BatchTooBig.class, SelectBigString.class
      };
  
      /**
       * Constructor needed for JUnit. Should just pass on parameters to superclass.
       * @param name The name of the method to test. This is just passed to the superclass.
       */
      public TestFailuresSuite(String name) {
          super(name);
      }
  
      // Subcase of ENG-800
      public void testBadVarcharToAnyCompare() throws IOException
      {
          System.out.println("STARTING testBadVarcharToAnyCompare");
          Client client = getClient();
  
          boolean threw = false;
          try
          {
              client.callProcedure("BadVarcharCompare", 1).getResults();
          }
          catch (ProcCallException e)
          {
              if (!isHSQL())
              {
                  if ((e.getMessage().contains("SQL ERROR")) &&
                          (e.getMessage().contains("cannot be cast for comparison to type VARCHAR")))
                  {
                      threw = true;
                  }
                  else
                  {
                      e.printStackTrace();
                  }
              }
              else
              {
                  threw = true;
              }
          }
          assertTrue(threw);
      }
  
      // Subcase of ENG-800
      public void testBadFloatToVarcharCompare() throws IOException
      {
          System.out.println("STARTING testBadFloatToVarcharCompare");
          Client client = getClient();
  
          boolean threw = false;
          try
          {
              client.callProcedure("BadFloatToVarcharCompare", 1).getResults();
          }
          catch (ProcCallException e)
          {
              if (!isHSQL())
              {
                  if ((e.getMessage().contains("SQL ERROR")) &&
                          (e.getMessage().contains("VARCHAR cannot be cast for comparison to type FLOAT")))
                  {
                      threw = true;
                  }
                  else
                  {
                      e.printStackTrace();
                  }
              }
              else
              {
                  threw = true;
              }
          }
          assertTrue(threw);
      }
  
      // Subcase of ENG-800
      public void testBadDecimalToVarcharCompare() throws IOException
      {
          System.out.println("STARTING testBadDecimalToVarcharCompare");
          Client client = getClient();
  
          boolean threw = false;
          try
          {
              client.callProcedure("BadDecimalToVarcharCompare", 1).getResults();
          }
          catch (ProcCallException e)
          {
              if (!isHSQL())
              {
                  if ((e.getMessage().contains("SQL ERROR")) &&
                          (e.getMessage().contains("VARCHAR cannot be cast for comparison to type DECIMAL")))
                  {
                      threw = true;
                  }
                  else
                  {
                      e.printStackTrace();
                  }
              }
              else
              {
                  threw = true;
              }
          }
          assertTrue(threw);
      }
  
      public void testViolateUniqueness() throws IOException {
          System.out.println("STARTING testVU");
          Client client = getClient();
  
          VoltTable[] results = null;
          try {
              results = client.callProcedure("ViolateUniqueness", 1L, 1L, 1L).getResults();
              System.out.println("testVU client response received");
              assertEquals(results.length, 0);
          } catch (ProcCallException e) {
              try {
                  results = client.callProcedure("InsertNewOrder", 2L, 2L, 2L).getResults();
              } catch (ProcCallException e1) {
                  fail(e1.toString());
              } catch (IOException e1) {
                  fail(e1.toString());
              }
              System.out.println("second client response received");
              assertEquals(1, results.length);
              assertEquals(1, results[0].asScalarLong());
  
              return;
          } catch (IOException e) {
              fail(e.toString());
              return;
          }
          fail("testViolateUniqueness should return while catching ProcCallException");
      }
  
      public void testViolateUniquenessAndCatchException() throws IOException {
          Client client = getClient();
          System.out.println("STARTING testViolateUniquenessAndCatchException");
          VoltTable[] results = null;
          try {
              results = client.callProcedure("ViolateUniquenessAndCatchException", 1L, 1L, (byte)1).getResults();
              assertTrue(results.length == 1);
              System.out.println("PASSED testViolateUandCE");
          } catch (ProcCallException e) {
              System.out.println("FAIL(1) testViolateUandCE");
              e.printStackTrace();
              fail("ViolateUniquenessAndCatchException should not have thrown a ProcCallException");
          } catch (IOException e) {
              System.out.println("FAIL(2) testViolateUandCE");
              fail(e.toString());
              return;
          }
  
          try {
              results = client.callProcedure("InsertNewOrder", 2L, 2L, 2L).getResults();
          } catch (ProcCallException e1) {
              fail(e1.toString());
          } catch (IOException e1) {
              fail(e1.toString());
          }
          assertEquals(1, results.length);
          assertEquals(1, results[0].asScalarLong());
  
          return;
      }
  
      //
      // Check that a very large ConstraintFailureException can serialize correctly.
      //
      public void testTicket511_ViolateUniquenessWithLargeString() throws Exception {
          Client client = getClient();
          System.out.println("STARTING testTicket511_ViolateUniquenessWithLargeString");
          byte stringData[] = new byte[60000];
          java.util.Arrays.fill(stringData, (byte)'a');
  
          client.callProcedure("InsertBigString", 0, new String(stringData, "UTF-8"));
  
          java.util.Arrays.fill(stringData, (byte)'b');
          boolean threwException = false;
          try {
              client.callProcedure("InsertBigString", 0, new String(stringData, "UTF-8"));
          } catch (ProcCallException e) {
              threwException = true;
              assertTrue(e.getMessage().contains("CONSTRAINT VIOLATION"));
              assertTrue(e.getCause().getMessage().toUpperCase().contains("UNIQUE"));
              if (!isHSQL()) {
                  VoltTable table = ((ConstraintFailureException)e.getCause()).getTuples();
                  table.resetRowPosition();
                  assertTrue(table.advanceRow());
                  assertTrue(java.util.Arrays.equals(stringData, table.getStringAsBytes(2)));
                  ConstraintFailureException cfe = (ConstraintFailureException)e.getCause();
                  assertTrue(cfe.getTableName().equalsIgnoreCase("FIVEK_STRING"));
  
              }
          }
          assertTrue(threwException);
      }
  
      public void testDivideByZero() throws IOException {
          System.out.println("STARTING DivideByZero");
          Client client = getClient();
  
          VoltTable[] results = null;
          try {
              results = client.callProcedure("DivideByZero", 0L, 0L, (byte)1).getResults();
              System.out.println("DivideByZero client response received");
              assertEquals(results.length, 0);
          } catch (ProcCallException e) {
              System.out.println(e.getMessage());
              if (e.getMessage().contains("SQL ERROR"))
                  return;
              if (isHSQL())
                  if (e.getMessage().contains("HSQL-BACKEND ERROR"))
                      return;
          } catch (IOException e) {
              fail(e.toString());
              return;
          }
          fail("testDivideByZero should return while catching ProcCallException");
      }
  
      //
      // Note: this test looks like it should be testing the 50MB buffer serialization
      // limit between the EE and Java but watching it run, it really fails on max
      // temp table serialization sizes. This needs more investigation.
      //
      public void testMemoryOverload() throws IOException, ProcCallException {
          if (isHSQL() || isValgrind()) return;
  
          final int STRLEN = 30000;
  
          int totalBytes = 0;
          int expectedMaxSuccessBytes = 40000000; // less than the 50*1024*1024 limit.
          int expectedRows = 0;
  
          System.out.println("STARTING testMemoryOverload");
  