blob: ceefecb5a302aee4517bd866f59e80a2e1a1a5bf [file] [log] [blame]
Marcel Offermans39e2e002012-02-07 19:18:43 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.deployment.rp.autoconf;
20
21import java.io.ByteArrayInputStream;
22import java.io.File;
23import java.io.IOException;
24import java.util.Dictionary;
25import java.util.Properties;
26
27import junit.framework.TestCase;
28
29import org.apache.felix.dm.Component;
30import org.apache.felix.dm.DependencyManager;
31import org.osgi.framework.Bundle;
32import org.osgi.framework.BundleContext;
33import org.osgi.framework.Filter;
34import org.osgi.framework.InvalidSyntaxException;
35import org.osgi.framework.ServiceReference;
36import org.osgi.service.cm.Configuration;
37import org.osgi.service.cm.ConfigurationAdmin;
38import org.osgi.service.deploymentadmin.DeploymentPackage;
39import org.osgi.service.deploymentadmin.spi.DeploymentSession;
40import org.osgi.service.log.LogService;
41
42public class AutoConfResourceProcessorTest extends TestCase {
43 /** Make sure the processor does not accept a 'null' session. */
44 public void testNullSession() throws Exception {
45 AutoConfResourceProcessor p = new AutoConfResourceProcessor();
46 try {
47 p.begin(null);
48 fail("Should have gotten an exception when trying to begin with null session.");
49 }
50 catch (Exception e) {
51 // expected
52 }
53 }
54
55 /** Go through a simple session, containing two empty configurations. */
56 public void testSimpleSession() throws Exception {
57 AutoConfResourceProcessor p = new AutoConfResourceProcessor();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +000058 Utils.configureObject(p, LogService.class);
Marcel Offermans37fa7732013-11-15 12:01:17 +000059 Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) {
60 public void remove(Component service) {
61 }
62 });
Marcel Offermans39e2e002012-02-07 19:18:43 +000063 File tempDir = File.createTempFile("persistence", "dir");
64 tempDir.delete();
65 tempDir.mkdirs();
66
67 System.out.println("Temporary dir: " + tempDir);
68
Marcel Offermanse5fa4a82012-02-07 23:41:54 +000069 Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir));
Marcel Offermans39e2e002012-02-07 19:18:43 +000070 Session s = new Session();
71 p.begin(s);
Marcel Offermans37fa7732013-11-15 12:01:17 +000072 Utils.configureObject(p, Component.class, Utils.createMockObjectAdapter(Component.class, new Object() {
73 public DependencyManager getDependencyManager() {
74 return new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class));
75 }
76 }));
Marcel Offermans39e2e002012-02-07 19:18:43 +000077 p.process("a", new ByteArrayInputStream("<MetaData />".getBytes()));
78 p.process("b", new ByteArrayInputStream("<MetaData />".getBytes()));
79 p.prepare();
80 p.commit();
81 p.postcommit();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +000082 Utils.removeDirectoryWithContent(tempDir);
Marcel Offermans39e2e002012-02-07 19:18:43 +000083 }
84
85 /** Go through a simple session, containing two empty configurations. */
86 public void testSimpleInstallAndUninstallSession() throws Throwable {
87 AutoConfResourceProcessor p = new AutoConfResourceProcessor();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +000088 Utils.configureObject(p, LogService.class);
Marcel Offermans37fa7732013-11-15 12:01:17 +000089 Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) {
90 public void remove(Component service) {
91 }
92 });
Marcel Offermans39e2e002012-02-07 19:18:43 +000093 Logger logger = new Logger();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +000094 Utils.configureObject(p, LogService.class, logger);
Marcel Offermans39e2e002012-02-07 19:18:43 +000095 File tempDir = File.createTempFile("persistence", "dir");
96 tempDir.delete();
97 tempDir.mkdirs();
98
99 System.out.println("Temporary dir: " + tempDir);
100
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000101 Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000102 Session s = new Session();
103 p.begin(s);
Marcel Offermans37fa7732013-11-15 12:01:17 +0000104 Utils.configureObject(p, Component.class, Utils.createMockObjectAdapter(Component.class, new Object() {
105 public DependencyManager getDependencyManager() {
106 return new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class));
107 }
108 }));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000109 p.process("a", new ByteArrayInputStream("<MetaData />".getBytes()));
110 p.prepare();
111 p.commit();
112 p.postcommit();
113 logger.failOnException();
114 s = new Session();
115 p.begin(s);
116 p.dropped("a");
117 p.prepare();
118 p.commit();
119 p.postcommit();
120 logger.failOnException();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000121 Utils.removeDirectoryWithContent(tempDir);
Marcel Offermans39e2e002012-02-07 19:18:43 +0000122 }
123
124 /** Go through a simple session, containing two empty configurations. */
125 public void testBasicConfigurationSession() throws Throwable {
126 AutoConfResourceProcessor p = new AutoConfResourceProcessor();
127 Logger logger = new Logger();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000128 Utils.configureObject(p, LogService.class, logger);
Marcel Offermans37fa7732013-11-15 12:01:17 +0000129 Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) {
130 public void remove(Component service) {
131 }
132 });
Marcel Offermans39e2e002012-02-07 19:18:43 +0000133 File tempDir = File.createTempFile("persistence", "dir");
134 tempDir.delete();
135 tempDir.mkdirs();
136
137 System.out.println("Temporary dir: " + tempDir);
138
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000139 Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000140 Session s = new Session();
141 p.begin(s);
Marcel Offermans37fa7732013-11-15 12:01:17 +0000142 Utils.configureObject(p, Component.class, Utils.createMockObjectAdapter(Component.class, new Object() {
143 public DependencyManager getDependencyManager() {
144 return new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class));
145 }
146 }));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000147 String config =
148 "<MetaData xmlns:metatype='http://www.osgi.org/xmlns/metatype/v1.0.0'>\n" +
149 " <OCD name='ocd' id='ocd'>\n" +
150 " <AD id='name' type='STRING' cardinality='0' />\n" +
151 " </OCD>\n" +
152 " <Designate pid='simple' bundle='osgi-dp:location'>\n" +
153 " <Object ocdref='ocd'>\n" +
154 " <Attribute adref='name'>\n" +
155 " <Value><![CDATA[test]]></Value>\n" +
156 " </Attribute>\n" +
157 " </Object>\n" +
158 " </Designate>\n" +
159 "</MetaData>\n";
160 p.process("basic", new ByteArrayInputStream(config.getBytes()));
161 p.prepare();
162 p.commit();
163 p.addConfigurationAdmin(null, new ConfigurationAdmin() {
164 public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException {
165 return null;
166 }
167
168 public Configuration getConfiguration(String pid, String location) throws IOException {
169 return new ConfigurationImpl();
170 }
171
172 public Configuration getConfiguration(String pid) throws IOException {
173 return null;
174 }
175
176 public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException {
177 return null;
178 }
179
180 public Configuration createFactoryConfiguration(String factoryPid) throws IOException {
181 return null;
182 }
183 });
184 p.postcommit();
185 logger.failOnException();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000186 Utils.removeDirectoryWithContent(tempDir);
Marcel Offermans39e2e002012-02-07 19:18:43 +0000187 }
188
189 /** Go through a simple session, containing two empty configurations. */
190 public void testFilteredConfigurationSession() throws Throwable {
191 AutoConfResourceProcessor p = new AutoConfResourceProcessor();
192 Logger logger = new Logger();
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000193 Utils.configureObject(p, LogService.class, logger);
Marcel Offermans37fa7732013-11-15 12:01:17 +0000194 BundleContext mockBC = (BundleContext) Utils.createMockObjectAdapter(BundleContext.class, new Object() {
Marcel Offermans39e2e002012-02-07 19:18:43 +0000195 public Filter createFilter(String condition) {
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000196 return (Filter) Utils.createMockObjectAdapter(Filter.class, new Object() {
Marcel Offermans39e2e002012-02-07 19:18:43 +0000197 public boolean match(ServiceReference ref) {
198 Object id = ref.getProperty("id");
199 if (id != null && id.equals(Integer.valueOf(42))) {
200 return true;
201 }
202 return false;
203 }
Marcel Offermans37fa7732013-11-15 12:01:17 +0000204 public void remove(Component service) {
205 }
Marcel Offermans39e2e002012-02-07 19:18:43 +0000206 });
207 }
Marcel Offermans37fa7732013-11-15 12:01:17 +0000208 });
209 Utils.configureObject(p, BundleContext.class, mockBC);
210 Utils.configureObject(p, DependencyManager.class, new DependencyManager(mockBC) {
211 public void remove(Component service) {
212 }
213 });
Marcel Offermans39e2e002012-02-07 19:18:43 +0000214 File tempDir = File.createTempFile("persistence", "dir");
215 tempDir.delete();
216 tempDir.mkdirs();
217
218 System.out.println("Temporary dir: " + tempDir);
219
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000220 Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000221 Session s = new Session();
222 p.begin(s);
Marcel Offermans37fa7732013-11-15 12:01:17 +0000223 Utils.configureObject(p, Component.class, Utils.createMockObjectAdapter(Component.class, new Object() {
224 public DependencyManager getDependencyManager() {
225 return new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class));
226 }
227 }));
Marcel Offermans39e2e002012-02-07 19:18:43 +0000228 String config =
229 "<MetaData xmlns:metatype='http://www.osgi.org/xmlns/metatype/v1.0.0' filter='(id=42)'>\n" +
230 " <OCD name='ocd' id='ocd'>\n" +
231 " <AD id='name' type='STRING' cardinality='0' />\n" +
232 " </OCD>\n" +
233 " <Designate pid='simple' bundle='osgi-dp:location'>\n" +
234 " <Object ocdref='ocd'>\n" +
235 " <Attribute adref='name'>\n" +
236 " <Value><![CDATA[test]]></Value>\n" +
237 " </Attribute>\n" +
238 " </Object>\n" +
239 " </Designate>\n" +
240 "</MetaData>\n";
241 p.process("basic", new ByteArrayInputStream(config.getBytes()));
242 p.prepare();
243 p.commit();
244 Properties props = new Properties();
245 props.put("id", Integer.valueOf(42));
246 final Configuration configuration = new ConfigurationImpl();
247 p.addConfigurationAdmin(new Reference(props), new ConfigurationAdmin() {
248 public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException {
249 return null;
250 }
251
252 public Configuration getConfiguration(String pid, String location) throws IOException {
253 return configuration;
254 }
255
256 public Configuration getConfiguration(String pid) throws IOException {
257 return null;
258 }
259
260 public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException {
261 return null;
262 }
263
264 public Configuration createFactoryConfiguration(String factoryPid) throws IOException {
265 return null;
266 }
267 });
268
269 final Configuration emptyConfiguration = new ConfigurationImpl();
270 p.addConfigurationAdmin(new Reference(new Properties()), new ConfigurationAdmin() {
271 public Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException {
272 return null;
273 }
274
275 public Configuration getConfiguration(String pid, String location) throws IOException {
276 return emptyConfiguration;
277 }
278
279 public Configuration getConfiguration(String pid) throws IOException {
280 return null;
281 }
282
283 public Configuration createFactoryConfiguration(String factoryPid, String location) throws IOException {
284 return null;
285 }
286
287 public Configuration createFactoryConfiguration(String factoryPid) throws IOException {
288 return null;
289 }
290 });
291 p.postcommit();
292 logger.failOnException();
293 assertEquals("test", configuration.getProperties().get("name"));
294 assertNull(emptyConfiguration.getProperties());
Marcel Offermanse5fa4a82012-02-07 23:41:54 +0000295 Utils.removeDirectoryWithContent(tempDir);
Marcel Offermans39e2e002012-02-07 19:18:43 +0000296 }
297
298 private static class ConfigurationImpl implements Configuration {
299 private String m_bundleLocation = "osgi-dp:location";
300 private Dictionary m_properties;
301
302 public String getPid() {
303 return null;
304 }
305
306 public Dictionary getProperties() {
307 return m_properties;
308 }
309
310 public void update(Dictionary properties) throws IOException {
311 m_properties = properties;
312 }
313
314 public void delete() throws IOException {
315 }
316
317 public String getFactoryPid() {
318 return null;
319 }
320
321 public void update() throws IOException {
322 }
323
324 public void setBundleLocation(String bundleLocation) {
325 m_bundleLocation = bundleLocation;
326 }
327
328 public String getBundleLocation() {
329 return m_bundleLocation;
330 }
331 }
332
333 /** Dummy session. */
334 private static class Session implements DeploymentSession {
335 public DeploymentPackage getTargetDeploymentPackage() {
336 return null;
337 }
338 public DeploymentPackage getSourceDeploymentPackage() {
339 return null;
340 }
341 public File getDataFile(Bundle bundle) {
342 return null;
343 }
344 }
345
346 private static class Logger implements LogService {
347 private static final String[] LEVEL = { "", "[ERROR]", "[WARN ]", "[INFO ]", "[DEBUG]" };
348 private Throwable m_exception;
349
350 public void log(int level, String message) {
351 System.out.println(LEVEL[level] + " - " + message);
352 }
353
354 public void log(int level, String message, Throwable exception) {
355 System.out.println(LEVEL[level] + " - " + message + " - " + exception.getMessage());
356 m_exception = exception;
357 }
358
359 public void log(ServiceReference sr, int level, String message) {
360 System.out.println(LEVEL[level] + " - " + message);
361 }
362
363 public void log(ServiceReference sr, int level, String message, Throwable exception) {
364 System.out.println(LEVEL[level] + " - " + message + " - " + exception.getMessage());
365 m_exception = exception;
366 }
367
368 public void failOnException() throws Throwable {
369 if (m_exception != null) {
370 throw m_exception;
371 }
372 }
373 }
374 private static class Reference implements ServiceReference {
375 private final Dictionary m_properties;
376
377 public Reference(Dictionary properties) {
378 m_properties = properties;
379 }
380
381 public Object getProperty(String key) {
382 return m_properties.get(key);
383 }
384
385 public String[] getPropertyKeys() {
386 return null;
387 }
388
389 public Bundle getBundle() {
390 return null;
391 }
392
393 public Bundle[] getUsingBundles() {
394 return null;
395 }
396
397 public boolean isAssignableTo(Bundle bundle, String className) {
398 return false;
399 }
400
401 public int compareTo(Object reference) {
402 return 0;
403 }
404 }
405}