/* * Copyright 2005-2010 the original author or authors. * * 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 org.wamblee.persistence; import static org.mockito.Mockito.*; import javax.persistence.EntityManager; import javax.persistence.Id; import org.junit.Before; import org.junit.Test; import static junit.framework.TestCase.*; public class JpaDetachableTest { public class X { @Id private int id; } private EntityManager em; @Before public void setUp() { em = mock(EntityManager.class); } @Test public void testDetachAttach() { X x = new X(); x.id = 100; JpaDetachable d = new JpaDetachable(em, x); assertNull(d.getReference()); d.detach(); assertEquals(100, d.getReference()); X x2 = new X(); x2.id = 100; when(em.find(X.class, 100)).thenReturn(x2); X x3 = d.get(); assertSame(x2, x3); verify(em).find(X.class, 100); } }