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 RetainValues False
   30    *<BR>
   31    *<B>Keywords:</B> cache
   32    *<BR>
   33    *<B>Assertion ID:</B> A12.5.1-3.
   34    *<BR>
   35    *<B>Assertion Description: </B>
   36   If users wish to automatically <code>evict</code> transactional instances at transaction
   37   <code>commit</code> time, then they should set <code>RetainValues</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 EvictingWithRetainValuesFalse extends PersistenceManagerTest {
   50   
   51       /** */
   52       private static final String ASSERTION_FAILED = 
   53           "Assertion A12.5.1-3 (EvictingWithRetainValuesFalse) 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(EvictingWithRetainValuesFalse.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   
   75   	    if (!isRetainValuesSupported()) {
   76       		printUnsupportedOptionalFeatureNotTested(
   77                       getClass().getName(), "javax.jdo.option.RetainValues");
   78   	    }
   79   		else
   80   		{
   81   	        pm = getPM();
   82   			PCPoint2 pt = getHollowInstance();
   83   
   84   			Transaction tx = pm.currentTransaction();
   85   			tx.setRetainValues(false); //This should cause eviction of transactional instances when transaction is later commited.
   86   						
   87   			//Test
   88   			tx.begin();
   89   			makePersistentClean(pt);
   90   			tx.commit(); // This should evict pt
   91   			verify(pt);
   92   		}
   93       }
   94   
   95   	/** */
   96       private PCPoint2 getHollowInstance() {
   97           Transaction tx = pm.currentTransaction();
   98           tx.begin();
   99           PCPoint2 pt = new PCPoint2(1,2);
  100           pm.makePersistent(pt);
  101           tx.commit();
  102   
  103   		int curr = currentState(pt);
  104   	    if (curr != HOLLOW){
  105   			fail(ASSERTION_FAILED,
  106   			     "Unable to create HOLLOW instance, state is " + states[curr]);
  107   	    }
  108   		return pt;
  109       }
  110   
  111       /** */
  112       private void makePersistentClean(PCPoint2 pt) {
  113   		pm.makeTransactional(pt);
  114           int curr = currentState(pt);
  115           if (curr != PERSISTENT_CLEAN){
  116           	fail(ASSERTION_FAILED,
  117           	     "Unable to create PERSISTENT_CLEAN instance, state is " + states[curr]);
  118           }
  119       }
  120   
  121   	/** */
  122       private void verify(PCPoint2 pt) {
  123   		// When PersistenceManager evicts instances, it: calls the jdoPreClear method on each instance
  124           if ( !pt.wasClearCalled()) {
  125               fail(ASSERTION_FAILED,
  126                    "missing call of jdoPreClear");
  127           }
  128   
  129   		// When PersistenceManager evicts instances, it: changes the state of the instances to hollow
  130   		int curr = currentState(pt);
  131           if ( (curr != HOLLOW) && (curr != PERSISTENT_NONTRANSACTIONAL) ) {
  132               fail(ASSERTION_FAILED,
  133                    "Instance should be HOLLOW after tx.commit(), state is " + states[curr]);
  134           }
  135   		
  136           pm.makeTransient(pt);
  137           // When PersistenceManager evicts instances, it: clears persistent fields on each instance (sets the value of the field to its Java default value)
  138           int x=pt.getX();
  139           Integer y=pt.getY();
  140           if ((x != 0) || (y != null)) {
  141           	fail(ASSERTION_FAILED,
  142           	     "persistent fields not cleared, pt.getX()=="+x+" and pt.getY()=="+y);
  143           }
  144       }	
  145   }

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