Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3659cee704fe6949de521bd3c512f8388f12f10a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * Copyright (c) 2004 - 2011 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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionUtil;
import org.eclipse.emf.cdo.common.revision.delta.CDOAddFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOClearFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOListFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOSetFeatureDelta;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.common.revision.delta.CDOListFeatureDeltaImpl;
import org.eclipse.emf.cdo.internal.server.mem.MEMStore;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevisionManager;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Customer;
import org.eclipse.emf.cdo.tests.model1.SalesOrder;
import org.eclipse.emf.cdo.tests.model3.NodeD;
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.view.CDOView;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.IOUtil;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.spi.cdo.InternalCDOTransaction;

/**
 * See bug 201266
 * 
 * @author Simon McDuff
 */
public class RevisionDeltaTest extends AbstractCDOTest
{
  public RevisionDeltaTest()
  {
  }

  public void testBasicRevisionDelta() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource1 = transaction.createResource(getResourcePath("/test1"));

    Company company1 = getModel1Factory().createCompany();
    Category category1 = getModel1Factory().createCategory();

    resource1.getContents().add(company1);
    company1.getCategories().add(category1);

    company1.setName("TEST");
    InternalCDORevision rev1 = getCopyCDORevision(company1);

    // Test simple attributes
    company1.setName("TEST3");
    InternalCDORevision rev2 = getCopyCDORevision(company1);

    CDORevisionDelta revisionDelta = rev2.compare(rev1);
    assertEquals(1, revisionDelta.getFeatureDeltas().size());
    CDOSetFeatureDelta setDelta = (CDOSetFeatureDelta)revisionDelta.getFeatureDeltas().get(0);
    assertEquals("TEST3", setDelta.getValue());

    // Test List clear
    company1.getCategories().clear();
    InternalCDORevision rev3 = getCopyCDORevision(company1);

    CDORevisionDelta revisionDelta3 = rev3.compare(rev2);
    assertEquals(1, revisionDelta3.getFeatureDeltas().size());
    CDOListFeatureDeltaImpl delta3List = (CDOListFeatureDeltaImpl)revisionDelta3.getFeatureDeltas().get(0);

    assertEquals(1, delta3List.getListChanges().size());
    assertEquals(true, delta3List.getListChanges().get(0) instanceof CDOClearFeatureDelta);

    // Test List Add
    for (int i = 0; i < 5; i++)
    {
      Category category = getModel1Factory().createCategory();
      company1.getCategories().add(category);
    }

    InternalCDORevision rev4 = getCopyCDORevision(company1);

    CDORevisionDelta revisionDelta4 = rev4.compare(rev3);
    assertEquals(1, revisionDelta4.getFeatureDeltas().size());
    CDOListFeatureDeltaImpl delta4List = (CDOListFeatureDeltaImpl)revisionDelta4.getFeatureDeltas().get(0);

