(no commit message)
[utils] / support / general / src / test / java / org / wamblee / persistence / JpaDetachableTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.persistence;
17
18 import static org.mockito.Mockito.*;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.Id;
22
23 import org.junit.Before;
24 import org.junit.Test;
25
26 import static junit.framework.TestCase.*;
27
28 public class JpaDetachableTest {
29
30     public class X {
31         @Id
32         private int id;
33
34     }
35
36     private EntityManager em;
37
38     @Before
39     public void setUp() {
40         em = mock(EntityManager.class);
41     }
42
43     @Test
44     public void testDetachAttach() {
45         X x = new X();
46         x.id = 100;
47
48         JpaDetachable<X> d = new JpaDetachable<X>(em, x);
49         assertNull(d.getReference());
50         d.detach();
51         assertEquals(100, d.getReference());
52
53         X x2 = new X();
54         x2.id = 100;
55         when(em.find(X.class, 100)).thenReturn(x2);
56         X x3 = d.get();
57         assertSame(x2, x3);
58         verify(em).find(X.class, 100);
59
60     }
61 }