blob: 2bf4bd08e3c8685db76bb970cfd17b53c27a15d9 [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;
24import java.util.List;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000025import java.util.Properties;
26
Felix Meschberger4b26df92011-02-01 12:41:45 +000027import junit.framework.TestCase;
28
Marcel Offermans33ad4a72011-01-31 19:59:18 +000029import org.junit.Test;
30import org.junit.runner.RunWith;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000031import org.ops4j.pax.exam.junit.JUnit4TestRunner;
32import org.osgi.framework.Bundle;
Marcel Offermans33ad4a72011-01-31 19:59:18 +000033import org.osgi.framework.BundleException;
34import org.osgi.framework.Constants;
35import org.osgi.framework.InvalidSyntaxException;
36import org.osgi.framework.ServiceEvent;
37import org.osgi.framework.ServiceListener;
38import org.osgi.framework.ServiceReference;
39import org.osgi.service.cm.ConfigurationAdmin;
40import org.osgi.service.cm.ConfigurationEvent;
41import org.osgi.service.cm.ConfigurationListener;
42
Marcel Offermans33ad4a72011-01-31 19:59:18 +000043
Felix Meschberger4b26df92011-02-01 12:41:45 +000044@RunWith(JUnit4TestRunner.class)
45public class FELIX2813_ConfigurationAdminStartupTest extends ConfigurationTestBase implements ServiceListener,
46 ConfigurationListener
47{
48
49 private Object lock = new Object();
50 private boolean eventSeen;
51
Marcel Offermans33ad4a72011-01-31 19:59:18 +000052
53 @Test
Felix Meschberger4b26df92011-02-01 12:41:45 +000054 public void testAddConfigurationWhenConfigurationAdminStarts() throws InvalidSyntaxException, BundleException
55 {
56
57 List<Bundle> bundles = new ArrayList<Bundle>();
58 ServiceReference[] refs = configAdminTracker.getServiceReferences();
59 if ( refs != null )
60 {
61 for ( ServiceReference ref : refs )
62 {
63 bundles.add( ref.getBundle() );
64 ref.getBundle().stop();
Marcel Offermans33ad4a72011-01-31 19:59:18 +000065 }
66 }
Felix Meschberger4b26df92011-02-01 12:41:45 +000067
68 bundleContext.registerService( ConfigurationListener.class.getName(), this, null );
69 bundleContext.addServiceListener( this, "(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName()
70 + ")" );
71
72 // ensure we do not have a false positive below
73 eventSeen = false;
74
75 for ( Bundle bundle : bundles )
76 {
77 bundle.start();
78 }
79
Marcel Offermans33ad4a72011-01-31 19:59:18 +000080 /*
81 * Look at the console output for the following exception:
Felix Meschberger4b26df92011-02-01 12:41:45 +000082 *
Marcel Offermans33ad4a72011-01-31 19:59:18 +000083 * *ERROR* Unexpected problem executing task
84 * java.lang.NullPointerException: reference and pid must not be null
85 * at org.osgi.service.cm.ConfigurationEvent.<init>(ConfigurationEvent.java:120)
86 * at org.apache.felix.cm.impl.ConfigurationManager$FireConfigurationEvent.run(ConfigurationManager.java:1818)
87 * at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:104)
88 * at java.lang.Thread.run(Thread.java:680)
Felix Meschberger4b26df92011-02-01 12:41:45 +000089 *
Marcel Offermans33ad4a72011-01-31 19:59:18 +000090 * It is in fact the service reference that is still null, because the service registration
91 * has not been 'set' yet.
Felix Meschberger4b26df92011-02-01 12:41:45 +000092 *
93 * This following code will ensure the situation did not occurr and the
94 * event has effectively been sent. The eventSeen flag is set by the
95 * configurationEvent method when the event for the test PID has been
96 * received. If the flag is not set, we wait at most 2 seconds for the
97 * event to arrive. If the event does not arrive by then, the test is
98 * assumed to have failed. This will rather generate false negatives
99 * (on slow machines) than false positives.
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000100 */
Felix Meschberger4b26df92011-02-01 12:41:45 +0000101 synchronized ( lock )
102 {
103 if ( !eventSeen )
104 {
105 try
106 {
107 lock.wait( 2000 );
108 }
109 catch ( InterruptedException ie )
110 {
111 // don't care ...
112 }
113 }
114
115 if ( !eventSeen )
116 {
117 TestCase.fail( "ConfigurationEvent not received within 2 seconds since bundle start" );
118 }
119 }
120
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000121 }
122
Felix Meschberger4b26df92011-02-01 12:41:45 +0000123
124 public void serviceChanged( ServiceEvent event )
125 {
126 if ( event.getType() == ServiceEvent.REGISTERED )
127 {
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000128 ServiceReference ref = event.getServiceReference();
Felix Meschberger4b26df92011-02-01 12:41:45 +0000129 ConfigurationAdmin ca = ( ConfigurationAdmin ) bundleContext.getService( ref );
130 try
131 {
132 org.osgi.service.cm.Configuration config = ca.getConfiguration( "test" );
133 Properties props = new Properties();
134 props.put( "abc", "123" );
135 config.update( props );
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000136 }
Felix Meschberger4b26df92011-02-01 12:41:45 +0000137 catch ( IOException e )
138 {
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000139 }
140 }
141 }
142
Felix Meschberger4b26df92011-02-01 12:41:45 +0000143
144 public void configurationEvent( ConfigurationEvent event )
145 {
146 if ( "test".equals( event.getPid() ) )
147 {
148 synchronized ( lock )
149 {
150 eventSeen = true;
151 lock.notifyAll();
152 }
153 }
Marcel Offermans33ad4a72011-01-31 19:59:18 +0000154 }
155}