1 package com.atlassian.plugin.osgi.util;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.Dictionary;
11 import java.util.HashMap;
12 import java.util.Hashtable;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.jar.JarFile;
17 import java.util.jar.Manifest;
18
19 import com.atlassian.plugin.JarPluginArtifact;
20 import com.atlassian.plugin.PluginArtifact;
21 import com.atlassian.plugin.PluginParseException;
22 import com.atlassian.plugin.osgi.factory.OsgiPlugin;
23 import com.atlassian.plugin.osgi.factory.transform.StubHostComponentRegistration;
24 import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
25 import com.atlassian.plugin.osgi.hostcomponents.impl.MockRegistration;
26 import com.atlassian.plugin.test.PluginJarBuilder;
27
28 import com.google.common.collect.Sets;
29
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.Constants;
35 import org.osgi.framework.Version;
36
37 import static junit.framework.Assert.assertEquals;
38 import static junit.framework.Assert.assertFalse;
39 import static junit.framework.Assert.assertTrue;
40 import static org.hamcrest.Matchers.notNullValue;
41 import static org.hamcrest.Matchers.nullValue;
42 import static org.hamcrest.core.Is.is;
43 import static org.hamcrest.core.IsEqual.equalTo;
44 import static org.junit.Assert.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47
48 public class TestOsgiHeaderUtil
49 {
50 @Test(expected = NullPointerException.class)
51 public void shouldAssertValueShouldNotBeNull()
52 {
53 Manifest manifest = new Manifest();
54 OsgiHeaderUtil.getValidatedAttribute(manifest, "some-random-key");
55 }
56
57 @Test(expected = IllegalArgumentException.class)
58 public void shouldAssertValueShouldNotBeEmpty() throws IOException
59 {
60 Manifest manifest = generateManifest();
61 OsgiHeaderUtil.getValidatedAttribute(manifest, "Some-other");
62 }
63
64 @Test(expected = IllegalArgumentException.class)
65 public void shouldRaiseAnExceptionWhenValueIsMissing() throws IOException
66 {
67 Manifest manifest = generateManifest();
68 OsgiHeaderUtil.getNonEmptyAttribute(manifest,"Some-other");
69 }
70
71 @Test
72 public void shouldGetValidatedAttribute() throws IOException
73 {
74 Manifest manifestFile = generateManifest();
75 assertThat(OsgiHeaderUtil.getValidatedAttribute(manifestFile, "Import-Package"), equalTo("javax.swing"));
76 }
77
78 @Test
79 public void shouldGetEmptyAttributeWithoutValidation() throws IOException
80 {
81 Manifest manifest = generateManifest();
82 assertThat(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "Some-other"), is(""));
83 }
84
85 @Test
86 public void shouldGetNullAttributeWithoutValidation() throws IOException
87 {
88 Manifest manifest = generateManifest();
89 Assert.assertNull(OsgiHeaderUtil.getAttributeWithoutValidation(manifest, "some-random"));
90 }
91
92 private Manifest generateManifest() throws IOException
93 {
94 File bundle = new PluginJarBuilder("someplugin")
95 .addResource("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" +
96 "Import-Package: javax.swing\n" +
97 "Bundle-SymbolicName: my.foo.symbolicName\n" +
98 "Some-other: \n" +
99 "Bundle-Version: 1.0\n")
100 .build();
101 JarPluginArtifact jarPluginArtifact = new JarPluginArtifact(bundle);
102 InputStream descriptorClassStream = jarPluginArtifact.getResourceAsStream("META-INF/MANIFEST.MF");
103 return new Manifest(descriptorClassStream);
104 }
105
106 @Test
107 public void testFindReferredPackages() throws IOException
108 {
109 Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(new ArrayList<HostComponentRegistration>()
110 {{
111 add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
112 }});
113
114 assertTrue(foundPackages.contains(HostComponentRegistration.class.getPackage().getName()));
115 }
116
117 @Test
118 public void testFindReferredPackagesMustNotReturnJavaPackages() throws IOException
119 {
120 List<HostComponentRegistration> regs = Arrays.<HostComponentRegistration>asList(new MockRegistration(Mockito.mock(DummyClass.class), DummyClass.class));
121
122 Set<String> foundPackages = OsgiHeaderUtil.findReferredPackageNames(regs);
123
124 assertFalse(foundPackages.contains("java.lang"));
125 }
126
127 @Test
128 public void testFindReferredPackagesWithVersion() throws IOException
129 {
130 Map<String, String> foundPackages = OsgiHeaderUtil.findReferredPackageVersions(new ArrayList<HostComponentRegistration>()
131 {{
132 add(new StubHostComponentRegistration(OsgiHeaderUtil.class));
133 }}, Collections.singletonMap(HostComponentRegistration.class.getPackage().getName(), "1.0.45"));
134
135 assertTrue(foundPackages.containsKey(HostComponentRegistration.class.getPackage().getName()));
136 assertEquals(foundPackages.get(HostComponentRegistration.class.getPackage().getName()), "1.0.45");
137 }
138
139 @Test
140 public void testGetPluginKeyBundle()
141 {
142 Dictionary headers = new Hashtable();
143 headers.put(Constants.BUNDLE_VERSION, "1.0");
144 headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
145
146 Bundle bundle = mock(Bundle.class);
147 when(bundle.getSymbolicName()).thenReturn("foo");
148 when(bundle.getHeaders()).thenReturn(headers);
149
150 assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(bundle));
151
152 headers.put(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
153 assertEquals("bar", OsgiHeaderUtil.getPluginKey(bundle));
154 }
155
156 @Test
157 public void testGetPluginKeyManifest()
158 {
159 Manifest mf = new Manifest();
160 mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.0");
161 mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "foo");
162
163 assertEquals("foo-1.0", OsgiHeaderUtil.getPluginKey(mf));
164
165 mf.getMainAttributes().putValue(OsgiPlugin.ATLASSIAN_PLUGIN_KEY, "bar");
166 assertEquals("bar", OsgiHeaderUtil.getPluginKey(mf));
167 }
168
169 @Test
170 public void testGeneratePackageVersionString()
171 {
172 Map<String, String> input = new HashMap<String, String>();
173 input.put("foo.bar", "1.2");
174 input.put("foo.baz", Version.emptyVersion.toString());
175
176 String output = OsgiHeaderUtil.generatePackageVersionString(input);
177
178 Set<String> set = Sets.newHashSet(output.split("[,]"));
179
180 assertTrue(set.contains("foo.bar;version=1.2"));
181 assertTrue(set.contains("foo.baz"));
182 assertEquals(2, set.size());
183 }
184
185 @Test
186 public void getManifestGetsManifest()
187 {
188 final String manifestString = "Manifest-Version: 1.0\r\nSome-Header: some value\r\n";
189 final PluginArtifact pluginArtifact = getPluginArtifactWithManifest(manifestString);
190 final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
191 assertThat(manifest, notNullValue());
192 assertThat(manifest.getMainAttributes().getValue("Some-Header"), is("some value"));
193 }
194
195 @Test
196 public void getManifestReturnsNullIfManifestIsMissing()
197 {
198 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
199
200 final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
201 assertThat(manifest, nullValue());
202 }
203
204 @Test
205 public void getManifestReturnsNullIfGetResourceAsStreamFails()
206 {
207 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
208 when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME))
209 .thenThrow(new PluginParseException("Intentional test fail"));
210 final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
211 assertThat(manifest, nullValue());
212 }
213
214 @Test
215 public void getManifestReturnsNullIfResourceStreamThrowsWhenRead() throws Exception
216 {
217 final InputStream inputStream = new InputStream()
218 {
219 @Override
220 public int read() throws IOException
221 {
222 throw new IOException("Intentional test fail");
223 }
224 };
225 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
226 when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME)).thenReturn(inputStream);
227 final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
228 assertThat(manifest, nullValue());
229 }
230
231 @Test
232 public void getManifestReturnsNullIfManifestIsCorrupt()
233 {
234
235 final String manifestString = "Manifest-Version: 1.0\r\nSome-Header:some value\r\n";
236 final PluginArtifact pluginArtifact = getPluginArtifactWithManifest(manifestString);
237 final Manifest manifest = OsgiHeaderUtil.getManifest(pluginArtifact);
238 assertThat(manifest, nullValue());
239 }
240
241 private PluginArtifact getPluginArtifactWithManifest(final String manifestString)
242 {
243 final PluginArtifact pluginArtifact = mock(PluginArtifact.class);
244 final ByteArrayInputStream manifestStream = new ByteArrayInputStream(manifestString.getBytes());
245 when(pluginArtifact.getResourceAsStream(JarFile.MANIFEST_NAME)).thenReturn(manifestStream);
246 return pluginArtifact;
247 }
248
249 private static interface DummyClass
250 {
251 void doSomething(Object obj);
252 }
253
254 }