/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.runtime.tck;
import java.util.Calendar;
import java.util.Date;
import javax.jdo.JDODataStoreException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.pobjects.tck.InstanceCallbackClass;
import org.objectweb.util.monolog.api.BasicLevel;
public class JdoCallbacksTck extends SpeedoTestHelper {
public JdoCallbacksTck(String s) {
super(s);
}
void checkInstances(String label, int intValue, String capturedNextObjName, int numberOfChildren, int sumOfChildrenIntValue) {
if(InstanceCallbackClass.processedIndex[intValue] != true) {
logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
//incrementErrorCount();
return;
}
if(capturedNextObjName != null & InstanceCallbackClass.capturedNextObjName[intValue] == null) {
logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should not have been null.");
//incrementErrorCount();
} else if(capturedNextObjName == null & InstanceCallbackClass.capturedNextObjName[intValue] != null) {
logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should have been null.");
//incrementErrorCount();
} else if(capturedNextObjName != null && !InstanceCallbackClass.capturedNextObjName[intValue].equals(capturedNextObjName)) {
logger.log(BasicLevel.ERROR, label + "nextObj.name attribute for object with intValue = " + intValue + " should have been \"" +
capturedNextObjName + "\". It was \"" + InstanceCallbackClass.capturedNextObjName[intValue] + "\" instead.");
//incrementErrorCount();
}
if(InstanceCallbackClass.numberOfChildren[intValue] != numberOfChildren) {
logger.log(BasicLevel.ERROR, label + "Number of instances in attribute children for object with intValue = " + intValue + " should have been " +
numberOfChildren + ". It was " + InstanceCallbackClass.numberOfChildren[intValue] + " instead.");
//incrementErrorCount();
}
if(InstanceCallbackClass.sumOfChildrenIntValue[intValue] != sumOfChildrenIntValue) {
logger.log(BasicLevel.ERROR, label + "Sum of intValue of instances in attribute children for object with intValue = " + intValue + " should have been " +
sumOfChildrenIntValue + ". It was " + InstanceCallbackClass.sumOfChildrenIntValue[intValue] + " instead.");
//incrementErrorCount();
}
}
void checkPMAccess(String label, int intValue, boolean transactionActive) {
if(InstanceCallbackClass.processedIndex[intValue] != true) {
logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
//incrementErrorCount();
return;
}
// Only verify isActive() returned true for the object if transactionActive is true
if(transactionActive & InstanceCallbackClass.transactionActive[intValue] != true) {
logger.log(BasicLevel.ERROR, label + "PersistenceManager.currentTransaction.isAcive() returned false");
//incrementErrorCount();
}
}
// The attributes are: label, name, date, intValue, doubleValue, childToDelete, charValue
void checkFieldValues(String label, int intValue, String name, Date timeStamp, double doubleValue, short childToDelete, char charValue) {
if(InstanceCallbackClass.processedIndex[intValue] != true) {
logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
//incrementErrorCount();
return;
}
if(!InstanceCallbackClass.capturedName[intValue].equals(name)) {
logger.log(BasicLevel.ERROR, label + "name attribute for object with intValue = " + intValue + " should be \"" + name + "\". It was \"" +
InstanceCallbackClass.capturedName[intValue] + "\" instead.");
//incrementErrorCount();
}
if(!InstanceCallbackClass.capturedTimeStamp[intValue].equals(timeStamp)) {
logger.log(BasicLevel.ERROR, label + "timeStamp attribute for object with intValue = " + intValue + " should be " + timeStamp + ". It was " +
InstanceCallbackClass.capturedTimeStamp[intValue] + " instead.");
//incrementErrorCount();
}
if(InstanceCallbackClass.capturedDoubleValue[intValue] != doubleValue) {
logger.log(BasicLevel.ERROR, label + "doubleValue attribute for object with intValue = " + intValue + " should be " + doubleValue + ". It was " +
InstanceCallbackClass.capturedDoubleValue[intValue] + " instead.");
//incrementErrorCount();
}
if(InstanceCallbackClass.capturedCharValue[intValue] != charValue) {
logger.log(BasicLevel.ERROR, label + "charValue attribute for object with intValue = " + intValue + " should be " + charValue + ". It was " +
InstanceCallbackClass.capturedCharValue[intValue] + " instead.");
//incrementErrorCount();
}
if(InstanceCallbackClass.capturedChildToDelete[intValue] != childToDelete) {
logger.log(BasicLevel.ERROR, label + "childToDelete attribute for object with intValue = " + intValue + " should be " + childToDelete + ". It was " +
InstanceCallbackClass.capturedChildToDelete[intValue] + " instead.");
//incrementErrorCount();
}
}
/** Touch fields to guarantee that they are loaded into the instance
*/
double touchFields (InstanceCallbackClass o) {
// make a checksum from the fields and return it; this cannot be optimized out...
double rc = o.doubleValue;
rc += o.intValue;
rc += o.charValue;
rc += o.childToDelete;
rc += o.name.length();
rc += o.timeStamp.getTime();
return rc;
}
protected String getLoggerName() {
return LOG_NAME + ".rt.tck.JdoCallbacksTck";
}
public void testCallingJdoPreclear() {
logger.log(BasicLevel.INFO, "testCallingJdoPreclear");
PersistenceManagerFactory fact = getPMF();
PersistenceManager pm = fact.getPersistenceManager();
Transaction t = pm.currentTransaction();
t.setRetainValues(false); // instances transition to hollow after commit
InstanceCallbackClass.initializeStaticsForTest();
t.begin();
InstanceCallbackClass.removeAllInstances(pm); // always start fresh with no instances
t.commit();
t.begin();
Calendar cal = Calendar.getInstance();
cal.set(1999, 1, 15, 12, 0);
Date createTime = cal.getTime();
cal.set(2002, 1, 15, 12, 0);
Date laterDate = cal.getTime();
InstanceCallbackClass secondaryObj = new InstanceCallbackClass("secondaryObj", createTime, 2, 2.2, (short)-20, '2', null);
InstanceCallbackClass primaryObj = new InstanceCallbackClass("primaryObj", laterDate, 1, 1.1, (short)-10, '1', secondaryObj);
pm.makePersistent(primaryObj);
pm.makePersistent(secondaryObj);
Object secondaryObjId = pm.getObjectId(secondaryObj);
Object primaryObjId = pm.getObjectId(primaryObj);
t.commit();
InstanceCallbackClass.performPreClearTests = true;
t.begin();
try {
primaryObj = (InstanceCallbackClass)pm.getObjectById(primaryObjId, true);
touchFields(primaryObj); // load fields of primaryObj (make it persistent-clean)
} catch (JDOUserException e) {
logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e);
fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction.");
} catch (JDODataStoreException e) {
logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e);
fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction.");
}
secondaryObj = primaryObj.nextObj;
if(secondaryObj == null) {
logger.log(BasicLevel.ERROR, "Failed to find secondaryObj created in previous transaction using reference from primaryObj.");
fail("CallingJdoPreclear: Failed to find secondaryObj created in previous transaction.");
}
touchFields(secondaryObj);
primaryObj.addChild(secondaryObj); // primaryObj contains one child; secondaryObj contains none. primaryObj is now dirty
cal.set(2005, 6, 28, 0, 0);
Date stillLaterDate = cal.getTime();
InstanceCallbackClass ternaryObj = new InstanceCallbackClass("ternaryObj", stillLaterDate, 3, 3.3, (short)-30, '3', null);
pm.makePersistent(ternaryObj);
ternaryObj.addChild(secondaryObj);
ternaryObj.addChild(primaryObj);
t.commit();
// verify attributes in what was persistent-clean object--secondaryObj
checkFieldValues("jdoPreClear attribute access: ", 2, "secondaryObj", createTime, 2.2, (short)-20, '2');
// verify attributes in what was persistent-dirty object--primaryObj
checkFieldValues("jdoPreClear attribute access: ", 1, "primaryObj", laterDate, 1.1, (short)-10, '1');
// verify attributes in what was persistent-new object--ternaryObj
checkFieldValues("jdoPreClear attribute access: ", 3, "ternaryObj", stillLaterDate, 3.3, (short)-30, '3');
pm.close();
/*
if( getErrorCount() > 0 )
return Status.failed("Assertion A10.3-1 (CallingJdoPreclear) failed");
else
return Status.passed("Assertion A10.3-1 (CallingJdoPreclear) passed");
*/
}
}