You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by mb...@apache.org on 2022/12/31 17:31:13 UTC

[db-jdo] branch JDO-823 updated: JDO-823: Tests should include assertions

This is an automated email from the ASF dual-hosted git repository.

mbo pushed a commit to branch JDO-823
in repository https://gitbox.apache.org/repos/asf/db-jdo.git


The following commit(s) were added to refs/heads/JDO-823 by this push:
     new 5c3894f8 JDO-823: Tests should include assertions
5c3894f8 is described below

commit 5c3894f89ed49c90bf6a875dc15c56b4c3349252
Author: Michael Bouschen <Mi...@akquinet.de>
AuthorDate: Sat Dec 31 18:31:05 2022 +0100

    JDO-823: Tests should include assertions
---
 .../test/java/javax/jdo/JDOHelperConfigTest.java   |  4 +-
 api/src/test/java/javax/jdo/JDOHelperTest.java     | 53 +++++++++++-----------
 .../javax/jdo/identity/ObjectIdentityTest.java     | 27 +++++++++--
 .../test/java/javax/jdo/spi/JDOImplHelperTest.java | 13 ++++--
 4 files changed, 63 insertions(+), 34 deletions(-)

diff --git a/api/src/test/java/javax/jdo/JDOHelperConfigTest.java b/api/src/test/java/javax/jdo/JDOHelperConfigTest.java
index e5f185b1..ed6f609f 100644
--- a/api/src/test/java/javax/jdo/JDOHelperConfigTest.java
+++ b/api/src/test/java/javax/jdo/JDOHelperConfigTest.java
@@ -274,6 +274,8 @@ public class JDOHelperConfigTest extends AbstractJDOConfigTest {
     Map<Object, Object> actual =
         JDOHelper.getPropertiesFromJdoconfig(
             JDOConstants.ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
+    assertNotNull("Anonymous PMF with no properties returned null", actual);
+    assertEquals("Anonymous PMF with no properties had properties", 0, actual.size());
   }
 
   public void testPositive02_GetAnonymousPMFWithNoProperties() throws IOException {
@@ -286,7 +288,7 @@ public class JDOHelperConfigTest extends AbstractJDOConfigTest {
         JDOHelper.getPropertiesFromJdoconfig(
             JDOConstants.ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, loader);
     assertNotNull("Anonymous PMF with no properties returned null", properties);
-    assertTrue("Anonymous PMF with no properties had properties", properties.size() == 0);
+    assertEquals("Anonymous PMF with no properties had properties", 0, properties.size());
   }
 
   public void testPositive03_PMF0_PMFClassNameViaServicesLookup() throws IOException {
diff --git a/api/src/test/java/javax/jdo/JDOHelperTest.java b/api/src/test/java/javax/jdo/JDOHelperTest.java
index e8846669..a4c71886 100644
--- a/api/src/test/java/javax/jdo/JDOHelperTest.java
+++ b/api/src/test/java/javax/jdo/JDOHelperTest.java
@@ -89,24 +89,28 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testGetPM() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.getPersistenceManager(p) != null) {
-      fail("JDOHelper.getPersistenceManager should return null pm for non-persistent instance");
-    }
+    assertNull(
+        "JDOHelper.getPersistenceManager should return null pm for non-persistent instance",
+        JDOHelper.getPersistenceManager(p));
 
     // TBD: test for persistent instance
   }
 
   /** */
   public void testMakeDirty() {
-    // TBD: test JDOHelper.makeDirty(pc, fieldName);
+    PCPoint p = new PCPoint(1, Integer.valueOf(1));
+    JDOHelper.makeDirty(p, "x");
+    assertFalse("JDOHelper.makeDirty should ignore non-persistent instance", JDOHelper.isDirty(p));
+
+    // TBD: test for persistent instance
   }
 
   /** */
   public void testGetObjectId() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.getObjectId(p) != null) {
-      fail("JDOHelper.getObjectId should return null ObjectId for non-persistent instance");
-    }
+    assertNull(
+        "JDOHelper.getObjectId should return null ObjectId for non-persistent instance",
+        JDOHelper.getObjectId(p));
 
     // TBD test JDOHelper.getObjectId(pc) for persistent instance
   }
@@ -114,10 +118,9 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testGetTransactionObjectId() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.getObjectId(p) != null) {
-      fail(
-          "JDOHelper.getTransactionalObjectId should return null ObjectId for non-persistent instance");
-    }
+    assertNull(
+        "JDOHelper.getTransactionalObjectId should return null ObjectId for non-persistent instance",
+        JDOHelper.getObjectId(p));
 
     // TBD test JDOHelper.getTransactionalObjectId(pc) for persistent instance
   }
@@ -125,9 +128,8 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testIsDirty() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.isDirty(p)) {
-      fail("JDOHelper.isDirty should return false for non-persistent instance");
-    }
+    assertFalse(
+        "JDOHelper.isDirty should return false for non-persistent instance", JDOHelper.isDirty(p));
 
     // TBD test JDOHelper.isDirty(pc) for persistent instance
   }
@@ -135,9 +137,9 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testIsTransactional() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.isTransactional(p)) {
-      fail("JDOHelper.isTransactional should return false for non-persistent instance");
-    }
+    assertFalse(
+        "JDOHelper.isTransactional should return false for non-persistent instance",
+        JDOHelper.isTransactional(p));
 
     // TBD test JDOHelper.isTransactional(pc) for persistent instance
   }
