--- /dev/null
+/*
+ * 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.wicket.inject;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.apache.wicket.Component;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.inject.Injector;
+import org.wamblee.inject.InjectorBuilder;
+import org.wamblee.inject.InjectorFactory;
+
+import static org.mockito.Mockito.*;
+
+public class InjectionBehaviorTest {
+
+ Component component;
+ Injector injector;
+ InjectorFactory injectorFactory;
+
+ @Before
+ public void setUp() {
+ component = mock(Component.class);
+ injector = mock(Injector.class);
+ injectorFactory = mock(InjectorFactory.class);
+ when(injectorFactory.create(any(Class.class))).thenReturn(injector);
+ InjectorBuilder.setInjectorFactory(injectorFactory);
+ }
+
+ @After
+ public void tearDown() {
+ InjectorBuilder.setInjectorFactory(null);
+ }
+
+ @Test
+ public void testNoInjectionInitially() {
+ InjectionBehavior behavior = new InjectionBehavior();
+ behavior.beforeRender(component);
+ verifyNoMoreInteractions(injector);
+ }
+
+ @Test
+ public void testInjectOnlyOnceAfterDeserialisation() throws Exception {
+ InjectionBehavior behavior = new InjectionBehavior();
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ObjectOutputStream os = new ObjectOutputStream(bos);
+ os.writeObject(behavior);
+ os.flush();
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+ ObjectInputStream ois = new ObjectInputStream(bis);
+ behavior = (InjectionBehavior)ois.readObject();
+
+ behavior.beforeRender(component);
+ verify(injector).inject(same(component));
+ reset(injector);
+ behavior.beforeRender(component);
+ verifyNoMoreInteractions(injector);
+
+ }
+}