Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.api.persistencemanager.cache » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17   
   18   
   19   package org.apache.jdo.tck.api.persistencemanager.cache;
   20   
   21   import javax.jdo.Transaction;
   22   
   23   import org.apache.jdo.tck.JDO_Test;
   24   import org.apache.jdo.tck.api.persistencemanager.PersistenceManagerTest;
   25   import org.apache.jdo.tck.pc.mylib.PCPoint2;
   26   import org.apache.jdo.tck.util.BatchTestRunner;
   27   
   28   /**
   29    *<B>Title:</B> Evicting With RestoreValues False
   30    *<BR>
   31    *<B>Keywords:</B> cache
   32    *<BR>
   33    *<B>Assertion ID:</B> A12.6.1-1.
   34    *<BR>
   35    *<B>Assertion Description: </B>
   36   If users wish to automatically <code>evict</code> transactional instances at transaction
   37   <code>rollback</code> time, then they should set <code>RestoreValues</code> to <code>false</code>.
   38   For each persistent-clean instance that the
   39   JDO <code>PersistenceManager</code> evicts, it:
   40   <UL>
   41   <LI>calls the <code>jdoPreClear</code> method on each instance,
   42   if the class of the instance implements <code>InstanceCallbacks</code></LI>
   43   <LI>clears persistent fields on each instance (sets the value of the field to its Java default value)</LI>
   44   <LI>changes the state of instances to hollow</LI>
   45   </UL>
   46   
   47    */
   48   
   49   public class EvictingWithRestoreValuesFalse extends PersistenceManagerTest {
   50   
   51       /** */
   52       private static final String ASSERTION_FAILED = 
   53           "Assertion A12.6.1-1 (EvictingWithRestoreValuesFalse) failed: ";
   54       
   55       /**
   56        * The <code>main</code> is called when the class
   57        * is directly executed from the command line.
   58        * @param args The arguments passed to the program.
   59        */
   60       public static void main(String[] args) {
   61           BatchTestRunner.run(EvictingWithRestoreValuesFalse.class);
   62       }	
   63   
   64       /**
   65        * @see JDO_Test#localSetUp()
   66        */
   67       protected void localSetUp() {
   68           addTearDownClass(PCPoint2.class);
   69           super.localSetUp();
   70       }
   71   
   72       /** */
   73       public void test() {
   74           pm = getPM();
   75   		PCPoint2 pt = getHollowInstance();
   76   
   77   		Transaction tx = pm.currentTransaction();
   78   		tx.setRestoreValues(false); //This should cause eviction of transactional instances when transaction is later rolled back.
   79   					
   80   		//Test
   81   		tx.begin();
   82   		makePersistentClean(pt);
   83   		tx.rollback(); // This should evict pt
   84   		verify(pt);
   85       }
   86   
   87   	/** */
   88       private PCPoint2 getHollowInstance() {
   89           Transaction tx = pm.currentTransaction();
   90           tx.begin();
   91           PCPoint2 pt = new PCPoint2(1,2);
   92           pm.makePersistent(pt);
   93           tx.commit();
   94   
   95   		int curr = currentState(pt);
   96   	    if (curr != HOLLOW){
   97   			fail(ASSERTION_FAILED,
   98   			     "Unable to create HOLLOW instance, state is " + states[curr]);
   99   	    }
  100   		return pt;
  101       }
  102   
  103       /** */
  104       private void makePersistentClean(PCPoint2 pt) {
  105   		pm.makeTransactional(pt);
  106           int curr = currentState(pt);
  107           if (curr != PERSISTENT_CLEAN){
  108           	fail(ASSERTION_FAILED,
  109           	     "Unable to create PERSISTENT_CLEAN instance, state is " + states[curr]);
  110           }
  111       }
  112   
  113   	/** */
  114       private void verify(PCPoint2 pt) {
  115   		// When PersistenceManager evicts instances, it: calls the jdoPreClear method on each instance
  116           if ( !pt.wasClearCalled()) {
  117               fail(ASSERTION_FAILED,
  118                    "missing call of jdoPreClear");
  119           }
  120   
  121   		// When PersistenceManager evicts instances, it: changes the state of the instances to hollow
  122   		int curr = currentState(pt);
  123           if ( (curr != HOLLOW) && (curr != PERSISTENT_NONTRANSACTIONAL) ) {
  124               fail(ASSERTION_FAILED,
  125                    "Instance should be HOLLOW after tx.rollback(), state is " + states[curr]);
  126           }
  127   		
  128           pm.makeTransient(pt);
  129           // When PersistenceManager evicts instances, it: clears persistent fields on each instance (sets the value of the field to its Java default value)
  130           int x=pt.getX();
  131           Integer y=pt.getY();
  132           if ((x != 0) || (y != null)) {
  133           	fail(ASSERTION_FAILED,
  134           	     "persistent fields not cleared, pt.getX()=="+x+" and pt.getY()=="+y);
  135           }
  136       }	
  137   }

Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.api.persistencemanager.cache » [javadoc | source]