1 package com.atlassian.plugin.osgi.hostcomponents.impl;
2
3 import com.atlassian.plugin.hostcontainer.HostContainer;
4 import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.runners.MockitoJUnitRunner;
8 import org.osgi.framework.BundleContext;
9
10 import java.io.Serializable;
11 import java.util.Arrays;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14
15 import static org.hamcrest.Matchers.equalTo;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.mockito.Matchers.argThat;
19 import static org.mockito.Mockito.eq;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyZeroInteractions;
23
24 @RunWith(MockitoJUnitRunner.class)
25 public class TestDefaultComponentRegistrar
26 {
27 @Test
28 public void testRegister()
29 {
30 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
31 Class[] ifs = new Class[]{Serializable.class};
32 registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
33 HostComponentRegistration reg =registrar.getRegistry().get(0);
34
35 assertNotNull(reg);
36 assertEquals("Foo", reg.getInstance());
37 assertEquals(Serializable.class.getName(), reg.getMainInterfaces()[0]);
38 assertEquals("foo", reg.getProperties().get(DefaultPropertyBuilder.BEAN_NAME));
39 assertEquals("bar", reg.getProperties().get("jim"));
40 }
41
42 @Test
43 public void testRegisterMultiple()
44 {
45 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
46 Class[] ifs = new Class[]{Serializable.class};
47 registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
48 registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("sarah", "bar");
49 assertEquals(2, registrar.getRegistry().size());
50 }
51
52 @Test(expected = IllegalArgumentException.class)
53 public void testRegisterOnlyInterfaces()
54 {
55 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
56 Class[] ifs = new Class[]{Object.class};
57 registrar.register(ifs).forInstance("Foo").withName("foo").withProperty("jim", "bar");
58 }
59
60 @Test
61 public void testWriteRegistry()
62 {
63 Class[] ifs = new Class[]{Serializable.class};
64 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
65 registrar.register(ifs).forInstance("Foo").withName("foo");
66
67 BundleContext ctx = mock(BundleContext.class);
68 registrar.writeRegistry(ctx);
69
70 verifyRegistration(ctx, ifs, "Foo", "foo");
71 }
72
73 @Test
74 public void testWriteRegistryRemovesHostContainer()
75 {
76 Class[] ifs = new Class[]{HostContainer.class};
77 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
78 registrar.register(ifs).forInstance("Foo").withName("foo");
79
80 BundleContext ctx = mock(BundleContext.class);
81 registrar.writeRegistry(ctx);
82
83 verifyZeroInteractions(ctx);
84 assertEquals(0, registrar.getRegistry().size());
85 }
86
87 @Test
88 public void testWriteRegistryNoInterface()
89 {
90 Class[] ifs = new Class[]{};
91 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
92 registrar.register(ifs).forInstance("Foo").withName("foo");
93
94 BundleContext ctx = mock(BundleContext.class);
95 registrar.writeRegistry(ctx);
96
97 verifyRegistration(ctx, ifs, "Foo", "foo");
98 }
99
100 @Test
101 public void testWriteRegistryGenBeanName()
102 {
103 Class[] ifs = new Class[]{Serializable.class};
104 DefaultComponentRegistrar registrar = new DefaultComponentRegistrar();
105 registrar.register(ifs).forInstance("Foo");
106
107 BundleContext ctx = mock(BundleContext.class);
108 registrar.writeRegistry(ctx);
109
110 verifyRegistration(ctx, ifs, "Foo", "hostComponent-" + Arrays.asList(registrar.getRegistry().get(0).getMainInterfaces()).hashCode());
111 }
112
113 private void verifyRegistration(BundleContext ctx, Class[] ifs, Object instance, String name)
114 {
115 Dictionary<String,String> properties = new Hashtable<String,String>();
116 properties.put(DefaultPropertyBuilder.BEAN_NAME, name);
117 properties.put(DefaultComponentRegistrar.HOST_COMPONENT_FLAG, "true");
118
119
120
121 verify(ctx).registerService(eq(toClassNames(ifs)), argThat(equalTo(instance)), eq(properties));
122 }
123
124 private static String[] toClassNames(Class[] classes)
125 {
126 String[] classNames = new String[classes.length];
127 for (int i = 0; i < classNames.length; i++)
128 {
129 classNames[i] = classes[i].getName();
130 }
131 return classNames;
132 }
133
134 }