Logo Search packages:      
Sourcecode: mysql-connector-c++ version File versions

example.cpp

/*
   Copyright 2008 - 2009 Sun Microsystems, Inc.  All rights reserved.

   The MySQL Connector/C++ is licensed under the terms of the GPL
   <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
   MySQL Connectors. There are special exceptions to the terms and
   conditions of the GPL as it is applied to this software, see the
   FLOSS License Exception
   <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
 */

#include "example.h"

namespace testsuite
{
namespace example
{

void example_test_class::test_hello_world1()
{
  logMsg("Hello world using framework magic");
  try
  {
    /*
     By default the framework will establish a connection in setUp() and connect
     to the configured MySQL Server and select the configured schema.
     No other members will be initialized, however, there are several
     auto-ptr members which you can make use of: this->stmt, this->pstmt, this->res.
     The good thing about the auto-ptr members is that you don't need to care much
     about them. tearDown() will reset them and auto-ptr will ensure proper
     memory management.
     */
    stmt.reset(con->createStatement());

    /* Running a SELECT and storing the returned result set in this->res */
    res.reset(stmt->executeQuery("SELECT 'Hello world!'"));

    /* Move result set cursor to first rowm, fetch result, write result to log  */
    res->next();
    logMsg(res->getString(1));

  }
  catch (sql::SQLException &e)
  {
    /* If anything goes wrong, write some info to the log... */
    logErr(e.what());
    logErr("SQLState: " + std::string(e.getSQLState()));

    /*
     ... and let the test fail. FAIL() is a macro.
     FAIL calls fail(const char* reason, const char* file, int line)
     */
    fail(e.what(), __FILE__, __LINE__);
  }
  /* If all goes fine, there is no need to call PASS() or something.     */
}

void example_test_class::test_hello_world2()
{
  logMsg("Hello world without framework magic");
  try
  {
    /*
     Connection, Statement and ResultSet are typedefs from unit_fixture.h:

     typedef std::auto_ptr<sql::Connection> Connection;
     typedef std::auto_ptr<sql::PreparedStatement> PreparedStatement;
     typedef std::auto_ptr<sql::ParameterMetaData> ParameterMetaData;
     typedef std::auto_ptr<sql::Statement> Statement;
     typedef std::auto_ptr<sql::Savepoint> Savepoint;
     typedef std::auto_ptr<sql::ResultSet> ResultSet;
     typedef sql::Driver Driver;
     typedef std::auto_ptr<sql::ResultSetMetaData> ResultSetMetaData;
     typedef std::auto_ptr<sql::DatabaseMetaData> DatabaseMetaData;

     Do yourself a favour and use auto_ptr in tests!
     */
    Connection my_con(getConnection());
    Statement my_stmt(my_con->createStatement());

    /*
     The framework defines a couple of public members (from unit_fixture.h):

     Connection con;
     PreparedStatement pstmt;
     Statement stmt;
     ResultSet res;

     The members are managed in unit_fixture.cpp setUp() and tearDown().
     You don't need to clean up them as long as setUp() and tearDown()
     are called and are not overwritten by your own method.

     However note, if you declare a local variable of the same type
     and name of any of the above members, for example ResultSet res, some
     compilers will spawn warnings that you are hiding members.
     */
    res.reset(my_stmt->executeQuery("SELECT 'Hello world!' AS _world"));

    res->next();
    logMsg(res->getString("_world"));
    res->close();

    ResultSet res2(my_stmt->executeQuery("SELECT 'What a boring example' AS _complain"));
    res2->next();
    logMsg(res2->getString("_complain"));
    res2->close();
  }
  catch (sql::SQLException &e)
  {
    logErr(e.what());
    logErr("SQLState: " + std::string(e.getSQLState()));
    fail(e.what(), __FILE__, __LINE__);
  }
}

void example_test_class::test_assert_equals()
{
  logMsg("ASSERT_EQUALS() macro demo");
  /*
   Be careful: ASSERT_EQUALS() is not available for each and every type!
   You can find more details in test_asserts.h.
   */
  int a=-1;
  ASSERT_EQUALS(a, a);

  unsigned int h=1;
  ASSERT_EQUALS(h, h);

  int64_t i=-10;
  ASSERT_EQUALS(i, i);

  uint64_t j=10;
  ASSERT_EQUALS(j, j);

  bool b=false;
  ASSERT_EQUALS(b, b);

  float c= -1.23f;
  ASSERT_EQUALS(c, c);

  double d=1.23;
  ASSERT_EQUALS(d, d);

  /*
   For fuzzy comparisons e.g. 1.01 vs. 1.01 fetched from MySQL
   */
  double k = 1.01;
  double l = 1.0999992;
  fuzzyEquals(k, l, 0.001);

  long double e=1.23e12;
  ASSERT_EQUALS(e, e);

  const char f[]="foo";
  ASSERT_EQUALS(f, f);

  std::string g("good idea");
  ASSERT_EQUALS(g, g);
}

void example_test_class::test_assert_lessthan()
{
  logMsg("ASSERT_LT() macro demo");
  /*
   Be careful: ASSERT_LT(expected, value) is not available for each and every type!
   In fact at the time of writing its only available for int and unsigned int
   */
  int a=1, b=2;

  ASSERT_LT(b, a);
  ASSERT_LT((unsigned int) b, (unsigned int) a);

  /* There is no "less or equal than", use LT instead.
  Example: 1 <= 2 */
  ASSERT_LT(3, 1);
}

void example_test_class::test_assert_greaterthan()
{
  logMsg("ASSERT_GT() macro demo");
  /*
   Be careful: ASSERT_GT(expected, value) is not available for each and every type!
   In fact at the time of writing its only available for int and unsigned int
   */
  int a=1, b=2;

  ASSERT_GT(a, b);
  ASSERT_GT((unsigned int) a, (unsigned int) b);

  /* There is no "greater or equal than", use GT instead.
  Example: 2 >= 1 */
  ASSERT_GT(0, 2);
}

void example_test_class::test_assert_equals_fail()
{
  logMsg("ASSERT_EQUALS failure - EXPECTED failure");
  ASSERT_EQUALS(true, false);
}

void example_test_class::test_assert_message()
{
  logMsg("ASSERT_MESSAGE example");
  /*
   Wrapper of
  void assertTrue(const char * msg, bool expression
                , const char* file, int line);
   */
  ASSERT_MESSAGE(1 < 2, "ASSERT_MESSAGE example (true)");
  ASSERT_MESSAGE(1 > 2, "ASSERT_MESSAGE example (fail) - EXPECTED failure");
}

void example_test_class::test_skip()
{
  logMsg("SKIP() example");
  /* Test if MySQL has a certain feature, e.g. if Procedures are supported */
  if (true)
    SKIP("EXAMPLE: the server does not support stored procedures");
  /*
   If MySQL does not suppoer feature xyz, call SKIP() and test execution ends.
   You get here only if you feature is available
   */
  logMsg("Feature is available, use it.");
}

void example_test_class::test_todo()
{
  logMsg("TODO() example");
  /* Test if MySQL has a certain feature, e.g. if Procedures are supported */
  TODO("If you know that a test mail fail and you intentionally want to let it fail temporarily, for example, as long as a bug has not been fixed, mark it as TODO");
  //FAIL("Expected failure, known failure - its on your TODO");
}

} /* namespace example */
} /* namespace testsuite */

Generated by  Doxygen 1.6.0   Back to index