db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject svn commit: r1787581 - in /db/jdo/trunk/tck/src: conf/ java/org/apache/jdo/tck/api/persistencemanager/close/ java/org/apache/jdo/tck/extents/ java/org/apache/jdo/tck/query/api/
Date Sat, 18 Mar 2017 17:42:30 GMT
Author: mbo
Date: Sat Mar 18 17:42:30 2017
New Revision: 1787581

URL: http://svn.apache.org/viewvc?rev=1787581&view=rev
Log:
JDO-735: add TCK test cases for try-with-resources for PM, Query and Extent

Added:
    db/jdo/trunk/tck/src/java/org/apache/jdo/tck/extents/AutoCloseable.java
    db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/AutoCloseable.java
Modified:
    db/jdo/trunk/tck/src/conf/extents.conf
    db/jdo/trunk/tck/src/conf/query.conf
    db/jdo/trunk/tck/src/java/org/apache/jdo/tck/api/persistencemanager/close/AutoCloseable.java

Modified: db/jdo/trunk/tck/src/conf/extents.conf
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/conf/extents.conf?rev=1787581&r1=1787580&r2=1787581&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/conf/extents.conf (original)
+++ db/jdo/trunk/tck/src/conf/extents.conf Sat Mar 18 17:42:30 2017
@@ -20,6 +20,7 @@ jdo.tck.standarddata =
 jdo.tck.mapping = 0
 jdo.tck.requiredOptions =
 jdo.tck.classes = \
+org.apache.jdo.tck.extents.AutoCloseable \
 org.apache.jdo.tck.extents.CloseAll \
 org.apache.jdo.tck.extents.CloseOfExtentIteratorIsIteratorSpecific \
 org.apache.jdo.tck.extents.GetCandidateClass \

Modified: db/jdo/trunk/tck/src/conf/query.conf
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/conf/query.conf?rev=1787581&r1=1787580&r2=1787581&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/conf/query.conf (original)
+++ db/jdo/trunk/tck/src/conf/query.conf Sat Mar 18 17:42:30 2017
@@ -20,6 +20,7 @@ jdo.tck.standarddata =
 jdo.tck.mapping = 0
 jdo.tck.requiredOptions =
 jdo.tck.classes = \
+org.apache.jdo.tck.query.api.AutoCloseable \
 org.apache.jdo.tck.query.api.ChangeQuery \
 org.apache.jdo.tck.query.api.Close \
 org.apache.jdo.tck.query.api.CloseAll \

Modified: db/jdo/trunk/tck/src/java/org/apache/jdo/tck/api/persistencemanager/close/AutoCloseable.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/java/org/apache/jdo/tck/api/persistencemanager/close/AutoCloseable.java?rev=1787581&r1=1787580&r2=1787581&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/java/org/apache/jdo/tck/api/persistencemanager/close/AutoCloseable.java
(original)
+++ db/jdo/trunk/tck/src/java/org/apache/jdo/tck/api/persistencemanager/close/AutoCloseable.java
Sat Mar 18 17:42:30 2017
@@ -17,9 +17,7 @@
 
 package org.apache.jdo.tck.api.persistencemanager.close;
 
-import javax.jdo.JDOUserException;
 import javax.jdo.PersistenceManager;
-import javax.jdo.Transaction;
 
 import org.apache.jdo.tck.api.persistencemanager.PersistenceManagerTest;
 import org.apache.jdo.tck.util.BatchTestRunner;
@@ -32,7 +30,8 @@ import org.apache.jdo.tck.util.BatchTest
  *<B>Assertion IDs:</B> A12.6 ?.
  *<BR>
  *<B>Assertion Description: </B>
-In a non-managed environment, if the PM is created with try-with-resources then it is automatically
closed at the end of that block.
+ * In a non-managed environment, if the PM is created with try-with-resources 
+ * then it is automatically closed at the end of that block.
  */
 
 public class AutoCloseable extends PersistenceManagerTest {
@@ -50,15 +49,50 @@ public class AutoCloseable extends Persi
         BatchTestRunner.run(AutoCloseable.class);
     }
 
