Re: Using AspectJ for cross-db unit testing.

From: Andriy Shapochka (ashapochk..otmail.com)
Date: Mon Oct 27 2003 - 05:44:20 EST

  • Next message: Mike Kienenberger: "Re: Using AspectJ for cross-db unit testing."

    ----- Original Message -----
    From: "Andrus Adamchik" <andru..bjectstyle.org>
    To: <cayenne-deve..bjectstyle.org>
    Sent: Monday, October 27, 2003 7:55 AM
    Subject: Using AspectJ for cross-db unit testing.

    > Some time back we had an offline conversation with Giulio Cesare about
    > AspectJ and AOP (actually it was "online", but off the list :-)) .

    Pity, you kept it to yourselves. I am delving into AspectJ and would have
    been interested in such a kind of discussion.

    > Looks like AspectJ may help to build an outside control mechanism over
    > the test matrix. Each target DB may have its own set of aspects
    > compiled into the test suite. We can cleanly disable certain test
    > cases, and generate a report at the end, mentioning which tests were
    > excluded.
    >
    > This requires some more thought (and learning on my part - I am pretty
    > much clueless at AOP, and never used it before). Anyone has any good
    > advise about AOP use in general and this case in particular?
    >
    > Andrus
    >

    I created a very rough sketch in AspectJ modelling your situation. Hope it
    will help sort things out. Probably there could be more elegant solutions,
    conceptually, but this also works. One remark though, in general the
    'unsupportedTests' pointcuts should be implemented more artfully to avoid
    the boring method enumerations. All the additional exception handling and
    argument passing technicalities can be easily handled with the well known
    AOP idioms, of course.

    To try it out run the attached jar (I used JSDK 1.4.1):

    D:\projects\ajtest>java -jar ajtest.jar
    Database to test: other
    test1 - success
    Test Case is disabled: void ajtest.TestSuite.test2()
    Test Case is disabled: void ajtest.TestSuite.test3()
    test cases - success

    or

    D:\projects\ajtest>java -Dtested-database=oracle -jar ajtest.jar
    Database to test: oracle
    test1 - success
    test2 - success
    test3 - success
    test cases - success

    or

    D:\projects\ajtest>java -Dtested-database=firebird -jar ajtest.jar
    Database to test: firebird
    Test Case is disabled: void ajtest.TestSuite.test1()
    test2 - success
    test3 - success
    test cases - success

    Here is the source of Main.java. To compile it you need AspectJ 1.1 or
    later, see its doc for details:

    package ajtest;

    //Emulate database tests
    public class Main {
      //Type of the database tested
      static final String TESTED_DATABASE =
          System.getProperty("tested-database", "other");

      public static void main(String[] args) {
        System.out.println("Database to test: " + TESTED_DATABASE);

        TestSuite testCases = new TestSuite();
        //Test the database with three tests at most
        testCases.test1();
        testCases.test2();
        testCases.test3();

        System.out.println("test cases - success");
      }
    }

    //There are three tests to perform at most
    //For certain databases some of them must be disabled
    class TestSuite {
      void test1() {
        System.out.println("test1 - success");
      }
      void test2() {
        System.out.println("test2 - success");
      }
      void test3() {
        System.out.println("test3 - success");
      }
    }

    //TestSwitch defines what to do for the disabled tests
    abstract aspect TestSwitch {
      //the subaspects define which test methods must be disabled in this
    pointcut
      abstract pointcut unsupportedTests();

      //around() works in the exclusive mode
      //if a test-case is unsupported by some of the databases
      //check whether it is not supported by the current database -
    'useSwitch()'
      //if so go around, otherwise the test is supported and we can 'proceed()'
      //with it
      void around() : unsupportedTests() {
        if (useSwitch()) {
          System.out.println("Test Case is disabled: " +
                             thisJoinPointStaticPart.getSignature());
        } else {
          proceed();
        }
      }

      boolean useSwitch() {
        return Main.TESTED_DATABASE.equals(getServicedDatabaseName());
      }

      abstract String getServicedDatabaseName();
    }

    //Each subaspect corresponds to a certain database type and defines
    //the tests unsupported by it.

    aspect OtherSwitch extends TestSwitch {
      pointcut unsupportedTests() :
          call(void TestSuite.test2()) ||
          call(void TestSuite.test3());

      String getServicedDatabaseName() {
        return "other";
      }
    }

    aspect OracleSwitch extends TestSwitch {
      pointcut unsupportedTests();

      String getServicedDatabaseName() {
        return "oracle";
      }
    }

    aspect FirebirdSwitch extends TestSwitch {
      pointcut unsupportedTests() :
          call(void TestSuite.test1());

      String getServicedDatabaseName() {
        return "firebird";
      }
    }

    //Andiy.







    This archive was generated by hypermail 2.0.0 : Mon Oct 27 2003 - 05:38:13 EST