1 22 package org.jboss.test.recover.interfaces; 23 24 import javax.transaction.xa.Xid ; 25 26 import org.jboss.tm.recovery.BatchRecoveryLogger; 27 import org.jboss.tm.recovery.HeuristicStatus; 28 import org.jboss.tm.recovery.RecoveryTestingException; 29 import org.jboss.tm.recovery.TxCompletionHandler; 30 31 38 public class MockLogger extends BatchRecoveryLogger 39 { 40 private boolean crashOnFailure = false; 41 42 private boolean failAfterCommitting = false; 43 private boolean failAfterPreparing = false; 44 private int failAfter = 5; 45 private int failAfterCount = 0; 46 47 private boolean failBeforeCommitting = false; 48 private boolean failBeforePreparing = false; 49 private int failBefore = 5; 50 private int failBeforeCount = 0; 51 52 private boolean failAfterSavingHeuristicStatus = false; 53 private boolean failBeforeSavingHeuristicStatus = false; 54 private boolean failAfterClearingHeuristicStatus = false; 55 private boolean failBeforeClearingHeuristicStatus = false; 56 57 private static void crash() 58 { 59 Runtime.getRuntime().halt(1); 60 } 61 62 private void crashOrThrowRecoveryTestingException(String message) 63 { 64 if (isCrashOnFailure()) 65 crash(); 66 else 67 throw new RecoveryTestingException(message); 68 } 69 70 public MockLogger() 71 { 72 } 73 74 public TxCompletionHandler saveCommitDecision(long localTransactionId, 75 String [] resources) 76 { 77 if (isFailBeforeCommitting() && ++failBeforeCount > getFailBefore()) 78 crashOrThrowRecoveryTestingException( 79 "FAILED BEFORE SAVING COMMIT DECISION"); 80 try 81 { 82 return super.saveCommitDecision(localTransactionId, resources); 83 } 84 finally 85 { 86 if (isFailAfterCommitting() && ++failAfterCount > getFailAfter()) 87 crashOrThrowRecoveryTestingException( 88 "FAILED AFTER SAVING COMMIT DECISION"); 89 } 90 } 91 92 public TxCompletionHandler savePrepareDecision(long localTransactionId, 93 int inboundFormatId, 94 byte[] globalTransactionId, 95 String recoveryCoordinator, 96 String [] resources) 97 { 98 if (isFailBeforePreparing() && ++failBeforeCount > getFailBefore()) 99 crashOrThrowRecoveryTestingException( 100 "FAILED BEFORE SAVING PREPARE DECISION"); 101 try 102 { 103 return super.savePrepareDecision(localTransactionId, 104 inboundFormatId, 105 globalTransactionId, 106 recoveryCoordinator, 107 resources); 108 } 109 finally 110 { 111 if (isFailAfterPreparing() && ++failAfterCount > getFailAfter()) 112 crashOrThrowRecoveryTestingException( 113 "FAILED AFTER SAVING PREPARE DECISION"); 114 } 115 } 116 117 public TxCompletionHandler savePrepareDecision(long localTransactionId, 118 Xid inboundXid, 119 String [] resources) 120 { 121 if (isFailBeforePreparing() && ++failBeforeCount > getFailBefore()) 122 crashOrThrowRecoveryTestingException( 123 "FAILED BEFORE SAVING PREPARE DECISION"); 124 try 125 { 126 return super.savePrepareDecision(localTransactionId, 127 inboundXid, 128 resources); 129 } 130 finally 131 { 132 if (isFailAfterPreparing() && ++failAfterCount > getFailAfter()) 133 crashOrThrowRecoveryTestingException( 134 "FAILED AFTER SAVING PREPARE DECISION"); 135 } 136 } 137 138 public void saveHeuristicStatus(long localTransactionId, 139 boolean foreignTx, 140 int formatId, 141 byte[] globalTransactionId, 142 byte[] inboundBranchQualifier, 143 int transactionStatus, 144 int heurStatusCode, 145 boolean locallyDetectedHeuristicHazard, 146 int[] xaResourceHeuristics, 147 HeuristicStatus[] remoteResourceHeuristics) 148 { 149 if (isFailBeforeSavingHeuristicStatus()) 150 crashOrThrowRecoveryTestingException( 151 "FAILED BEFORE SAVING HEURISTIC STATUS"); 152 try 153 { 154 super.saveHeuristicStatus(localTransactionId, 155 foreignTx, 156 formatId, 157 globalTransactionId, 158 inboundBranchQualifier, 159 transactionStatus, 160 heurStatusCode, 161 locallyDetectedHeuristicHazard, 162 xaResourceHeuristics, 163 remoteResourceHeuristics); 164 } 165 finally 166 { 167 if (isFailAfterSavingHeuristicStatus()) 168 crashOrThrowRecoveryTestingException( 169 "FAILED AFTER SAVING HEURISTIC STATUS"); 170 } 171 } 172 173 public void clearHeuristicStatus(long localTransactionId) 174 { 175 if (isFailBeforeClearingHeuristicStatus()) 176 crashOrThrowRecoveryTestingException( 177 "FAILED BEFORE CLEARING HEURISTIC STATUS"); 178 try 179 { 180 super.clearHeuristicStatus(localTransactionId); 181 } 182 finally 183 { 184 if (isFailAfterClearingHeuristicStatus()) 185 crashOrThrowRecoveryTestingException( 186 "FAILED AFTER CLEARING HEURISTIC STATUS"); 187 } 188 } 189 190 public boolean isCrashOnFailure() 191 { 192 return crashOnFailure; 193 } 194 195 public void setCrashOnFailure(boolean crashOnFailure) 196 { 197 this.crashOnFailure = crashOnFailure; 198 } 199 200 public boolean isFailAfterCommitting() 201 { 202 return failAfterCommitting; 203 } 204 205 public void setFailAfterCommitting(boolean failAfterCommitting) 206 { 207 this.failAfterCommitting = failAfterCommitting; 208 } 209 210 public boolean isFailAfterPreparing() 211 { 212 return failAfterPreparing; 213 } 214 215 public void setFailAfterPreparing(boolean failAfterPreparing) 216 { 217 this.failAfterPreparing = failAfterPreparing; 218 } 219 220 public int getFailAfter() 221 { 222 return failAfter; 223 } 224 225 public void setFailAfter(int failAfter) 226 { 227 this.failAfter = failAfter; 228 } 229 230 public boolean isFailBeforeCommitting() 231 { 232 return failBeforeCommitting; 233 } 234 235 public void setFailBeforeCommitting(boolean failBeforeCommitting) 236 { 237 this.failBeforeCommitting = failBeforeCommitting; 238 } 239 240 public boolean isFailBeforePreparing() 241 { 242 return failBeforePreparing; 243 } 244 245 public void setFailBeforePreparing(boolean failBeforePreparing) 246 { 247 this.failBeforePreparing = failBeforePreparing; 248 } 249 250 public int getFailBefore() 251 { 252 return failBefore; 253 } 254 255 public void setFailBefore(int failBefore) 256 { 257 this.failBefore = failBefore; 258 } 259 260 public boolean isFailAfterSavingHeuristicStatus() 261 { 262 return failAfterSavingHeuristicStatus; 263 } 264 265 public void setFailAfterSavingHeuristicStatus( 266 boolean failAfterSavingHeuristicStatus) 267 { 268 this.failAfterSavingHeuristicStatus = failAfterSavingHeuristicStatus; 269 } 270 271 public boolean isFailBeforeSavingHeuristicStatus() 272 { 273 return failBeforeSavingHeuristicStatus; 274 } 275 276 public void setFailBeforeSavingHeuristicStatus( 277 boolean failBeforeSavingHeuristicStatus) 278 { 279 this.failBeforeSavingHeuristicStatus = failBeforeSavingHeuristicStatus; 280 } 281 282 public boolean isFailAfterClearingHeuristicStatus() 283 { 284 return failAfterClearingHeuristicStatus; 285 } 286 287 public void setFailAfterClearingHeuristicStatus( 288 boolean failAfterClearingHeuristicStatus) 289 { 290 this.failAfterClearingHeuristicStatus = failAfterClearingHeuristicStatus; 291 } 292 293 public boolean isFailBeforeClearingHeuristicStatus() 294 { 295 return failBeforeClearingHeuristicStatus; 296 } 297 298 public void setFailBeforeClearingHeuristicStatus( 299 boolean failBeforeClearingHeuristicStatus) 300 { 301 this.failBeforeClearingHeuristicStatus = failBeforeClearingHeuristicStatus; 302 } 303 304 } 305 | Popular Tags |