db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker FieldConversionTest_2.java FieldConversionTest_3.java AllTests.java
Date Fri, 28 Feb 2003 17:05:42 GMT
arminw      2003/02/28 09:05:41

  Modified:    src/test/org/apache/ojb/broker AllTests.java
  Added:       src/test/org/apache/ojb/broker FieldConversionTest_2.java
                        FieldConversionTest_3.java
  Log:
  rename tests, update AllTests
  
  Revision  Changes    Path
  1.19      +3 -2      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- AllTests.java	16 Feb 2003 17:32:05 -0000	1.18
  +++ AllTests.java	28 Feb 2003 17:05:41 -0000	1.19
  @@ -57,7 +57,6 @@
           suite.addTest(new TestSuite(RepositoryPersistorTest.class));
           suite.addTest(new TestSuite(CustomAttributesTest.class));
           suite.addTest(new TestSuite(SequenceManagerTest.class));
  -        suite.addTest(new TestSuite(ConvertedKeyReferenceTest.class));
           suite.addTest(new TestSuite(KeyConstraintViolationTest.class));
           suite.addTest(new TestSuite(RsIteratorTest.class));
           //suite.addTest(new TestSuite(BlobTest.class)); /* doesn't work for hsqldb */
  @@ -66,7 +65,9 @@
           suite.addTest(new TestSuite(OneToOneTest.class));
           suite.addTest(new TestSuite(MetaDataSerializationTest.class));
           suite.addTest(new TestSuite(MetadataTest.class));
  -        suite.addTest(new TestSuite(FieldConversionForeigenKeyTest.class));
  +        suite.addTest(new TestSuite(FieldConversionTest.class));
  +        suite.addTest(new TestSuite(FieldConversionTest_2.class));
  +        suite.addTest(new TestSuite(FieldConversionTest_3.class));
           suite.addTest(new TestSuite(BatchModeTest.class));
           return suite;
       }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/FieldConversionTest_2.java
  
  Index: FieldConversionTest_2.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import junit.framework.TestCase;
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.commons.lang.builder.ToStringStyle;
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryFactory;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  /**
   *
   * @author J. Russell Smyth
   * @version $Id: FieldConversionTest_2.java,v 1.1 2003/02/28 17:05:41 arminw Exp $
   */
  public class FieldConversionTest_2 extends TestCase
  {
      private PersistenceBroker broker;
  
      public FieldConversionTest_2(String testName)
      {
          super(testName);
      }
  
      public static void main(String[] args)
      {
          String[] arr = {FieldConversionTest_2.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public void setUp()
      {
          try
          {
              broker = PersistenceBrokerFactory.defaultPersistenceBroker();
          }
          catch (Exception e)
          {
              LoggerFactory.getBootLogger().error("error getting broker", e);
          }
      }
  
      protected void tearDown() throws Exception
      {
          super.tearDown();
          if (broker != null) broker.close();
      }
  
      public void testConvertedReferenceLookup()
      {
          Collection coll = null;
          Criteria c = null;
          Query q = QueryFactory.newQuery(ConversionReferrer.class, c);
          coll = broker.getCollectionByQuery(q);
          assertTrue("There should be more than 0 matching items", coll.size() > 0);
  
          Iterator i = coll.iterator();
          while (i.hasNext())
          {
              ConversionReferrer cref = (ConversionReferrer) i.next();
              assertTrue("PK value should not be converted, id should be < 1000, found
" + cref, cref.getPk1() < 1000);
              assertTrue("Reference id should be > 1000, found " + cref, cref.getRef1()
> 1000);
              assertTrue("Reference should be non-null, found " + cref, cref.getReferred()
!= null);
              assertEquals("Reference does not select correct item", cref.getRef1(), cref.getReferred().getPk1());
          }
      }
  
      public void testMultipleConverted()
      {
          String error = "Indicate that the field conversion was not/or multiple times called
for a value, expected > 100 - found ";
          Collection coll = null;
          Criteria c = null;
          Query q = QueryFactory.newQuery(ConversionReferrer.class, c);
          coll = broker.getCollectionByQuery(q);
          assertTrue("There should be more than 0 matching items", coll.size() > 0);
  
          Iterator i = coll.iterator();
          while (i.hasNext())
          {
              ConversionReferrer cref = (ConversionReferrer) i.next();
              assertTrue("PK value should not be converted, id should be < 1000, found
" + cref, cref.getPk1() < 1000);
              assertTrue("Reference should be non-null, found " + cref, cref.getReferred()
!= null);
              assertEquals("Reference selected incorrect item", cref.getRef1(), cref.getReferred().getPk1());
  
              /*
              The used conversion does the following
              val = Integer.MAX_VALUE - val;
              for both conversion directions.
              The result was e.g.
              sqlToJava: 10 --> 2147483637
              javaToSql: 2147483637 --> 10
  
              */
  
              int value = 0;
              value = cref.getRef1();
              assertTrue(error + cref, value > 1000);
  
              value = cref.getTestId();
              assertTrue(error + cref, value > 1000);
  
              value = cref.getReferred().getPk1();
              assertTrue(error + cref, value > 1000);
  
              value = cref.getReferred().getTestId();
              assertTrue(error + cref, value > 1000);
          }
      }
  
      public void testConvertedReferenceInsert()
      {
          String error = "Maybe field conversion was not called or multiple times";
          int no = 110;
          int noRef = Integer.MAX_VALUE - 109;
          int noTest = Integer.MAX_VALUE - 108;
          int noTestRef = Integer.MAX_VALUE - 107;
  
          ConversionReferrer cref = new ConversionReferrer();
          cref.setPk1(no);
          cref.setTestId(noTest);
  
          ConversionReferred crefed = new ConversionReferred();
          crefed.setPk1(noRef);
          crefed.setTestId(noTestRef);
          // set reference
          cref.setReferred(crefed);
  
          broker.store(crefed);
          broker.store(cref);
  
          broker.clearCache();
  
          // save id for recapturing object
          Identity id = new Identity(cref, broker);
          broker.clearCache();
  
          ConversionReferrer referrer = (ConversionReferrer) broker.getObjectByIdentity(id);
          assertNotNull(cref.getReferred());
          assertNotNull("We should found a reference, found " + referrer, referrer.getReferred());
          assertEquals("Stored reference ID should match refed object pk", referrer.getRef1(),
crefed.getPk1());
          assertEquals(error, cref.getPk1(), referrer.getPk1());
          assertEquals(error, cref.getTestId(), referrer.getTestId());
          assertEquals(error, cref.getReferred().getPk1(), referrer.getReferred().getPk1());
  
          assertEquals(error, cref.getReferred().getTestId(), referrer.getReferred().getTestId());
  
          // delete objects
          broker.delete(crefed);
          broker.delete(cref);
      }
  
  
      //****************************************************************************
      // inner class
      //****************************************************************************
      /**
       * A conversion class for unit testing. The conversion is nonsensical - java
       * field is difference of Integer.MAX_VALUE and db value.
       * @author  drfish
       */
      public static class TestInt2IntConverter implements FieldConversion
      {
  
          /** Creates a new instance of FromMaxInt2IntConversion */
          public TestInt2IntConverter()
          {
          }
  
          /** convert a Java object to its SQL pendant, used for insert & update */
          public Object javaToSql(Object source) throws ConversionException
          {
  
              int val = ((Integer) source).intValue();
              val = Integer.MAX_VALUE - val;
              return new Integer(val);
          }
  
          /** convert a SQL value to a Java Object, used for SELECT */
          public Object sqlToJava(Object source) throws ConversionException
          {
              int val = ((Integer) source).intValue();
              val = Integer.MAX_VALUE - val;
              return new Integer(val);
          }
      }
  
  
      //****************************************************************************
      // inner class
      //****************************************************************************
      public static class ConversionReferrer
      {
  
          private int pk1;
          private int testId;
          private int ref1;
          private ConversionReferred referred;
  
          /** Creates a new instance of ConversionParent */
          public ConversionReferrer()
          {
          }
  
          public String toString()
          {
              ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
              buf.append("pk1", pk1);
              buf.append("ref1", ref1);
              buf.append("testId", testId);
              buf.append("referred", referred);
              return buf.toString();
          }
  
          public int getTestId()
          {
              return testId;
          }
  
          public void setTestId(int testId)
          {
              this.testId = testId;
          }
  
          /** Getter for property pk1.
           * @return Value of property pk1.
           *
           */
          public int getPk1()
          {
              return pk1;
          }
  
          /** Setter for property pk1.
           * @param pk1 New value of property pk1.
           *
           */
          public void setPk1(int pk1)
          {
              this.pk1 = pk1;
          }
  
          /** Getter for property ref1.
           * @return Value of property ref1.
           *
           */
          public int getRef1()
          {
              return ref1;
          }
  
          /** Setter for property ref1.
           * @param ref1 New value of property ref1.
           *
           */
          public void setRef1(int ref1)
          {
              this.ref1 = ref1;
          }
  
          /** Getter for property referred.
           * @return Value of property referred.
           *
           */
          public ConversionReferred getReferred()
          {
              return referred;
          }
  
          /** Setter for property referred.
           * @param referred New value of property referred.
           *
           */
          public void setReferred(ConversionReferred referred)
          {
              this.referred = referred;
          }
      }
  
      //****************************************************************************
      // inner class
      //****************************************************************************
      public static class ConversionReferred
      {
  
          private int pk1;
          private int testId;
  
          /** Creates a new instance of ConversionReferred */
          public ConversionReferred()
          {
          }
  
          public String toString()
          {
              ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
              buf.append("pk1", pk1);
              buf.append("testId", testId);
              return buf.toString();
          }
  
          public int getTestId()
          {
              return testId;
          }
  
          public void setTestId(int testId)
          {
              this.testId = testId;
          }
  
          /** Getter for property pk1.
           * @return Value of property pk1.
           *
           */
          public int getPk1()
          {
              return pk1;
          }
  
          /** Setter for property pk1.
           * @param pk1 New value of property pk1.
           *
           */
          public void setPk1(int pk1)
          {
              this.pk1 = pk1;
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/FieldConversionTest_3.java
  
  Index: FieldConversionTest_3.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import junit.framework.TestCase;
  import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.QueryFactory;
  
  import java.math.BigDecimal;
  
  /**
   * Test using field conversions for PK values.
   *
   * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
   * @version $Id: FieldConversionTest_3.java,v 1.1 2003/02/28 17:05:41 arminw Exp $
   */
  public class FieldConversionTest_3 extends TestCase
  {
      private PersistenceBroker broker;
  
      public static void main(String[] args)
      {
          String[] arr = {FieldConversionTest_3.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public void setUp() throws PBFactoryException
      {
          broker = PersistenceBrokerFactory.defaultPersistenceBroker();
      }
  
      public void tearDown()
      {
          try
          {
              broker.clearCache();
              broker.close();
          }
          catch (PersistenceBrokerException e)
          {
          }
      }
  
      /**
       * store nested classes needed field conversion of a primary key field.
       */
      public void testStoreNestedNodes() throws Exception
      {
          //String strQuery = "select allNodes from " + Node.class.getName();
          long id = System.currentTimeMillis();
          Node node = new Node(id, null, true);
          Node child = new Node(id + 1, node, false);
  
          int before;
  
          broker.beginTransaction();
          before = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null));
  
          broker.store(child);
  
          broker.commitTransaction();
  
          broker.beginTransaction();
          int after = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null));
          broker.commitTransaction();
  
          assertFalse(after == 0);
          assertEquals(before + 2, after);
      }
  
      /**
       * store class needed field conversion of a primary key field.
       */
      public void testStoreNode() throws Exception
      {
          //String strQuery = "select allNodes from " + Node.class.getName();
          long id = System.currentTimeMillis();
          Node node = new Node(id, null, false);
  
          int before;
  
          broker.beginTransaction();
          before = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null));
  
          broker.store(node);
  
          broker.commitTransaction();
  
          broker.beginTransaction();
          int after = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null));
          broker.commitTransaction();
  
          assertFalse(after == 0);
          assertEquals(before + 1, after);
      }
  
  
      //****************************************************************************
      // inner class
      //****************************************************************************
      public static class LongToBigDecimalConversion implements FieldConversion
      {
          public LongToBigDecimalConversion()
          {
          }
  
          public Object javaToSql(Object source) throws ConversionException
          {
              Object ret;
              if (source instanceof Long)
              {
                  ret = new BigDecimal(((Long) source).doubleValue());
              }
              else
              {
                  throw new ConversionException(
                          "java-->sql, expected type was"+Long.class.getClass()+
                          ", found type "+source.getClass());
              }
              return ret;
          }
  
          public Object sqlToJava(Object source) throws ConversionException
          {
              Object ret;
              if (source instanceof BigDecimal)
              {
                  ret = new Long(((BigDecimal) source).longValue());
              }
              else
              {
                  throw new ConversionException(
                          "sql-->java, expected type was"+BigDecimal.class.getClass()+
                          ", found type "+source.getClass());
              }
              return ret;
          }
      }
  
  
      //****************************************************************************
      // inner class
      //****************************************************************************
      public class Node
      {
          private long uid;  // primary key
          private long refId;
          private boolean nodeState;
          Node parent;
  
          public Node()
          {
          }
  
          public Node(long uid, Node parent, boolean nodeState)
          {
              this.uid = uid;
              this.parent = parent;
              this.nodeState = nodeState;
          }
  
          public long getUid()
          {
              return uid;
          }
  
          public void setUid(long uid)
          {
              this.uid = uid;
          }
  
          public boolean isState()
          {
              return nodeState;
          }
  
          public void setState(boolean state)
          {
              this.nodeState = state;
          }
  
          public long getRefId()
          {
              return refId;
          }
  
          public void setRefId(long refId)
          {
              this.refId = refId;
          }
  
          public Node getParent()
          {
              return parent;
          }
  
          public void setParent(Node parent)
          {
              this.parent = parent;
          }
      }
  }
  
  
  

Mime
View raw message