KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > tck > JdoCallbacksTck


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.tck;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import javax.jdo.JDODataStoreException;
24 import javax.jdo.JDOUserException;
25 import javax.jdo.PersistenceManager;
26 import javax.jdo.PersistenceManagerFactory;
27 import javax.jdo.Transaction;
28
29 import org.objectweb.speedo.SpeedoTestHelper;
30 import org.objectweb.speedo.pobjects.tck.InstanceCallbackClass;
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 public class JdoCallbacksTck extends SpeedoTestHelper {
34
35     
36         
37     public JdoCallbacksTck(String JavaDoc s) {
38         super(s);
39     }
40
41     
42     void checkInstances(String JavaDoc label, int intValue, String JavaDoc capturedNextObjName, int numberOfChildren, int sumOfChildrenIntValue) {
43         if(InstanceCallbackClass.processedIndex[intValue] != true) {
44             logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
45             //incrementErrorCount();
46
return;
47         }
48         if(capturedNextObjName != null & InstanceCallbackClass.capturedNextObjName[intValue] == null) {
49             logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should not have been null.");
50             //incrementErrorCount();
51
} else if(capturedNextObjName == null & InstanceCallbackClass.capturedNextObjName[intValue] != null) {
52             logger.log(BasicLevel.ERROR, label + "nextObj attribute for object with intValue = " + intValue + " should have been null.");
53             //incrementErrorCount();
54
} else if(capturedNextObjName != null && !InstanceCallbackClass.capturedNextObjName[intValue].equals(capturedNextObjName)) {
55             logger.log(BasicLevel.ERROR, label + "nextObj.name attribute for object with intValue = " + intValue + " should have been \"" +
56                     capturedNextObjName + "\". It was \"" + InstanceCallbackClass.capturedNextObjName[intValue] + "\" instead.");
57             //incrementErrorCount();
58
}
59         
60         if(InstanceCallbackClass.numberOfChildren[intValue] != numberOfChildren) {
61             logger.log(BasicLevel.ERROR, label + "Number of instances in attribute children for object with intValue = " + intValue + " should have been " +
62                     numberOfChildren + ". It was " + InstanceCallbackClass.numberOfChildren[intValue] + " instead.");
63             //incrementErrorCount();
64
}
65         
66         if(InstanceCallbackClass.sumOfChildrenIntValue[intValue] != sumOfChildrenIntValue) {
67             logger.log(BasicLevel.ERROR, label + "Sum of intValue of instances in attribute children for object with intValue = " + intValue + " should have been " +
68                     sumOfChildrenIntValue + ". It was " + InstanceCallbackClass.sumOfChildrenIntValue[intValue] + " instead.");
69             //incrementErrorCount();
70
}
71     }
72     
73     void checkPMAccess(String JavaDoc label, int intValue, boolean transactionActive) {
74         if(InstanceCallbackClass.processedIndex[intValue] != true) {
75             logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
76             //incrementErrorCount();
77
return;
78         }
79         // Only verify isActive() returned true for the object if transactionActive is true
80
if(transactionActive & InstanceCallbackClass.transactionActive[intValue] != true) {
81             logger.log(BasicLevel.ERROR, label + "PersistenceManager.currentTransaction.isAcive() returned false");
82             //incrementErrorCount();
83
}
84     }
85     
86     // The attributes are: label, name, date, intValue, doubleValue, childToDelete, charValue
87
void checkFieldValues(String JavaDoc label, int intValue, String JavaDoc name, Date JavaDoc timeStamp, double doubleValue, short childToDelete, char charValue) {
88         if(InstanceCallbackClass.processedIndex[intValue] != true) {
89             logger.log(BasicLevel.ERROR, label + "Callback never made on object with intValue = " + intValue);
90             //incrementErrorCount();
91
return;
92         }
93         
94         if(!InstanceCallbackClass.capturedName[intValue].equals(name)) {
95             logger.log(BasicLevel.ERROR, label + "name attribute for object with intValue = " + intValue + " should be \"" + name + "\". It was \"" +
96                     InstanceCallbackClass.capturedName[intValue] + "\" instead.");
97             //incrementErrorCount();
98
}
99
100         if(!InstanceCallbackClass.capturedTimeStamp[intValue].equals(timeStamp)) {
101             logger.log(BasicLevel.ERROR, label + "timeStamp attribute for object with intValue = " + intValue + " should be " + timeStamp + ". It was " +
102                     InstanceCallbackClass.capturedTimeStamp[intValue] + " instead.");
103             //incrementErrorCount();
104
}
105
106         if(InstanceCallbackClass.capturedDoubleValue[intValue] != doubleValue) {
107             logger.log(BasicLevel.ERROR, label + "doubleValue attribute for object with intValue = " + intValue + " should be " + doubleValue + ". It was " +
108                     InstanceCallbackClass.capturedDoubleValue[intValue] + " instead.");
109             //incrementErrorCount();
110
}
111
112         if(InstanceCallbackClass.capturedCharValue[intValue] != charValue) {
113             logger.log(BasicLevel.ERROR, label + "charValue attribute for object with intValue = " + intValue + " should be " + charValue + ". It was " +
114                     InstanceCallbackClass.capturedCharValue[intValue] + " instead.");
115             //incrementErrorCount();
116
}
117
118         if(InstanceCallbackClass.capturedChildToDelete[intValue] != childToDelete) {
119             logger.log(BasicLevel.ERROR, label + "childToDelete attribute for object with intValue = " + intValue + " should be " + childToDelete + ". It was " +
120                     InstanceCallbackClass.capturedChildToDelete[intValue] + " instead.");
121             //incrementErrorCount();
122
}
123     }
124     
125     /** Touch fields to guarantee that they are loaded into the instance
126      */

127     double touchFields (InstanceCallbackClass o) {
128         // make a checksum from the fields and return it; this cannot be optimized out...
129
double rc = o.doubleValue;
130         rc += o.intValue;
131         rc += o.charValue;
132         rc += o.childToDelete;
133         rc += o.name.length();
134         rc += o.timeStamp.getTime();
135         return rc;
136     }
137     
138     protected String JavaDoc getLoggerName() {
139         return LOG_NAME + ".rt.tck.JdoCallbacksTck";
140     }
141
142     public void testCallingJdoPreclear() {
143         logger.log(BasicLevel.INFO, "testCallingJdoPreclear");
144         
145         PersistenceManagerFactory fact = getPMF();
146         PersistenceManager pm = fact.getPersistenceManager();
147         Transaction t = pm.currentTransaction();
148         t.setRetainValues(false); // instances transition to hollow after commit
149

150         InstanceCallbackClass.initializeStaticsForTest();
151         t.begin();
152         InstanceCallbackClass.removeAllInstances(pm); // always start fresh with no instances
153
t.commit();
154         
155         t.begin();
156         Calendar JavaDoc cal = Calendar.getInstance();
157         cal.set(1999, 1, 15, 12, 0);
158         Date JavaDoc createTime = cal.getTime();
159         cal.set(2002, 1, 15, 12, 0);
160         Date JavaDoc laterDate = cal.getTime();
161         InstanceCallbackClass secondaryObj = new InstanceCallbackClass("secondaryObj", createTime, 2, 2.2, (short)-20, '2', null);
162         InstanceCallbackClass primaryObj = new InstanceCallbackClass("primaryObj", laterDate, 1, 1.1, (short)-10, '1', secondaryObj);
163         pm.makePersistent(primaryObj);
164         pm.makePersistent(secondaryObj);
165         Object JavaDoc secondaryObjId = pm.getObjectId(secondaryObj);
166         Object JavaDoc primaryObjId = pm.getObjectId(primaryObj);
167         t.commit();
168
169         InstanceCallbackClass.performPreClearTests = true;
170         t.begin();
171         try {
172             primaryObj = (InstanceCallbackClass)pm.getObjectById(primaryObjId, true);
173             touchFields(primaryObj); // load fields of primaryObj (make it persistent-clean)
174

175         } catch (JDOUserException e) {
176             logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e);
177             fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction.");
178
179         } catch (JDODataStoreException e) {
180             logger.log(BasicLevel.ERROR, "Failed to find primaryObj created in previous transaction. Got JDOUserException " + e);
181             fail("CallingJdoPreclear: Failed to find primaryObj created in previous transaction.");
182         }
183         
184         secondaryObj = primaryObj.nextObj;
185         if(secondaryObj == null) {
186             logger.log(BasicLevel.ERROR, "Failed to find secondaryObj created in previous transaction using reference from primaryObj.");
187             fail("CallingJdoPreclear: Failed to find secondaryObj created in previous transaction.");
188         }
189         touchFields(secondaryObj);
190         
191         primaryObj.addChild(secondaryObj); // primaryObj contains one child; secondaryObj contains none. primaryObj is now dirty
192

193         cal.set(2005, 6, 28, 0, 0);
194         Date JavaDoc stillLaterDate = cal.getTime();
195         InstanceCallbackClass ternaryObj = new InstanceCallbackClass("ternaryObj", stillLaterDate, 3, 3.3, (short)-30, '3', null);
196         pm.makePersistent(ternaryObj);
197         ternaryObj.addChild(secondaryObj);
198         ternaryObj.addChild(primaryObj);
199         t.commit();
200         
201         // verify attributes in what was persistent-clean object--secondaryObj
202
checkFieldValues("jdoPreClear attribute access: ", 2, "secondaryObj", createTime, 2.2, (short)-20, '2');
203
204         // verify attributes in what was persistent-dirty object--primaryObj
205
checkFieldValues("jdoPreClear attribute access: ", 1, "primaryObj", laterDate, 1.1, (short)-10, '1');
206
207
208         // verify attributes in what was persistent-new object--ternaryObj
209
checkFieldValues("jdoPreClear attribute access: ", 3, "ternaryObj", stillLaterDate, 3.3, (short)-30, '3');
210         pm.close();
211         /*
212         if( getErrorCount() > 0 )
213             return Status.failed("Assertion A10.3-1 (CallingJdoPreclear) failed");
214         else
215             return Status.passed("Assertion A10.3-1 (CallingJdoPreclear) passed");
216         */

217     }
218     
219 }
220
221
Popular Tags