content: /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  package org.apache.aries.blueprint.itests.cm;
  
  import static org.junit.Assert.assertArrayEquals;
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.assertNull;
  import static org.ops4j.pax.exam.CoreOptions.keepCaches;
  import static org.ops4j.pax.exam.CoreOptions.streamBundle;
  
  import java.io.InputStream;
  import java.util.Hashtable;
  import javax.inject.Inject;
  
  import org.apache.aries.blueprint.itests.AbstractBlueprintIntegrationTest;
  import org.apache.aries.blueprint.itests.Helper;
  import org.apache.aries.blueprint.itests.cm.service.Foo;
  import org.apache.aries.blueprint.itests.cm.service.FooFactory;
  import org.apache.aries.blueprint.itests.cm.service.FooInterface;
  import org.junit.Test;
  import org.ops4j.pax.exam.Option;
  import org.ops4j.pax.exam.ProbeBuilder;
  import org.ops4j.pax.exam.TestProbeBuilder;
  import org.ops4j.pax.tinybundles.core.TinyBundles;
  import org.osgi.framework.Constants;
  import org.osgi.framework.InvalidSyntaxException;
  import org.osgi.framework.ServiceReference;
  import org.osgi.service.cm.Configuration;
  import org.osgi.service.cm.ConfigurationAdmin;
  
  public class CmPropertyPlaceholderTest extends AbstractBlueprintIntegrationTest {
      private static final String TEST_BUNDLE = "org.apache.aries.blueprint.cm.test.b1";
  
      @Inject
      ConfigurationAdmin ca;
  
      @ProbeBuilder
      public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
          probe.setHeader(Constants.EXPORT_PACKAGE, Foo.class.getPackage().getName());
          probe.setHeader(Constants.IMPORT_PACKAGE, Foo.class.getPackage().getName());
          return probe;
      }
  
      @org.ops4j.pax.exam.Configuration
      public Option[] config() {
          return new Option[] {
              baseOptions(), 
              Helper.blueprintBundles(), 
              keepCaches(),
              streamBundle(testBundle())
          };
      }
  
      protected InputStream testBundle() {
          return TinyBundles.bundle() //
              .add(FooInterface.class) //
              .add(Foo.class) //
              .add(FooFactory.class) //
              .add("OSGI-INF/blueprint/context.xml", getResource("CmPropertyPlaceholderTest.xml"))
              .set(Constants.BUNDLE_SYMBOLICNAME, TEST_BUNDLE) //
              .set(Constants.EXPORT_PACKAGE, Foo.class.getPackage().getName()) //
              .set(Constants.IMPORT_PACKAGE, Foo.class.getPackage().getName()) //
              .build(TinyBundles.withBnd());
      }
  
      @Test
      public void testProperties() throws Exception {
          ServiceReference sr = getServiceRef(FooInterface.class, "(key=foo5)");
          assertNotNull(sr);
          FooInterface foo = (FooInterface)context().getService(sr);
          assertNotNull(foo);
          assertEquals("42", foo.getB());
          assertNull(foo.getC());
  
          Configuration cf = ca.getConfiguration("blueprint-sample-properties.pid", null);
          Hashtable<String,Object> props = new Hashtable<String,Object>();
          props.put("pb", "43");
          cf.update(props);
          Thread.sleep(500);
          sr = getServiceRef(FooInterface.class, "(key=foo5)");
          foo = (FooInterface)context().getService(sr);
          assertEquals("43", foo.getB());
  
          props.clear();
          props.put("pc", 7);
          props.put("pd", new int[] { 3, 4 });
          cf.update(props);
          Thread.sleep(500);
          sr = getServiceRef(FooInterface.class, "(key=foo5)");
          foo = (FooInterface)context().getService(sr);
          assertEquals(7L, (long) foo.getC());
          assertArrayEquals(new int[] { 3, 4 }, (int[]) foo.getD());
  
          cf.delete();
          Thread.sleep(500);
          sr = getServiceRef(FooInterface.class, "(key=foo5)");
          foo = (FooInterface)context().getService(sr);
          assertEquals(foo.getB(), "42");
      }
  
      @SuppressWarnings("rawtypes")
      private ServiceReference getServiceRef(Class serviceInterface, String filter)
              throws InvalidSyntaxException {
          int tries = 0;
          do {
              ServiceReference[] srAr = bundleContext.getServiceReferences(serviceInterface.getName(), filter);
              if (srAr != null && srAr.length > 0) {
                  return srAr[0];
              }
              tries++;
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  // Ignore
              }
          } while (tries < 100);
          throw new RuntimeException("Could not find service " + serviceInterface.getName() + ", " + filter);
      }
  
  }
  