-    /** */
-    public void test() {
+    /** 
+     * The method creates a pm with try-with-resources and checks that it is closed after
the block.
+     */
+    public void testTryWithResource() {
+
+        try (PersistenceManager pm1 = getPM()) {
+            pm = pm1;
+            if (pm.isClosed()) {
+                fail(ASSERTION_FAILED,
+                        "PersistenceManager is expected to be open inside try-with-resource
block.");
+            }
+        }
+
+        if (!pm.isClosed()) {
+            fail(ASSERTION_FAILED,
+                    "PersistenceManager should be closed after try-with-resource block.");
+        }
+    }
+
+   /** 
+     * The method creates a pm with try-with-resources and checks that it is closed after
the block,
+     * if the block is ended with an exception. 
+     */
+    public void testTryWithResourceThrowingException() {
 
-        try (PersistenceManager pm1 = getPM())
-        {
+        try (PersistenceManager pm1 = getPM()) {
             pm = pm1;
-            assertFalse(pm.isClosed());
+            if (pm.isClosed()) {
+                fail(ASSERTION_FAILED,
+                        "PersistenceManager is expected to be open inside try-with-resource
block.");
+            }
+            throw new DummyException();
+        } catch (DummyException ex) {
+            // exception is expected
         }
 
-        assertTrue(pm.isClosed());
+        if (!pm.isClosed()) {
+            fail(ASSERTION_FAILED,
+                    "PersistenceManager should be closed after try-with-resource block.");
+        }
     }
+
+    /**
+     * DummyException used in method testTryWithResourceThrowingException.
+     */
+    private static final class DummyException extends Exception {}
 }

Added: db/jdo/trunk/tck/src/java/org/apache/jdo/tck/extents/AutoCloseable.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/java/org/apache/jdo/tck/extents/AutoCloseable.java?rev=1787581&view=auto
==============================================================================
--- db/jdo/trunk/tck/src/java/org/apache/jdo/tck/extents/AutoCloseable.java (added)
+++ db/jdo/trunk/tck/src/java/org/apache/jdo/tck/extents/AutoCloseable.java Sat Mar 18 17:42:30
2017
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+package org.apache.jdo.tck.extents;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javax.jdo.Extent;
+import javax.jdo.PersistenceManager;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+import org.apache.jdo.tck.util.BatchTestRunner;
+import org.apache.jdo.tck.pc.company.Department;
+
+/**
+ *<B>Title:</B> AutoCloseable
+ *<BR>
+ *<B>Keywords:</B> exception
+ *<BR>
+ *<B>Assertion IDs:</B> A12.6 ?.
+ *<BR>
+ *<B>Assertion Description: </B>
+ * In a non-managed environment, if the extent is created with try-with-resources
+ * all results of execute(...) methods on this query instance are automatically 
+ * closed at the end of that block and all resources associated with it are released.
+ */
+public class AutoCloseable extends ExtentTest {
+    
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion A12.6-? (AutoCloseable) failed: ";
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(AutoCloseable.class);
+    }
+
+    /**
+     * This methods creates an extent instance with try-with-resources and
+     * checks that an iterator for the query result is not accessible after the block.
+     */
+    public void testTryWithResource() {
+
+        PersistenceManager pm = getPM();
+        Transaction tx = pm.currentTransaction();
+
+        try {
+            tx.begin();
+
+            Extent<Department> extent = null;
+            Iterator<Department> iterator = null;
+            try (Extent<Department> extent1 = pm.getExtent(Department.class)) {
+                extent = extent1;
+                iterator = extent1.iterator();
+                if (!iterator.hasNext()) {
+                    fail(ASSERTION_FAILED, "(1) Open extent iterator should have elements.");
+                }
+            } catch (Exception ex) {
+                fail(ASSERTION_FAILED, "(2) Unexpected exception " + ex);
+            }
+
+            // check iterator retrieved in try-with-resource block
+            if (iterator.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(3) Closed extent iterator should return false on hasNext().");
+            }
+            try {
+                Department next = iterator.next();
+                fail(ASSERTION_FAILED,
+                        "(4) Closed extent iterator should throw NoSuchElementException on
next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            Iterator<Department> iterator2 = extent.iterator();
+            if (!iterator2.hasNext()) {
+                fail(ASSERTION_FAILED, "(5) extent should be usable and should have elements.");
+            }
+
+            tx.commit();
+        } finally {
+            if (tx != null && tx.isActive()) {
+                tx.rollback();
+            }
+        }
+    }
+
+    /**
+     * This methods creates an extent instance with try-with-resources and
+     * checks that an iterator for the query result is not accessible after the block,
+     * if the block is ended with an exception.
+     */
+    public void testTryWithResourceThrowingException() {
+
+        PersistenceManager pm = getPM();
+        Transaction tx = pm.currentTransaction();
+
+        try {
+            tx.begin();
+
+            Extent<Department> extent = null;
+            Iterator<Department> iterator = null;
+            try (Extent<Department> extent1 = pm.getExtent(Department.class)) {
+                extent = extent1;
+                iterator = extent1.iterator();
+                if (!iterator.hasNext()) {
+                    fail(ASSERTION_FAILED, "(1) Open extent iterator should have elements.");
+                }
+                throw new DummyException();
+            } catch (DummyException ex) {
+                // expected exception
+            } catch (Exception ex) {
+                fail(ASSERTION_FAILED, "(2) Unexpected exception " + ex);
+            }
+
+            // check iterator retrieved in try-with-resource block
+            if (iterator.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(3) Closed extent iterator should return false on hasNext().");
+            }
+            try {
+                Department next = iterator.next();
+                fail(ASSERTION_FAILED,
+                        "(4) Closed extent iterator should throw NoSuchElementException on
next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            Iterator<Department> iterator2 = extent.iterator();
+            if (!iterator2.hasNext()) {
+                fail(ASSERTION_FAILED, "(5) extent should be usable and should have elements.");
+            }
+
+            tx.commit();
+        } finally {
+            if (tx != null && tx.isActive()) {
+                tx.rollback();
+            }
+        }
+    }
+
+    /**
+     * DummyException used in method testTryWithResourceThrowingException.
+     */
+    private static final class DummyException extends Exception {}
+
+}

Added: db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/AutoCloseable.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/AutoCloseable.java?rev=1787581&view=auto
==============================================================================
--- db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/AutoCloseable.java (added)
+++ db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/AutoCloseable.java Sat Mar 18 17:42:30
2017
@@ -0,0 +1,251 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+package org.apache.jdo.tck.query.api;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import javax.jdo.JDOUserException;
+import javax.jdo.Query;
+import javax.jdo.PersistenceManager;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.tck.query.QueryTest;
+import org.apache.jdo.tck.util.BatchTestRunner;
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+
+/**
+ *<B>Title:</B> AutoCloseable
+ *<BR>
+ *<B>Keywords:</B> query close
+ *<BR>
+ *<B>Assertion IDs:</B> A14.6.7-3.
+ *<BR>
+ *<B>Assertion Description: </B>
+ * In a non-managed environment, if the query is created with try-with-resources
+ * all results of execute(...) methods on this query instance are automatically 
+ * closed at the end of that block and all resources associated with it are released.
+ */
+public class AutoCloseable extends QueryTest {
+    
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion A14.6.7-3 (AutoCloseable) failed: ";
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(AutoCloseable.class);
+    }
+
+    /**
+     * This methods creates a query instance with try-with-resources and 
+     * checks that an iterator for the query result is not accessible after the block.
+     */
+    public void testTryWithResource() {
+
+        PersistenceManager pm = getPM();
+        Transaction tx = pm.currentTransaction();
+
+        try {
+            tx.begin();
+
+            Query query = null;
+            List<PCPoint> queryResult = null;
+            Iterator<PCPoint> iterator = null;
+            try (Query query1 = pm.newQuery(PCPoint.class)) {
+                query = query1;
+                queryResult = (List<PCPoint>) query1.execute();
+                iterator = queryResult.iterator();
+                if (!iterator.hasNext()) {
+                    fail(ASSERTION_FAILED, "(1) Iterator of open query result should have
elements.");
+                }
+            } catch (Exception ex) {
+                fail(ASSERTION_FAILED, "(2) Unexpected exception " + ex);
+            }
+
+            // check iterator retrieved in try-with-resource block
+            if (iterator.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(3) Iterator of closed query result should return false on hasNext().");
+            }
+            try {
+                PCPoint next = iterator.next();
+                fail(ASSERTION_FAILED,
+                        "(4) Iterator of closed query result should throw NoSuchElementException
on next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            // create new Iterator and check its behaviour
+            Iterator<PCPoint> iterator2 = queryResult.iterator();
+            if (iterator2.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(5) Iterator of closed query result should return false on hasNext().");
+            }
+            try {
+                PCPoint next = iterator2.next();
+                fail(ASSERTION_FAILED,
+                        "(6) Iterator of closed query result should throw NoSuchElementException
on next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            // check query result itself
+            try {
+                int size = queryResult.size();
+                fail(ASSERTION_FAILED, "(7) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+            try {
+                PCPoint elem = queryResult.get(0);
+                fail(ASSERTION_FAILED, "(8) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+            try {
+                boolean empty = queryResult.isEmpty();
+                fail(ASSERTION_FAILED, "(9) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+            // Check query instance is still usable
+            queryResult = (List<PCPoint>) query.execute();
+            if (queryResult.isEmpty()) {
+                fail(ASSERTION_FAILED,
+                        "(10) query instance should be usable and execution should return
a non empty result.");
+            }
+
+            tx.commit();
+        } finally {
+            if (tx != null && tx.isActive()) {
+                tx.rollback();
+            }
+        }
+    }
+
+    /**
+     * This methods creates a query instance with try-with-resources and 
+     * checks that an iterator for the query result is not accessible after the block,
+     * if the block is ended with an exception. 
+     */
+    public void testTryWithResourceThrowingException() {
+
+        PersistenceManager pm = getPM();
+        Transaction tx = pm.currentTransaction();
+
+        try {
+            tx.begin();
+
+            Query query = null;
+            List<PCPoint> queryResult = null;
+            Iterator<PCPoint> iterator = null;
+            try (Query query1 = pm.newQuery(PCPoint.class)) {
+                query = query1;
+                queryResult = (List<PCPoint>) query1.execute();
+                iterator = queryResult.iterator();
+                if (!iterator.hasNext()) {
+                    fail(ASSERTION_FAILED, "(1) Iterator of open query result should have
elements.");
+                }
+                throw new DummyException();
+            } catch (DummyException ex) {
+                // expected exception
+            } catch (Exception ex) {
+                fail(ASSERTION_FAILED, "(2) Unexpected exception " + ex);
+            }
+
+            // check iterator retrieved in try-with-resource block
+            if (iterator.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(3) Iterator of closed query result should return false on hasNext().");
+            }
+            try {
+                PCPoint next = iterator.next();
+                fail(ASSERTION_FAILED,
+                        "(4) Iterator of closed query result should throw NoSuchElementException
on next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            // create new Iterator and check its behaviour
+            Iterator<PCPoint> iterator2 = queryResult.iterator();
+            if (iterator2.hasNext()) {
+                fail(ASSERTION_FAILED,
+                        "(5) Iterator of closed query result should return false on hasNext().");
+            }
+            try {
+                PCPoint next = iterator2.next();
+                fail(ASSERTION_FAILED,
+                        "(6) Iterator of closed query result should throw NoSuchElementException
on next().");
+            } catch (NoSuchElementException ex) {
+                // expected exception
+            }
+
+            // check query result itself
+            try {
+                int size = queryResult.size();
+                fail(ASSERTION_FAILED, "(7) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+            try {
+                PCPoint elem = queryResult.get(0);
+                fail(ASSERTION_FAILED, "(8) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+            try {
+                boolean empty = queryResult.isEmpty();
+                fail(ASSERTION_FAILED, "(9) closed query result should not be accessible.");
+            } catch (JDOUserException ex) {
+                // expected exception when accessing closed query result
+            }
+
+            // Check query instance is still usable
+            queryResult = (List<PCPoint>) query.execute();
+            if (queryResult.isEmpty()) {
+                fail(ASSERTION_FAILED,
+                        "(10) query instance should be usable and execution should return
a non empty result.");
+            }
+
+            tx.commit();
+        } finally {
+            if (tx != null && tx.isActive()) {
+                tx.rollback();
+            }
+        }
+    }
+
+    /**
+     * @see JDO_Test#localSetUp()
+     */
+    protected void localSetUp() {
+        addTearDownClass(PCPoint.class);
+        loadAndPersistPCPoints(getPM());
+    }
+
+    /**
+     * DummyException used in method testTryWithResourceThrowingException.
+     */
+    private static final class DummyException extends Exception {}
+}



Mime
View raw message