Skip to main content
summaryrefslogtreecommitdiffstats
blob: 080ea5df51f1a5ed9003040147a7f6886965614a (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
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.tests

import java.util.Collection
import org.eclipse.emf.diffmerge.patch.AttributeEntry
import org.eclipse.emf.diffmerge.patch.ElementEntry
import org.eclipse.emf.diffmerge.patch.ReferenceEntry
import org.eclipse.emf.diffmerge.patch.api.ChangeDirection
import org.eclipse.emf.diffmerge.patch.api.EntryType
import org.eclipse.emf.diffmerge.patch.api.Identifiable
import org.eclipse.emf.diffmerge.patch.api.ModelPatchBuilder
import org.eclipse.emf.diffmerge.patch.api.ModelPatchReverser
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

import static org.junit.Assert.*

@RunWith(Parameterized)
class BasicReverseModelPatchTests {
  private extension val ModelPatchReverser modelPatchReverser = ModelPatchReverser.INSTANCE
  private val REVERSE_PATCH_ENTRIES_SIZE = 1
  private val CONTEXT_ID = "context"
  private val FEATURE_ID = "feature"
  private val TARGET_ID = "target"
  private val TYPE_ID = "type"
  private val VALUE = "value"

  @Parameterized.Parameters(name = "{1} {0}")
  def static Collection<Object[]> testData() {
    return newArrayList(
      #[EntryType.ATTRIBUTE, ChangeDirection.ADD],
      #[EntryType.ATTRIBUTE, ChangeDirection.REMOVE],
      #[EntryType.ELEMENT, ChangeDirection.ADD],
      #[EntryType.ELEMENT, ChangeDirection.REMOVE],
      #[EntryType.REFERENCE, ChangeDirection.ADD],
      #[EntryType.REFERENCE, ChangeDirection.REMOVE]
    )
  }

  @Parameterized.Parameter(0)
  public EntryType type
  @Parameterized.Parameter(1)
  public ChangeDirection direction

  @Test
  public def test() {
    // Prepare original patch
    val originalPatch = ModelPatchBuilder.create.addEntry(
      (ModelPatchBuilder.entryBuilder => [
        it.context = new Identifiable(CONTEXT_ID)
        it.direction = direction
        switch type {
          case ATTRIBUTE: {
            it.feature = new Identifiable(FEATURE_ID)
            it.value = VALUE
          }
          case ELEMENT: {
            it.type = new Identifiable(TYPE_ID)
          }
          case REFERENCE: {
            it.feature = new Identifiable(FEATURE_ID)
            it.target = new Identifiable(TARGET_ID)
          }
        }
      ]).build(type)
    ).build

    // Create reverse patch
    val reversePatch = originalPatch.reverse

    // Check reverse patch
    assertEquals(REVERSE_PATCH_ENTRIES_SIZE, reversePatch.entries.size)
    val reverseEntry = reversePatch.entries.get(0)
    assertEquals(CONTEXT_ID, reverseEntry.context.identifier)
    assertEquals(direction.reverse, reverseEntry.direction)
    assertEquals(type, reverseEntry.entryType)
    switch type {
      case ATTRIBUTE: {
        assertEquals(FEATURE_ID, (reverseEntry as AttributeEntry).feature.identifier)
        assertEquals(VALUE, (reverseEntry as AttributeEntry).value)
      }
      case ELEMENT: {
        assertEquals(TYPE_ID, (reverseEntry as ElementEntry).type.identifier)
      }
      case REFERENCE: {
        assertEquals(FEATURE_ID, (reverseEntry as ReferenceEntry).feature.identifier)
        assertEquals(TARGET_ID, (reverseEntry as ReferenceEntry).target.identifier)
      }
    }
  }

  private def getReverse(ChangeDirection direction) {
    if(direction==ChangeDirection.ADD) {
      return ChangeDirection.REMOVE
    } else {
      return ChangeDirection.ADD
    }
  }
}

Back to the top