@@ -145,9 +147,9 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testIsPersistent() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.isPersistent(p)) {
-      fail("JDOHelper.isPersistent should return false for non-persistent instance");
-    }
+    assertFalse(
+        "JDOHelper.isPersistent should return false for non-persistent instance",
+        JDOHelper.isPersistent(p));
 
     // TBD test JDOHelper.isPersistent(pc) for persistent instance
   }
@@ -155,9 +157,8 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testIsNew() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.isNew(p)) {
-      fail("JDOHelper.isNew should return false for non-persistent instance");
-    }
+    assertFalse(
+        "JDOHelper.isNew should return false for non-persistent instance", JDOHelper.isNew(p));
 
     // TBD test JDOHelper.isNew(pc) for persistent instance
   }
@@ -165,9 +166,9 @@ public class JDOHelperTest extends AbstractTest {
   /** */
   public void testIsDeleted() {
     PCPoint p = new PCPoint(1, Integer.valueOf(1));
-    if (JDOHelper.isDeleted(p)) {
-      fail("JDOHelper.isDeleted should return false for non-persistent instance");
-    }
+    assertFalse(
+        "JDOHelper.isDeleted should return false for non-persistent instance",
+        JDOHelper.isDeleted(p));
 
     // TBD test JDOHelper.isDeleted(pc) for persistent instance
   }
diff --git a/api/src/test/java/javax/jdo/identity/ObjectIdentityTest.java b/api/src/test/java/javax/jdo/identity/ObjectIdentityTest.java
index 3e87da28..b0bccf98 100644
--- a/api/src/test/java/javax/jdo/identity/ObjectIdentityTest.java
+++ b/api/src/test/java/javax/jdo/identity/ObjectIdentityTest.java
@@ -27,6 +27,7 @@ import java.math.BigDecimal;
 import java.security.PrivilegedAction;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Currency;
 import java.util.Date;
 import java.util.Locale;
@@ -227,14 +228,31 @@ public class ObjectIdentityTest extends SingleFieldIdentityTest {
   public void testStringDateConstructor() {
     SimpleDateFormat usDateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
     helper.registerDateFormat(usDateFormat);
-    new ObjectIdentity(Object.class, "java.util.Date:Jan 01, 1970 00:00:00 AM");
+
+    // construct date instance for 1.1.1970 00:00:00
+    Calendar cal = Calendar.getInstance(Locale.US);
+    cal.clear();
+    cal.set(1970, 0, 1, 0, 0, 0);
+    Date date = cal.getTime();
+
+    ObjectIdentity c1 = new ObjectIdentity(Object.class, "java.util.Date:Jan 01, 1970 00:00:00 AM");
+    ObjectIdentity c2 = new ObjectIdentity(Object.class, date);
+    assertEquals("Equal ObjectIdentity instances compare not equal.", 0, c1.compareTo(c2));
+
     helper.registerDateFormat(DateFormat.getDateTimeInstance());
   }
 
   public void testStringDefaultDateConstructor() {
     DateFormat dateFormat = DateFormat.getDateTimeInstance();
-    String rightNow = dateFormat.format(new Date());
-    new ObjectIdentity(Object.class, "java.util.Date:" + rightNow);
+    Calendar cal = Calendar.getInstance();
+    // nullify millisecond field since dateFormat does not take milliseconds into account
+    cal.clear(Calendar.MILLISECOND);
+    Date date = cal.getTime();
+    String dateAsString = dateFormat.format(date);
+
+    ObjectIdentity c1 = new ObjectIdentity(Object.class, "java.util.Date:" + dateAsString);
+    ObjectIdentity c2 = new ObjectIdentity(Object.class, date);
+    assertEquals("Equal ObjectIdentity instances compare not equal.", 0, c1.compareTo(c2));
   }
 
   public void testBadStringDateConstructor() {
@@ -273,7 +291,8 @@ public class ObjectIdentityTest extends SingleFieldIdentityTest {
 
   public void testStringCurrencyConstructor() {
     if (!isClassLoadable("java.util.Currency")) return;
-    new ObjectIdentity(Object.class, "java.util.Currency:USD");
+    ObjectIdentity c1 = new ObjectIdentity(Object.class, "java.util.Currency:USD");
+    assertEquals("Expected USD currency", Currency.getInstance("USD"), c1.getKeyAsObject());
   }
 
   public void testBadStringCurrencyConstructor() {
diff --git a/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java b/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java
index cc9c92b0..decddd56 100644
--- a/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java
+++ b/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java
@@ -118,13 +118,20 @@ public class JDOImplHelperTest extends AbstractTest {
 
   /** */
   public void testNewInstance() {
-    // TBD: test JDOImplHelper.newInstance(pcClass, sm) and
-    // JDOImplHelper.newInstance(pcClass, sm, oid)
+    JDOImplHelper implHelper = JDOImplHelper.getInstance();
+    PersistenceCapable pcpoint = implHelper.newInstance(PCPoint.class, null);
+    assertTrue("instance created by newInstance is not a PCPoint", pcpoint instanceof PCPoint);
+
+    pcpoint = implHelper.newInstance(PCPoint.class, null, null);
+    assertTrue("instance created by newInstance is not a PCPoint", pcpoint instanceof PCPoint);
   }
 
   /** */
   public void testNewObjectIdInstance() {
-    // TBD: test JDOImplHelper.newObjectIdInstance(pcClass)
+    JDOImplHelper implHelper = JDOImplHelper.getInstance();
+    Object oid = implHelper.newObjectIdInstance(PCPoint.class);
+    // The ObjrectId od javax.jdo.pc.PCPoint is always null
+    assertNull("Unexpected non-null ObjectId of PCPoint", oid);
   }
 
   /** */