4a2fef21c8df962e8aa7ffdf738073244aa93bbf
[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.inject.Injector;
28 import org.wamblee.inject.InjectorBuilder;
29 import org.wamblee.inject.InjectorFactory;
30
31 import static org.mockito.Mockito.*;
32
33 public class InjectionBehaviorTest {
34
35     Component component;
36     Injector injector;
37     InjectorFactory injectorFactory;
38
39     @Before
40     public void setUp() {
41         component = mock(Component.class);
42         injector = mock(Injector.class);
43         injectorFactory = mock(InjectorFactory.class);
44         when(injectorFactory.create(any(Class.class))).thenReturn(injector);
45         InjectorBuilder.setInjectorFactory(injectorFactory);
46     }
47
48     @After
49     public void tearDown() {
50         InjectorBuilder.setInjectorFactory(null);
51     }
52
53     @Test
54     public void testNoInjectionInitially() {
55         InjectionBehavior behavior = new InjectionBehavior();
56         behavior.beforeRender(component); 
57         verifyNoMoreInteractions(injector);
58     }
59     
60     @Test
61     public void testInjectOnlyOnceAfterDeserialisation() throws Exception { 
62         InjectionBehavior behavior = new InjectionBehavior();
63         
64         ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
65         ObjectOutputStream os = new ObjectOutputStream(bos); 
66         os.writeObject(behavior);
67         os.flush();
68         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
69         ObjectInputStream ois = new ObjectInputStream(bis); 
70         behavior = (InjectionBehavior)ois.readObject();
71         
72         behavior.beforeRender(component); 
73         verify(injector).inject(same(component));
74         reset(injector);
75         behavior.beforeRender(component); 
76         verifyNoMoreInteractions(injector);
77
78     }
79 }