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 org.mockito.Mockito.*;
20 import org.junit.Before;
21 import org.junit.Test;
23 import static junit.framework.TestCase.*;
25 public class AbstractDetachableTest {
27 public static class X {
31 public static interface DetachableMethods {
32 Integer getReference(X aObject);
34 X load(Integer aReference);
37 public class MyDetachable extends AbstractDetachable<X, Integer> {
39 public MyDetachable(X aObj) {
44 protected Integer getReference(X aObject) {
45 return methods.getReference(aObject);
49 protected X load(Integer aReference) {
50 return methods.load(aReference);
55 private DetachableMethods methods;
56 private AbstractDetachable<X, Integer> detachable;
61 methods = mock(DetachableMethods.class);
64 @Test(expected = IllegalArgumentException.class)
65 public void testNullPassedIn() {
66 detachable = new MyDetachable(null);
70 public void testDetach() {
73 when(methods.getReference(any(X.class))).thenReturn(ref);
74 detachable = new MyDetachable(x);
75 verifyNoMoreInteractions(methods);
77 verify(methods).getReference(same(x));
78 assertNull(detachable.getObject());
79 assertEquals(ref, detachable.getReference());
83 public void testDetachTwice() {
88 verifyNoMoreInteractions(methods);
91 @Test(expected = IllegalStateException.class)
92 public void testDetachWithNullReference() {
95 when(methods.getReference(any(X.class))).thenReturn(null);
96 detachable = new MyDetachable(x);
97 verifyNoMoreInteractions(methods);
102 public void testReattach() {
106 X x2 = new X(); // the new X loaded from persistent storage.
107 when(methods.load(detachable.getReference())).thenReturn(x2);
109 X x3 = detachable.get();
110 verify(methods).load(100);
113 assertSame(x2, detachable.getObject());
117 public void testGetInvokedTwiceNoLoads() {
122 verifyNoMoreInteractions(methods);