1 package com.atlassian.plugin.osgi.factory;
2
3 import com.atlassian.plugin.Application;
4 import com.atlassian.plugin.JarPluginArtifact;
5 import com.atlassian.plugin.event.PluginEventManager;
6 import com.atlassian.plugin.factories.PluginFactory;
7 import com.atlassian.plugin.osgi.container.OsgiContainerManager;
8 import com.atlassian.plugin.osgi.container.OsgiPersistentCache;
9 import com.atlassian.plugin.test.PluginJarBuilder;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.Parameterized;
13 import org.junit.runners.Parameterized.Parameter;
14 import org.junit.runners.Parameterized.Parameters;
15 import org.osgi.framework.Constants;
16
17 import javax.annotation.Nonnull;
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.TimeUnit;
26
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.notNullValue;
29 import static org.hamcrest.Matchers.nullValue;
30 import static org.mockito.Mockito.mock;
31
32
33
34
35
36
37
38
39
40
41
42 @RunWith(Parameterized.class)
43 public class TestPluginFactorySelection {
44 private static final String OSGI_BUNDLE = "OsgiBundleFactory";
45 private static final String OSGI_PLUGIN = "OsgiPluginFactory";
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 @Parameters(name = "test({0},{1},{2},{3})={4}")
81 public static Collection<Object[]> data() {
82 return Arrays.asList(new Object[][]{
83
84
85 {false, false, true, false, OSGI_BUNDLE},
86 {true, false, true, false, OSGI_BUNDLE},
87 {false, true, true, false, OSGI_PLUGIN},
88 {true, true, true, false, OSGI_PLUGIN},
89 {false, false, true, true, OSGI_BUNDLE},
90 {true, false, true, true, OSGI_PLUGIN},
91 {false, true, true, true, OSGI_BUNDLE},
92 {true, true, true, true, OSGI_PLUGIN},
93
94 {false, false, false, false, null},
95 {true, false, false, false, null},
96 {false, true, false, false, OSGI_PLUGIN},
97 {true, true, false, false, OSGI_PLUGIN},
98 {false, false, false, true, OSGI_BUNDLE},
99 {true, false, false, true, OSGI_PLUGIN},
100 {false, true, false, true, OSGI_BUNDLE},
101 {true, true, false, true, OSGI_PLUGIN},
102 });
103 }
104
105 private final
106 @Nonnull
107 OsgiPluginFactory opFactory;
108 private final
109 @Nonnull
110 OsgiBundleFactory obFactory;
111
112 @Parameter(value = 0)
113 public boolean hasSpring;
114
115 @Parameter(value = 1)
116 public boolean isPlugin;
117
118 @Parameter(value = 2)
119 public boolean isOsgiBundle;
120
121 @Parameter(value = 3)
122 public boolean isTransformless;
123
124 @Parameter(value = 4)
125 public String expected;
126
127 public TestPluginFactorySelection() {
128 OsgiContainerManager osgi = mock(OsgiContainerManager.class);
129 this.opFactory = new OsgiPluginFactory(
130 "atlassian-plugin.xml",
131 Collections.<Application>emptySet(),
132 mock(OsgiPersistentCache.class),
133 osgi,
134 mock(PluginEventManager.class)
135 );
136 this.obFactory = new OsgiBundleFactory(osgi);
137 }
138
139 @Test
140 public void verificator() throws IOException {
141
142 Map<String, String> manifest = new HashMap<>();
143 manifest.put(Constants.BUNDLE_NAME, "TestPlugin");
144 manifest.put(Constants.BUNDLE_VENDOR, "Cool Tests Inc.");
145 manifest.put(Constants.BUNDLE_DESCRIPTION, "Test plugin");
146
147 if (isOsgiBundle || isTransformless) {
148 manifest.put(Constants.BUNDLE_VERSION, "1.0.0");
149 manifest.put(Constants.BUNDLE_SYMBOLICNAME, "test-plugin");
150 }
151
152 if (isTransformless) {
153 manifest.put("Atlassian-Plugin-Key", "test-plugin");
154 }
155
156 if (hasSpring) {
157 manifest.put("Spring-Context", "*");
158 }
159
160
161
162
163 PluginJarBuilder builder = new PluginJarBuilder("test-plugin.jar").manifest(manifest);
164 if (isPlugin) {
165 builder.addPluginInformation("test-plugin", "Test Plugin", "1.0.0");
166 }
167
168
169 File bundleJar = builder.build();
170 if (!bundleJar.setLastModified(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1))) {
171 throw new IOException("Test broken, cannot backdate bundleJar '" + bundleJar + "'");
172 }
173
174
175 JarPluginArtifact ar = new JarPluginArtifact(bundleJar);
176 if (expected == null) {
177 assertThat("OsgiPlugin", opFactory.canCreate(ar), nullValue());
178 assertThat("OsgiBundle", obFactory.canCreate(ar), nullValue());
179 } else if (expected == OSGI_PLUGIN) {
180 assertThat("OsgiPlugin", opFactory.canCreate(ar), notNullValue());
181 assertThat("OsgiBundle", obFactory.canCreate(ar), nullValue());
182 } else if (expected == OSGI_BUNDLE) {
183 assertThat("OsgiPlugin", opFactory.canCreate(ar), nullValue());
184 assertThat("OsgiBundle", obFactory.canCreate(ar), notNullValue());
185 }
186 }
187 }