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() {
131 private static class X6 {
135 public X1 getNotaGetter(String aMessage) {
139 public void getNotaGetter2() {
144 private static class X7 {
148 private void getX() {
149 fail("Private getters should not be used");
154 public void testSimple() {
161 JpaMergeSupport.merge(x, y);
163 assertEquals(x.id, y.id);
164 assertEquals(x.version, y.version);
167 @Test(expected = IllegalArgumentException.class)
168 public void testSimplePkMismatch() {
175 JpaMergeSupport.merge(x, y);
179 public void testTraverseList() {
193 y.getList().add(new X1());
194 y.getList().add(new X1());
196 JpaMergeSupport.merge(x, y);
198 assertEquals(x.id, y.id);
199 assertEquals(x.getList().get(0).id, y.getList().get(0).id);
200 assertEquals(x.getList().get(1).id, y.getList().get(1).id);
201 assertEquals(x.getList().get(0).version, y.getList().get(0).version);
202 assertEquals(x.getList().get(1).version, y.getList().get(1).version);
205 @Test(expected = IllegalArgumentException.class)
206 public void testTraverseListWrongSize() {
220 y.getList().add(new X1());
222 JpaMergeSupport.merge(x, y);
226 public void testTraverseSet() {
244 JpaMergeSupport.merge(x, y);
245 assertEquals(x.id, y.id);
246 assertEquals(a.id, ya.id);
247 assertEquals(a.version, ya.version);
248 assertEquals(b.version, yb.version);
251 @Test(expected = IllegalArgumentException.class)
252 public void testTraverseSetWrongSize() {
269 JpaMergeSupport.merge(x, y);
273 public void testTraverseMap() {
282 x.getMap().put("a", a);
283 x.getMap().put("b", b);
289 y.getMap().put("a", ya);
290 y.getMap().put("b", yb);
291 JpaMergeSupport.merge(x, y);
292 assertEquals(x.id, y.id);
293 assertEquals(a.id, ya.id);
294 assertEquals(a.version, ya.version);
295 assertEquals(b.version, yb.version);
299 @Test(expected = IllegalArgumentException.class)
300 public void testTraverseMapWrongKey() {
309 x.getMap().put("a", a);
310 x.getMap().put("b", b);
316 y.getMap().put("a", ya);
317 y.getMap().put("c", yb);
318 JpaMergeSupport.merge(x, y);
321 @Test(expected = IllegalArgumentException.class)
322 public void testTraverseMapWrongSize() {
331 x.getMap().put("a", a);
332 x.getMap().put("b", b);
338 y.getMap().put("a", ya);
339 JpaMergeSupport.merge(x, y);
343 public void testTraverseArray() {
352 x.setArray(new X1[] { a, b });
358 y.setArray(new X1[] { ya, yb });
359 JpaMergeSupport.merge(x, y);
360 assertEquals(x.id, y.id);
361 assertEquals(a.id, ya.id);
362 assertEquals(a.version, ya.version);
363 assertEquals(b.version, yb.version);
366 @Test(expected = IllegalArgumentException.class)
367 public void testTraverseArrayWrongSize() {
376 x.setArray(new X1[] { a, b });
381 y.setArray(new X1[] { ya });
382 JpaMergeSupport.merge(x, y);
386 public void testNotAGetter() {
391 JpaMergeSupport.merge(x,y);
392 assertEquals(x.id, y.id);
396 public void testPrivateGetter() {
400 JpaMergeSupport.merge(x,y);
401 assertEquals(x.id, y.id);