Skip to main content
summaryrefslogtreecommitdiffstats
blob: 999dd65620ce2b899a31514cc4147f17cfc7053f (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
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * 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:
 *   Abel Hegedus, Tamas Borbas, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.tests

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 static org.junit.Assert.*

class ComplexReverseModelPatchTest {
  private extension val ModelPatchReverser modelPatchReverser = ModelPatchReverser.INSTANCE

  @Test
  public def referencedElementAdditionReverse() {
    // Prepare original patch
    val patchBuilder = ModelPatchBuilder.create
    val entryBuilder = ModelPatchBuilder.entryBuilder

    val element = new Identifiable("newElement")
    val type = new Identifiable("type")
    val elementEntry =	(entryBuilder => [
                it.context = element
                it.direction = ChangeDirection.ADD
                it.type = type
              ]).buildElementEntry
    patchBuilder.addEntry(elementEntry)

    val container = new Identifiable("container")
    val feature = new Identifiable("elements")
    val referenceEntry = (entryBuilder => [
                it.context = container
                it.direction = ChangeDirection.ADD
                it.feature = feature
                it.target = element
              ]).buildReferenceEntry
    patchBuilder.addEntry(referenceEntry)

    val originalPatch = patchBuilder.build


    // Create reverse patch
    val reversePatch = originalPatch.reverse


    // Check reverse patch
    assertEquals(2, reversePatch.entries.size)

    val reverseReferenceEntry = reversePatch.entries.get(0)
    assertEquals(container.identifier, reverseReferenceEntry.context.identifier)
    assertEquals(ChangeDirection.REMOVE, reverseReferenceEntry.direction)
    assertEquals(EntryType.REFERENCE, reverseReferenceEntry.entryType)
    assertEquals(feature.identifier, (reverseReferenceEntry as ReferenceEntry).feature.identifier)
    assertEquals(element.identifier, (reverseReferenceEntry as ReferenceEntry).target.identifier)

    val reverseElementEntry = reversePatch.entries.get(1)
    assertEquals(element.identifier, reverseElementEntry.context.identifier)
    assertEquals(ChangeDirection.REMOVE, reverseElementEntry.direction)
    assertEquals(EntryType.ELEMENT, reverseElementEntry.entryType)
    assertEquals(type.identifier, (reverseElementEntry as ElementEntry).type.identifier)
  }

  @Test
  public def referencedElementDeletionReverse() {
    // Prepare original patch
    val patchBuilder = ModelPatchBuilder.create
    val entryBuilder = ModelPatchBuilder.entryBuilder

    val element = new Identifiable("newElement")
    val container = new Identifiable("container")
    val feature = new Identifiable("elements")
    val referenceEntry = (entryBuilder => [
                  it.context = container
                  it.direction = ChangeDirection.REMOVE
                  it.feature = feature
                  it.target = element
                ]).buildReferenceEntry
    patchBuilder.addEntry(referenceEntry)

    val type = new Identifiable("type")
    val elementEntry = (entryBuilder => [
                  it.context = element
                  it.direction = ChangeDirection.REMOVE
                  it.type = type
                ]).buildElementEntry
    patchBuilder.addEntry(elementEntry)

    val originalPatch = patchBuilder.build


    // Create reverse patch
    val reversePatch = originalPatch.reverse


    // Check reverse patch
    assertEquals(2, reversePatch.entries.size)

    val reverseElementEntry = reversePatch.entries.get(0)
    assertEquals(element.identifier, reverseElementEntry.context.identifier)
    assertEquals(ChangeDirection.ADD, reverseElementEntry.direction)
    assertEquals(EntryType.ELEMENT, reverseElementEntry.entryType)
    assertEquals(type.identifier, (reverseElementEntry as ElementEntry).type.identifier)

    val reverseReferenceEntry = reversePatch.entries.get(1)
    assertEquals(container.identifier, reverseReferenceEntry.context.identifier)
    assertEquals(ChangeDirection.ADD, reverseReferenceEntry.direction)
    assertEquals(EntryType.REFERENCE, reverseReferenceEntry.entryType)
    assertEquals(feature.identifier, (reverseReferenceEntry as ReferenceEntry).feature.identifier)
    assertEquals(element.identifier, (reverseReferenceEntry as ReferenceEntry).target.identifier)
  }

  @Test
  public def attributeModificationReverse() {
    // Prepare original patch
    val patchBuilder = ModelPatchBuilder.create
    val entryBuilder = ModelPatchBuilder.entryBuilder

    val container = new Identifiable("namedElement")
    val feature = new Identifiable("name")
    val oldValue = "old"
    val removedEntry = (entryBuilder => [
                it.context = container
                it.direction = ChangeDirection.REMOVE
                it.feature = feature
                it.value = oldValue
              ]).buildAttributeEntry
    patchBuilder.addEntry(removedEntry)

    val newValue = "new"
    val addedEntry = (entryBuilder => [
                it.context = container
                it.direction = ChangeDirection.ADD
                it.feature = feature
                it.value = newValue
              ]).buildAttributeEntry
    patchBuilder.addEntry(addedEntry)

    val originalPatch = patchBuilder.build


    // Create reverse patch
    val reversePatch = originalPatch.reverse


    // Check reverse patch
    assertEquals(2, reversePatch.entries.size)

    val reverseAddedEntry = reversePatch.entries.get(0)
    assertEquals(container.identifier, reverseAddedEntry.context.identifier)
    assertEquals(ChangeDirection.REMOVE, reverseAddedEntry.direction)
    assertEquals(EntryType.ATTRIBUTE, reverseAddedEntry.entryType)
    assertEquals(feature.identifier, (reverseAddedEntry as AttributeEntry).feature.identifier)
    assertEquals(newValue, (reverseAddedEntry as AttributeEntry).value)

    val reverseRemovedEntry = reversePatch.entries.get(1)
    assertEquals(container.identifier, reverseRemovedEntry.context.identifier)
    assertEquals(ChangeDirection.ADD, reverseRemovedEntry.direction)
    assertEquals(EntryType.ATTRIBUTE, reverseRemovedEntry.entryType)
    assertEquals(feature.identifier, (reverseRemovedEntry as AttributeEntry).feature.identifier)
    assertEquals(oldValue, (reverseRemovedEntry as AttributeEntry).value)
  }

}

Back to the top