1   package com.atlassian.spring.extension.registration;
2   
3   import junit.framework.TestCase;
4   import com.mockobjects.dynamic.Mock;
5   import com.mockobjects.dynamic.C;
6   import org.springframework.beans.factory.BeanFactory;
7   
8   public class KeyValueRegistrationTest extends TestCase
9   {
10      public void testNormalOperation() throws RegistrationException
11      {
12          testSuccessfulRegistration(new ThingToRegister());
13      }
14  
15      public void testMatchesSubclass() throws RegistrationException
16      {
17          testSuccessfulRegistration(new SubThingToRegister());
18      }
19  
20      public void testTargetBeanNotFound()
21      {
22          Object bean = new ThingToRegister();
23  
24          Mock mockBeanFactory = new Mock(BeanFactory.class);
25  
26          mockBeanFactory.matchAndReturn("getBean", "target1", null);
27          mockBeanFactory.matchAndReturn("getBean", "bean1", bean);
28  
29          KeyValueRegistration registration = new KeyValueRegistration("target1", "key1", "bean1", "methodWithNoKey");
30  
31          try
32          {
33              registration.register((BeanFactory) mockBeanFactory.proxy());
34              fail("Expected registration exception");
35          }
36          catch (RegistrationException e)
37          {
38              // expected
39          }
40      }
41  
42      public void testSourceBeanNotFound()
43      {
44          Mock mockTarget = new Mock(ThingToRegisterWith.class);
45          Mock mockBeanFactory = new Mock(BeanFactory.class);
46  
47          mockBeanFactory.matchAndReturn("getBean", "target1", mockTarget.proxy());
48          mockBeanFactory.matchAndReturn("getBean", "bean1", null);
49  
50          KeyValueRegistration registration = new KeyValueRegistration("target1", "key1", "bean1", "methodWithNoKey");
51  
52          try
53          {
54              registration.register((BeanFactory) mockBeanFactory.proxy());
55              fail("Expected registration exception");
56          }
57          catch (RegistrationException e)
58          {
59              // expected
60          }
61      }
62  
63      public void testMethodWithWrongArgs()
64      {
65          Object bean = new ThingToRegister();
66  
67          Mock mockTarget = new Mock(ThingToRegisterWith.class);
68          Mock mockBeanFactory = new Mock(BeanFactory.class);
69  
70          mockBeanFactory.matchAndReturn("getBean", "target1", mockTarget.proxy());
71          mockBeanFactory.matchAndReturn("getBean", "bean1", bean);
72  
73          KeyValueRegistration registration = new KeyValueRegistration("target1", "key1", "bean1", "methodWithNoKey");
74  
75          try
76          {
77              registration.register((BeanFactory) mockBeanFactory.proxy());
78              fail("Expected registration exception");
79          }
80          catch (RegistrationException e)
81          {
82              // expected
83          }
84      }
85  
86      public void testMethodWithWrongClass()
87      {
88          Object bean = new ThingToRegister();
89  
90          Mock mockTarget = new Mock(ThingToRegisterWith.class);
91          Mock mockBeanFactory = new Mock(BeanFactory.class);
92  
93          mockBeanFactory.matchAndReturn("getBean", "target1", mockTarget.proxy());
94          mockBeanFactory.matchAndReturn("getBean", "bean1", bean);
95  
96          try
97          {
98              KeyValueRegistration registration = new KeyValueRegistration("target1", "key1", "bean1", "methodWithWrongType");
99              registration.register((BeanFactory) mockBeanFactory.proxy());
100             fail("Expected registration exception");
101         }
102         catch (RegistrationException e)
103         {
104             // expected
105         }
106     }
107 
108     private void testSuccessfulRegistration(Object bean) throws RegistrationException
109     {
110         Mock mockTarget = new Mock(ThingToRegisterWith.class);
111         Mock mockBeanFactory = new Mock(BeanFactory.class);
112 
113         mockBeanFactory.matchAndReturn("getBean", "target1", mockTarget.proxy());
114         mockBeanFactory.matchAndReturn("getBean", "bean1", bean);
115 
116         mockTarget.expect("addThingToRegister", C.args(C.eq("key1"), C.same(bean)));
117 
118         KeyValueRegistration registration = new KeyValueRegistration("target1", "key1", "bean1", "addThingToRegister");
119         registration.register((BeanFactory) mockBeanFactory.proxy());
120         mockTarget.verify();
121     }
122 }