blob: c69e47c7b5e8f0ce80c3b452e3ecaf086d624508 [file] [log] [blame]
Felix Meschberger72f95072011-02-04 23:02:42 +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 */
Marcel Offermans33ad4a72011-01-31 19:59:18 +000019package org.apache.felix.cm.integration;
20
Marcel Offermans33ad4a72011-01-31 19:59:18 +000021
22import java.io.IOException;
Felix Meschberger4b26df92011-02-01 12:41:45 +000023import java.util.ArrayList;
Felix Meschbergerabb0bc22012-05-30 11:07:49 +000024import java.util.Hashtable;
Felix Meschberger4b26df92011-02-01 12:41:45 +000025import java.util.List;
Felix Meschberger0770cad2012-06-11 12:36:52 +000026import org.apache.felix.cm.integration.helper.SynchronousTestListener;
27import org.apache.felix.cm.integration.helper.TestListener;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000028import org.junit.Test;
29import org.junit.runner.RunWith;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000030import org.ops4j.pax.exam.junit.JUnit4TestRunner;
31import org.osgi.framework.Bundle;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000032import org.osgi.framework.BundleException;
33import org.osgi.framework.Constants;
34import org.osgi.framework.InvalidSyntaxException;
35import org.osgi.framework.ServiceEvent;
36import org.osgi.framework.ServiceListener;
37import org.osgi.framework.ServiceReference;
38import org.osgi.service.cm.ConfigurationAdmin;
39import org.osgi.service.cm.ConfigurationEvent;
40import org.osgi.service.cm.ConfigurationListener;
Felix Meschbergere94b1572012-07-14 11:59:29 +000041import org.osgi.service.cm.SynchronousConfigurationListener;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000042
Marcel Offermans33ad4a72011-01-31 19:59:18 +000043
Felix Meschberger4b26df92011-02-01 12:41:45 +000044@RunWith(JUnit4TestRunner.class)
Felix Meschberger0770cad2012-06-11 12:36:52 +000045public class FELIX2813_ConfigurationAdminStartupTest extends ConfigurationTestBase implements ServiceListener
Felix Meschberger4b26df92011-02-01 12:41:45 +000046{
47
Marcel Offermans33ad4a72011-01-31 19:59:18 +000048 @Test
Felix Meschberger4b26df92011-02-01 12:41:45 +000049 public void testAddConfigurationWhenConfigurationAdminStarts() throws InvalidSyntaxException, BundleException
50 {
51
52 List<Bundle> bundles = new ArrayList<Bundle>();
53 ServiceReference[] refs = configAdminTracker.getServiceReferences();
54 if ( refs != null )
55 {
56 for ( ServiceReference ref : refs )
57 {
58 bundles.add( ref.getBundle() );
59 ref.getBundle().stop();
Marcel Offermans33ad4a72011-01-31 19:59:18 +000060 }
61 }
Felix Meschberger4b26df92011-02-01 12:41:45 +000062
Felix Meschberger0770cad2012-06-11 12:36:52 +000063 final TestListener listener = new TestListener();
64 bundleContext.registerService( ConfigurationListener.class.getName(), listener, null );
65 final TestListener syncListener = new SynchronousTestListener();
Felix Meschbergere94b1572012-07-14 11:59:29 +000066 bundleContext.registerService( SynchronousConfigurationListener.class.getName(), syncListener, null );
67 final TestListener syncListenerAsync = new SynchronousTestListener();
68 bundleContext.registerService( ConfigurationListener.class.getName(), syncListenerAsync, null );
Felix Meschberger4b26df92011-02-01 12:41:45 +000069 bundleContext.addServiceListener( this, "(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName()
70 + ")" );
71
Felix Meschberger4b26df92011-02-01 12:41:45 +000072 for ( Bundle bundle : bundles )
73 {
74 bundle.start();
75 }
76
Marcel Offermans33ad4a72011-01-31 19:59:18 +000077 /*
78 * Look at the console output for the following exception:
Felix Meschberger4b26df92011-02-01 12:41:45 +000079 *
Marcel Offermans33ad4a72011-01-31 19:59:18 +000080 * *ERROR* Unexpected problem executing task
81 * java.lang.NullPointerException: reference and pid must not be null
82 * at org.osgi.service.cm.ConfigurationEvent.<init>(ConfigurationEvent.java:120)
83 * at org.apache.felix.cm.impl.ConfigurationManager$FireConfigurationEvent.run(ConfigurationManager.java:1818)
84 * at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:104)
85 * at java.lang.Thread.run(Thread.java:680)
Felix Meschberger4b26df92011-02-01 12:41:45 +000086 *
Marcel Offermans33ad4a72011-01-31 19:59:18 +000087 * It is in fact the service reference that is still null, because the service registration
88 * has not been 'set' yet.
Felix Meschberger4b26df92011-02-01 12:41:45 +000089 *
90 * This following code will ensure the situation did not occurr and the
91 * event has effectively been sent. The eventSeen flag is set by the
92 * configurationEvent method when the event for the test PID has been
93 * received. If the flag is not set, we wait at most 2 seconds for the
94 * event to arrive. If the event does not arrive by then, the test is
95 * assumed to have failed. This will rather generate false negatives
96 * (on slow machines) than false positives.
Marcel Offermans33ad4a72011-01-31 19:59:18 +000097 */
Felix Meschberger0770cad2012-06-11 12:36:52 +000098 delay();
99 listener.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, true, 1 );
100 syncListener.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, false, 1 );
Felix Meschbergere94b1572012-07-14 11:59:29 +0000101 syncListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, true, 1 );
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000102 }
103
Felix Meschberger4b26df92011-02-01 12:41:45 +0000104
105 public void serviceChanged( ServiceEvent event )
106 {
107 if ( event.getType() == ServiceEvent.REGISTERED )
108 {
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000109 ServiceReference ref = event.getServiceReference();
Felix Meschberger4b26df92011-02-01 12:41:45 +0000110 ConfigurationAdmin ca = ( ConfigurationAdmin ) bundleContext.getService( ref );
111 try
112 {
113 org.osgi.service.cm.Configuration config = ca.getConfiguration( "test" );
Felix Meschbergerabb0bc22012-05-30 11:07:49 +0000114 Hashtable<String, Object> props = new Hashtable<String, Object>();
Felix Meschberger4b26df92011-02-01 12:41:45 +0000115 props.put( "abc", "123" );
116 config.update( props );
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000117 }
Felix Meschberger4b26df92011-02-01 12:41:45 +0000118 catch ( IOException e )
119 {
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000120 }
121 }
122 }
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000123}