    assertEquals(5, delta4List.getListChanges().size());
    assertEquals(true, delta4List.getListChanges().get(0) instanceof CDOAddFeatureDelta);
    transaction.rollback();
    transaction.close();
    session.close();
  }

  /**
   * Sending deltas doesn't adjust CDOIDs
   * <p>
   * See bug 214374
   */
  public void testBugzilla214374() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    SalesOrder salesOrder = getModel1Factory().createSalesOrder();
    resource.getContents().add(salesOrder);
    transaction.commit();

    Customer customer = getModel1Factory().createCustomer();
    salesOrder.setCustomer(customer);

    resource.getContents().add(customer);
    transaction.commit();
    transaction.close();

    CDOTransaction transaction2 = session.openTransaction();
    SalesOrder salesOrder2 = (SalesOrder)CDOUtil.getEObject(transaction2.getObject(CDOUtil.getCDOObject(salesOrder)
        .cdoID(), true));
    CDORevision salesRevision = CDOUtil.getCDOObject(salesOrder2).cdoRevision();
    EStructuralFeature customerFeature = getModel1Package().getSalesOrder_Customer();

    Object value = salesRevision.data().get(customerFeature, 0);
    assertEquals(true, value instanceof CDOID);
    transaction2.close();
    session.close();
  }

  /**
   * CDOView.getRevision() does not work for transactions/dirty objects (INVALID)
   * <p>
   * See bug 214431
   */
  public void testBugzilla214431() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    SalesOrder salesOrder = getModel1Factory().createSalesOrder();
    resource.getContents().add(salesOrder);
    transaction.commit();

    salesOrder.setId(4711);
    assertNotSame(CDOUtil.getCDOObject(salesOrder).cdoRevision(),
        ((InternalCDOTransaction)transaction).getRevision(CDOUtil.getCDOObject(salesOrder).cdoID(), true));
    assertEquals(CDOUtil.getCDOObject(salesOrder).cdoRevision(),
        transaction.getDirtyObjects().get(CDOUtil.getCDOObject(salesOrder).cdoID()).cdoRevision());
    transaction.close();
    session.close();
  }

  /**
   * CDOView.getRevision() does not work for transactions/dirty objects (INVALID)
   * <p>
   * See bug 214374
   */
  public void testBugzilla214374_HandleClear() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    Customer customer = getModel1Factory().createCustomer();
    resource.getContents().add(customer);

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

    salesOrder = getModel1Factory().createSalesOrder();
    customer = getModel1Factory().createCustomer();
    resource.getContents().add(salesOrder);
    resource.getContents().add(customer);
    salesOrder.setCustomer(customer);
    transaction.commit();
    transaction.close();
    session.close();
  }

  /**
   * java.lang.IllegalStateException with MEMStore
   * <p>
   * See bug 243282
   */
  public void testBugzilla243282_Exception() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    Customer customer = getModel1Factory().createCustomer();
    resource.getContents().add(customer);

    transaction.commit();

    InternalCDORevisionManager revisionManager = getRepository().getRevisionManager();
    CDORevision revision = CDOUtil.getCDOObject(customer).cdoRevision();
    revisionManager.getCache().removeRevision(revision.getID(), revision);

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

    IStore store = getRepository().getStore();
    if (store instanceof MEMStore)
    {
      MEMStore memStore = (MEMStore)store;
      CDORevisionUtil.dumpAllRevisions(memStore.getAllRevisions(), IOUtil.OUT());
    }

    transaction.commit();
    transaction.close();
    session.close();
  }

  @SuppressWarnings("unused")
  public void testDetachWithXRef() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    NodeD a = getModel3Factory().createNodeD();
    NodeD b = getModel3Factory().createNodeD();
    NodeD c = getModel3Factory().createNodeD();

    a.getChildren().add(b);
    c.getOtherNodes().add(b);

    resource.getContents().add(a);
    resource.getContents().add(c);

    if (true)
    {
      transaction.commit();
      transaction.close();
      transaction = session.openTransaction();
      resource = transaction.getResource(getResourcePath("/test1"));
    }

    // Start test logic

    a = (NodeD)resource.getContents().get(0);
    b = a.getChildren().get(0);
    c = (NodeD)resource.getContents().get(1);

    a.getChildren().remove(0);
    Object[] cB = c.getOtherNodes().toArray();

    CDORevisionDelta aDelta = transaction.getRevisionDeltas().get(CDOUtil.getCDOObject(a).cdoID());
    CDORevisionDelta cDelta = transaction.getRevisionDeltas().get(CDOUtil.getCDOObject(c).cdoID());

    session.close();
  }

  @SuppressWarnings("unused")
  public void testDetachWithXRef_Remove() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    NodeD a = getModel3Factory().createNodeD();
    NodeD b = getModel3Factory().createNodeD();
    NodeD c = getModel3Factory().createNodeD();

    a.getChildren().add(b);
    c.getOtherNodes().add(b);

    resource.getContents().add(a);
    resource.getContents().add(c);

    if (true)
    {
      transaction.commit();
      transaction.close();
      transaction = session.openTransaction();
      resource = transaction.getResource(getResourcePath("/test1"));
    }

    // Start test logic

    a = (NodeD)resource.getContents().get(0);
    b = a.getChildren().get(0);
    c = (NodeD)resource.getContents().get(1);

    a.getChildren().remove(0);
    c.getOtherNodes().remove(0);

    Object[] cB = c.getOtherNodes().toArray();

    CDORevisionDelta aDelta = transaction.getRevisionDeltas().get(CDOUtil.getCDOObject(a).cdoID());
    CDORevisionDelta cDelta = transaction.getRevisionDeltas().get(CDOUtil.getCDOObject(c).cdoID());

    CDOListFeatureDelta list = (CDOListFeatureDelta)cDelta.getFeatureDelta(getModel3Package().getNodeD_OtherNodes());
    assertEquals(1, list.getListChanges().size());

    session.close();
  }

  public void testListRemoveMiddle()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.remove(5);
      }
    });
  }

  public void testListRemoveLast()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.remove(list.size() - 1);
      }
    });
  }

  public void testListRemoveFirst()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.remove(0);
      }
    });
  }

  public void testListSimpleMove()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.move(2, 7);
      }
    });
  }

  public void testListMoveToLast()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.move(2, list.size() - 1);
      }
    });
  }

  public void testListMoveToFirst()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.move(2, 0);
      }
    });
  }

  public void testListTwoIndependentMoves()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.move(1, 3);
        list.move(7, 4);
      }
    });
  }

  public void testListTwoIntersectingMoves()
  {
    testStoreDelta(new ListManipulator()
    {
      public void doManipulations(EList<?> list)
      {
        list.move(1, 7);
        list.move(3, 4);
      }
    });
  }

  public void testListInsertFirst()
  {
    testStoreDelta(new ListManipulator()
    {
      @SuppressWarnings("unchecked")
      public void doManipulations(EList<?> list)
      {
        EList<Company> l = (EList<Company>)list;
        Company company = getModel1Factory().createCompany();
        company.setName("NewEntry");
        l.add(0, company);
      }
    });
  }

  public void testListInsertMiddle()
  {
    testStoreDelta(new ListManipulator()
    {
      @SuppressWarnings("unchecked")
      public void doManipulations(EList<?> list)
      {
        EList<Company> l = (EList<Company>)list;
        Company company = getModel1Factory().createCompany();
        company.setName("NewEntry");
        l.add(5, company);
      }
    });
  }

  public void testListInsertLast()
  {
    testStoreDelta(new ListManipulator()
    {
      @SuppressWarnings("unchecked")
      public void doManipulations(EList<?> list)
      {
        EList<Company> l = (EList<Company>)list;
        Company company = getModel1Factory().createCompany();
        company.setName("NewEntry");
        l.add(company);
      }
    });
  }

  public void testListMultipleOpsWithClear()
  {
    testStoreDelta(new ListManipulator()
    {
      @SuppressWarnings("unchecked")
      public void doManipulations(EList<?> list)
      {
        EList<Company> l = (EList<Company>)list;

        l.remove(4);
        l.remove(7);

        Company company = getModel1Factory().createCompany();
        company.setName("NewEntry 1");
        l.add(5, company);

        l.move(1, 2);
        l.move(7, 0);

        l.clear();

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 2");
        l.add(company);

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 3");
        l.add(company);

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 4");
        l.add(0, company);

        l.move(1, 2);
      }
    });
  }

  public void testListMultipleOps()
  {
    testStoreDelta(new ListManipulator()
    {
      @SuppressWarnings("unchecked")
      public void doManipulations(EList<?> list)
      {
        EList<Company> l = (EList<Company>)list;

        l.remove(7);

        Company company = getModel1Factory().createCompany();
        company.setName("NewEntry 1");
        l.add(5, company);

        l.move(1, 2);

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 2");
        l.add(company);

        l.move(7, 0);

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 3");
        l.add(company);

        l.remove(4);

        company = getModel1Factory().createCompany();
        company.setName("NewEntry 4");
        l.add(0, company);

        l.move(1, 2);
      }
    });
  }

  private InternalCDORevision getCopyCDORevision(Object object)
  {
    return (InternalCDORevision)CDOUtil.getCDOObject((EObject)object).cdoRevision().copy();
  }

  protected void testStoreDelta(ListManipulator manipulator)
  {
    BasicEList<Company> reference = new BasicEList<Company>();

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/test1"));

      for (int i = 0; i < 20; i++)
      {
        String name = "company " + i;
        Company company = getModel1Factory().createCompany();
        company.setName(name);
        resource.getContents().add(company);

        company = getModel1Factory().createCompany();
        company.setName(name);
        reference.add(company);
      }

      try
      {
        transaction.commit();
      }
      catch (CommitException ex)
      {
        throw WrappedException.wrap(ex);
      }

      transaction.close();
      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/test1"));

      manipulator.doManipulations(resource.getContents());
      manipulator.doManipulations(reference);

      try
      {
        transaction.commit();
      }
      catch (CommitException ex)
      {
        throw WrappedException.wrap(ex);
      }

      transaction.close();
      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    {
      CDOSession session = openSession();
      CDOView view = session.openView();
      CDOResource resource = view.getResource(getResourcePath("/test1"));

      assertEquals(reference.size(), resource.getContents().size());

      for (int i = 0; i < reference.size(); i++)
      {
        assertEquals(reference.get(i).getName(), ((Company)resource.getContents().get(i)).getName());
      }

      view.close();
      session.close();
    }
  }

  /**
   * @author Simon McDuff
   */
  protected static interface ListManipulator
  {
    public void doManipulations(EList<?> list);
  }
}

Back to the top