Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07f8dbbaa3f94f3d07283ad28cc7654a17ffb847 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Esteban Dugueperoux - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.net4j.protocol.CommitTransactionRequest;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSession.Options;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.IStoreAccessor.CommitContext;
import org.eclipse.emf.cdo.server.ITransaction;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Customer;
import org.eclipse.emf.cdo.tests.model1.PurchaseOrder;
import org.eclipse.emf.cdo.tests.model1.SalesOrder;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.net4j.util.om.monitor.OMMonitor;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

import org.junit.Assert;

/**
 * Bug 377173 : test commit progress/cancel.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_377173_Test extends AbstractCDOTest
{
  private static final String RESOURCE_PATH = "/test1";

  public void testCancelCommitWithProgressMonitor() throws Exception
  {
    CDOSession session = openSession();
    CDONet4jSession.Options options = (Options)session.options();
    options.setCommitTimeout(100000 * CommitTransactionRequest.DEFAULT_MONITOR_TIMEOUT_SECONDS);

    CDOTransaction transaction1 = session.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath(RESOURCE_PATH));

    // 1. Create a example model
    Company customer1 = initializeModel(resource1);

    resource1.getContents().add(customer1);

    CommitWaiter commitWaiter = new CommitWaiter();
    getRepository().addHandler(commitWaiter);
    ProgressMonitorAsserter progressMonitorAsserter = new ProgressMonitorAsserter();
    try
    {
      transaction1.commit(progressMonitorAsserter);
      fail("An OperationCanceledException should be thrown as the commit has been canceled");
    }
    catch (Exception ex)
    {
      Assert.assertTrue(ex instanceof OperationCanceledException);
    }
    finally
    {
      getRepository().removeHandler(commitWaiter);
    }

  }

  private static class ProgressMonitorAsserter extends NullProgressMonitor
  {

    @Override
    public void internalWorked(double work)
    {
      super.internalWorked(work);
      setCanceled(true);
    }

  }

  private static class CommitWaiter implements IRepository.WriteAccessHandler
  {

    public void handleTransactionBeforeCommitting(ITransaction transaction, CommitContext commitContext,
        OMMonitor monitor) throws RuntimeException
    {
      sleep(2000);
    }

    public void handleTransactionAfterCommitted(ITransaction transaction, CommitContext commitContext, OMMonitor monitor)
    {
    }

  }

  private Company initializeModel(CDOResource resource)
  {
    Company company = getModel1Factory().createCompany();

    Customer customer = getModel1Factory().createCustomer();
    customer.setName("Martin Fluegge");
    customer.setStreet("ABC Street 7");
    customer.setCity("Berlin");

    SalesOrder salesOrder = getModel1Factory().createSalesOrder();
    customer.getSalesOrders().add(salesOrder);
    resource.getContents().add(salesOrder);
    resource.getContents().add(customer);

    for (int i = 0; i < 1000; i++)
    {
      PurchaseOrder purchaseOrder = getModel1Factory().createPurchaseOrder();
      company.getPurchaseOrders().add(purchaseOrder);
    }

    company.getCustomers().add(customer);

    return company;
  }
}

Back to the top