Skip to main content
summaryrefslogtreecommitdiffstats
blob: ef2e46f80851e43b70a09c3ffe319a4864d8682c (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
/*
 * Copyright (c) 2004 - 2012 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.compare.CDOCompare;
import org.eclipse.emf.cdo.compare.CDOCompareUtil;
import org.eclipse.emf.cdo.compare.CDOComparisonScope;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Product1;
import org.eclipse.emf.cdo.tests.model1.legacy.Model1Factory;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.diff.DefaultDiffEngine;
import org.eclipse.emf.compare.diff.DiffBuilder;
import org.eclipse.emf.compare.diff.FeatureFilter;
import org.eclipse.emf.compare.diff.IDiffEngine;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import java.util.Collections;
import java.util.Set;

/**
 * @author Eike Stepper
 */
@Requires(IRepositoryConfig.CAPABILITY_AUDITING)
public class EMFCompareTest extends AbstractCDOTest
{
  public void testAllContentsOfCompany() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = createCompany();
    company.setName("ESC");

    resource.getContents().add(company);
    resource.getContents().add(createCompany());
    resource.getContents().add(createCompany());
    transaction.commit();

    company.setName("Sympedia");
    CDOCommitInfo commit2 = transaction.commit();

    company.setName("Eclipse");
    transaction.commit();

    Comparison comparison = CDOCompareUtil.compare(CDOUtil.getCDOObject(company), session.openView(commit2), null);
    dump(comparison);
  }

  public void testAllContentsOfRoot() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = createCompany();
    company.setName("ESC");

    resource.getContents().add(company);
    resource.getContents().add(createCompany());
    resource.getContents().add(createCompany());
    transaction.commit();

    company.setName("Sympedia");
    CDOCommitInfo commit2 = transaction.commit();

    company.setName("Eclipse");
    transaction.commit();

    Comparison comparison = CDOCompareUtil.compare(CDOUtil.getCDOObject(transaction.getRootResource()),
        session.openView(commit2), null);
    dump(comparison);
  }

  public void testMinimal() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = createCompany();

    resource.getContents().add(company);
    resource.getContents().add(createCompany());
    resource.getContents().add(createCompany());
    transaction.commit();

    company.setName("Sympedia");
    CDOCommitInfo commit2 = transaction.commit();

    company.setName("Eclipse");
    transaction.commit();

    Comparison comparison = CDOCompareUtil.compare(transaction, session.openView(commit2), null);
    dump(comparison);
  }

  public void testContainmentProxy() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource1 = transaction.createResource(getResourcePath("/res1"));
    CDOResource resource2 = transaction.createResource(getResourcePath("/res2"));

    Company company = getModel1Factory().createCompany();
    company.setName("Eclipse");
    resource1.getContents().add(company);

    Category categoryCrossContained = getModel1Factory().createCategory();
    categoryCrossContained.setName("Category");
    company.getCategories().add(categoryCrossContained);
    resource2.getContents().add(categoryCrossContained);

    for (int i = 0; i < 10; ++i)
    {
      Product1 product = getModel1Factory().createProduct1();
      categoryCrossContained.getProducts().add(product);
    }

    CDOCommitInfo commitInfo = transaction.commit();

    Product1 product0 = categoryCrossContained.getProducts().get(0);
    product0.setName("CHANGED");

    transaction.commit();

    Comparison comparison = CDOCompareUtil.compare(transaction, session.openView(commitInfo), null);
    dump(comparison);
    assertEquals(0, comparison.getMatch(categoryCrossContained).getDifferences().size());
  }

  public void testContainmentProxyEMF() throws Exception
  {
    ResourceSet resourceSetA = createResourceSet();
    Category categoryCrossContained = populate(resourceSetA);

    ResourceSet resourceSetB = createResourceSet();
    Product1 product0 = populate(resourceSetB).getProducts().get(0);
    product0.setName("ProductB");

    IComparisonScope scope = EMFCompare.createDefaultScope(resourceSetA, resourceSetB);

    // Configure EMF Compare
    // IEObjectMatcher matcher = DefaultMatchEngine.createDefaultEObjectMatcher(UseIdentifiers.NEVER);
    // IComparisonFactory comparisonFactory = new DefaultComparisonFactory(new DefaultEqualityHelperFactory());
    // IMatchEngine matchEngine = new DefaultMatchEngine(matcher, comparisonFactory);
    // EMFCompare comparator = EMFCompare.builder().setMatchEngine(matchEngine).build();
    EMFCompare comparator = EMFCompare.builder().build();

    // Compare the two models
    Comparison comparison = comparator.compare(scope);
    dump(comparison);
    assertEquals(0, comparison.getMatch(categoryCrossContained).getDifferences().size());
    assertEquals(1, comparison.getMatch(product0).getDifferences().size());
  }

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

    Company company = getModel1Factory().createCompany();
    company.setName("Company");
    resource1.getContents().add(company);

    Category category = getModel1Factory().createCategory();
    category.setName("Category");
    company.getCategories().add(category);

    for (int i = 0; i < 10; ++i)
    {
      Product1 product = getModel1Factory().createProduct1();
      category.getProducts().add(product);
    }

    company.setName("Company2");

    CDOCommitInfo commitInfo = transaction.commit();

    category.getProducts().get(0).setName("EclipseProduct");
    transaction.commit();

    Comparison comparison = CDOCompareUtil.compare(transaction, session.openView(commitInfo), null);
    dump(comparison);
    assertEquals(0, comparison.getMatch(category).getDifferences().size());
  }

  public void testChanges() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = getModel1Factory().createCompany();
    company.setName("Company");
    resource.getContents().add(company);

    Category category1 = getModel1Factory().createCategory();
    category1.setName("Category1");
    company.getCategories().add(category1);

    transaction.commit();

    // Change category1
    category1.setName("CHANGED");

    Category category2 = getModel1Factory().createCategory();
    category2.setName("Category2");

    // Change company, add category2
    company.getCategories().add(category2);

    Comparison comparison = CDOCompareUtil.compareUncommittedChanges(transaction);
    dump(comparison);
    assertEquals(1, comparison.getMatch(category1).getDifferences().size());
    assertEquals(1, comparison.getMatch(company).getDifferences().size());
  }

  public void testChangesDelete() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = getModel1Factory().createCompany();
    company.setName("Company");
    resource.getContents().add(company);

    Category category = getModel1Factory().createCategory();
    category.setName("Category");
    company.getCategories().add(category);

    transaction.commit();

    company.getCategories().clear();

    Comparison comparison = CDOCompareUtil.compareUncommittedChanges(transaction);
    dump(comparison);
    assertEquals(1, comparison.getMatch(company).getDifferences().size());
  }

  public void testFeatureFilter() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));

    Company company = createCompany();
    company.setName("ESC");
    company.setCity("Berlin");
    resource.getContents().add(company);
    CDOCommitInfo commit1 = transaction.commit();

    company.setName("Eclipse");
    company.setCity("Ottawa");
    transaction.commit();

    IComparisonScope scope = CDOComparisonScope.Minimal.create(transaction, session.openView(commit1), null);
    Comparison comparison = new CDOCompare()
    {
      @Override
      protected IDiffEngine createDiffEngine()
      {
        return new DefaultDiffEngine(new DiffBuilder())
        {
          @Override
          protected FeatureFilter createFeatureFilter()
          {
            return new FeatureFilter()
            {
              Set<? extends EStructuralFeature> ignored = Collections.singleton(getModel1Package().getAddress_City());

              @Override
              protected boolean isIgnoredAttribute(EAttribute attribute)
              {
                return ignored.contains(attribute);
              }
            };
          }
        };
      }
    }.compare(scope);

    dump(comparison);
    assertEquals(1, comparison.getMatch(company).getDifferences().size());
  }

  private Company createCompany()
  {
    Company company = getModel1Factory().createCompany();
    company.getCategories().add(getModel1Factory().createCategory());
    company.getCategories().add(getModel1Factory().createCategory());
    company.getCategories().add(getModel1Factory().createCategory());
    company.getCategories().add(getModel1Factory().createCategory());
    company.getCategories().add(getModel1Factory().createCategory());
    return company;
  }

  private static ResourceSet createResourceSet()
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
    return resourceSet;
  }

  private static Category populate(ResourceSet resourceSet)
  {
    Model1Factory factory = org.eclipse.emf.cdo.tests.model1.legacy.Model1Factory.eINSTANCE;

    Resource resourceA1 = resourceSet.createResource(URI.createURI("res1"));
    Resource resourceA2 = resourceSet.createResource(URI.createURI("res2"));

    Company company = factory.createCompany();
    company.setName("Eclipse");
    resourceA1.getContents().add(company);

    Category categoryCrossContained = factory.createCategory();
    categoryCrossContained.setName("Category");
    company.getCategories().add(categoryCrossContained);
    resourceA2.getContents().add(categoryCrossContained);

    for (int i = 0; i < 10; ++i)
    {
      Product1 product = factory.createProduct1();
      product.setName("Product" + i);
      categoryCrossContained.getProducts().add(product);
    }

    return categoryCrossContained;
  }

  private static void dump(Comparison comparison)
  {
    dump(comparison.getMatches(), "");
  }

  private static void dump(EList<Match> matches, String indent)
  {
    for (Match match : matches)
    {
      System.out.println(indent + "Match:");
      System.out.println(indent + "   Left:   " + toString(match == null ? null : match.getLeft()));
      System.out.println(indent + "   Right:  " + toString(match == null ? null : match.getRight()));
      String origin = toString(match.getOrigin());
      if (origin != null)
      {
        System.out.println(indent + "   Origin: " + origin);
      }

      System.out.println(indent + "   Differences:");
      for (Diff diff : match.getDifferences())
      {
        System.out.println(indent + "      " + diff);
      }

      dump(match.getSubmatches(), indent + "   ");
    }
  }

  private static String toString(EObject object)
  {
    if (object == null)
    {
      return null;
    }

    CDOObject cdoObject = CDOUtil.getCDOObject(object);
    if (cdoObject != null)
    {
      CDORevision revision = cdoObject.cdoRevision();
      if (revision != null)
      {
        return revision.toString();
      }
    }

    return object.toString();
  }
}

Back to the top