You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by Graham Leggett <mi...@sharp.fm> on 2014/08/17 15:10:11 UTC

Torque, Derby and unit tests

Hi all,

I am trying to test some torque code against an in-memory derby database, and am running into a chicken-and-egg situation with respect to the database schema.

When the torque code runs it assumes the database has had a schema loaded, which in the case of the in-memory derby database isn’t true. I am looking for some switch in the torque configuration that will cause torque to apply a schema on startup after the database is created by derby, but I am not finding one.

A trawl of both Google and the torque docs have come up with a blank, there seems to be a base assumption that the database will always exist and the schema correctly applied before torque initialises itself, which makes the writing of unit tests difficult.

Am I missing something fundamental?

Regards,
Graham
—


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Re: Torque, Derby and unit tests

Posted by Thomas Fox <th...@seitenbau.com>.
In case you are trying to run the test project, have you tried adding

add -DonSqlError=continue

to your maven command line ?

If not, you have to execute the SQL manually. Something like the following usually does the trick.

    try
    {
      String driverClassName = ...;
      logger.info("Trying to load driver " + driverClassName);
      Class.forName(driverClassName);

      String username = ...;
      String password = ...;

      InputStream inputStream = getClass().getResourceAsStream(...);

      schemaConnection = DriverManager.getConnection(p.getProperty("connection.url"), username, password);

      BufferedReader bufferedReader = new BufferedReader(
          new InputStreamReader(inputStream, SQL_CHARSET));
      String line;
      StringBuilder queryBuffer = new StringBuilder();
      while ((line = bufferedReader.readLine()) != null)
      {
        if (line.trim().length() == 0
            || line.trim().startsWith("--")
            || line.trim().startsWith("#"))
        {
          continue;
        }
        queryBuffer.append(line);
      }
      bufferedReader.close();
      StringTokenizer tokenizer = new StringTokenizer(queryBuffer.toString(), ";", false);
      int successStatements = 0;
      int errorStatements = 0;
      while (tokenizer.hasMoreTokens())
      {
        String sqlQuery = tokenizer.nextToken();
        Statement statement = schemaConnection.createStatement();
        try
        {
          statement.execute(sqlQuery);
          ++successStatements;
        }
        catch (SQLException e)
        {
          logger.warn(e.getMessage() + ", failed statement : " + sqlQuery);
          ++errorStatements;
        }
        statement.close();
      }
      logger.info(
          "Executed " + successStatements + " statements sucessfully and " + errorStatements + " unsuccessfully");
    }
    catch (Exception e)
    {
      throw new RuntimeException(e);
    }
  }


   Thanks,

          Thomas

----- Ursprüngliche Mail -----
Von: "Graham Leggett" <mi...@sharp.fm>
An: "Apache Torque Developers List" <to...@db.apache.org>
Gesendet: Sonntag, 17. August 2014 15:10:11
Betreff: Torque, Derby and unit tests

Hi all,

I am trying to test some torque code against an in-memory derby database, and am running into a chicken-and-egg situation with respect to the database schema.

When the torque code runs it assumes the database has had a schema loaded, which in the case of the in-memory derby database isn’t true. I am looking for some switch in the torque configuration that will cause torque to apply a schema on startup after the database is created by derby, but I am not finding one.

A trawl of both Google and the torque docs have come up with a blank, there seems to be a base assumption that the database will always exist and the schema correctly applied before torque initialises itself, which makes the writing of unit tests difficult.

Am I missing something fundamental?

Regards,
Graham
—


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org