Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9ce84a6b50d271017e4be2c2b4393e8b024bd2c8 (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
129
130
131
132
133
134
/*
 * Copyright (c) 2014-2016 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:
 *    Alex Lagarde - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Skips;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.util.ConcurrentAccessException;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests ensuring that moving elements inside the same containment list does not cause any issue when committing.
 *
 * @author Alex Lagarde
 */
@Skips({ IRepositoryConfig.CAPABILITY_AUDITING, IRepositoryConfig.CAPABILITY_BRANCHING, IRepositoryConfig.CAPABILITY_UNORDERED_LISTS })
public class Bugzilla_435532_Test extends AbstractCDOTest
{
  private static final int CHILDREN_NUMBER = 100;

  private static final int CHILDREN_TO_MOVE_NUMBER = 50;

  private Company root;

  private CDOSession cdoSession;

  private CDOTransaction cdoTransaction;

  @Override
  protected void doSetUp() throws Exception
  {
    super.doSetUp();

    // Initialize semantic model
    cdoSession = openSession();
    cdoTransaction = cdoSession.openTransaction();
    CDOResource resource = cdoTransaction.createResource(getResourcePath("resource"));

    // Create a root with CHILDREN_NUMBER children
    root = getModel1Factory().createCompany();
    for (int i = 0; i < CHILDREN_NUMBER; i++)
    {
      root.getCategories().add(getModel1Factory().createCategory());
    }
    resource.getContents().add(root);
    cdoTransaction.commit();
  }

  /**
   * Ensures that moving elements at the end of the same containment list does not cause any issue when committing.
   */
  public void testDragChildrenAtListEnd() throws ConcurrentAccessException, CommitException
  {
    doTestDragChildren(20, CHILDREN_NUMBER - 1);
  }

  /**
   * Ensures that moving elements at the beginning of the same containment list does not cause any issue when committing.
   */
  public void testDragChildrenAtListBegin() throws ConcurrentAccessException, CommitException
  {
    doTestDragChildren(20, 0);
  }

  public void testDragChildrenFromEndToBegin() throws ConcurrentAccessException, CommitException
  {
    doTestDragChildren(CHILDREN_NUMBER - CHILDREN_TO_MOVE_NUMBER - 1, 0);
  }

  private void doTestDragChildren(int oldPosition, int newPosition) throws ConcurrentAccessException, CommitException
  {
    // Step 1: get children to move
    List<Category> childrenToMove = new ArrayList<Category>(root.getCategories().subList(oldPosition, oldPosition + CHILDREN_TO_MOVE_NUMBER));
    List<CDOID> childrenToMoveIds = new ArrayList<CDOID>();
    for (Category child : childrenToMove)
    {
      childrenToMoveIds.add(CDOUtil.getCDOObject(child).cdoID());
    }

    // Step 2: move each child through a command
    for (int i = 0; i < childrenToMove.size(); i++)
    {
      Category childToMove = childrenToMove.get(i);
      root.getCategories().move(newPosition, childToMove);
    }

    // Step 3: commit
    cdoTransaction.commit();

    // Step 4: check that move correctly occured
    cdoTransaction.close();
    cdoSession.close();
    cdoSession = openSession();
    cdoTransaction = cdoSession.openTransaction();
    root = (Company)cdoTransaction.getResource(getResourcePath("resource")).getContents().get(0);
    childrenToMove = new ArrayList<Category>();
    for (CDOID childID : childrenToMoveIds)
    {
      childrenToMove.add((Category)CDOUtil.getEObject(cdoTransaction.getObject(childID)));
    }
    for (int i = 0; i < childrenToMove.size(); i++)
    {
      int expectedPosition = Math.abs(newPosition - i);
      Category child = childrenToMove.get(childrenToMove.size() - 1 - i);
      assertEquals(expectedPosition, root.getCategories().indexOf(child));
    }
  }

  @Override
  protected void doTearDown() throws Exception
  {
    cdoTransaction.close();
    cdoSession.close();
    super.doTearDown();
  }
}

Back to the top