You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ol...@apache.org on 2003/03/16 19:14:50 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/otm AllTests.java OtmExamples.java

olegnitz    2003/03/16 10:14:50

  Added:       src/test/org/apache/ojb/otm AllTests.java OtmExamples.java
  Log:
  Added test for basic OTM functions
  
  Revision  Changes    Path
  1.1                  db-ojb/src/test/org/apache/ojb/otm/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  package org.apache.ojb.otm;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.ojb.broker.metadata.CustomAttributesTest;
  import org.apache.ojb.broker.metadata.MetadataTest;
  import org.apache.ojb.broker.metadata.RepositoryPersistorTest;
  import org.apache.ojb.broker.sequence.SequenceManagerTest;
  
  /**
   * the facade to all TestCases in this package.
   *
   * @author: Thomas Mahler
   */
  public class AllTests extends junit.framework.TestSuite
  {
      /** static reference to .class.
       * Java does not provide any way to obtain the Class object from
       * static method without naming it.
       */
      private static Class CLASS = AllTests.class;
  
      /**
       * runs the suite in a junit.textui.TestRunner.
       */
      public static void main(String[] args)
      {
          String[] arr = {CLASS.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      /** build a TestSuite from all the TestCases in this package*/
      public static Test suite()
      {
          TestSuite suite = new TestSuite();
          suite.addTest(new TestSuite(OtmExamples.class));
          return suite;
      }
  
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/otm/OtmExamples.java
  
  Index: OtmExamples.java
  ===================================================================
  package org.apache.ojb.otm;
  
  import java.util.List;
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.Article;
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBrokerFactory;
  import org.apache.ojb.otm.OTMKit;
  import org.apache.ojb.otm.kit.SimpleKit;
  import org.apache.ojb.otm.OTMConnection;
  import org.apache.ojb.otm.core.Transaction;
  import org.apache.ojb.otm.transaction.TransactionFactory;
  
  /**
   * Demo Application that shows basic concepts for Applications
   * using the OJB OTM layer directly.
   */
  public class OtmExamples extends TestCase
  {
      private static Class CLASS = OtmExamples.class;
      private OTMKit _kit;
      private OTMConnection _conn;
  
      public OtmExamples(String name)
      {
          super(name);
      }
  
      public void setUp()
      {
          _kit = SimpleKit.getInstance();
          _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
      }
  
      public void tearDown()
      {
          _conn = null;
      }
  
      public void testOtmSession() throws Exception
      {
          Transaction tx = null;
  
          //perform transaction
          try
          {
              tx = _kit.getTransaction(_conn);
              tx.begin();
  
              Article example = Article.createInstance();
              example.setArticleId(77777);
              example.setProductGroupId(7);
              example.setStock(333);
              _conn.makePersistent(example);
  
              tx.commit();
  
              Identity oid = _conn.getIdentity(example);
  
              tx = _kit.getTransaction(_conn);
              tx.begin();
              example = (Article) _conn.getObjectByIdentity(oid);
              assertEquals("should be equal", 7, example.getProductGroupId());
              assertEquals("should be equal", 333, example.getStock());
              _conn.deletePersistent(example);
              tx.commit();
          }
          catch (Exception ex)
          {
              try
              {
                  if (tx != null && tx.isInProgress())
                  {
                      tx.rollback();
                  }
              }
              catch (Exception ex2)
              {
              }
              throw ex;
          }
      }
  
      public static void main(String[] args)
      {
          String[] arr = {CLASS.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
  }