summaryrefslogtreecommitdiff
path: root/src/test/scala/com/google/gimd/modification/ModificationTestCase.scala
blob: 2f9e09cde4d0964d1912a6e42abba52e20ddfb88 (plain)
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
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gimd.modification


import file.{FileType, File}
import org.junit.Test
import org.junit.Assert._
import query._
import UserType._

final class ModificationTestCase {

  val nestedMember = new NestedMember("node", TreeNodeType)

  case class TreeNode(id: Int, name: String)
  object TreeNodeType extends UserType[TreeNode] {
    val id = FieldSpecOne("id", IntField, _.id)
    val name = FieldSpecOne("name", StringField, _.name)
    def fields = id :: name
    override def children = Seq(nestedMember)
    def toUserObject(m: Message) = new TreeNode(id(m), name(m))
  }
  object MockFileType extends FileType[TreeNode] {
    val pathPrefix = None
    val pathSuffix = None
    val userType = TreeNodeType
    def name(m: Message) = userType.id(m).toString
  }
  case class MockFile(message: Message) extends File[TreeNode] {
    val path = ""
    val fileType = MockFileType
    val userObject = fileType.userType.toUserObject(message)
  }

  val root = new TreeNode(-1, "a")
  val child_0 = new TreeNode(0, "a")
  val child_1 = new TreeNode(1, "b")
  val child_2 = new TreeNode(2, "a")

  val child_msg0 = TreeNodeType.toMessage(child_0)
  val child_msg1 = TreeNodeType.toMessage(child_1)
  val child_msg2 = TreeNodeType.toMessage(child_2)

  val message = TreeNodeType.toMessageBuffer(root).
    add("node", child_msg0).
    add("node", child_msg1).
    add("node", child_msg2).readOnly

  @Test
  def insertUnderEmptyPath {
    val file = MockFile(message)
    val handle = CompleteHandle(file, PathHandle.empty)
    val child_3 = TreeNode(3, "a")
    val child_msg3 = TreeNodeType.toMessage(child_3)
    val modification = DatabaseModification.empty.insert(handle, nestedMember, child_3)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(root).
                              add("node", child_msg0).
                              add("node", child_msg1).
                              add("node", child_msg2).
                              add("node", child_msg3).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test
  def insertNewFile {
    val modification = DatabaseModification.empty.insertFile(MockFileType, root)
    val (_, newFiles) = modification.reduce
    val root_msg = TreeNodeType.toMessage(root)
    assertEquals(List((MockFileType, root_msg)), newFiles)
  }

  @Test
  def insertUnderNonEmptyPath {
    val file = MockFile(message)
    val handle = CompleteHandle(file, path(child_msg1))
    val child_3 = TreeNode(3, "a")
    val child_msg3 = TreeNodeType.toMessage(child_3)
    val modification = DatabaseModification.empty.insert(handle, nestedMember, child_3)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(root).
                              add("node", child_msg0).
                              add("node", child_msg1 + MessageField("node", child_msg3)).
                              add("node", child_msg2).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test
  def removeLeaf {
    val file = MockFile(message)
    val handle = CompleteHandle(file, path(child_msg1))
    val modification = DatabaseModification.empty.remove(handle)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(root).
                              add("node", child_msg0).
                              add("node", child_msg2).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test
  def removeTop {
    val file = MockFile(message)
    val handle = CompleteHandle(file, PathHandle.empty)
    val modification = DatabaseModification.empty.remove(handle)
    assertEquals(None, modification.reduce._1(file))
  }

  @Test
  def editTop {
    val file = MockFile(message)
    val handle = CompleteHandle[TreeNode](file, PathHandle.empty)
    val newRoot = TreeNode(-1, "c")
    val modification = DatabaseModification.empty.modify(handle, newRoot)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(newRoot).
                              add("node", child_msg0).
                              add("node", child_msg1).
                              add("node", child_msg2).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test
  def editChildAndInsertUnderneath {
    val file = MockFile(message)
    val handle = CompleteHandle[TreeNode](file, path(child_msg1))
    val newChild_1 = TreeNode(1, "c")
    val newChild_msg1 = TreeNodeType.toMessage(newChild_1)
    val child_3 = TreeNode(3, "a")
    val child_msg3 = TreeNodeType.toMessage(child_3)
    val modification = DatabaseModification.empty.
                        modify(handle, newChild_1).
                        insert(handle, nestedMember, child_3)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(root).
                              add("node", child_msg0).
                              add("node", newChild_msg1 + MessageField("node", child_msg3)).
                              add("node", child_msg2).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test
  def insertUnderneathChildAndEdit {
    val file = MockFile(message)
    val handle = CompleteHandle[TreeNode](file, path(child_msg1))
    val newChild_1 = TreeNode(1, "c")
    val newChild_msg1 = TreeNodeType.toMessage(newChild_1)
    val child_3 = TreeNode(3, "a")
    val child_msg3 = TreeNodeType.toMessage(child_3)
    val modification = DatabaseModification.empty.
                        insert(handle, nestedMember, child_3).
                        modify(handle, newChild_1)
    val (modFiles, _) = modification.reduce
    val newTopMessage = modFiles(file).get
    val expectedTopMessage = TreeNodeType.toMessageBuffer(root).
                              add("node", child_msg0).
                              add("node", newChild_msg1 + MessageField("node", child_msg3)).
                              add("node", child_msg2).readOnly
    assertEquals(expectedTopMessage, newTopMessage)
  }

  @Test{val expected = classOf[ConflictingModificationException]}
  def removeTopAndInsertUnderneath {
    val file = MockFile(message)
    val handle = CompleteHandle(file, PathHandle.empty)
    val child_3 = TreeNode(3, "a")
    DatabaseModification.empty.remove(handle).insert(handle, nestedMember, child_3)
  }

  @Test{val expected = classOf[ConflictingModificationException]}
  def editTopTwoTimes {
    val file = MockFile(message)
    val handle = CompleteHandle[TreeNode](file, PathHandle.empty)
    DatabaseModification.empty.modify(handle, root).modify(handle, root)
  }

  @Test{val expected = classOf[IllegalArgumentException]}
  def editWrongType {
    val file = MockFile(message)
    val handle = CompleteHandle[Unit](file, PathHandle.empty)
    DatabaseModification.empty.modify(handle, ())
  }

  @Test{val expected = classOf[IllegalArgumentException]}
  def passWrongNestedMember {
    val file = MockFile(message)
    val handle = CompleteHandle[TreeNode](file, PathHandle.empty)
    val child_3 = TreeNode(3, "a")
    DatabaseModification.empty.insert(handle, new NestedMember("wrong", TreeNodeType), child_3)
  }

  private def path(msgs: Message*) =
    PathHandle(List.make(msgs.size, TreeNodeType) zip msgs.map(MessageField("node", _)).toList)

}