Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.api.persistencemanagerfactory » [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   package org.apache.jdo.tck.api.persistencemanagerfactory;
   19   
   20   import java.util.Date;
   21   
   22   import java.util.Properties;
   23   import javax.jdo.Constants;
   24   import javax.jdo.JDOHelper;
   25   import javax.jdo.JDOReadOnlyException;
   26   import javax.jdo.PersistenceManagerFactory;
   27   import javax.jdo.Transaction;
   28   
   29   import org.apache.jdo.tck.JDO_Test;
   30   import org.apache.jdo.tck.pc.company.Address;
   31   import org.apache.jdo.tck.pc.company.Company;
   32   import org.apache.jdo.tck.util.BatchTestRunner;
   33   
   34   /**
   35    *<B>Title:</B>ReadOnly property of PersistenceManagerFactory  
   36    *<BR>
   37    *<B>Keywords:</B> persistencemanagerfactory
   38    *<BR>
   39    *<B>Assertion IDs:</B> A11.1-28 
   40    *<BR>
   41    *<B>Assertion Description: </B>
   42    * if an attempt is made to write (via flush) then JDOReadOnlyException 
   43    * is thrown.
   44    */
   45   
   46   
   47   public class FlushThrowsIfReadOnly extends JDO_Test {
   48       
   49       /** */
   50       private static final String ASSERTION_FAILED = 
   51           "Assertion A11.1-28 FlushThrowsIfReadOnly failed: ";
   52   
   53       /** Second PMF that is read-only */
   54       PersistenceManagerFactory pmf2;
   55   
   56       /** The company oid */
   57       Object oid;
   58       /**
   59        * The <code>main</code> is called when the class
   60        * is directly executed from the command line.
   61        * @param args The arguments passed to the program.
   62        */
   63       public static void main(String[] args) {
   64           BatchTestRunner.run(FlushThrowsIfReadOnly.class);
   65       }
   66   
   67       /**
   68        * @see JDO_Test#localSetUp()
   69        */
   70       protected void localSetUp() {
   71           addTearDownClass(Company.class);
   72           Properties properties = loadProperties(PMFProperties);
   73           properties.put(Constants.PROPERTY_READONLY, "true");
   74           pmf2 = JDOHelper.getPersistenceManagerFactory(properties);
   75           // insert an instance to find
   76           getPM();
   77           pm.currentTransaction().begin();
   78           Company comp = new Company(1L, "Sun Microsystems", new Date(), 
   79                                      new Address(0,"","","","",""));
   80           pm.makePersistent(comp);
   81           oid = pm.getObjectId(comp);
   82           pm.currentTransaction().commit();
   83           pm.close();
   84       }
   85   
   86       /** */
   87       public void testMakePersistent() {
   88           //Try to makePersistent and flush the transaction
   89           pm = pmf2.getPersistenceManager();
   90           Transaction tx = pm.currentTransaction();
   91           tx.begin();
   92           Company comp = new Company(2L, "Sun Microsystems2", new Date(), 
   93                                      new Address(0,"","","","",""));
   94           try {
   95               pm.makePersistent(comp);
   96               pm.flush();
   97               fail("When the PersistenceManagerFactory is read only, " +
   98                       "flush of a persistent-new instance must throw " +
   99                       "JDOReadOnlyException.");
  100           } catch (JDOReadOnlyException jDOReadOnlyException) {
  101               // good catch
  102           } catch (Throwable t) {
  103               fail("When the PersistenceManagerFactory is read only, " +
  104                       "flush of a persistent-deleted instance must throw " +
  105                       "JDOReadOnlyException. Flush threw " + t);
  106           }
  107           tx.rollback();
  108           pm.close();
  109           pm = null;
  110           closePMF(pmf2);
  111       }
  112   
  113       /** */
  114       public void testUpdate() {
  115           //Try to update and flush the transaction
  116           pm = pmf2.getPersistenceManager();
  117           pm.getExtent(Company.class); // make sure that oid class is loaded
  118           Transaction tx = pm.currentTransaction();
  119           tx.begin();
  120           Company comp = (Company)pm.getObjectById(oid);
  121           try {
  122               comp.setName("new name");
  123               pm.flush();
  124               fail("When the PersistenceManagerFactory is read only, " +
  125                       "flush of an updated instance must throw " +
  126                       "JDOReadOnlyException.");
  127           } catch (JDOReadOnlyException jDOReadOnlyException) {
  128               // good catch
  129           } catch (Throwable t) {
  130               fail("When the PersistenceManagerFactory is read only, " +
  131                       "flush of a persistent-deleted instance must throw " +
  132                       "JDOReadOnlyException. Flush threw " + t);
  133           }
  134           tx.rollback();
  135           pm.close();
  136           pm = null;
  137           closePMF(pmf2);
  138       }
  139   
  140       /** */
  141       public void testDeletePersistent() {
  142           //Try to deletePersistent and flush the transaction
  143           pm = pmf2.getPersistenceManager();
  144           pm.getExtent(Company.class); // make sure that oid class is loaded
  145           Transaction tx = pm.currentTransaction();
  146           tx.begin();
  147           Company comp = (Company)pm.getObjectById(oid);
  148           try {
  149               pm.deletePersistent(comp);
  150               pm.flush();
  151               fail("When the PersistenceManagerFactory is read only, " +
  152                       "flush of a persistent-deleted instance must throw " +
  153                       "JDOReadOnlyException.");
  154           } catch (JDOReadOnlyException jDOReadOnlyException) {
  155               // good catch
  156           } catch (Throwable t) {
  157               fail("When the PersistenceManagerFactory is read only, " +
  158                       "flush of a persistent-deleted instance must throw " +
  159                       "JDOReadOnlyException. Flush threw " + t);
  160           }
  161           tx.rollback();
  162           pm.close();
  163           pm = null;
  164           closePMF(pmf2);
  165       }
  166   }

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