01: package org.mdarad.samples.petstore.facades;
02:
03: import org.dataisland.primitives.exception.InstanceNotLocalizedException;
04: import org.dataisland.primitives.exception.NoLocalizablePropertyException;
05: import org.mdarad.framework.exception.ConcurrencyException;
06: import org.mdarad.framework.exception.SystemException;
07: import org.mdarad.samples.petstore.entities.AbstractProductTestCase;
08: import org.mdarad.samples.petstore.entities.Item;
09: import org.mdarad.samples.petstore.entities.ItemTestCase;
10: import org.mdarad.samples.petstore.entities.Product;
11: import org.mdarad.samples.petstore.entities.ProductTestCase;
12:
13: public class ConcurrentTestCase extends AbstractProductTestCase {
14:
15: protected Product referenceProduct;
16: protected Product p1;
17: protected Product p2;
18: protected Item i1;
19: protected Item i2;
20:
21: protected void setUp() throws Exception {
22: super .setUp();
23: referenceProduct = ProductTestCase.createMinimalProduct();
24: String referenceId = referenceProduct.getId();
25:
26: //Fetch 2 instances of a product
27: p1 = ProductBusinessObjectFacade.getInstance().fetchProduct(
28: referenceId, localizationContext);
29: p2 = ProductBusinessObjectFacade.getInstance().fetchProduct(
30: referenceId, localizationContext);
31:
32: //Populate 2 different items
33: i1 = ItemTestCase.populateMinimalItem();
34: i1.setItemName("FIRST TRANSACTION");
35: i2 = ItemTestCase.populateMinimalItem();
36: i2.setItemName("SECOND TRANSACTION");
37: }
38:
39: protected void tearDown() throws Exception {
40: super .tearDown();
41:
42: Product deletedProduct = ProductBusinessObjectFacade
43: .getInstance().fetchProduct(referenceProduct.getId(),
44: localizationContext);
45: ProductBusinessObjectFacade.getInstance().deleteProduct(
46: deletedProduct);
47: }
48:
49: public void testConcurrentModifications()
50: throws NoLocalizablePropertyException,
51: InstanceNotLocalizedException, SystemException,
52: ConcurrencyException, InterruptedException {
53: p1.addItems(i1);
54: Thread.sleep(1000);
55: p1 = ProductBusinessObjectFacade.getInstance().saveProduct(p1);
56: assertEquals(1, p1.getItems().size());
57:
58: p2.addItems(i2);
59: boolean exception = false;
60: try {
61: p2 = ProductBusinessObjectFacade.getInstance().saveProduct(
62: p2);
63: } catch (ConcurrencyException ce) {
64: //OK
65: exception = true;
66: }
67: assertTrue(exception);
68: }
69: }
|