|
Added
Link Here
|
| 1 |
/** |
| 2 |
* Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Eike Stepper - initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.cdo.tests; |
| 12 |
|
| 13 |
import org.eclipse.emf.cdo.CDOObject; |
| 14 |
import org.eclipse.emf.cdo.CDOState; |
| 15 |
import org.eclipse.emf.cdo.common.id.CDOID; |
| 16 |
import org.eclipse.emf.cdo.eresource.CDOResource; |
| 17 |
import org.eclipse.emf.cdo.net4j.CDOSession; |
| 18 |
import org.eclipse.emf.cdo.spi.server.InternalRepository; |
| 19 |
import org.eclipse.emf.cdo.tests.bundle.OM; |
| 20 |
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig; |
| 21 |
import org.eclipse.emf.cdo.tests.model1.Category; |
| 22 |
import org.eclipse.emf.cdo.tests.model1.Company; |
| 23 |
import org.eclipse.emf.cdo.tests.model1.Customer; |
| 24 |
import org.eclipse.emf.cdo.tests.model1.OrderDetail; |
| 25 |
import org.eclipse.emf.cdo.tests.model1.Product1; |
| 26 |
import org.eclipse.emf.cdo.tests.model1.PurchaseOrder; |
| 27 |
import org.eclipse.emf.cdo.tests.model1.SalesOrder; |
| 28 |
import org.eclipse.emf.cdo.tests.model1.Supplier; |
| 29 |
import org.eclipse.emf.cdo.transaction.CDOTransaction; |
| 30 |
import org.eclipse.emf.cdo.util.CommitException; |
| 31 |
import org.eclipse.emf.cdo.util.ObjectNotFoundException; |
| 32 |
import org.eclipse.emf.cdo.view.CDOView; |
| 33 |
|
| 34 |
import org.eclipse.emf.internal.cdo.CDOObjectImpl; |
| 35 |
import org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl; |
| 36 |
|
| 37 |
import org.eclipse.emf.common.util.EList; |
| 38 |
import org.eclipse.emf.common.util.TreeIterator; |
| 39 |
import org.eclipse.emf.ecore.EAttribute; |
| 40 |
import org.eclipse.emf.ecore.EObject; |
| 41 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 42 |
import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper; |
| 43 |
|
| 44 |
import java.util.ArrayList; |
| 45 |
import java.util.Calendar; |
| 46 |
import java.util.Collections; |
| 47 |
import java.util.HashSet; |
| 48 |
import java.util.Iterator; |
| 49 |
import java.util.List; |
| 50 |
import java.util.Set; |
| 51 |
|
| 52 |
/** |
| 53 |
* Revert testing subject: |
| 54 |
* <ul> |
| 55 |
* <li>simple attribute values |
| 56 |
* <li>deleted object revert when is reverting non deleted parent along with deleted child |
| 57 |
* <li>deleted object revert when deleted object with all recursive content is selected to revert, but parent is not for |
| 58 |
* revert |
| 59 |
* <li>moved object revert |
| 60 |
* <li>roll-back for revert changes. Roll-back of revert changes locally is tested for failure, because TRANSIENT and |
| 61 |
* DIRTY are not supported yet |
| 62 |
* |
| 63 |
* @author Egidijus Vaisnora |
| 64 |
*/ |
| 65 |
public class RevertTest extends AbstractCDOTest |
| 66 |
{ |
| 67 |
private static final String RESOURCE_NAME = "resource"; |
| 68 |
|
| 69 |
private static final String TESTING_COMPANY_NAME = "CompanyTesting"; |
| 70 |
|
| 71 |
private static final String TESTING_COMPANY2_NAME = "CompanyTestingAnother"; |
| 72 |
|
| 73 |
private static final int TESTING_SALESORDER_ID = -1; |
| 74 |
|
| 75 |
private long timeStampOfCommit; |
| 76 |
|
| 77 |
@Override |
| 78 |
protected void doSetUp() throws Exception |
| 79 |
{ |
| 80 |
super.doSetUp(); |
| 81 |
|
| 82 |
// create model history |
| 83 |
CDOSession session = (CDOSession)openSession(); |
| 84 |
CDOTransaction openTransaction = session.openTransaction(); |
| 85 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 86 |
|
| 87 |
// creating initial commit |
| 88 |
Company createdCompany = getModel1Factory().createCompany(); |
| 89 |
createdCompany.setName(TESTING_COMPANY_NAME); |
| 90 |
createdCompany.setCity("City"); |
| 91 |
createdCompany.setStreet("Street"); |
| 92 |
|
| 93 |
Supplier createSupplier = getModel1Factory().createSupplier(); |
| 94 |
createSupplier.setName("CompanySupplier"); |
| 95 |
createSupplier.setCity("CitySupplier"); |
| 96 |
createSupplier.setStreet("StreetSupplier"); |
| 97 |
createdCompany.getSuppliers().add(createSupplier); |
| 98 |
|
| 99 |
Category category = getModel1Factory().createCategory(); |
| 100 |
category.setName("Category"); |
| 101 |
createdCompany.getCategories().add(category); |
| 102 |
resource.getContents().add(createdCompany); |
| 103 |
|
| 104 |
Category category2 = getModel1Factory().createCategory(); |
| 105 |
category2.setName("Category2"); |
| 106 |
createdCompany.getCategories().add(category2); |
| 107 |
resource.getContents().add(createdCompany); |
| 108 |
|
| 109 |
Customer customer = getModel1Factory().createCustomer(); |
| 110 |
customer.setCity("city"); |
| 111 |
customer.setName("name"); |
| 112 |
customer.setStreet("street"); |
| 113 |
createdCompany.getCustomers().add(customer); |
| 114 |
|
| 115 |
// product 1 |
| 116 |
Product1 createProduct1 = getModel1Factory().createProduct1(); |
| 117 |
createProduct1.setDescription("description"); |
| 118 |
createProduct1.setName("product name"); |
| 119 |
category.getProducts().add(createProduct1); |
| 120 |
|
| 121 |
// second product |
| 122 |
Product1 createProduct2 = getModel1Factory().createProduct1(); |
| 123 |
createProduct2.setDescription("description"); |
| 124 |
createProduct2.setName("product name"); |
| 125 |
category.getProducts().add(createProduct2); |
| 126 |
|
| 127 |
// customer |
| 128 |
Customer createCustomer = getModel1Factory().createCustomer(); |
| 129 |
createCustomer.setName("customer name"); |
| 130 |
createdCompany.getCustomers().add(createCustomer); |
| 131 |
|
| 132 |
// purchase order |
| 133 |
OrderDetail createOrderDetail = getModel1Factory().createOrderDetail(); |
| 134 |
createOrderDetail.setPrice(12f); |
| 135 |
createOrderDetail.setProduct(createProduct1); |
| 136 |
|
| 137 |
PurchaseOrder createPurchaseOrder = getModel1Factory().createPurchaseOrder(); |
| 138 |
createPurchaseOrder.setDate(Calendar.getInstance().getTime()); |
| 139 |
createPurchaseOrder.getOrderDetails().add(createOrderDetail); |
| 140 |
createdCompany.getPurchaseOrders().add(createPurchaseOrder); |
| 141 |
|
| 142 |
// sale order |
| 143 |
OrderDetail createOrderDetail2 = getModel1Factory().createOrderDetail(); |
| 144 |
createOrderDetail.setPrice(13f); |
| 145 |
createOrderDetail.setProduct(createProduct2); |
| 146 |
|
| 147 |
SalesOrder createSaleOrder = getModel1Factory().createSalesOrder(); |
| 148 |
createSaleOrder.setId(TESTING_SALESORDER_ID); |
| 149 |
createSaleOrder.setCustomer(createCustomer); |
| 150 |
createSaleOrder.getOrderDetails().add(createOrderDetail2); |
| 151 |
resource.getContents().add(createSaleOrder); |
| 152 |
|
| 153 |
Company createdCompany2 = getModel1Factory().createCompany(); |
| 154 |
createdCompany2.setName(TESTING_COMPANY2_NAME); |
| 155 |
createdCompany2.setCity("City2"); |
| 156 |
createdCompany2.setStreet("Street2"); |
| 157 |
resource.getContents().add(createdCompany2); |
| 158 |
|
| 159 |
timeStampOfCommit = openTransaction.commit().getTimeStamp(); |
| 160 |
session.close(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Testing simple attribute value revert. Collecting values from given resource, changing EAttribute values and |
| 165 |
* committing. Reverting to the previous version and comparing models |
| 166 |
*/ |
| 167 |
public void testEAttributeValueRevert() throws CommitException |
| 168 |
{ |
| 169 |
CDOSession session = (CDOSession)openSession(); |
| 170 |
CDOTransaction openTransaction = session.openTransaction(); |
| 171 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 172 |
|
| 173 |
// simple property modification |
| 174 |
for (TreeIterator<EObject> it = resource.getAllContents(); it.hasNext();) |
| 175 |
{ |
| 176 |
EObject next = it.next(); |
| 177 |
EList<EAttribute> eAllStructuralFeatures = next.eClass().getEAllAttributes(); |
| 178 |
for (int i = 0; i < eAllStructuralFeatures.size(); i++) |
| 179 |
{ |
| 180 |
EAttribute eAttribute = eAllStructuralFeatures.get(i); |
| 181 |
changeAttribute(next, eAttribute); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
openTransaction.commit(); |
| 186 |
session.close(); |
| 187 |
|
| 188 |
// revert session |
| 189 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 190 |
|
| 191 |
// asserting |
| 192 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Test that object doesn't obtain new CDO version after revert, if it was not eligible for revert |
| 197 |
*/ |
| 198 |
public void testVoidReverse() throws CommitException |
| 199 |
{ |
| 200 |
CDOSession session = (CDOSession)openSession(); |
| 201 |
CDOTransaction openTransaction = session.openTransaction(); |
| 202 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 203 |
|
| 204 |
// revert session |
| 205 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 206 |
|
| 207 |
// simple property modification |
| 208 |
for (TreeIterator<EObject> it = resource.getAllContents(); it.hasNext();) |
| 209 |
{ |
| 210 |
EObject next = it.next(); |
| 211 |
assertEquals(CDOState.CLEAN, ((CDOObject)next).cdoState()); |
| 212 |
} |
| 213 |
|
| 214 |
session.close(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Removes objects and reverts removed object from historical revision. Second attempt is to remove all root object |
| 219 |
* contents along with object |
| 220 |
*/ |
| 221 |
public void testRemovedObjectsRevertInResource() throws CommitException |
| 222 |
{ |
| 223 |
// modifying model |
| 224 |
CDOSession session = (CDOSession)openSession(); |
| 225 |
CDOTransaction openTransaction = session.openTransaction(); |
| 226 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 227 |
|
| 228 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 229 |
|
| 230 |
EList<Category> categories = found.getCategories(); |
| 231 |
List<Product1> products = new ArrayList<Product1>(); |
| 232 |
for (int i = 0; i < categories.size(); i++) |
| 233 |
{ |
| 234 |
products.addAll(categories.get(i).getProducts()); |
| 235 |
} |
| 236 |
|
| 237 |
for (int i = 0; i < products.size(); i++) |
| 238 |
{ |
| 239 |
EcoreUtil.delete(products.get(i), true); |
| 240 |
} |
| 241 |
|
| 242 |
openTransaction.commit(); |
| 243 |
session.close(); |
| 244 |
|
| 245 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 246 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 247 |
|
| 248 |
// remove all contents |
| 249 |
session = (CDOSession)openSession(); |
| 250 |
openTransaction = session.openTransaction(); |
| 251 |
CDOResource object = openTransaction.getResource(RESOURCE_NAME); |
| 252 |
List<EObject> eContents = new ArrayList<EObject>(object.eContents()); |
| 253 |
for (Iterator<EObject> it = eContents.iterator(); it.hasNext();) |
| 254 |
{ |
| 255 |
EObject next = it.next(); |
| 256 |
EcoreUtil.delete(next, true); |
| 257 |
} |
| 258 |
|
| 259 |
openTransaction.commit(); |
| 260 |
session.close(); |
| 261 |
|
| 262 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 263 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* When historical object contains less references then reverting one, then extra references are removed from the |
| 268 |
* containment. After revert, removed such object doesn't have parent and must be "garbage collected" |
| 269 |
*/ |
| 270 |
public void testAdditinalObjectDeletingAfterRevert() throws CommitException |
| 271 |
{ |
| 272 |
// modifying model |
| 273 |
CDOSession session = (CDOSession)openSession(); |
| 274 |
CDOTransaction openTransaction = session.openTransaction(); |
| 275 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 276 |
|
| 277 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 278 |
|
| 279 |
Category createCategory = getModel1Factory().createCategory(); |
| 280 |
found.getCategories().add(createCategory); |
| 281 |
|
| 282 |
openTransaction.commit(); |
| 283 |
CDOID deletedObject = ((CDOObjectImpl)createCategory).cdoID(); |
| 284 |
session.close(); |
| 285 |
|
| 286 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 287 |
assertResources(RESOURCE_NAME, timeStampOfCommit, Collections.singleton(deletedObject)); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Reverting object move. Consist of single object move. Second part makes move of child object of moved object |
| 292 |
*/ |
| 293 |
public void testObjectAndItsChildMoveRevert() throws CommitException |
| 294 |
{ |
| 295 |
// TEST1 move of child |
| 296 |
CDOSession session = (CDOSession)openSession(); |
| 297 |
CDOTransaction openTransaction = session.openTransaction(); |
| 298 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 299 |
|
| 300 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 301 |
|
| 302 |
EList<Category> categories = found.getCategories(); |
| 303 |
|
| 304 |
Category category = categories.get(0); |
| 305 |
Category category2 = categories.get(1); |
| 306 |
|
| 307 |
category2.getProducts().add(category.getProducts().remove(0)); |
| 308 |
|
| 309 |
openTransaction.commit(); |
| 310 |
session.close(); |
| 311 |
|
| 312 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 313 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 314 |
|
| 315 |
// TEST2 move of moved child |
| 316 |
session = (CDOSession)openSession(); |
| 317 |
openTransaction = session.openTransaction(); |
| 318 |
resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 319 |
|
| 320 |
Company found1 = null; |
| 321 |
Company found2 = null; |
| 322 |
EList<EObject> contents = resource.getContents(); |
| 323 |
for (int i = 0; i < contents.size() && (found1 == null || found2 == null); i++) |
| 324 |
{ |
| 325 |
EObject eObject = contents.get(i); |
| 326 |
if (eObject instanceof Company) |
| 327 |
{ |
| 328 |
if (found1 == null) |
| 329 |
{ |
| 330 |
found1 = (Company)eObject; |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
found2 = (Company)eObject; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
EList<Category> categoriesFound1 = found1.getCategories(); |
| 340 |
Category category1Found1 = categoriesFound1.get(0); |
| 341 |
Category category2Found1 = categoriesFound1.get(1); |
| 342 |
|
| 343 |
// move of category |
| 344 |
found2.getCategories().add(category1Found1); |
| 345 |
|
| 346 |
// move of product |
| 347 |
category2Found1.getProducts().add(category1Found1.getProducts().get(0)); |
| 348 |
|
| 349 |
openTransaction.commit(); |
| 350 |
session.close(); |
| 351 |
|
| 352 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 353 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Moving object child to another object, while old parent was removed |
| 358 |
*/ |
| 359 |
public void testObjectMoveWithRemoveRevert() throws CommitException |
| 360 |
{ |
| 361 |
CDOSession session = (CDOSession)openSession(); |
| 362 |
CDOTransaction openTransaction = session.openTransaction(); |
| 363 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 364 |
|
| 365 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 366 |
Company found2 = findTestingCompany(resource, TESTING_COMPANY2_NAME); |
| 367 |
found2.getPurchaseOrders().add(found.getPurchaseOrders().remove(0)); |
| 368 |
|
| 369 |
// delete parent of purchase order |
| 370 |
EcoreUtil.delete(found, true); |
| 371 |
|
| 372 |
openTransaction.commit(); |
| 373 |
session.close(); |
| 374 |
|
| 375 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 376 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Removes objects and reverts removed object recursively. Parent of removed object is not reverted and after revert |
| 381 |
* must ensure to attache deleted object to parent |
| 382 |
*/ |
| 383 |
public void testRemovedObjectRecursiveRevert() throws CommitException |
| 384 |
{ |
| 385 |
// modifying model |
| 386 |
CDOSession session = (CDOSession)openSession(); |
| 387 |
CDOTransaction openTransaction = session.openTransaction(); |
| 388 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 389 |
|
| 390 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 391 |
CDOID revertId = ((CDOObject)found).cdoID(); |
| 392 |
SalesOrder findTestingSalesOrder = findTestingSalesOrder(resource); |
| 393 |
CDOID salesOrderId = ((CDOObject)findTestingSalesOrder).cdoID(); |
| 394 |
|
| 395 |
EcoreUtil.delete(found, true); |
| 396 |
|
| 397 |
openTransaction.commit(); |
| 398 |
session.close(); |
| 399 |
|
| 400 |
/* |
| 401 |
* When deleting company, we delete customer along with modified salesOrder, which had reference pointing to |
| 402 |
* customer. Revert restores deleted objects - reverts customer reference to SalesOrder, but doesn't restore |
| 403 |
* SalesOrder reference to Customer. Therefore passing SalesOrder to revert too |
| 404 |
*/ |
| 405 |
revertObject(revertId, timeStampOfCommit, true, salesOrderId); |
| 406 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Testing general revert roll-back. After revert, roll-back must clear changes made by revert. |
| 411 |
*/ |
| 412 |
public void testRevertRollback() throws CommitException |
| 413 |
{ |
| 414 |
CDOSession session = (CDOSession)openSession(); |
| 415 |
CDOTransaction openTransaction = session.openTransaction(); |
| 416 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 417 |
|
| 418 |
editingModelForRollback(resource); |
| 419 |
|
| 420 |
long timeStampCommit = openTransaction.commit().getTimeStamp(); |
| 421 |
session.close(); |
| 422 |
|
| 423 |
session = (CDOSession)openSession(); |
| 424 |
CDOView openAudit = session.openView(timeStampOfCommit); |
| 425 |
CDOResource historicalResource = openAudit.getResource(RESOURCE_NAME); |
| 426 |
List<CDOObject> tmpObjects = collectAllRevertDataFromResource(historicalResource); |
| 427 |
|
| 428 |
CDOTransactionImpl openTransaction2 = (CDOTransactionImpl)session.openTransaction(); |
| 429 |
openTransaction2.revert(tmpObjects.toArray(new CDOObject[tmpObjects.size()])); |
| 430 |
|
| 431 |
openTransaction2.rollback(); |
| 432 |
openTransaction2.commit(); |
| 433 |
session.close(); |
| 434 |
|
| 435 |
assertResources(RESOURCE_NAME, timeStampCommit, null); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Testing case when revert need to restore state, where it doesn't contain objects. Revert to deletion |
| 440 |
*/ |
| 441 |
public void testHoleRevert() throws CommitException |
| 442 |
{ |
| 443 |
CDOSession session = (CDOSession)openSession(); |
| 444 |
CDOTransaction openTransaction = session.openTransaction(); |
| 445 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 446 |
|
| 447 |
Set<CDOID> ids = new HashSet<CDOID>(); |
| 448 |
for (Iterator<EObject> it = resource.getAllContents(); it.hasNext();) |
| 449 |
{ |
| 450 |
ids.add(((CDOObject)it.next()).cdoID()); |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
List<EObject> contents = new ArrayList<EObject>(resource.getContents()); |
| 455 |
for (int i = 0; i < contents.size(); i++) |
| 456 |
{ |
| 457 |
EcoreUtil.delete(contents.get(i)); |
| 458 |
} |
| 459 |
long holesTimestamp = openTransaction.commit().getTimeStamp(); |
| 460 |
session.close(); |
| 461 |
|
| 462 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 463 |
|
| 464 |
revertResource(RESOURCE_NAME, holesTimestamp); |
| 465 |
|
| 466 |
assertResources(RESOURCE_NAME, holesTimestamp, ids); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Testing case when revert need to restore state of deleted object, which was deleted somewhere between current and |
| 471 |
* revert time-stamps. Covers version calculation, when version is restoring for detached element |
| 472 |
*/ |
| 473 |
public void testVersionNumberCalculationOnRevert() throws CommitException |
| 474 |
{ |
| 475 |
CDOSession session = (CDOSession)openSession(); |
| 476 |
CDOTransaction openTransaction = session.openTransaction(); |
| 477 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 478 |
|
| 479 |
List<EObject> contents = new ArrayList<EObject>(resource.getContents()); |
| 480 |
for (int i = 0; i < contents.size(); i++) |
| 481 |
{ |
| 482 |
EcoreUtil.delete(contents.get(i)); |
| 483 |
} |
| 484 |
long holesTimestamp = openTransaction.commit().getTimeStamp(); |
| 485 |
session.close(); |
| 486 |
|
| 487 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 488 |
revertResource(RESOURCE_NAME, holesTimestamp); |
| 489 |
|
| 490 |
InternalRepository repository = getScenario().getRepositoryConfig() |
| 491 |
.getRepository(IRepositoryConfig.REPOSITORY_NAME); |
| 492 |
clearCache(repository.getRevisionManager()); |
| 493 |
|
| 494 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 495 |
assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Complex situation - moved content from deleted object to newly created. Revert must restore deleted object, detach |
| 501 |
* object which was newly created ("garbage collected"), while containment of deleted object is moved back to restored |
| 502 |
* object |
| 503 |
*/ |
| 504 |
public void testObjectCreationMoveDeleteRevert() throws CommitException |
| 505 |
{ |
| 506 |
CDOSession session = (CDOSession)openSession(); |
| 507 |
CDOTransaction openTransaction = session.openTransaction(); |
| 508 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 509 |
// apply editing |
| 510 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 511 |
|
| 512 |
Category createCategoryNew = getModel1Factory().createCategory(); |
| 513 |
createCategoryNew.getProducts().addAll(found.getCategories().get(0).getProducts()); |
| 514 |
|
| 515 |
Company found2 = findTestingCompany(resource, TESTING_COMPANY2_NAME); |
| 516 |
found2.getCategories().add(createCategoryNew); |
| 517 |
|
| 518 |
EcoreUtil.delete(found, true); |
| 519 |
found2.setName("ChangedCompanyName"); |
| 520 |
|
| 521 |
openTransaction.commit(); |
| 522 |
// removed id on revert |
| 523 |
CDOID removedId = ((CDOObject)createCategoryNew).cdoID(); |
| 524 |
session.close(); |
| 525 |
|
| 526 |
// getting back to the beginning |
| 527 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 528 |
assertResources(RESOURCE_NAME, timeStampOfCommit, Collections.singleton(removedId)); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* If object was reverted after it was edited, changes must be applied from the revert. Currently this test is testing |
| 533 |
* for failure, because TRANSIENT and DIRTY are not supported yet |
| 534 |
*/ |
| 535 |
public void testEditedObjectRevertFailure() throws CommitException |
| 536 |
{ |
| 537 |
CDOSession session = (CDOSession)openSession(); |
| 538 |
CDOTransaction openTransaction = session.openTransaction(); |
| 539 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 540 |
|
| 541 |
editingModelForRollback(resource); |
| 542 |
|
| 543 |
CDOView openAudit = session.openView(timeStampOfCommit); |
| 544 |
CDOResource historicalResource = openAudit.getResource(RESOURCE_NAME); |
| 545 |
List<CDOObject> tmpObjects = collectAllRevertDataFromResource(historicalResource); |
| 546 |
|
| 547 |
try |
| 548 |
{ |
| 549 |
((CDOTransactionImpl)openTransaction).revert(tmpObjects.toArray(new CDOObject[tmpObjects.size()])); |
| 550 |
fail(); |
| 551 |
} |
| 552 |
catch (IllegalStateException ex) |
| 553 |
{ |
| 554 |
// NOT supported yet |
| 555 |
OM.LOG.info("Not supported revert for objects with state: TRANSIENT, DIRTY", ex); |
| 556 |
} |
| 557 |
|
| 558 |
// openTransaction.commit(); |
| 559 |
session.close(); |
| 560 |
|
| 561 |
// assertResources(RESOURCE_NAME, timeStampOfCommit, null); |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Testing object revert till specified saving point. Expected behavior is to roll-back only changes made by revert, |
| 566 |
* but leave changes applied before revert. At current time it is testing for failure, because TRANSIENT and DIRTY are |
| 567 |
* not supported yet |
| 568 |
*/ |
| 569 |
public void testRevertRollbackToSavePointFailure() throws CommitException |
| 570 |
{ |
| 571 |
CDOSession session = (CDOSession)openSession(); |
| 572 |
CDOTransaction openTransaction = session.openTransaction(); |
| 573 |
CDOResource resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 574 |
|
| 575 |
// apply editing |
| 576 |
editingModelForRollback(resource); |
| 577 |
openTransaction.commit(); |
| 578 |
|
| 579 |
// long timeStampOfComparingModel = openTransaction.getLastCommitTime(); |
| 580 |
session.close(); |
| 581 |
|
| 582 |
// getting back to the beginning |
| 583 |
revertResource(RESOURCE_NAME, timeStampOfCommit); |
| 584 |
|
| 585 |
// applying editing again |
| 586 |
session = (CDOSession)openSession(); |
| 587 |
openTransaction = session.openTransaction(); |
| 588 |
resource = openTransaction.getOrCreateResource(RESOURCE_NAME); |
| 589 |
editingModelForRollback(resource); |
| 590 |
|
| 591 |
// creating save point |
| 592 |
// CDOSavepoint setSavepoint = openTransaction.setSavepoint(); |
| 593 |
|
| 594 |
// reverting, but not committing |
| 595 |
CDOView openAudit = session.openView(timeStampOfCommit); |
| 596 |
CDOResource historicalResource = openAudit.getResource(RESOURCE_NAME); |
| 597 |
List<CDOObject> tmpObjects = collectAllRevertDataFromResource(historicalResource); |
| 598 |
try |
| 599 |
{ |
| 600 |
((CDOTransactionImpl)openTransaction).revert(tmpObjects.toArray(new CDOObject[tmpObjects.size()])); |
| 601 |
fail(); |
| 602 |
} |
| 603 |
catch (IllegalStateException ex) |
| 604 |
{ |
| 605 |
// NOT supported yet |
| 606 |
OM.LOG.info("Not supported revert for objects with state: TRANSIENT, DIRTY", ex); |
| 607 |
} |
| 608 |
|
| 609 |
// roll-backing to save point |
| 610 |
// openTransaction.rollback(setSavepoint); |
| 611 |
|
| 612 |
// it must be equal |
| 613 |
// assertResources(RESOURCE_NAME, timeStampOfComparingModel, null); |
| 614 |
} |
| 615 |
|
| 616 |
private void editingModelForRollback(CDOResource resource) throws CommitException |
| 617 |
{ |
| 618 |
Company found = findTestingCompany(resource, TESTING_COMPANY_NAME); |
| 619 |
|
| 620 |
Category createCategoryNew = getModel1Factory().createCategory(); |
| 621 |
createCategoryNew.getProducts().addAll(found.getCategories().get(0).getProducts()); |
| 622 |
|
| 623 |
Company found2 = findTestingCompany(resource, TESTING_COMPANY2_NAME); |
| 624 |
found2.getCategories().add(createCategoryNew); |
| 625 |
|
| 626 |
EcoreUtil.delete(found, true); |
| 627 |
found2.setName("ChangedCompanyName"); |
| 628 |
} |
| 629 |
|
| 630 |
private Company findTestingCompany(CDOResource resource, String name) |
| 631 |
{ |
| 632 |
Company found = null; |
| 633 |
EList<EObject> contents = resource.getContents(); |
| 634 |
for (int i = 0; i < contents.size() && found == null; i++) |
| 635 |
{ |
| 636 |
EObject eObject = contents.get(i); |
| 637 |
if (eObject instanceof Company && name.equals(((Company)eObject).getName())) |
| 638 |
{ |
| 639 |
found = (Company)eObject; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
return found; |
| 644 |
} |
| 645 |
|
| 646 |
private SalesOrder findTestingSalesOrder(CDOResource resource) |
| 647 |
{ |
| 648 |
SalesOrder found = null; |
| 649 |
EList<EObject> contents = resource.getContents(); |
| 650 |
for (int i = 0; i < contents.size() && found == null; i++) |
| 651 |
{ |
| 652 |
EObject eObject = contents.get(i); |
| 653 |
if (eObject instanceof SalesOrder && ((SalesOrder)eObject).getId() == TESTING_SALESORDER_ID) |
| 654 |
{ |
| 655 |
found = (SalesOrder)eObject; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
return found; |
| 660 |
} |
| 661 |
|
| 662 |
private long revertResource(String resourceName, long timestamp) throws CommitException |
| 663 |
{ |
| 664 |
CDOSession session = (CDOSession)openSession(); |
| 665 |
CDOView openAudit = session.openView(timestamp); |
| 666 |
CDOResource historicalResource = openAudit.getResource(resourceName); |
| 667 |
List<CDOObject> tmpObjects = collectAllRevertDataFromResource(historicalResource); |
| 668 |
|
| 669 |
CDOTransaction openTransaction2 = session.openTransaction(); |
| 670 |
((CDOTransactionImpl)openTransaction2).revert(tmpObjects.toArray(new CDOObject[tmpObjects.size()])); |
| 671 |
long ret = 0; |
| 672 |
if (openTransaction2.isDirty()) |
| 673 |
{ |
| 674 |
ret = openTransaction2.commit().getTimeStamp(); |
| 675 |
} |
| 676 |
|
| 677 |
session.close(); |
| 678 |
return ret; |
| 679 |
} |
| 680 |
|
| 681 |
private List<CDOObject> collectAllRevertDataFromResource(CDOResource historicalResource) |
| 682 |
{ |
| 683 |
List<CDOObject> tmpObjects = new ArrayList<CDOObject>(); |
| 684 |
tmpObjects.add(historicalResource); |
| 685 |
for (TreeIterator<EObject> allContents = historicalResource.getAllContents(); allContents.hasNext();) |
| 686 |
{ |
| 687 |
CDOObject next = (CDOObject)allContents.next(); |
| 688 |
tmpObjects.add(next); |
| 689 |
} |
| 690 |
|
| 691 |
return tmpObjects; |
| 692 |
} |
| 693 |
|
| 694 |
private void assertResources(String resourceName, long timestamp, Set<CDOID> detached) |
| 695 |
{ |
| 696 |
CDOSession session; |
| 697 |
session = (CDOSession)openSession(); |
| 698 |
CDOView currentView = session.openView(); |
| 699 |
CDOResource currentResource = currentView.getResource(resourceName); |
| 700 |
CDOView historyView = session.openView(timestamp); |
| 701 |
CDOResource historyResource = historyView.getResource(resourceName); |
| 702 |
|
| 703 |
// assertTrue(EcoreUtil.equals(currentResource, historyResource)); |
| 704 |
assertTrue(new RevertEqualityHelper().equals(currentResource.getContents(), historyResource.getContents())); |
| 705 |
|
| 706 |
if (detached != null) |
| 707 |
{ |
| 708 |
for (Iterator<CDOID> iterator = detached.iterator(); iterator.hasNext();) |
| 709 |
{ |
| 710 |
CDOID cdoid = iterator.next(); |
| 711 |
try |
| 712 |
{ |
| 713 |
currentView.getObject(cdoid); |
| 714 |
fail(); |
| 715 |
} |
| 716 |
catch (ObjectNotFoundException ex) |
| 717 |
{ |
| 718 |
// Expected exception |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
session.close(); |
| 724 |
} |
| 725 |
|
| 726 |
private void revertObject(CDOID objectId, long timestamp, boolean recursively, CDOID... additionalIds) |
| 727 |
throws CommitException |
| 728 |
{ |
| 729 |
CDOSession session; |
| 730 |
session = (CDOSession)openSession(); |
| 731 |
CDOView openAudit = session.openView(timestamp); |
| 732 |
CDOObject historicalObject = openAudit.getObject(objectId); |
| 733 |
List<CDOObject> tmpObjects = new ArrayList<CDOObject>(); |
| 734 |
tmpObjects.add(historicalObject); |
| 735 |
if (recursively) |
| 736 |
{ |
| 737 |
for (TreeIterator<EObject> allContents = historicalObject.eAllContents(); allContents.hasNext();) |
| 738 |
{ |
| 739 |
CDOObject next = (CDOObject)allContents.next(); |
| 740 |
tmpObjects.add(next); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
for (CDOID id : additionalIds) |
| 745 |
{ |
| 746 |
CDOObject object = openAudit.getObject(id); |
| 747 |
tmpObjects.add(object); |
| 748 |
} |
| 749 |
|
| 750 |
CDOTransaction openTransaction2 = session.openTransaction(); |
| 751 |
((CDOTransactionImpl)openTransaction2).revert(tmpObjects.toArray(new CDOObject[tmpObjects.size()])); |
| 752 |
openTransaction2.commit(); |
| 753 |
session.close(); |
| 754 |
} |
| 755 |
|
| 756 |
private void changeAttribute(EObject next, EAttribute eAttribute) |
| 757 |
{ |
| 758 |
Object value = next.eGet(eAttribute); |
| 759 |
if (eAttribute.getEType().getInstanceClass() == String.class) |
| 760 |
{ |
| 761 |
next.eSet(eAttribute, String.valueOf(value) + String.valueOf(value)); |
| 762 |
} |
| 763 |
else if (eAttribute.getEType().getInstanceClass() == Float.TYPE) |
| 764 |
{ |
| 765 |
next.eSet(eAttribute, ((Float)value).floatValue() + 1); |
| 766 |
} |
| 767 |
else if (eAttribute.getEType().getInstanceClass() == Double.TYPE) |
| 768 |
{ |
| 769 |
next.eSet(eAttribute, ((Double)value).doubleValue() + 1); |
| 770 |
} |
| 771 |
else if (eAttribute.getEType().getInstanceClass() == Integer.TYPE) |
| 772 |
{ |
| 773 |
next.eSet(eAttribute, ((Integer)value).intValue() + 1); |
| 774 |
} |
| 775 |
else if (eAttribute.getEType().getInstanceClass() == Long.TYPE) |
| 776 |
{ |
| 777 |
next.eSet(eAttribute, ((Long)value).longValue() + 1); |
| 778 |
} |
| 779 |
else if (eAttribute.getEType().getInstanceClass() == java.util.Date.class) |
| 780 |
{ |
| 781 |
next.eSet(eAttribute, Calendar.getInstance().getTime()); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
public static class RevertEqualityHelper extends EqualityHelper |
| 786 |
{ |
| 787 |
private static final long serialVersionUID = -729066319986556429L; |
| 788 |
|
| 789 |
@Override |
| 790 |
public boolean equals(EObject eObject1, EObject eObject2) |
| 791 |
{ |
| 792 |
boolean eq = super.equals(eObject1, eObject2); |
| 793 |
if (eq) |
| 794 |
{ |
| 795 |
if (eObject1 instanceof CDOObject) |
| 796 |
{ |
| 797 |
eq = ((CDOObject)eObject1).cdoID().equals(((CDOObject)eObject2).cdoID()); |
| 798 |
} |
| 799 |
} |
| 800 |
return eq; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
} |