2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.persistence;
18 import static junit.framework.Assert.*;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
27 import javax.persistence.Id;
28 import javax.persistence.Version;
30 import org.junit.Test;
32 public class JpaMergeSupportTest {
34 private static class X1 {
47 public X1(String aValue) {
51 public String getValue() {
56 public boolean equals(Object aObj) {
60 if (!(aObj instanceof X1)) {
63 return value.equals(((X1) aObj).getValue());
67 private static class X2 {
71 private List<X1> list;
74 list = new ArrayList<X1>();
77 public List<X1> getList() {
82 private static class X3 {
89 set = new HashSet<X1>();
92 public Set<X1> getSet() {
97 private static class X4 {
101 private Map<String, X1> map;
104 map = new HashMap<String, X1>();
107 public Map<String, X1> getMap() {
112 private static class X5 {
122 public void setArray(X1[] aArray) {
126 public X1[] getArray() {
132 public void testSimple() {
139 JpaMergeSupport.merge(x, y);
141 assertEquals(x.id, y.id);
142 assertEquals(x.version, y.version);
145 @Test(expected = IllegalArgumentException.class)
146 public void testSimplePkMismatch() {
153 JpaMergeSupport.merge(x, y);
157 public void testTraverseList() {
171 y.getList().add(new X1());
172 y.getList().add(new X1());
174 JpaMergeSupport.merge(x, y);
176 assertEquals(x.id, y.id);
177 assertEquals(x.getList().get(0).id, y.getList().get(0).id);
178 assertEquals(x.getList().get(1).id, y.getList().get(1).id);
179 assertEquals(x.getList().get(0).version, y.getList().get(0).version);
180 assertEquals(x.getList().get(1).version, y.getList().get(1).version);
183 @Test(expected = IllegalArgumentException.class)
184 public void testTraverseListWrongSize() {
198 y.getList().add(new X1());
200 JpaMergeSupport.merge(x, y);
204 public void testTraverseSet() {
222 JpaMergeSupport.merge(x, y);
223 assertEquals(x.id, y.id);
224 assertEquals(a.id, ya.id);
225 assertEquals(a.version, ya.version);
226 assertEquals(b.version, yb.version);
229 @Test(expected = IllegalArgumentException.class)
230 public void testTraverseSetWrongSize() {
247 JpaMergeSupport.merge(x, y);
251 public void testTraverseMap() {
260 x.getMap().put("a", a);
261 x.getMap().put("b", b);
267 y.getMap().put("a", ya);
268 y.getMap().put("b", yb);
269 JpaMergeSupport.merge(x, y);
270 assertEquals(x.id, y.id);
271 assertEquals(a.id, ya.id);
272 assertEquals(a.version, ya.version);
273 assertEquals(b.version, yb.version);
277 @Test(expected = IllegalArgumentException.class)
278 public void testTraverseMapWrongKey() {
287 x.getMap().put("a", a);
288 x.getMap().put("b", b);
294 y.getMap().put("a", ya);
295 y.getMap().put("c", yb);
296 JpaMergeSupport.merge(x, y);
299 @Test(expected = IllegalArgumentException.class)
300 public void testTraverseMapWrongSize() {
309 x.getMap().put("a", a);
310 x.getMap().put("b", b);
316 y.getMap().put("a", ya);
317 JpaMergeSupport.merge(x, y);
321 public void testTraverseArray() {
330 x.setArray(new X1[] { a, b });
336 y.setArray(new X1[] { ya, yb });
337 JpaMergeSupport.merge(x, y);
338 assertEquals(x.id, y.id);
339 assertEquals(a.id, ya.id);
340 assertEquals(a.version, ya.version);
341 assertEquals(b.version, yb.version);
344 @Test(expected = IllegalArgumentException.class)
345 public void testTraverseArrayWrongSize() {
354 x.setArray(new X1[] { a, b });
359 y.setArray(new X1[] { ya });
360 JpaMergeSupport.merge(x, y);