(no commit message)
[utils] / wicket / joe / src / test / java / org / wamblee / wicket / inject / InjectionBehaviorTest.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.wicket.inject;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22
23 import org.apache.wicket.Component;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.wamblee.general.ObjectSerializationUtils;
28 import org.wamblee.inject.Injector;
29 import org.wamblee.inject.InjectorBuilder;
30 import org.wamblee.inject.InjectorFactory;
31
32 import static org.mockito.Mockito.*;
33
34 public class InjectionBehaviorTest {
35
36     private Component component;
37     private Injector injector;
38     private InjectorFactory injectorFactory;
39
40     @Before
41     public void setUp() {
42         component = mock(Component.class);
43         injector = mock(Injector.class);
44         injectorFactory = mock(InjectorFactory.class);
45         when(injectorFactory.create(any(Class.class))).thenReturn(injector);
46         InjectorBuilder.setInjectorFactory(injectorFactory);
47     }
48
49     @After
50     public void tearDown() {
51         InjectorBuilder.setInjectorFactory(null);
52     }
53
54     @Test
55     public void testNoInjectionInitially() {
56         InjectionBehavior behavior = new InjectionBehavior();
57         behavior.beforeRender(component);
58         verifyNoMoreInteractions(injector);
59     }
60
61     @Test
62     public void testInjectOnlyOnceAfterDeserialisation() throws Exception {
63         InjectionBehavior behavior = new InjectionBehavior();
64
65         behavior = ObjectSerializationUtils.deserialize(
66             ObjectSerializationUtils.serialize(behavior),
67             InjectionBehavior.class);
68
69         behavior.beforeRender(component);
70         verify(injector).inject(same(component));
71         reset(injector);
72         behavior.beforeRender(component);
73         verifyNoMoreInteractions(injector);
74
75     }
76 }