1   package com.atlassian.plugin.osgi.hostcomponents.impl;
2   
3   import junit.framework.TestCase;
4   
5   import java.io.Serializable;
6   import java.util.Dictionary;
7   import java.util.Hashtable;
8   import java.util.Arrays;
9   
10  import org.osgi.framework.BundleContext;
11  import com.mockobjects.dynamic.Mock;
12  import com.mockobjects.dynamic.C;
13  import com.mockobjects.constraint.Constraint;
14  import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
15  import com.atlassian.plugin.hostcontainer.HostContainer;
16  
17  public class TestDefaultComponentRegistrar extends TestCase
18  {
19      public void testRegister()
20      {
21          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
22          Class[] ifs = new Class[]{Serializable.class};
23          registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
24          HostComponentRegistration reg =registrar.getRegistry().get(0);
25  
26          assertNotNull(reg);
27          assertEquals("Foo", reg.getInstance());
28          assertEquals(Serializable.class.getName(), reg.getMainInterfaces()[0]);
29          assertEquals("foo", reg.getProperties().get(DefaultPropertyBuilder.BEAN_NAME));
30          assertEquals("bar", reg.getProperties().get("jim"));
31      }
32  
33      public void testRegisterMultiple()
34      {
35          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
36          Class[] ifs = new Class[]{Serializable.class};
37          registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
38          registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("sarah", "bar");
39          assertEquals(2, registrar.getRegistry().size());
40      }
41  
42      public void testRegisterOnlyInterfaces()
43      {
44          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
45          Class[] ifs = new Class[]{Object.class};
46          try
47          {
48              registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
49              fail("Should have failed");
50          }
51          catch (IllegalArgumentException ex)
52          {
53              // very good...
54          }
55      }
56  
57      public void testWriteRegistry()
58      {
59          Class[] ifs = new Class[]{Serializable.class};
60          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
61          registrar.register(ifs).forInstance("Foo").withName("foo");
62  
63          Mock mockBundleContext = new Mock(BundleContext.class);
64          registerInMock(mockBundleContext, ifs, "Foo", "foo");
65  
66          registrar.writeRegistry((BundleContext) mockBundleContext.proxy());
67  
68          mockBundleContext.verify();
69      }
70  
71      public void testWriteRegistryRemovesHostContainer()
72      {
73          Class[] ifs = new Class[]{HostContainer.class};
74          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
75          registrar.register(ifs).forInstance("Foo").withName("foo");
76  
77          Mock mockBundleContext = new Mock(BundleContext.class);
78  
79          registrar.writeRegistry((BundleContext) mockBundleContext.proxy());
80  
81          mockBundleContext.verify();
82          assertEquals(0, registrar.getRegistry().size());
83      }
84  
85      public void testWriteRegistryNoInterface()
86      {
87          Class[] ifs = new Class[]{};
88          DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
89          registrar.register(ifs).forInstance("Foo").withName("foo");
90  
91          Mock mockBundleContext = new Mock(BundleContext.class);
92          registerInMock(mockBundleContext, ifs, "Foo", "foo");
93  
94          registrar.writeRegistry((BundleContext) mockBundleContext.proxy());
95  
96          mockBundleContext.verify();
97      }
98  
99      public void testWriteRegistryGenBeanName()
100     {
101         Class[] ifs = new Class[]{Serializable.class};
102         DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
103         registrar.register(ifs).forInstance("Foo");
104 
105         Mock mockBundleContext = new Mock(BundleContext.class);
106         registerInMock(mockBundleContext, ifs, "Foo", "hostComponent-" + Arrays.asList(registrar.getRegistry().get(0).getMainInterfaces()).hashCode());
107 
108         registrar.writeRegistry((BundleContext) mockBundleContext.proxy());
109 
110         mockBundleContext.verify();
111     }
112 
113 
114     private void registerInMock(Mock mockBundleContext, Class[] ifs, Object instance, String name)
115     {
116         Dictionary<String,String> properties = new Hashtable<String,String>();
117         properties.put(DefaultPropertyBuilder.BEAN_NAME, name);
118         properties.put(DefaultComponentRegistrar.HOST_COMPONENT_FLAG, "true");
119 
120         mockBundleContext.expect("registerService", C.args(isEqInterfaceList(ifs),
121                                                            C.eq(instance),
122                                                            C.eq(properties)));
123     }
124 
125     static Constraint isEqInterfaceList(Class[] array)
126     {
127         return new ArrayConstraint(array);
128     }
129 
130     static class ArrayConstraint implements Constraint
131     {
132         private Class[] expected;
133 
134         public ArrayConstraint(Class[] expected) {this.expected = expected;}
135 
136         public boolean eval(Object o)
137         {
138             if (o != null)
139             {
140                 String[] totest = (String[])o;
141                 if (expected.length == totest.length)
142                 {
143                     boolean fail = false;
144                     for (int x=0; x<expected.length; x++)
145                     {
146                         if (!expected[x].getName().equals(totest[x]))
147                         {
148                             fail = true;
149                             break;
150                         }
151                     }
152                     if (!fail)
153                         return true;
154                 }
155 
156             }
157             return false;
158         }
159     }
160 
161 }