Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.deployment.rp.autoconf; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.util.Dictionary; |
| 25 | import java.util.Properties; |
| 26 | |
| 27 | import junit.framework.TestCase; |
| 28 | |
| 29 | import org.apache.felix.dm.Component; |
| 30 | import org.apache.felix.dm.DependencyManager; |
| 31 | import org.osgi.framework.Bundle; |
| 32 | import org.osgi.framework.BundleContext; |
| 33 | import org.osgi.framework.Filter; |
| 34 | import org.osgi.framework.InvalidSyntaxException; |
| 35 | import org.osgi.framework.ServiceReference; |
| 36 | import org.osgi.service.cm.Configuration; |
| 37 | import org.osgi.service.cm.ConfigurationAdmin; |
| 38 | import org.osgi.service.deploymentadmin.DeploymentPackage; |
| 39 | import org.osgi.service.deploymentadmin.spi.DeploymentSession; |
| 40 | import org.osgi.service.log.LogService; |
| 41 | |
| 42 | public 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 58 | Utils.configureObject(p, LogService.class); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 59 | Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) { |
| 60 | public void remove(Component service) { |
| 61 | } |
| 62 | }); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 63 | File tempDir = File.createTempFile("persistence", "dir"); |
| 64 | tempDir.delete(); |
| 65 | tempDir.mkdirs(); |
| 66 | |
| 67 | System.out.println("Temporary dir: " + tempDir); |
| 68 | |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 69 | Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir)); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 70 | Session s = new Session(); |
| 71 | p.begin(s); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 72 | 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 Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 77 | 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 82 | Utils.removeDirectoryWithContent(tempDir); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** Go through a simple session, containing two empty configurations. */ |
| 86 | public void testSimpleInstallAndUninstallSession() throws Throwable { |
| 87 | AutoConfResourceProcessor p = new AutoConfResourceProcessor(); |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 88 | Utils.configureObject(p, LogService.class); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 89 | Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) { |
| 90 | public void remove(Component service) { |
| 91 | } |
| 92 | }); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 93 | Logger logger = new Logger(); |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 94 | Utils.configureObject(p, LogService.class, logger); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 95 | File tempDir = File.createTempFile("persistence", "dir"); |
| 96 | tempDir.delete(); |
| 97 | tempDir.mkdirs(); |
| 98 | |
| 99 | System.out.println("Temporary dir: " + tempDir); |
| 100 | |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 101 | Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir)); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 102 | Session s = new Session(); |
| 103 | p.begin(s); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 104 | 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 Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 109 | 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 121 | Utils.removeDirectoryWithContent(tempDir); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 122 | } |
| 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 128 | Utils.configureObject(p, LogService.class, logger); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 129 | Utils.configureObject(p, DependencyManager.class, new DependencyManager((BundleContext) Utils.createNullObject(BundleContext.class)) { |
| 130 | public void remove(Component service) { |
| 131 | } |
| 132 | }); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 133 | File tempDir = File.createTempFile("persistence", "dir"); |
| 134 | tempDir.delete(); |
| 135 | tempDir.mkdirs(); |
| 136 | |
| 137 | System.out.println("Temporary dir: " + tempDir); |
| 138 | |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 139 | Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir)); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 140 | Session s = new Session(); |
| 141 | p.begin(s); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 142 | 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 Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 147 | 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 186 | Utils.removeDirectoryWithContent(tempDir); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 187 | } |
| 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 193 | Utils.configureObject(p, LogService.class, logger); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 194 | BundleContext mockBC = (BundleContext) Utils.createMockObjectAdapter(BundleContext.class, new Object() { |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 195 | public Filter createFilter(String condition) { |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 196 | return (Filter) Utils.createMockObjectAdapter(Filter.class, new Object() { |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 197 | 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 Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 204 | public void remove(Component service) { |
| 205 | } |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 206 | }); |
| 207 | } |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 208 | }); |
| 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 Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 214 | File tempDir = File.createTempFile("persistence", "dir"); |
| 215 | tempDir.delete(); |
| 216 | tempDir.mkdirs(); |
| 217 | |
| 218 | System.out.println("Temporary dir: " + tempDir); |
| 219 | |
Marcel Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 220 | Utils.configureObject(p, PersistencyManager.class, new PersistencyManager(tempDir)); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 221 | Session s = new Session(); |
| 222 | p.begin(s); |
Marcel Offermans | 37fa773 | 2013-11-15 12:01:17 +0000 | [diff] [blame^] | 223 | 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 Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 228 | 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 Offermans | e5fa4a8 | 2012-02-07 23:41:54 +0000 | [diff] [blame] | 295 | Utils.removeDirectoryWithContent(tempDir); |
Marcel Offermans | 39e2e00 | 2012-02-07 19:18:43 +0000 | [diff] [blame] | 296 | } |
| 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 | } |